Skip to content

Commit 66aaa28

Browse files
author
Bruno Macabeus
committed
update delta to allow different thresholds for each side
1 parent a7e596a commit 66aaa28

File tree

4 files changed

+111
-4
lines changed

4 files changed

+111
-4
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ Spread `handlers` onto the element you wish to track swipes on.
5555
}
5656
```
5757

58+
#### Delta
59+
60+
You can also set a different delta for each side:
61+
62+
```js
63+
{
64+
delta: { right: 10, left: 10, top: 20, bottom: 20 } // right and left starts when ">= 10", top and bottom when ">= 20"
65+
}
66+
```
67+
5868
## Swipe Event Data
5969

6070
All Event Handlers are called with the below event data, `SwipeEventData`.

__tests__/useSwipeable.spec.tsx

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,81 @@ describe("useSwipeable", () => {
351351
expect(onSwipedRight).not.toHaveBeenCalled();
352352
});
353353

354+
it("allows different delta for each side", () => {
355+
const onSwipedRight = jest.fn();
356+
const onSwipedLeft = jest.fn();
357+
const onSwipedUp = jest.fn();
358+
const onSwipedDown = jest.fn();
359+
const { getByText } = render(
360+
<SwipeableUsingHook
361+
onSwipedRight={onSwipedRight}
362+
onSwipedLeft={onSwipedLeft}
363+
onSwipedUp={onSwipedUp}
364+
onSwipedDown={onSwipedDown}
365+
delta={{
366+
right: 10,
367+
left: 20,
368+
up: 30,
369+
down: 40,
370+
}}
371+
/>
372+
);
373+
374+
const touchArea = getByText(TESTING_TEXT);
375+
376+
// check right
377+
fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
378+
fireEvent[TM](touchArea, cte({ x: 105, y: 100 }));
379+
fireEvent[TE](touchArea, cte({}));
380+
381+
expect(onSwipedRight).not.toHaveBeenCalled();
382+
383+
fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
384+
fireEvent[TM](touchArea, cte({ x: 110, y: 100 }));
385+
fireEvent[TE](touchArea, cte({}));
386+
387+
expect(onSwipedRight).toHaveBeenCalledTimes(1);
388+
389+
// check left
390+
fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
391+
fireEvent[TM](touchArea, cte({ x: 90, y: 100 }));
392+
fireEvent[TE](touchArea, cte({}));
393+
394+
expect(onSwipedLeft).not.toHaveBeenCalled();
395+
396+
fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
397+
fireEvent[TM](touchArea, cte({ x: 80, y: 100 }));
398+
fireEvent[TE](touchArea, cte({}));
399+
400+
expect(onSwipedLeft).toHaveBeenCalledTimes(1);
401+
402+
// check up
403+
fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
404+
fireEvent[TM](touchArea, cte({ x: 100, y: 80 }));
405+
fireEvent[TE](touchArea, cte({}));
406+
407+
expect(onSwipedUp).not.toHaveBeenCalled();
408+
409+
fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
410+
fireEvent[TM](touchArea, cte({ x: 100, y: 70 }));
411+
fireEvent[TE](touchArea, cte({}));
412+
413+
expect(onSwipedUp).toHaveBeenCalledTimes(1);
414+
415+
// check down
416+
fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
417+
fireEvent[TM](touchArea, cte({ x: 100, y: 130 }));
418+
fireEvent[TE](touchArea, cte({}));
419+
420+
expect(onSwipedDown).not.toHaveBeenCalled();
421+
422+
fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
423+
fireEvent[TM](touchArea, cte({ x: 100, y: 140 }));
424+
fireEvent[TE](touchArea, cte({}));
425+
426+
expect(onSwipedDown).toHaveBeenCalledTimes(1);
427+
});
428+
354429
it("Handle Rotation by 90 degree", () => {
355430
const swipeFuncsRight = getMockedSwipeFunctions();
356431
const { getByText, rerender } = render(

src/index.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ function getDirection(
7070
return UP;
7171
}
7272

73+
function uncapitalizeDirection(
74+
dir: SwipeDirections
75+
): Uncapitalize<SwipeDirections> {
76+
switch (dir) {
77+
case "Down":
78+
return "down";
79+
case "Left":
80+
return "left";
81+
case "Right":
82+
return "right";
83+
case "Up":
84+
return "up";
85+
}
86+
}
87+
7388
function rotateXYByAngle(pos: Vector2, angle: number): Vector2 {
7489
if (angle === 0) return pos;
7590
const angleInRadians = (Math.PI / 180) * angle;
@@ -131,11 +146,15 @@ function getHandlers(
131146
const velocity = Math.sqrt(absX * absX + absY * absY) / (time || 1);
132147
const vxvy: Vector2 = [deltaX / (time || 1), deltaY / (time || 1)];
133148

149+
const dir = getDirection(absX, absY, deltaX, deltaY);
150+
134151
// if swipe is under delta and we have not started to track a swipe: skip update
135-
if (absX < props.delta && absY < props.delta && !state.swiping)
136-
return state;
152+
const delta =
153+
typeof props.delta === "number"
154+
? props.delta
155+
: props.delta[uncapitalizeDirection(dir)];
156+
if (absX < delta && absY < delta && !state.swiping) return state;
137157

138-
const dir = getDirection(absX, absY, deltaX, deltaY);
139158
const eventData = {
140159
absX,
141160
absY,

src/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ export type SwipeableCallbacks = {
4040
};
4141

4242
// Configuration Options
43+
export type ConfigurationOptionDelta =
44+
| number
45+
| { [key in Uncapitalize<SwipeDirections>]: number };
4346
export interface ConfigurationOptions {
44-
delta: number;
47+
delta: ConfigurationOptionDelta;
4548
preventDefaultTouchmoveEvent: boolean;
4649
rotationAngle: number;
4750
trackMouse: boolean;

0 commit comments

Comments
 (0)