Skip to content

Commit 0b6bd25

Browse files
authored
size refinement and delta default (#262)
1 parent 46dd4a9 commit 0b6bd25

File tree

6 files changed

+550
-22
lines changed

6 files changed

+550
-22
lines changed

Diff for: README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Spread `handlers` onto the element you wish to track swipes on.
4747

4848
```js
4949
{
50-
delta: 10, // min distance(px) before a swipe starts
50+
delta: 10, // min distance(px) before a swipe starts. *See Notes*
5151
preventDefaultTouchmoveEvent: false, // call e.preventDefault *See Details*
5252
trackTouch: true, // track touch input
5353
trackMouse: false, // track mouse input
@@ -57,11 +57,11 @@ Spread `handlers` onto the element you wish to track swipes on.
5757

5858
#### Delta
5959

60-
You can also set a different delta for each side:
60+
`delta` can be either a `number` or an `object` specifying different deltas for each direction, [`left`, `right`, `up`, `down`], direction values are optional and will default to `10`;
6161

6262
```js
6363
{
64-
delta: { right: 10, left: 10, top: 20, bottom: 20 } // right and left starts when ">= 10", top and bottom when ">= 20"
64+
delta: { top: 20, bottom: 20 } // top and bottom when ">= 20", left and right default to ">= 10"
6565
}
6666
```
6767

Diff for: __tests__/useSwipeable.spec.tsx

+47
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,53 @@ describe("useSwipeable", () => {
426426
expect(onSwipedDown).toHaveBeenCalledTimes(1);
427427
});
428428

429+
it("defaults delta for side", () => {
430+
const onSwipedRight = jest.fn();
431+
const onSwipedLeft = jest.fn();
432+
const onSwipedUp = jest.fn();
433+
const onSwipedDown = jest.fn();
434+
const { getByText } = render(
435+
<SwipeableUsingHook
436+
onSwipedRight={onSwipedRight}
437+
onSwipedLeft={onSwipedLeft}
438+
onSwipedUp={onSwipedUp}
439+
onSwipedDown={onSwipedDown}
440+
delta={{
441+
right: 40,
442+
down: 40,
443+
}}
444+
/>
445+
);
446+
447+
const touchArea = getByText(TESTING_TEXT);
448+
449+
// check left
450+
fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
451+
fireEvent[TM](touchArea, cte({ x: 95, y: 100 }));
452+
fireEvent[TE](touchArea, cte({}));
453+
454+
expect(onSwipedLeft).not.toHaveBeenCalled();
455+
456+
fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
457+
fireEvent[TM](touchArea, cte({ x: 90, y: 100 }));
458+
fireEvent[TE](touchArea, cte({}));
459+
460+
expect(onSwipedLeft).toHaveBeenCalledTimes(1);
461+
462+
// check up
463+
fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
464+
fireEvent[TM](touchArea, cte({ x: 100, y: 95 }));
465+
fireEvent[TE](touchArea, cte({}));
466+
467+
expect(onSwipedUp).not.toHaveBeenCalled();
468+
469+
fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
470+
fireEvent[TM](touchArea, cte({ x: 100, y: 90 }));
471+
fireEvent[TE](touchArea, cte({}));
472+
473+
expect(onSwipedUp).toHaveBeenCalledTimes(1);
474+
});
475+
429476
it("Handle Rotation by 90 degree", () => {
430477
const swipeFuncsRight = getMockedSwipeFunctions();
431478
const { getByText, rerender } = render(

Diff for: src/index.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
LEFT,
1010
RIGHT,
1111
Setter,
12+
SwipeableCallbacks,
1213
SwipeableHandlers,
1314
SwipeableProps,
1415
SwipeablePropsWithDefaultOptions,
@@ -137,7 +138,8 @@ function getHandlers(
137138
const delta =
138139
typeof props.delta === "number"
139140
? props.delta
140-
: props.delta[dir.toLowerCase() as Lowercase<SwipeDirections>];
141+
: props.delta[dir.toLowerCase() as Lowercase<SwipeDirections>] ||
142+
defaultProps.delta;
141143
if (absX < delta && absY < delta && !state.swiping) return state;
142144

143145
const eventData = {
@@ -191,10 +193,9 @@ function getHandlers(
191193
eventData = { ...state.eventData, event };
192194
props.onSwiped && props.onSwiped(eventData);
193195

194-
const onSwipedDir = `onSwiped${eventData.dir}`;
195-
if (onSwipedDir in props) {
196-
((props as any)[onSwipedDir] as SwipeCallback)(eventData);
197-
}
196+
const onSwipedDir =
197+
props[`onSwiped${eventData.dir}` as keyof SwipeableCallbacks];
198+
onSwipedDir && onSwipedDir(eventData);
198199
} else {
199200
props.onTap && props.onTap({ event });
200201
}

Diff for: src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export type SwipeableCallbacks = {
4242
// Configuration Options
4343
export type ConfigurationOptionDelta =
4444
| number
45-
| { [key in Uncapitalize<SwipeDirections>]: number };
45+
| { [key in Lowercase<SwipeDirections>]?: number };
4646
export interface ConfigurationOptions {
4747
delta: ConfigurationOptionDelta;
4848
preventDefaultTouchmoveEvent: boolean;

Diff for: tsconfig.base.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"compilerOptions": {
33
"allowJs": true,
4+
"removeComments": true,
45
"baseUrl": ".",
56
"esModuleInterop": true,
67
"jsx": "react",

0 commit comments

Comments
 (0)