Skip to content

Commit

Permalink
feat(🆕): Add useValues() 🎣 (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon authored Nov 13, 2019
1 parent 6b57068 commit 16899bd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"rules": {
"react-native/no-color-literals": 0,
"import/prefer-default-export": 0,
"import/no-unresolved": 0
"import/no-unresolved": 0,
"import/export": 0
}
}
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,22 @@ contains(values: Node[], value: Node) => Node

## Animations

### `useValues(...Default Values[], deps)`

Create animation values which lifecycle is granted by `deps`.
For instance the code snippet below:

```
const [toggle, state] = useValues([0, State.UNDETERMINED], []);
```

is a shortcut for

```
const [toggle state] = useMemoOne(() => [new Value(0), new Value(State.UNDETERMINED)], []);
```


### `useTransition(state: any, source: Node, destination: Node, duration: number, easing: EasingFunction): Node`

Returns an animation value that follows a component state.
Expand Down
20 changes: 20 additions & 0 deletions src/Animations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,23 @@ export const useToggle = (
easing: Animated.EasingFunction = Easing.linear
) =>
useTransition(toggle, toggle ? 1 : 0, not(toggle ? 1 : 0), duration, easing);

type Dependencies = readonly unknown[];
type Atomic = string | number | boolean;

export const useValues = <V extends Atomic>(
values: V[],
deps: Dependencies
): Animated.Value<V>[] => useMemoOne(() => values.map(v => new Value(v)), deps);

export const useNamedValues = <V extends Atomic, K extends string>(
values: Record<K, V>,
deps: Dependencies
): Record<K, Animated.Value<V>> =>
useMemoOne(() => {
const result: Record<string, Animated.Value<V>> = {};
Object.keys(values).forEach(key => {
result[key as K] = new Value(values[key as K]);
});
return result;
}, deps);

0 comments on commit 16899bd

Please sign in to comment.