Skip to content

Commit 3a0163b

Browse files
authored
setup onSwipeStart prop (#226)
1 parent 51435d4 commit 3a0163b

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,16 @@ Spread `handlers` onto the element you wish to track swipes on.
3333
onSwipedRight, // After RIGHT swipe (SwipeEventData) => void
3434
onSwipedUp, // After UP swipe (SwipeEventData) => void
3535
onSwipedDown, // After DOWN swipe (SwipeEventData) => void
36+
onSwipeStart, // Start of swipe (SwipeEventData) => void *see details*
3637
onSwiping, // During swiping (SwipeEventData) => void
3738
onTap, // After a tap ({ event }) => void
3839
}
3940
```
4041

42+
#### Details
43+
- `onSwipeStart` - called only once per swipe at the start and before the first `onSwiping` callback
44+
- The `first` property of the `SwipeEventData` will be `true`
45+
4146
### Configuration props and default values
4247

4348
```js

__tests__/useSwipeable.spec.tsx

+38-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from "react";
22
import { render, fireEvent, act } from "@testing-library/react";
33
import { useSwipeable } from "../src/index";
4-
import { LEFT, RIGHT, UP, DOWN } from "../src/types";
4+
import { LEFT, RIGHT, UP, DOWN, SwipeableProps } from "../src/types";
55
import { expectSwipeFuncsDir } from "./helpers";
66

77
const DIRECTIONS: [typeof LEFT, typeof RIGHT, typeof UP, typeof DOWN] = [
@@ -34,7 +34,10 @@ const TESTING_TEXT = "touch here";
3434
/*
3535
* Wrapping component for the hook testing
3636
*/
37-
function SwipeableUsingHook({ nodeName = "div", ...rest }) {
37+
function SwipeableUsingHook({
38+
nodeName = "div",
39+
...rest
40+
}: SwipeableProps & { nodeName?: string }) {
3841
const eventHandlers = useSwipeable(rest);
3942
const Elem = nodeName as React.ElementType;
4043
return (
@@ -222,6 +225,33 @@ describe("useSwipeable", () => {
222225
);
223226
});
224227

228+
it("correctly calls onSwipeStart for first swipe event", () => {
229+
const onSwipeStart = jest.fn();
230+
const { getByText } = render(
231+
<SwipeableUsingHook onSwipeStart={onSwipeStart} />
232+
);
233+
234+
const touchArea = getByText(TESTING_TEXT);
235+
236+
// first swipe
237+
fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
238+
fireEvent[TM](touchArea, cte({ x: 100, y: 125 }));
239+
fireEvent[TM](touchArea, cte({ x: 100, y: 150 }));
240+
fireEvent[TM](touchArea, cte({ x: 100, y: 175 }));
241+
fireEvent[TE](touchArea, cte({}));
242+
243+
expect(onSwipeStart).toHaveBeenCalledTimes(1);
244+
245+
// second swipe
246+
fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
247+
fireEvent[TM](touchArea, cte({ x: 125, y: 125 }));
248+
fireEvent[TM](touchArea, cte({ x: 150, y: 150 }));
249+
fireEvent[TM](touchArea, cte({ x: 175, y: 175 }));
250+
fireEvent[TE](touchArea, cte({}));
251+
252+
expect(onSwipeStart).toHaveBeenCalledTimes(2);
253+
});
254+
225255
it("calls preventDefault when swiping in direction that has a callback", () => {
226256
const onSwipedDown = jest.fn();
227257

@@ -346,22 +376,22 @@ describe("useSwipeable", () => {
346376
expectSwipeFuncsDir(swipeFuncsLeft, LEFT);
347377

348378
// check up
349-
const swipeFunsUp = getMockedSwipeFunctions();
350-
rerender(<SwipeableUsingHook {...swipeFunsUp} rotationAngle={90} />);
379+
const swipeFuncsUp = getMockedSwipeFunctions();
380+
rerender(<SwipeableUsingHook {...swipeFuncsUp} rotationAngle={90} />);
351381
fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
352382
fireEvent[TM](touchArea, cte({ x: 125, y: 100 }));
353383
fireEvent[TM](touchArea, cte({ x: 150, y: 100 }));
354384
fireEvent[TE](touchArea, cte({}));
355-
expectSwipeFuncsDir(swipeFunsUp, UP);
385+
expectSwipeFuncsDir(swipeFuncsUp, UP);
356386

357387
// check down
358-
const swipeFunsDown = getMockedSwipeFunctions();
359-
rerender(<SwipeableUsingHook {...swipeFunsDown} rotationAngle={90} />);
388+
const swipeFuncsDown = getMockedSwipeFunctions();
389+
rerender(<SwipeableUsingHook {...swipeFuncsDown} rotationAngle={90} />);
360390
fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
361391
fireEvent[TM](touchArea, cte({ x: 75, y: 100 }));
362392
fireEvent[TM](touchArea, cte({ x: 50, y: 100 }));
363393
fireEvent[TE](touchArea, cte({}));
364-
expectSwipeFuncsDir(swipeFunsDown, DOWN);
394+
expectSwipeFuncsDir(swipeFuncsDown, DOWN);
365395
});
366396

367397
it('Handle "odd" rotations', () => {

src/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ function getHandlers(
149149
vxvy,
150150
};
151151

152+
// call onSwipeStart if present and is first swipe event
153+
eventData.first && props.onSwipeStart && props.onSwipeStart(eventData);
154+
155+
// Call onSwiping if present
152156
props.onSwiping && props.onSwiping(eventData);
153157

154158
// track if a swipe is cancelable(handler for swiping or swiped(dir) exists)

src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export type TapCallback = ({ event }: { event: HandledEvents }) => void;
2929

3030
export type SwipeableCallbacks = {
3131
// Event handler/callbacks
32+
onSwipeStart: SwipeCallback;
3233
onSwiped: SwipeCallback;
3334
onSwipedDown: SwipeCallback;
3435
onSwipedLeft: SwipeCallback;

0 commit comments

Comments
 (0)