|
| 1 | +# useLongPress |
| 2 | + |
| 3 | +`useLongPress` is a React hook that detects when an element is pressed and held for a specified duration. It handles both mouse and touch events, making it work consistently across desktop and mobile devices. |
| 4 | + |
| 5 | +## Interface |
| 6 | + |
| 7 | +```ts |
| 8 | +function useLongPress<E extends HTMLElement>( |
| 9 | + onLongPress: (event: React.MouseEvent<E> | React.TouchEvent<E>) => void, |
| 10 | + options: UseOptionsObject |
| 11 | +): Object; |
| 12 | +``` |
| 13 | + |
| 14 | +### Parameters |
| 15 | + |
| 16 | +<Interface |
| 17 | + required |
| 18 | + name="onLongPress" |
| 19 | + type="(event: React.MouseEvent<E> | React.TouchEvent<E>) => void" |
| 20 | + description="The callback function to be executed when a long press is detected." |
| 21 | +/> |
| 22 | +<Interface |
| 23 | + name="options" |
| 24 | + type="Object" |
| 25 | + description="Configuration options for the long press behavior." |
| 26 | + :nested="[ |
| 27 | + { |
| 28 | + name: 'options.delay', |
| 29 | + type: 'number', |
| 30 | + required: false, |
| 31 | + defaultValue: '500', |
| 32 | + description: 'The time in milliseconds before triggering the long press. Defaults to 500ms.' |
| 33 | + }, |
| 34 | + { |
| 35 | + name: 'options.moveThreshold', |
| 36 | + type: 'Object', |
| 37 | + required: false, |
| 38 | + description: 'Maximum movement allowed before canceling a long press.' |
| 39 | + }, |
| 40 | + { |
| 41 | + name: 'options.moveThreshold.x', |
| 42 | + type: 'number', |
| 43 | + required: false, |
| 44 | + description: 'Maximum horizontal movement in pixels.' |
| 45 | + }, |
| 46 | + { |
| 47 | + name: 'options.moveThreshold.y', |
| 48 | + type: 'number', |
| 49 | + required: false, |
| 50 | + description: 'Maximum vertical movement in pixels.' |
| 51 | + }, |
| 52 | + { |
| 53 | + name: 'options.onClick', |
| 54 | + type: '(event) => void', |
| 55 | + required: false, |
| 56 | + description: 'Optional function to execute on a normal click (press and release before delay).' |
| 57 | + }, |
| 58 | + { |
| 59 | + name: 'options.onLongPressEnd', |
| 60 | + type: '(event) => void', |
| 61 | + required: false, |
| 62 | + description: 'Optional function to execute when a long press ends.' |
| 63 | + } |
| 64 | + ]" |
| 65 | +/> |
| 66 | + |
| 67 | +### Return Value |
| 68 | + |
| 69 | +<Interface |
| 70 | + name="" |
| 71 | + type="Object" |
| 72 | + description="An object containing event handlers to spread onto a JSX element." |
| 73 | + :nested="[ |
| 74 | + { |
| 75 | + name: 'onMouseDown', |
| 76 | + type: 'function', |
| 77 | + description: 'Handler for mouse down events.' |
| 78 | + }, |
| 79 | + { |
| 80 | + name: 'onMouseUp', |
| 81 | + type: 'function', |
| 82 | + description: 'Handler for mouse up events.' |
| 83 | + }, |
| 84 | + { |
| 85 | + name: 'onMouseLeave', |
| 86 | + type: 'function', |
| 87 | + description: 'Handler for mouse leave events.' |
| 88 | + }, |
| 89 | + { |
| 90 | + name: 'onTouchStart', |
| 91 | + type: 'function', |
| 92 | + description: 'Handler for touch start events.' |
| 93 | + }, |
| 94 | + { |
| 95 | + name: 'onTouchEnd', |
| 96 | + type: 'function', |
| 97 | + description: 'Handler for touch end events.' |
| 98 | + }, |
| 99 | + { |
| 100 | + name: 'onTouchMove', |
| 101 | + type: 'function', |
| 102 | + description: 'Handler for touch move events (only included when moveThreshold is specified).' |
| 103 | + }, |
| 104 | + { |
| 105 | + name: 'onMouseMove', |
| 106 | + type: 'function', |
| 107 | + description: 'Handler for mouse move events (only included when moveThreshold is specified).' |
| 108 | + } |
| 109 | + ]" |
| 110 | +/> |
| 111 | + |
| 112 | +## Example |
| 113 | + |
| 114 | +```tsx |
| 115 | +import { useLongPress } from 'react-simplikit'; |
| 116 | +import { useState } from 'react'; |
| 117 | + |
| 118 | +function ContextMenu() { |
| 119 | + const [menuVisible, setMenuVisible] = useState(false); |
| 120 | + |
| 121 | + const longPressHandlers = useLongPress(() => setMenuVisible(true), { |
| 122 | + delay: 400, |
| 123 | + onClick: () => console.log('Normal click'), |
| 124 | + onLongPressEnd: () => console.log('Long press completed'), |
| 125 | + }); |
| 126 | + |
| 127 | + return ( |
| 128 | + <div> |
| 129 | + <button {...longPressHandlers}>Press and hold</button> |
| 130 | + {menuVisible && <div className="context-menu">Context Menu</div>} |
| 131 | + </div> |
| 132 | + ); |
| 133 | +} |
| 134 | +``` |
0 commit comments