-
Notifications
You must be signed in to change notification settings - Fork 579
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #407 from antonioru/feat/use-long-press
feat(hook): introduces useLongPress hook
- Loading branch information
Showing
9 changed files
with
220 additions
and
42 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
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
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,87 @@ | ||
# useLongPress | ||
|
||
A hook that facilitates the implementation of a long press functionality on a given target, supporting both mouse and touch events. | ||
|
||
### Why? 💡 | ||
|
||
- Provides an easy way to add long-press functionality to a specific target element | ||
- Automatically adds mouse event listeners to the specified target element | ||
- Automatically removes the listeners when the component unmounts | ||
- Enables abstractions on mouse-related and touch-related events | ||
|
||
### Basic Usage: | ||
|
||
```jsx harmony | ||
import { useRef, useState } from 'react'; | ||
import { Tag, Space, Typography, Alert } from 'antd'; | ||
import useLongPress from 'beautiful-react-hooks/useLongPress'; | ||
|
||
const MyComponent = () => { | ||
const [coordinates, setCoordinates] = useState([0, 0]); | ||
const ref = useRef(); | ||
const [longPressCount, setLongPressCount] = useState(0) | ||
const { isLongPressing, onLongPressStart, onLongPressEnd } = useLongPress(ref); | ||
|
||
onLongPressStart(() => { | ||
setLongPressCount(() => { | ||
return longPressCount + 1 | ||
}); | ||
}); | ||
|
||
onLongPressEnd(() => { | ||
setLongPressCount(() => { | ||
return longPressCount + 1 | ||
}); | ||
}) | ||
|
||
return ( | ||
<DisplayDemo title="useLongPress"> | ||
<div ref={ref}> | ||
<Space direction="vertical"> | ||
<Alert message="Long press this box to get information on the long press event" type="info" showIcon /> | ||
<Tag color={isLongPressing ? 'green' : 'red'}>isLongPressing: {isLongPressing ? 'yes' : 'no'}</Tag> | ||
{!!longPressCount && ( | ||
<Typography.Paragraph> | ||
Long press events count: | ||
<Tag color="green">{longPressCount}</Tag> | ||
</Typography.Paragraph> | ||
)} | ||
</Space> | ||
</div> | ||
</DisplayDemo> | ||
); | ||
}; | ||
|
||
<MyComponent /> | ||
``` | ||
|
||
### Press duration: | ||
|
||
You can specify the duration of the long press by passing a number as the second argument to the hook. | ||
|
||
```jsx harmony | ||
import { useRef, useState } from 'react'; | ||
import { Tag, Space, Typography, Alert } from 'antd'; | ||
import useLongPress from 'beautiful-react-hooks/useLongPress'; | ||
|
||
const MyComponent = () => { | ||
const [coordinates, setCoordinates] = useState([0, 0]); | ||
const ref = useRef(); | ||
const { isLongPressing } = useLongPress(ref, 1000); | ||
|
||
return ( | ||
<DisplayDemo title="useLongPress"> | ||
<div ref={ref}> | ||
<Space direction="vertical"> | ||
<Alert message="Long press this box to get information on the long press event" type="info" showIcon /> | ||
<Tag color={isLongPressing ? 'green' : 'red'}>isLongPressing: {isLongPressing ? 'yes' : 'no'}</Tag> | ||
</Space> | ||
</div> | ||
</DisplayDemo> | ||
); | ||
}; | ||
|
||
<MyComponent /> | ||
``` | ||
|
||
<!-- Types --> |
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
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,64 @@ | ||
import { type RefObject, useCallback, useState } from 'react' | ||
import useMouseEvents from './useMouseEvents' | ||
import useConditionalTimeout from './useConditionalTimeout' | ||
import createHandlerSetter from './factory/createHandlerSetter' | ||
import useTouchEvents from './useTouchEvents' | ||
import { type CallbackSetter } from './shared/types' | ||
|
||
/** | ||
* A hook that facilitates the implementation of the long press functionality on a given target, supporting both mouse and touch events. | ||
*/ | ||
const useLongPress = <TElement extends HTMLElement>(target: RefObject<TElement>, duration = 500) => { | ||
const { onMouseDown, onMouseUp, onMouseLeave } = useMouseEvents<TElement>(target, false) | ||
const { onTouchStart, onTouchEnd } = useTouchEvents(target, false) | ||
const [isLongPressing, setIsLongPressing] = useState(false) | ||
const [timerOn, startTimer] = useState(false) | ||
const [onLongPressStart, setOnLongPressStart] = createHandlerSetter<void>() | ||
const [onLongPressEnd, setOnLongPressEnd] = createHandlerSetter<void>() | ||
|
||
const longPressStart = useCallback((event: MouseEvent | TouchEvent) => { | ||
event.preventDefault() | ||
startTimer(true) | ||
}, []) | ||
|
||
const longPressStop = useCallback((event: MouseEvent | TouchEvent) => { | ||
if (!isLongPressing) return | ||
clearTimeout() | ||
setIsLongPressing(false) | ||
startTimer(false) | ||
event.preventDefault() | ||
|
||
if (onLongPressEnd?.current) { | ||
onLongPressEnd.current() | ||
} | ||
}, [isLongPressing]) | ||
|
||
const [, clearTimeout] = useConditionalTimeout(() => { | ||
setIsLongPressing(true) | ||
|
||
if (onLongPressStart?.current) { | ||
onLongPressStart.current() | ||
} | ||
}, duration, timerOn) | ||
|
||
onMouseDown(longPressStart) | ||
onMouseLeave(longPressStop) | ||
onMouseUp(longPressStop) | ||
|
||
onTouchStart(longPressStart) | ||
onTouchEnd(longPressStop) | ||
|
||
return Object.freeze<UseLongPressResult>({ | ||
isLongPressing, | ||
onLongPressStart: setOnLongPressStart, | ||
onLongPressEnd: setOnLongPressEnd | ||
}) | ||
} | ||
|
||
export interface UseLongPressResult { | ||
isLongPressing: boolean | ||
onLongPressStart: CallbackSetter<void> | ||
onLongPressEnd: CallbackSetter<void> | ||
} | ||
|
||
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,18 @@ | ||
import { cleanup, renderHook } from '@testing-library/react-hooks' | ||
import useLongPress from '../dist/useLongPress' | ||
import assertHook from './utils/assertHook' | ||
|
||
describe('useLongPress', () => { | ||
beforeEach(() => cleanup()) | ||
|
||
assertHook(useLongPress) | ||
|
||
it('should return a boolean value reporting whether the long-press event is happening as well as the handlers setters', () => { | ||
const ref = { current: document.createElement('div') } | ||
const { result } = renderHook(() => useLongPress(ref)) | ||
|
||
expect(result.current.isLongPressing).to.be.a('boolean') | ||
expect(result.current.onLongPressStart).to.be.a('function') | ||
expect(result.current.onLongPressEnd).to.be.a('function') | ||
}) | ||
}) |