kipluo
React7 min read3 steps

How to Implement Swipe-to-Delete in React

Swipe-to-delete feels effortless in native mobile apps and awful on the web when scroll-locking is skipped. Here is how to lock axes cleanly and expose a 100% keyboard-accessible companion button.

You needReact 18+CSS touch-action propertyPointer Events
Loading demo
Drag any inbox row left to reveal actions, or click the keyboard Actions button on the right.Component page

1. Axis-lock after a 6px slop threshold

Never move a list row on the very first pixel of pointermove. Record startX and startY on pointerdown, wait until Math.hypot(dx, dy) > 6, and then decide once: if |dx| > |dy|, lock to 'horizontal' and call setPointerCapture; otherwise lock to 'vertical' and let the browser scroll normally.

SwipeRow.tsx
import { useRef, useState } from "react";

const ACTION_WIDTH = 92;

export function SwipeRow({ title, onDelete }: { title: string; onDelete: () => void }) {
  const [offset, setOffset] = useState(0);
  const state = useRef<{ x0: number; y0: number; base: number; axis: "none" | "x" | "y" }>({
    x0: 0, y0: 0, base: 0, axis: "none",
  });

  const rubber = (x: number) => {
    if (x > 0) return x * 0.2; // Gentle resistance swiping right
    if (x < -ACTION_WIDTH) return -ACTION_WIDTH + (x + ACTION_WIDTH) * 0.28;
    return x;
  };

  return (
    <div className="relative overflow-hidden rounded-xl border">
      <div className="absolute inset-y-0 right-0 flex w-[92px] items-center justify-center bg-red-600 text-white">
        <button type="button" onClick={onDelete} className="h-full w-full text-xs font-bold">
          Delete
        </button>
      </div>
      <div
        style={{ transform: `translate3d(${offset}px, 0, 0)` }}
        className="relative flex items-center justify-between bg-white px-4 py-3 touch-pan-y transition-transform duration-200"
        onPointerDown={(e) => {
          state.current = { x0: e.clientX, y0: e.clientY, base: offset, axis: "none" };
        }}
        onPointerMove={(e) => {
          const dx = e.clientX - state.current.x0;
          const dy = e.clientY - state.current.y0;
          if (state.current.axis === "none") {
            if (Math.hypot(dx, dy) < 6) return;
            state.current.axis = Math.abs(dx) > Math.abs(dy) ? "x" : "y";
            if (state.current.axis === "x") e.currentTarget.setPointerCapture(e.pointerId);
          }
          if (state.current.axis === "x") setOffset(rubber(state.current.base + dx));
        }}
        onPointerUp={() => {
          if (state.current.axis !== "x") return;
          setOffset((cur) => (cur < -ACTION_WIDTH / 2 ? -ACTION_WIDTH : 0));
          state.current.axis = "none";
        }}
      >
        <span className="text-sm font-medium">{title}</span>
        <button
          type="button"
          aria-label={`Delete ${title}`}
          onClick={onDelete}
          className="rounded-md border px-2 py-1 text-xs"
        >
          Delete
        </button>
      </div>
    </div>
  );
}

2. Add rubber-band resistance past the detent

Hard-clamping a drag at -92px feels like hitting a brick wall. Multiplying overshoot past the action width by ~0.25–0.3 gives the physical tension users expect from iOS Mail and Slack.

3. Never make swipe the only way to delete

Screen-reader users, trackpad users, and keyboard navigators cannot reliably trigger touch swipes. Always render an explicit focusable button or menu trigger on the row and avoid hiding destructive actions behind pointer-only gestures.

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 have one simple list with a single delete action and basic CSS snap.

Install Kipluo when

You want true spring settlement without CSS transition snapping, velocity flick open/close, sound cues, and multi-action trays.

Keep building