Usage
Reports a drag on an element as values, for a surface the product moves itself: a card that follows the finger, a drawer pulled by its handle. The drag claims its touch at UIKit’s 10 pt, as UIPanGestureRecognizer does, so a tap or a scroll that starts on the element stays a tap or a scroll, and a press or a long press is never reported as a pan. The hook is a thin layer over createPanGesture, the same API for code without React. A drag has no keyboard or assistive-technology path of its own: whatever the drag does needs another way to do it, such as buttons or arrow keys (WCAG 2.5.7), and useDragToSnapPoints provides settleTo for that. The pan moves nothing itself, so Reduce Motion applies to the motion you drive from it: animate with useAnimator, which plans every animation for Reduce Motion. Where a component already offers the behavior, use its prop instead: BottomSheet’s snapPoints, Carousel’s hasSnap.
tsimport {usePanGesture} from '@astryxdesign/core/hooks'
Best practices
| Guidance | Practices |
|---|---|
| Do | Move the element from |
| Do | At |
| Do | Give every drag another way to do the same thing from a keyboard and from assistive technology, such as buttons, arrow keys or a menu. |
| Do | Use a component's own prop when it covers the job: BottomSheet's |
| Don't | Set a new React state on every |
| Don't | Recognize taps or long presses with it; |
Parameters
| Param | Type | Description |
|---|---|---|
refrequired | RefObject<Element | null> | The element to drag. The hook creates one pan for the element behind the ref, creates a new one when the ref moves to another node, and destroys it at unmount. |
optionsrequired | Configuration object. | |
options.axis | 'x' | 'y' | Limits the drag to one axis. A drag along the other axis is not reported, and that component of every value reads 0. Omit it to report both axes. |
options.onPanrequired | (event: ) => void | Called with |
Returns
| Field | Type | Description |
|---|---|---|
| void | void | Nothing: the hook hands out no recognizer. Without React, |
Use with shadcn
Already using the shadcn registry workflow? Install the real Astryx package and a local public re-export. Component implementation source stays in Astryx.
This install URL expires with the draft preview.bashnpx shadcn@4.19.0 add https://astryx-c3nxq2ics-fbopensource.vercel.app/shadcn/hooks/use-pan-gesture.json