Skip to content
Tutorials Center
My learning

Topics

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.

JSX
<div
  onPointerDown={(e) => e.currentTarget.setPointerCapture(e.pointerId)}
  onPointerMove={handleMove}
  onPointerUp={handleUp}
/>

Try it

A draggable box with pointer capture

pointerType lets you adapt to the device, and buttons is a bit mask of the pressed buttons.

Reporting the pointer type

Syntax

JSX
<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 (or pan-y and similar) on draggable elements. Otherwise the browser may start scrolling and send pointercancel instead of more moves.
  • Handle onPointerCancel like onPointerUp; the system can take a gesture away at any time.
  • Capture is released automatically on pointerup or pointercancel; call releasePointerCapture only if you want to stop early.
  • onPointerEnter and onPointerLeave do not bubble; Over and Out do. 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 a Map kept in a ref.
  • Prefer pointer events over mouse and touch events in new code; they are supported by all current browsers.