the numbers are the whole component: the first action appears at 44px of travel, the second at 88, and the gesture commits past 58 and snaps back under it. pixels rather than fractions of the row, because a fraction makes the gesture change length with the screen and the thresholds stop being tunable by feel.
deciding the direction
the part that is actually hard is the axis lock. the first few pixels decide whether this is a horizontal swipe or a vertical scroll, and once decided it must not flip, or a diagonal drag hands the gesture back and forth between the row and the list and neither of them wins.
stopping where you expect
momentum is off on purpose too: with it on, a fast flick sails past the open position and gets pulled back, which reads as a bounce nobody asked for.
take it with you
three steps, and the stage above runs in your own project.
install
nothing to add. it runs on react alone, 18 or newer.
copy
4 files, kept together in one folder, swipe-actions/. the styles travel with them.
"use client";import { useRef, useState, type PointerEvent, type TransitionEvent } from "react";import { useReducedMotion } from "./motion";import { lockAxis, restingX, revealProgress, type Axis } from "./thresholds";import "./swipe-actions.css";export interface SwipeRowProps { label: string; // px of drag past which the row stays open on release commit?: number; // px of drag that fully shows the first action; the second follows over the same distance firstReveal?: number; onArchive: () => void; onDelete: () => void; // fires as a drag crosses the commit line either way: hook a haptic or a sound here onCross?: () => void;
}const OPEN = 116;type Leaving = "archive" | "delete";export default function SwipeRow({ label, commit = 58, firstReveal = 44, onArchive, onDelete, onCross,
}: SwipeRowProps) { const reduced = useReducedMotion(); const [x, setX] = useState(0); const [dragging, setDragging] = useState(false); const [leaving, setLeaving] = useState<Leaving | null>(null); const startRef = useRef({ x: 0, y: 0, from: 0 }); const axisRef = useRef<Axis>(null); const draggingRef = useRef(false); const onPointerDown = (e: PointerEvent<HTMLDivElement>) => { if (reduced || leaving) return; e.currentTarget.setPointerCapture(e.pointerId); startRef.current = { x: e.clientX, y: e.clientY, from: x }; axisRef.current = null; draggingRef.current = true; setDragging(true);
}; const onPointerMove = (e: PointerEvent<HTMLDivElement>) => { if (!draggingRef.current) return; const { x: sx, y: sy, from } = startRef.current; const dx = e.clientX - sx; const dy = e.clientY - sy; if (axisRef.current === null) axisRef.current = lockAxis(dx, dy); if (axisRef.current !== "x") return; const next = Math.min(0, Math.max(-OPEN - 24, from + dx)); if (-x >= commit !== -next >= commit) onCross?.(); setX(next);
}; const onPointerUp = () => { if (!draggingRef.current) return; draggingRef.current = false; setDragging(false); setX(restingX(x, { commit, open: OPEN }));
}; const dismiss = (kind: Leaving) => { if (reduced) {
(kind === "delete" ? onDelete : onArchive)(); return;
} if (leaving) return; setLeaving(kind);
}; const onTransitionEnd = (e: TransitionEvent<HTMLDivElement>) => { if (e.target !== e.currentTarget || e.propertyName !== "grid-template-rows") return; if (!leaving) return;
(leaving === "delete" ? onDelete : onArchive)();
}; const progress = revealProgress(x, firstReveal, firstReveal * 2); const shown = progress.filter((p) => p === 1).length; const revealStyle = (p: number) => leaving ? undefined : { opacity: p, transform: `translateX(${(1 - p) * 8}px)` }; return (
<div className="swipe-row" data-shown={shown} data-dragging={String(dragging)}
{...(leaving ? { "data-leaving": leaving } : {})} onTransitionEnd={onTransitionEnd}
>
<div className="swipe-row-clip">
<div className="swipe-actions">
<button type="button" className="swipe-action" style={revealStyle(progress[0])} onClick={() => dismiss("archive")}
> archive </button>
<button type="button" className="swipe-action swipe-action--bad" style={revealStyle(progress[1])} onClick={() => dismiss("delete")}
> delete </button>
</div> <div className="swipe-face" style={{ transform: leaving ? undefined : `translateX(${x}px)` }} onPointerDown={onPointerDown} onPointerMove={onPointerMove} onPointerUp={onPointerUp} onPointerCancel={onPointerUp}
>
<span>{label}</span>
</div>
</div>
</div>
);
}
use
the stage above, exactly as it runs here. the comments mark what to change.