What Pointer Events are supported in React?
In React, Pointer Events provide a way to handle mouse, touch, and pen interactions consistently across devices. React implements a wrapper around the native Pointer Events API for better cross-browser support and event normalization.
React supports the following pointer events:
Event Name | Description |
---|---|
onPointerDown | Fired when a pointer (mouse, touch, pen) is pressed down. |
onPointerMove | Fired when a pointer moves across the screen. |
onPointerUp | Fired when a pointer is released (mouse button up, touch ends, etc.). |
onPointerCancel | Fired when the pointer event is canceled (e.g., by the browser). |
onPointerEnter | Fired when a pointer enters the boundaries of an element (similar to onMouseEnter ). |
onPointerLeave | Fired when a pointer leaves the boundaries of an element (similar to onMouseLeave ). |
onPointerOver | Fired when a pointer is moved onto an element or one of its children. |
onPointerOut | Fired when a pointer is moved off an element or one of its children. |
onGotPointerCapture | Fired when an element captures a pointer using setPointerCapture() . |
onLostPointerCapture | Fired when an element loses pointer capture. |
Key Points to Remember:
- Pointer events are more powerful than regular mouse or touch events because they work across different input types (mouse, touch, pen).
- They can help reduce the need for separate handling of
onMouseDown
,onTouchStart
, etc. - React Synthetic Events wrap native pointer events to ensure cross-browser compatibility.
Example:
function PointerExample() { const handlePointerDown = (event) => { console.log('Pointer down:', event.pointerType); }; return ( <div onPointerDown={handlePointerDown} style={{ width: 200, height: 200, backgroundColor: 'lightblue' }} > Click, touch, or use a pen here! </div> ); }
In this example, no matter if the user clicks with a mouse, touches the screen, or uses a pen — the same event handler will be triggered!