'use client' import { useRef, useState, type ReactNode } from 'react' import { cn } from '../../lib/cn' import type { IconComponent } from '../../lib/types' export interface RowAction { id: string label: string icon: IconComponent onSelect: () => void tone?: 'neutral' | 'accent' | 'danger' } const TONES: Record, string> = { neutral: 'bg-surface-muted text-ink', accent: 'bg-accent text-accent-ink', danger: 'bg-danger text-white', } export interface SwipeRowProps { children: ReactNode /** Revealed by swiping right, i.e. sitting on the left edge. */ leading?: RowAction[] /** Revealed by swiping left. The destructive one belongs here. */ trailing?: RowAction[] /** Width of one action button, in pixels. */ actionWidth?: number /** Blocks interaction and dims the control. */ disabled?: boolean /** Merged last, so it wins. */ className?: string } /** * A list row that slides aside to reveal its actions. * * The actions are laid under the row rather than inside it, so the row itself * is one opaque surface on a single `translateX` — no clipping, no measuring, * and the actions do not reflow as it moves. Past a third of their width the * row snaps fully open; short of that it snaps shut, which is what makes the * gesture feel decided rather than approximate. * * The actions are real buttons in the DOM at all times, so Tab reaches them * whether or not anyone has swiped — and focusing one opens the row, so the * keyboard path shows exactly what the gesture would have shown. Hiding the * actions until a pointer drag is what usually makes this pattern unreachable. */ export function SwipeRow({ children, leading = [], trailing = [], actionWidth = 76, disabled = false, className, }: SwipeRowProps) { const [offset, setOffset] = useState(0) const [dragging, setDragging] = useState(false) const start = useRef<{ x: number; offset: number } | null>(null) const leadingWidth = leading.length * actionWidth const trailingWidth = trailing.length * actionWidth const close = () => setOffset(0) const onPointerDown = (event: React.PointerEvent) => { if (disabled || (leading.length === 0 && trailing.length === 0)) return start.current = { x: event.clientX, offset } setDragging(true) event.currentTarget.setPointerCapture(event.pointerId) } const onPointerMove = (event: React.PointerEvent) => { if (!start.current) return const next = start.current.offset + (event.clientX - start.current.x) setOffset(Math.max(-trailingWidth, Math.min(leadingWidth, next))) } const onPointerUp = () => { if (!start.current) return start.current = null setDragging(false) // A third of the way is enough to commit; anything less snaps back. if (offset > leadingWidth / 3) setOffset(leadingWidth) else if (offset < -trailingWidth / 3) setOffset(-trailingWidth) else setOffset(0) } const actionList = (actions: RowAction[], side: 'left' | 'right') => (
{actions.map((action) => ( ))}
) return (
{leading.length > 0 && actionList(leading, 'left')} {trailing.length > 0 && actionList(trailing, 'right')}
{children}
) }