React Reference ยท Props and Attributes
React pointer events
Reference for React pointer events (onPointerDown, onPointerMove, onPointerUp): pointer capture, pointerType, touch-action and a draggable example.
Pointer events are one set of events for mouse, pen and touch input. Instead of writing separate mouse and touch handlers, use onPointerDown, onPointerMove and onPointerUp; each event tells you which device it came from (pointerType) and where it is (clientX, clientY).
For dragging, call setPointerCapture on the element during pointerdown. All later events for that pointer then go to the element even when the pointer leaves it, so you do not need listeners on document.
<div onPointerDown={(e) => e.currentTarget.setPointerCapture(e.pointerId)} onPointerMove={handleMove} onPointerUp={handleUp} />
Try it
pointerType lets you adapt to the device, and buttons is a bit mask of the pressed buttons.
Syntax
<element onPointerDown onPointerMove onPointerUp onPointerCancel
onPointerEnter onPointerLeave onPointerOver onPointerOut
onGotPointerCapture onLostPointerCapture />
| Property | Description |
|---|---|
pointerId |
Identifies one pointer (one finger, the mouse, a pen). Use it with setPointerCapture and releasePointerCapture. |
pointerType |
"mouse", "pen" or "touch". |
clientX, clientY |
Position relative to the viewport. |
buttons |
Bit mask of the pressed buttons (1 = primary). |
isPrimary |
true for the main pointer of its type, useful to ignore extra fingers. |
pressure, width, height |
Pen pressure and contact size, where supported. |
Usage notes
- Set
touch-action: none(orpan-yand similar) on draggable elements. Otherwise the browser may start scrolling and sendpointercancelinstead of more moves. - Handle
onPointerCancellikeonPointerUp; the system can take a gesture away at any time. - Capture is released automatically on
pointeruporpointercancel; callreleasePointerCaptureonly if you want to stop early. onPointerEnterandonPointerLeavedo not bubble;OverandOutdo. Use Enter and Leave for hover effects on one element.- Prevent text selection while dragging with
user-select: none. - Compute offsets from the start point, as in the example, so the box does not jump to the pointer.
- Multi-touch: each finger has its own
pointerId; track them in aMapkept in a ref. - Prefer pointer events over mouse and touch events in new code; they are supported by all current browsers.