Skip to content

Commit

Permalink
feat(👋🏻): New helper functions (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon authored Sep 19, 2019
1 parent c0ef7f8 commit 16846c3
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 8 deletions.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,59 @@ return (
);
```
### `panGestureHandler()`
```tsx
const {
gestureEvent,
state
translationX,
velocityX,
translationY,
velocityY
} = panGestureHandler();
return (
<PanGestureHandler {...gestureEvent} />
);
```
### `horizontalPanGestureHandler()`
```tsx
const {
gestureEvent,
state
translationX,
velocityX,
} = horizontalPanGestureHandler();
return (
<PanGestureHandler {...gestureEvent} />
);
```
### `verticalPanGestureHandler()`
```tsx
const {
gestureEvent,
state
translationY,
velocityY,
} = verticalPanGestureHandler();
return (
<PanGestureHandler {...gestureEvent} />
);
```
### `panGestureHandler()`
```tsx
const {gestureEvent, translationX, translationY} = panGestureHandler();
return (
<PanGestureHandler {...gestureEvent} />
);
```
### `withSpring({ value: Node, velocity: Node, state: Value, offset: Node, config, snapPoints: Node[], onSnap: (value) => void }): Node`
Decorates animated value to spring when the gesture ends.
Expand Down
63 changes: 55 additions & 8 deletions src/Gesture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
FlingGestureHandlerEventExtra
} from "react-native-gesture-handler";

import { clamp } from "./Math";
import { snapPoint } from "./Animations";

const {
Expand Down Expand Up @@ -174,13 +173,6 @@ export const withDecay = (config: WithDecayParams) => {
]);
};

export const withClamp = (
value: Animated.Node<number>,
state: Animated.Value<State>,
min: Animated.Adaptable<number>,
max: Animated.Adaptable<number>
) => cond(eq(state, State.ACTIVE), clamp(value, min, max), value);

export const preserveMultiplicativeOffset = (
value: Animated.Adaptable<number>,
state: Animated.Adaptable<number>
Expand Down Expand Up @@ -234,3 +226,58 @@ export const onGestureEvent = (
onGestureEvent: gestureEvent
};
};

export const panGestureHandler = () => {
const translationX = new Value(0);
const velocityX = new Value(0);
const translationY = new Value(0);
const velocityY = new Value(0);
const state = new Value(State.UNDETERMINED);
const gestureHandler = onGestureEvent({
translationX,
velocityX,
state
});
return {
translationX,
velocityX,
translationY,
velocityY,
state,
gestureHandler
};
};

export const horizontalPanGestureHandler = () => {
const translationX = new Value(0);
const velocityX = new Value(0);
const state = new Value(State.UNDETERMINED);
const gestureHandler = onGestureEvent({
translationX,
velocityX,
state
});
return {
translationX,
state,
velocityX,
gestureHandler
};
};

export const verticalPanGestureHandler = () => {
const translationY = new Value(0);
const velocityY = new Value(0);
const state = new Value(State.UNDETERMINED);
const gestureHandler = onGestureEvent({
translationY,
velocityY,
state
});
return {
translationY,
state,
velocityY,
gestureHandler
};
};

0 comments on commit 16846c3

Please sign in to comment.