kipluo
React8 min read3 steps

How to Create Interactive Dragging with Pointer Events

Stop duplicating onMouseDown and onTouchStart handlers. The Pointer Events API unifies mouse, touch, and stylus dragging—if you handle capture, cancellation, and touch-action correctly.

You needReact 18+Basic DOM Events
Loading demo
Drag any handle with mouse or touch, or press Space + Arrow keys to reorder with spring displacement.Component page

1. Why setPointerCapture beats window listeners

Historically, developers attached mousemove and mouseup to window so fast drags wouldn't break when the cursor left the element. With Pointer Events, calling e.currentTarget.setPointerCapture(e.pointerId) routes all subsequent pointermove, pointerup, and pointercancel events directly to your element until release.

DraggablePill.tsx
import { useRef } from "react";

export function DraggablePill() {
  const boxRef = useRef<HTMLDivElement>(null);
  const drag = useRef({ active: false, pointerId: -1, ox: 0, oy: 0, x: 0, y: 0 });

  return (
    <div
      ref={boxRef}
      style={{ touchAction: "none" }}
      onPointerDown={(e) => {
        if (e.button !== 0) return; // Primary button / touch only
        e.currentTarget.setPointerCapture(e.pointerId);
        drag.current = {
          active: true,
          pointerId: e.pointerId,
          ox: e.clientX - drag.current.x,
          oy: e.clientY - drag.current.y,
          x: drag.current.x,
          y: drag.current.y,
        };
      }}
      onPointerMove={(e) => {
        if (!drag.current.active || e.pointerId !== drag.current.pointerId) return;
        drag.current.x = e.clientX - drag.current.ox;
        drag.current.y = e.clientY - drag.current.oy;
        if (boxRef.current) {
          boxRef.current.style.transform = `translate3d(${drag.current.x}px, ${drag.current.y}px, 0)`;
        }
      }}
      onPointerUp={(e) => {
        if (e.pointerId === drag.current.pointerId) drag.current.active = false;
      }}
      onPointerCancel={(e) => {
        if (e.pointerId === drag.current.pointerId) drag.current.active = false;
      }}
      className="inline-flex cursor-grab select-none rounded-full border bg-white px-4 py-2 text-sm font-semibold shadow-sm active:cursor-grabbing"
    >
      Drag me
    </div>
  );
}

2. Always handle pointercancel (especially on mobile)

On iOS and Android, an incoming notification, OS edge-swipe, or browser scroll takeover fires pointercancel instead of pointerup. If you forget onPointerCancel, your drag state stays stuck forever.

Also check e.pointerId === activePointerId so a second finger resting on the screen cannot hijack an active single-finger drag.

3. Pick the right touch-action value

Use touch-action: none for 2D free drags (knobs, canvas nodes, color wheels), touch-action: pan-y for horizontal swipes inside vertical lists (carousels, swipe rows, card decks), and touch-action: pan-x for vertical drag sheets.

Accessibility checklist

Tick each one off as you check your build. Your ticks stay in this browser.

0 of 3 checked

Build it yourself or install it

Build it yourself when

You are building a custom canvas tool or diagram editor with domain-specific hit testing.

Install Kipluo when

You want ready-made gesture patterns (Sortable List, Card Stack, Swipe Action, Bottom Sheet, Carousel) with pointer capture and keyboard reordering already battle-tested.

Keep building