-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# `useLongPress` | ||
|
||
React sensor hook that fires a callback after long pressing. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import { useLongPress } from 'react-use'; | ||
|
||
const Demo = () => { | ||
const onLongPress = () => { | ||
console.log('calls callback after long pressing 300ms'); | ||
}; | ||
|
||
const defaultDelay = 300; | ||
const longPressEvent = useLongPress(onLongPress, defaultDelay); | ||
|
||
return <button {...longPressEvent}>useLongPress</button>; | ||
}; | ||
``` | ||
|
||
## Reference | ||
|
||
```ts | ||
const { | ||
onMouseDown, | ||
onTouchStart, | ||
onMouseUp, | ||
onMouseLeave, | ||
onTouchEnd | ||
} = useLongPress( | ||
callback: (e: TouchEvent | MouseEvent) => void, | ||
delay?: number = 300 | ||
) | ||
``` | ||
|
||
- `callback` — callback function. | ||
- `delay` — delay in milliseconds after which to calls provided callback, defaults to `300`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { useCallback, useRef } from 'react'; | ||
|
||
const isTouchEvent = (event: Event): event is TouchEvent => { | ||
return 'touches' in event; | ||
}; | ||
|
||
const preventDefault = (event: Event) => { | ||
if (!isTouchEvent(event)) return; | ||
|
||
if (event.touches.length < 2 && event.preventDefault) { | ||
event.preventDefault(); | ||
} | ||
}; | ||
|
||
const useLongPress = (callback: (e: TouchEvent | MouseEvent) => void, delay: number = 300) => { | ||
const timeout = useRef<ReturnType<typeof setTimeout>>(); | ||
const target = useRef<EventTarget>(); | ||
|
||
const start = useCallback( | ||
(event: TouchEvent | MouseEvent) => { | ||
// prevent ghost click on mobile devices | ||
if (event.target) { | ||
target.current = event.target; | ||
event.target.addEventListener('touchend', preventDefault, { passive: false }); | ||
} | ||
|
||
timeout.current = setTimeout(() => callback(event), delay); | ||
}, | ||
[callback, delay] | ||
); | ||
|
||
const clear = useCallback(() => { | ||
// clearTimeout and removeEventListener | ||
timeout.current && clearTimeout(timeout.current); | ||
|
||
target.current && target.current.removeEventListener('touchend', preventDefault); | ||
}, []); | ||
|
||
return { | ||
onMouseDown: (e: any) => start(e), | ||
onTouchStart: (e: any) => start(e), | ||
onMouseUp: clear, | ||
onMouseLeave: clear, | ||
onTouchEnd: clear, | ||
} as const; | ||
}; | ||
|
||
export default useLongPress; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { storiesOf } from '@storybook/react'; | ||
import * as React from 'react'; | ||
import { useLongPress } from '../src'; | ||
import ShowDocs from './util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const onLongPress = () => { | ||
console.log('calls callback after long pressing 300ms'); | ||
}; | ||
|
||
const defaultDelay = 300; | ||
const longPressEvent = useLongPress(onLongPress, defaultDelay); | ||
|
||
return <button {...longPressEvent}>useLongPress</button>; | ||
}; | ||
|
||
storiesOf('Sensors|useLongPress', module) | ||
.add('Docs', () => <ShowDocs md={require('../docs/useLongPress.md')} />) | ||
.add('Demo', () => <Demo />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import useLongPress from '../src/useLongPress'; | ||
|
||
const callback = jest.fn(); | ||
const defaultDelay = 300; | ||
const mouseDown = new MouseEvent('mousedown'); | ||
const touchStart = new TouchEvent('touchstart'); | ||
|
||
beforeAll(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
callback.mockRestore(); | ||
jest.clearAllTimers(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it('should not call provided callback without trigger any event', () => { | ||
renderHook(() => useLongPress(callback)); | ||
|
||
expect(callback).toHaveBeenCalledTimes(0); | ||
|
||
jest.advanceTimersByTime(defaultDelay); | ||
|
||
expect(callback).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('should call provided callback onMouseDown', () => { | ||
const { result } = renderHook(() => useLongPress(callback)); | ||
const { onMouseDown } = result.current; | ||
|
||
expect(callback).toHaveBeenCalledTimes(0); | ||
onMouseDown(mouseDown); | ||
|
||
jest.advanceTimersByTime(defaultDelay - 20); | ||
expect(callback).toHaveBeenCalledTimes(0); | ||
|
||
jest.advanceTimersByTime(20); | ||
expect(callback).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should call provided callback with custom delay', () => { | ||
const customDelay = 1000; | ||
const { result } = renderHook(() => useLongPress(callback, customDelay)); | ||
const { onMouseDown } = result.current; | ||
|
||
expect(callback).toHaveBeenCalledTimes(0); | ||
onMouseDown(mouseDown); | ||
|
||
jest.advanceTimersByTime(customDelay - 20); | ||
expect(callback).toHaveBeenCalledTimes(0); | ||
|
||
jest.advanceTimersByTime(20); | ||
expect(callback).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should not call provided callback if interrupted by onMouseLeave', () => { | ||
const { result } = renderHook(() => useLongPress(callback)); | ||
const { onMouseDown, onMouseLeave } = result.current; | ||
|
||
expect(callback).toHaveBeenCalledTimes(0); | ||
onMouseDown(mouseDown); | ||
|
||
jest.advanceTimersByTime(defaultDelay - 20); | ||
expect(callback).toHaveBeenCalledTimes(0); | ||
|
||
onMouseLeave(); | ||
|
||
jest.advanceTimersByTime(20); | ||
expect(callback).toHaveBeenCalledTimes(0); | ||
expect(setTimeout).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should not call provided callback if interrupted by onMouseUp', () => { | ||
const { result } = renderHook(() => useLongPress(callback)); | ||
const { onMouseDown, onMouseUp } = result.current; | ||
|
||
expect(callback).toHaveBeenCalledTimes(0); | ||
onMouseDown(mouseDown); | ||
|
||
jest.advanceTimersByTime(defaultDelay - 20); | ||
expect(callback).toHaveBeenCalledTimes(0); | ||
|
||
onMouseUp(); | ||
|
||
jest.advanceTimersByTime(20); | ||
expect(callback).toHaveBeenCalledTimes(0); | ||
expect(setTimeout).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should call provided callback onTouchStart', () => { | ||
const customDelay = 1000; | ||
const { result } = renderHook(() => useLongPress(callback, customDelay)); | ||
const { onMouseDown } = result.current; | ||
|
||
expect(callback).toHaveBeenCalledTimes(0); | ||
onMouseDown(mouseDown); | ||
|
||
jest.advanceTimersByTime(customDelay - 20); | ||
expect(callback).toHaveBeenCalledTimes(0); | ||
|
||
jest.advanceTimersByTime(20); | ||
expect(callback).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should not call provided callback if interrupted by onTouchEnd', () => { | ||
const { result } = renderHook(() => useLongPress(callback)); | ||
const { onTouchStart, onTouchEnd } = result.current; | ||
|
||
expect(callback).toHaveBeenCalledTimes(0); | ||
onTouchStart(touchStart); | ||
|
||
jest.advanceTimersByTime(defaultDelay - 20); | ||
expect(callback).toHaveBeenCalledTimes(0); | ||
|
||
onTouchEnd(); | ||
|
||
jest.advanceTimersByTime(20); | ||
expect(callback).toHaveBeenCalledTimes(0); | ||
expect(setTimeout).toHaveBeenCalledTimes(1); | ||
}); |