Pointer Events, touch-action, and Pointer Capture
Pointer Events unify mouse, touch, and stylus input into one stream (pointerdown, pointermove, pointerup). Declaring CSS touch-action tells the compositor which gestures belong to the browser (preventing pointercancel mid-drag without sacrificing Passive Event Listeners) while setPointerCapture() locks fast drags to the handle.
Pointer Events, touch-action, and setPointerCapture
Trace compositor touch-action negotiation, pointer capture, and pointercancel prevention.
Before JS runs, compositor checks touch-action: pan-y on the slider track (horizontal drag owned by JS).
.slider-thumb { touch-action: pan-y; }- touchAction
- pan-y
- compositorOwns
- vertical scroll
- jsOwns
- horizontal drag
Under the Hood
At touchstart, the compositor thread reads CSS touch-action off the hit-tested layer to decide whether native pan/zoom owns the gesture without waiting for main-thread JS. Like painting dedicated bike lanes before rush hour, touch-action: none reserves the stream for your drag handler so the browser never fires pointercancel mid-drag.
- Compositor Pre-Check: At
touchstart, the compositor readstouch-action(none,pan-x,pan-y,manipulation) without waiting for JS. - No
pointercancel: Ontouch-action: none, the browser suppresses native pan/zoom sopointermovestreams cleanly. - Pointer Capture: Calling
el.setPointerCapture(e.pointerId)redirects all subsequent DOM Event Propagation for that pointer straight toeluntilpointerup.
knob.addEventListener('pointerdown', (e) => {
knob.setPointerCapture(e.pointerId);
knob.dataset.dragging = 'true';
});
knob.addEventListener('pointermove', (e) => {
if (knob.dataset.dragging === 'true') moveKnob(e.clientX);
});
knob.addEventListener('pointerup', () => delete knob.dataset.dragging);Problem Genealogy
- 2008 (Split Mouse & Touch Events): Developers duplicated
mousedown/mousemoveandtouchstart/touchmovehandlers with 300 ms tap delays. - 2015 (W3C Pointer Events): Unified input coordinates, pressure, tilt, and
pointerIdacross all devices. - 2019 (Universal
touch-action: manipulation): Eliminated the 300 ms double-tap zoom delay on interactive controls.
Production Pitfalls
- Missing
touch-actionon Sliders: Forgettingtouch-action: pan-yon a horizontal carousel causes mobile browsers to abort drags withpointercancel. - Orphaned Drag State: Relying on
pointerupon the knob withoutsetPointerCapture()drops the release event when the cursor leaves the element.
- When not to use
touch-action: none: Never applytouch-action: noneto large scrollable sections, it traps mobile users who try to scroll past the widget.
Connections & Logical Next Steps
Pointer events follow standard capture/target/bubble propagation unless redirected by setPointerCapture().
CSS touch-action negotiates scroll ownership on the compositor without blocking passive listeners.
Passive listeners promise no preventDefault() in JS, whereas CSS touch-action declares scroll constraints to the compositor.