Skip to content

Commit

Permalink
Merge f56597b into 62d93d3
Browse files Browse the repository at this point in the history
  • Loading branch information
upatel32 committed Sep 7, 2020
2 parents 62d93d3 + f56597b commit 2149523
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -52,7 +52,8 @@ All Event Handlers are called with the below event data.
deltaY, // y offset (current.y - initial.y)
absX, // absolute deltaX
absY, // absolute deltaY
velocity, // √(absX^2 + absY^2) / time
velocity, // √(absX^2 + absY^2) / time - "absolute velocity" (speed)
vxvy, // [ deltaX/time, deltaY/time] - velocity per axis
dir, // direction of swipe (Left|Right|Up|Down)
}
```
Expand Down
10 changes: 10 additions & 0 deletions __tests__/__snapshots__/useSwipeable.spec.tsx.snap
Expand Up @@ -16,6 +16,7 @@ Object {
100,
],
"velocity": Any<Number>,
"vxvy": Any<Array>,
}
`;
Expand All @@ -35,6 +36,7 @@ Object {
100,
],
"velocity": Any<Number>,
"vxvy": Any<Array>,
}
`;
Expand All @@ -54,6 +56,10 @@ Object {
100,
],
"velocity": Any<Number>,
"vxvy": ArrayContaining [
Any<Number>,
Any<Number>,
],
}
`;
Expand All @@ -73,5 +79,9 @@ Object {
100,
],
"velocity": Any<Number>,
"vxvy": ArrayContaining [
Any<Number>,
Any<Number>,
],
}
`;
8 changes: 4 additions & 4 deletions __tests__/useSwipeable.spec.tsx
Expand Up @@ -114,11 +114,11 @@ describe("useSwipeable", () => {
expect(swipeFuncs.onSwipedLeft).not.toHaveBeenCalled();
expect(swipeFuncs.onSwipedRight).not.toHaveBeenCalled();
expect(swipeFuncs.onSwiped.mock.calls[0][0]).toMatchSnapshot(
{ velocity: expect.any(Number) },
{ velocity: expect.any(Number), vxvy: expect.arrayContaining([expect.any(Number), expect.any(Number)]) },
`useSwipeable onSwiped trackTouch`
);
expect(swipeFuncs.onSwiping.mock.calls[0][0]).toMatchSnapshot(
{ velocity: expect.any(Number) },
{ velocity: expect.any(Number), vxvy: expect.arrayContaining([expect.any(Number), expect.any(Number)]) },
`useSwipeable onSwiping trackTouch`
);
});
Expand Down Expand Up @@ -147,11 +147,11 @@ describe("useSwipeable", () => {
expect(swipeFuncs.onSwipedDown).not.toHaveBeenCalled();
expect(swipeFuncs.onSwipedLeft).not.toHaveBeenCalled();
expect(swipeFuncs.onSwiped.mock.calls[0][0]).toMatchSnapshot(
{ velocity: expect.any(Number) },
{ velocity: expect.any(Number), vxvy: expect.any(Array) },
`useSwipeable onSwiped trackMouse`
);
expect(swipeFuncs.onSwiping.mock.calls[0][0]).toMatchSnapshot(
{ velocity: expect.any(Number) },
{ velocity: expect.any(Number), vxvy: expect.any(Array) },
`useSwipeable onSwiping trackMouse`
);
});
Expand Down
1 change: 1 addition & 0 deletions migration.md
Expand Up @@ -6,5 +6,6 @@
- _TODO_ add example for creating one and migrating
- update calculation of `deltaX` and `deltaY` from `initial - current` to `current - initial`
- Example: `const deltaX = state.xy[0] - x;` is now `const deltaX = x - state.xy[0];`
- add new `vxvy` vector which contains velocity per each dimension

## Migrate Swipeable v5 to v6
4 changes: 4 additions & 0 deletions src/index.ts
Expand Up @@ -12,6 +12,7 @@ export type EventData = {
first: boolean;
initial: Vector2;
velocity: number;
vxvy: Vector2;
dir: "Left" | "Right" | "Up" | "Down";
};

Expand Down Expand Up @@ -48,6 +49,7 @@ type StateEventData = {
first: boolean;
initial: Vector2;
velocity?: number;
vxvy?: Vector2;
dir?: "Left" | "Right" | "Up" | "Down";
};

Expand Down Expand Up @@ -180,6 +182,7 @@ function getHandlers(
const absY = Math.abs(deltaY);
const time = (event.timeStamp || 0) - state.start;
const velocity = Math.sqrt(absX * absX + absY * absY) / (time || 1);
const vxvy = [deltaX / (time || 1), deltaY / (time || 1)];

// if swipe is under delta and we have not started to track a swipe: skip update
if (absX < props.delta && absY < props.delta && !state.swiping)
Expand All @@ -194,6 +197,7 @@ function getHandlers(
deltaX,
deltaY,
velocity,
vxvy,
dir,
} as EventData;

Expand Down

0 comments on commit 2149523

Please sign in to comment.