"use client";
import {
useEffect,
useId,
useLayoutEffect,
useMemo,
useRef,
useState,
type FocusEvent as ReactFocusEvent,
type KeyboardEvent as ReactKeyboardEvent,
type MouseEvent as ReactMouseEvent,
type PointerEvent as ReactPointerEvent,
type ReactNode,
} from "react";
import {
AnimatePresence,
MotionConfig,
animate,
motion,
useIsPresent,
useMotionValue,
useReducedMotion,
useTransform,
type MotionValue,
} from "motion/react";
import { cn } from "@/lib/utils";
export interface SwipeAction {
id: string;
label: string;
icon?: ReactNode;
color: string;
dismiss?: boolean;
announce?: string;
}
export interface SwipeRow {
id: string;
title: string;
description?: string;
meta?: string;
}
export type SwipeActionList<T extends SwipeRow = SwipeRow> =
| SwipeAction[]
| ((row: T) => SwipeAction[]);
export interface SwipeActionsProps<T extends SwipeRow = SwipeRow> {
rows: T[];
leading?: SwipeActionList<T>;
trailing?: SwipeActionList<T>;
onAction?: (rowId: string, actionId: string) => void;
renderRow?: (row: T) => ReactNode;
revealWidth?: number;
fullSwipe?: boolean;
fullSwipeThreshold?: number;
rubberBand?: number;
velocityThreshold?: number;
stiffness?: number;
damping?: number;
reducedMotion?: boolean;
className?: string;
}
type Side = "leading" | "trailing";
interface Settings {
rows: SwipeRow[];
onAction?: (rowId: string, actionId: string) => void;
revealWidth: number;
fullSwipe: boolean;
fullSwipeThreshold: number;
rubberBand: number;
velocityThreshold: number;
stiffness: number;
damping: number;
reduce: boolean;
}
interface ListApi {
settings: { readonly current: Settings };
claim(id: string, close: () => void): void;
release(id: string): void;
touch(id: string): void;
move(id: string, to: number | "first" | "last"): void;
handoff(id: string): void;
announce(text: string): void;
activate(id: string): void;
register(id: string, el: HTMLElement): () => void;
}
const AXIS_SLOP = 6;
const AXIS_BIAS = 1.2;
const PROJECTION = 0.15;
const VELOCITY_WINDOW = 80;
const ARM_MARGIN = 24;
const ARM_HYSTERESIS = 12;
const LABEL_FROM = 40;
const LABEL_SPAN = 16;
const FLY_OUT = 1.05;
const REMOVAL_GRACE = 700;
const ARM_SPRING = { type: "spring" as const, stiffness: 700, damping: 50 };
const FLY_SPRING = { type: "spring" as const, stiffness: 900, damping: 60, restDelta: 1 };
const FOLD = { duration: 0.24, ease: [0.32, 0.72, 0, 1] as const };
export default function SwipeActions<T extends SwipeRow>({
rows,
leading,
trailing,
onAction,
renderRow,
revealWidth = 76,
fullSwipe = true,
fullSwipeThreshold = 60,
rubberBand = 0.55,
velocityThreshold = 500,
stiffness = 500,
damping = 42,
reducedMotion = false,
className,
}: SwipeActionsProps<T>) {
const reduce = Boolean(useReducedMotion()) || reducedMotion;
const rootRef = useRef<HTMLDivElement>(null);
const [activeId, setActiveId] = useState<string | null>(null);
const [message, setMessage] = useState({ text: "", key: 0 });
const settings = useRef<Settings>({
rows,
onAction,
revealWidth,
fullSwipe,
fullSwipeThreshold,
rubberBand,
velocityThreshold,
stiffness,
damping,
reduce,
});
useLayoutEffect(() => {
settings.current = {
rows,
onAction,
revealWidth,
fullSwipe,
fullSwipeThreshold,
rubberBand,
velocityThreshold,
stiffness,
damping,
reduce,
};
});
const openRow = useRef<{ id: string; close: () => void } | null>(null);
const targets = useRef(new Map<string, HTMLElement>());
const api = useMemo<ListApi>(() => {
const focus = (id: string, preventScroll: boolean) => {
setActiveId(id);
targets.current.get(id)?.focus({ preventScroll });
};
return {
settings,
claim(id, close) {
const prev = openRow.current;
if (prev && prev.id !== id) prev.close();
openRow.current = { id, close };
},
release(id) {
if (openRow.current?.id === id) openRow.current = null;
},
touch(id) {
const prev = openRow.current;
if (prev && prev.id !== id) prev.close();
},
move(id, to) {
const list = settings.current.rows;
if (list.length === 0) return;
const from = list.findIndex((row) => row.id === id);
const index =
to === "first"
? 0
: to === "last"
? list.length - 1
: Math.min(list.length - 1, Math.max(0, from + to));
const next = list[index];
if (next) focus(next.id, false);
},
handoff(id) {
const list = settings.current.rows;
const from = list.findIndex((row) => row.id === id);
const next = list[from + 1] ?? list[from - 1];
if (next && next.id !== id) focus(next.id, true);
},
announce(text) {
setMessage((prev) => ({ text, key: prev.key + 1 }));
},
activate(id) {
setActiveId(id);
},
register(id, el) {
targets.current.set(id, el);
return () => {
if (targets.current.get(id) === el) targets.current.delete(id);
};
},
};
}, []);
useEffect(() => {
const onDown = (event: PointerEvent) => {
const open = openRow.current;
if (!open) return;
const root = rootRef.current;
if (root && event.target instanceof Node && root.contains(event.target)) return;
open.close();
};
const onScroll = (event: Event) => {
const open = openRow.current;
const root = rootRef.current;
if (!open || !root) return;
const target = event.target;
if (target === document || (target instanceof Node && target.contains(root))) {
open.close();
}
};
document.addEventListener("pointerdown", onDown, true);
document.addEventListener("scroll", onScroll, { capture: true, passive: true });
return () => {
document.removeEventListener("pointerdown", onDown, true);
document.removeEventListener("scroll", onScroll, { capture: true });
};
}, []);
const tabId =
activeId !== null && rows.some((row) => row.id === activeId)
? activeId
: (rows[0]?.id ?? null);
return (
<MotionConfig reducedMotion={reduce ? "always" : "user"}>
<div ref={rootRef} className={cn("relative w-full", className)}>
<ul role="list" className="m-0 list-none p-0">
<AnimatePresence initial={false}>
{rows.map((row) => (
<Row
key={row.id}
id={row.id}
title={row.title}
content={renderRow ? renderRow(row) : <DefaultRow row={row} />}
leading={actionsFor(leading, row)}
trailing={actionsFor(trailing, row)}
tabbable={row.id === tabId}
buttonWidth={revealWidth}
reduce={reduce}
api={api}
/>
))}
</AnimatePresence>
</ul>
<p aria-live="polite" className="sr-only">
<span key={message.key}>{message.text}</span>
</p>
</div>
</MotionConfig>
);
}
function actionsFor<T extends SwipeRow>(
list: SwipeActionList<T> | undefined,
row: T,
): SwipeAction[] {
if (!list) return [];
return typeof list === "function" ? list(row) : list;
}
interface RowProps {
id: string;
title: string;
content: ReactNode;
leading: SwipeAction[];
trailing: SwipeAction[];
tabbable: boolean;
buttonWidth: number;
reduce: boolean;
api: ListApi;
}
interface Gesture {
pointer: number;
startX: number;
startY: number;
origin: number;
axis: "x" | "y" | null;
width: number;
armed: boolean;
samples: { t: number; x: number }[];
}
function Row({
id,
title,
content,
leading,
trailing,
tabbable,
buttonWidth,
reduce,
api,
}: RowProps) {
const present = useIsPresent();
const x = useMotionValue(0);
const arm = useMotionValue(0);
const [open, setOpen] = useState<Side | null>(null);
const bodyId = useId();
const hintId = useId();
const surfaceRef = useRef<HTMLDivElement>(null);
const contentRef = useRef<HTMLDivElement>(null);
const buttons = useRef(new Map<string, HTMLButtonElement>());
const gesture = useRef<Gesture | null>(null);
const unbind = useRef<(() => void) | null>(null);
const flight = useRef<(() => void) | null>(null);
const grace = useRef<number | undefined>(undefined);
const status = useRef({
open: null as Side | null,
target: 0,
committing: false,
suppressClick: false,
focusSide: null as Side | null,
});
const latest = useRef({ id, title, leading, trailing, present });
useLayoutEffect(() => {
latest.current = { id, title, leading, trailing, present };
});
useLayoutEffect(() => {
const el = contentRef.current;
if (!el) return;
return api.register(id, el);
}, [api, id]);
useEffect(() => {
const listeners = unbind;
const flying = flight;
const timer = grace;
return () => {
listeners.current?.();
flying.current?.();
window.clearTimeout(timer.current);
api.release(id);
};
}, [api, id]);
useLayoutEffect(() => {
if (!open || status.current.focusSide !== open) return;
status.current.focusSide = null;
focusInnermost(open);
});
function settings() {
return api.settings.current;
}
function actionsOn(side: Side) {
return side === "leading" ? latest.current.leading : latest.current.trailing;
}
function trayWidth(side: Side) {
return settings().revealWidth * actionsOn(side).length;
}
function glide(to: number, velocity = 0, onComplete?: () => void) {
status.current.target = to;
const s = settings();
if (s.reduce) {
x.jump(to);
onComplete?.();
return;
}
animate(x, to, {
type: "spring",
stiffness: s.stiffness,
damping: s.damping,
velocity,
restDelta: 0.5,
restSpeed: 10,
onComplete,
});
}
function snap(armed: boolean) {
if (settings().reduce) arm.jump(armed ? 1 : 0);
else animate(arm, armed ? 1 : 0, ARM_SPRING);
}
function trayHasFocus() {
const active = document.activeElement;
return (
!!active &&
active !== contentRef.current &&
!!surfaceRef.current?.contains(active)
);
}
function mark(side: Side | null) {
if (status.current.open === side) return;
status.current.open = side;
setOpen(side);
if (side) api.claim(latest.current.id, () => close(0, "none"));
else api.release(latest.current.id);
}
function close(velocity = 0, focus: "auto" | "row" | "none" = "auto") {
const pull = focus === "row" || (focus === "auto" && trayHasFocus());
glide(0, velocity);
mark(null);
if (pull) contentRef.current?.focus({ preventScroll: true });
}
function openTo(side: Side, velocity = 0) {
glide(signOf(side) * trayWidth(side), velocity);
mark(side);
}
function focusInnermost(side: Side) {
const count = actionsOn(side).length;
buttons.current.get(`${side}:${count - 1}`)?.focus({ preventScroll: true });
}
function fire(action: SwipeAction) {
settings().onAction?.(latest.current.id, action.id);
api.announce(`${action.announce ?? action.label}: ${latest.current.title}`);
}
function commit(side: Side, action: SwipeAction, velocity = 0) {
if (status.current.committing) return;
if (!action.dismiss) {
const pull = trayHasFocus();
fire(action);
glide(0, velocity, () => {
if (!gesture.current) arm.jump(0);
});
mark(null);
if (pull) contentRef.current?.focus({ preventScroll: true });
return;
}
status.current.committing = true;
if (surfaceRef.current?.contains(document.activeElement)) {
api.handoff(latest.current.id);
}
mark(null);
snap(true);
const width = surfaceRef.current?.offsetWidth ?? 0;
const away = signOf(side) * width * FLY_OUT;
status.current.target = away;
let fired = false;
const done = () => {
if (fired) return;
fired = true;
flight.current?.();
flight.current = null;
fire(action);
grace.current = window.setTimeout(() => {
if (!latest.current.present) return;
status.current.committing = false;
glide(0, 0, () => {
if (!gesture.current) arm.jump(0);
});
}, REMOVAL_GRACE);
};
if (settings().reduce) {
done();
return;
}
flight.current = x.on("change", (v) => {
if (Math.abs(v) >= width) done();
});
animate(x, away, { ...FLY_SPRING, velocity, onComplete: done });
}
function place(raw: number, g: Gesture) {
const s = settings();
const side: Side = raw < 0 ? "trailing" : "leading";
const count = actionsOn(side).length;
if (count === 0 || raw === 0) {
if (g.armed) {
g.armed = false;
snap(false);
}
return 0;
}
const reach = Math.abs(raw);
const tray = s.revealWidth * count;
let out: number;
if (reach <= tray) out = reach;
else if (s.fullSwipe) out = Math.min(reach, g.width);
else out = tray + rubber(reach - tray, s.rubberBand, g.width);
let armed = false;
if (s.fullSwipe) {
const at = Math.max((s.fullSwipeThreshold / 100) * g.width, tray + ARM_MARGIN);
armed = out >= (g.armed ? at - ARM_HYSTERESIS : at);
}
if (armed !== g.armed) {
g.armed = armed;
snap(armed);
}
return signOf(side) * out;
}
function onPointerDown(event: ReactPointerEvent<HTMLDivElement>) {
if (event.pointerType === "mouse" && event.button !== 0) return;
const surface = surfaceRef.current;
if (!surface || gesture.current || status.current.committing) return;
api.touch(latest.current.id);
status.current.suppressClick = false;
x.stop();
arm.jump(0);
gesture.current = {
pointer: event.pointerId,
startX: event.clientX,
startY: event.clientY,
origin: x.get(),
axis: null,
width: surface.offsetWidth,
armed: false,
samples: [],
};
const move = (e: PointerEvent) => onMove(e);
const up = (e: PointerEvent) => onEnd(e, true);
const cancel = (e: PointerEvent) => onEnd(e, false);
const lost = (e: PointerEvent) => {
if (e.target === surface) onEnd(e, false);
};
window.addEventListener("pointermove", move);
window.addEventListener("pointerup", up);
window.addEventListener("pointercancel", cancel);
surface.addEventListener("lostpointercapture", lost);
unbind.current = () => {
window.removeEventListener("pointermove", move);
window.removeEventListener("pointerup", up);
window.removeEventListener("pointercancel", cancel);
surface.removeEventListener("lostpointercapture", lost);
};
}
function onMove(event: PointerEvent) {
const g = gesture.current;
if (!g || event.pointerId !== g.pointer) return;
if (event.pointerType === "mouse" && event.buttons === 0) {
onEnd(event, false);
return;
}
const dx = event.clientX - g.startX;
const dy = event.clientY - g.startY;
if (!g.axis) {
if (Math.hypot(dx, dy) < AXIS_SLOP) return;
if (Math.abs(dx) > AXIS_BIAS * Math.abs(dy)) {
g.axis = "x";
status.current.suppressClick = true;
try {
surfaceRef.current?.setPointerCapture(g.pointer);
} catch {}
} else {
g.axis = "y";
onEnd(event, false);
return;
}
}
const shown = place(g.origin + dx, g);
x.set(shown);
g.samples.push({ t: event.timeStamp, x: shown });
if (g.samples.length > 16) g.samples.shift();
}
function onEnd(event: PointerEvent, released: boolean) {
const g = gesture.current;
if (!g || event.pointerId !== g.pointer) return;
gesture.current = null;
unbind.current?.();
unbind.current = null;
if (g.axis !== "x") {
if (g.axis === "y" && status.current.open) close();
else glide(status.current.target);
return;
}
const s = settings();
const v = released ? velocityOf(g.samples, event.timeStamp) : 0;
const at = x.get();
const side: Side | null = at < 0 ? "trailing" : at > 0 ? "leading" : null;
if (released && g.armed && side) {
const primary = actionsOn(side)[0];
if (primary) {
commit(side, primary, v);
return;
}
}
if (g.armed) snap(false);
if (!side) {
close(v);
return;
}
const sign = signOf(side);
const keep =
Math.abs(v) > s.velocityThreshold
? sign * v > 0
: sign * (at + v * PROJECTION) > trayWidth(side) / 2;
if (keep) openTo(side, v);
else close(v);
}
function onClickCapture(event: ReactMouseEvent<HTMLDivElement>) {
if (status.current.suppressClick) {
status.current.suppressClick = false;
event.preventDefault();
event.stopPropagation();
return;
}
if (status.current.open) {
event.preventDefault();
event.stopPropagation();
close();
}
}
function onBlur(event: ReactFocusEvent<HTMLDivElement>) {
const next = event.relatedTarget;
if (!(next instanceof Node) || surfaceRef.current?.contains(next)) return;
if (status.current.open && !status.current.committing) close(0, "none");
}
function reveal(side: Side) {
api.touch(latest.current.id);
arm.jump(0);
if (status.current.open === side) {
focusInnermost(side);
return;
}
status.current.focusSide = side;
openTo(side);
}
function onKeyDown(event: ReactKeyboardEvent<HTMLDivElement>) {
if (event.target !== event.currentTarget) return;
if (event.altKey || event.ctrlKey || event.metaKey || status.current.committing) return;
const key = event.key;
if (key === "ArrowDown" || key === "ArrowUp") {
event.preventDefault();
api.move(latest.current.id, key === "ArrowDown" ? 1 : -1);
return;
}
if (key === "Home" || key === "End") {
event.preventDefault();
api.move(latest.current.id, key === "Home" ? "first" : "last");
return;
}
if (key === "ArrowLeft" || key === "ArrowRight") {
const side: Side = key === "ArrowLeft" ? "trailing" : "leading";
const shown = status.current.open;
if (shown && shown !== side) {
event.preventDefault();
close();
return;
}
if (actionsOn(side).length === 0) return;
event.preventDefault();
reveal(side);
return;
}
if (key === "Escape") {
if (status.current.open) {
event.preventDefault();
close();
}
return;
}
if (key === "Delete" || key === "Backspace") {
const primary = latest.current.trailing[0];
if (primary?.dismiss) {
event.preventDefault();
commit("trailing", primary);
}
}
}
function onTrayKeyDown(
event: ReactKeyboardEvent<HTMLButtonElement>,
side: Side,
slot: number,
) {
if (event.altKey || event.ctrlKey || event.metaKey || status.current.committing) return;
const key = event.key;
if (key === "Escape") {
event.preventDefault();
close(0, "row");
return;
}
if (key === "ArrowDown" || key === "ArrowUp") {
event.preventDefault();
api.move(latest.current.id, key === "ArrowDown" ? 1 : -1);
return;
}
if (key !== "ArrowLeft" && key !== "ArrowRight") return;
const count = actionsOn(side).length;
const order = Array.from({ length: count }, (_, i) =>
side === "leading" ? i : count - 1 - i,
);
const at = order.indexOf(slot) + (key === "ArrowRight" ? 1 : -1);
if (at >= 0 && at < count) {
event.preventDefault();
buttons.current.get(`${side}:${order[at]}`)?.focus();
return;
}
if (side === "trailing" ? at < 0 : at >= count) {
event.preventDefault();
close(0, "row");
}
}
function registerButton(side: Side, slot: number, el: HTMLButtonElement | null) {
const key = `${side}:${slot}`;
if (el) buttons.current.set(key, el);
else buttons.current.delete(key);
}
const hint = hintFor(leading, trailing);
return (
<motion.li
initial={{ height: 0, opacity: 0 }}
animate={{ height: "auto", opacity: 1 }}
exit={{ height: 0, opacity: 0 }}
transition={reduce ? { duration: 0 } : FOLD}
className="relative overflow-hidden border-current/10 not-last:border-b"
>
<div
ref={surfaceRef}
inert={!present}
className="relative touch-pan-y select-none"
onPointerDown={onPointerDown}
onBlur={onBlur}
>
{leading.length > 0 ? (
<Tray
side="leading"
actions={leading}
open={open === "leading"}
x={x}
arm={arm}
buttonWidth={buttonWidth}
register={registerButton}
onRun={commit}
onKeyDown={onTrayKeyDown}
/>
) : null}
<motion.div
ref={contentRef}
role="group"
tabIndex={tabbable ? 0 : -1}
aria-labelledby={bodyId}
aria-describedby={hint ? hintId : undefined}
style={{ x }}
className="relative focus-visible:outline-2 focus-visible:-outline-offset-2"
onKeyDown={onKeyDown}
onFocus={() => api.activate(id)}
onClickCapture={onClickCapture}
>
<div id={bodyId} onDragStart={(event) => event.preventDefault()}>
{content}
</div>
</motion.div>
{trailing.length > 0 ? (
<Tray
side="trailing"
actions={trailing}
open={open === "trailing"}
x={x}
arm={arm}
buttonWidth={buttonWidth}
register={registerButton}
onRun={commit}
onKeyDown={onTrayKeyDown}
/>
) : null}
</div>
{hint ? (
<span id={hintId} hidden>
{hint}
</span>
) : null}
</motion.li>
);
}
interface TrayProps {
side: Side;
actions: SwipeAction[];
open: boolean;
x: MotionValue<number>;
arm: MotionValue<number>;
buttonWidth: number;
register: (side: Side, slot: number, el: HTMLButtonElement | null) => void;
onRun: (side: Side, action: SwipeAction) => void;
onKeyDown: (
event: ReactKeyboardEvent<HTMLButtonElement>,
side: Side,
slot: number,
) => void;
}
function Tray({
side,
actions,
open,
x,
arm,
buttonWidth,
register,
onRun,
onKeyDown,
}: TrayProps) {
const count = actions.length;
const slots = Array.from({ length: count }, (_, i) =>
side === "leading" ? i : count - 1 - i,
);
return (
<div inert={!open} className="pointer-events-none absolute inset-0">
{slots.map((slot) => {
const action = actions[slot];
return (
<Slice
key={action.id}
action={action}
side={side}
slot={slot}
count={count}
x={x}
arm={arm}
buttonWidth={buttonWidth}
buttonRef={(el) => register(side, slot, el)}
onRun={() => onRun(side, action)}
onKeyDown={(event) => onKeyDown(event, side, slot)}
/>
);
})}
</div>
);
}
interface SliceProps {
action: SwipeAction;
side: Side;
slot: number;
count: number;
x: MotionValue<number>;
arm: MotionValue<number>;
buttonWidth: number;
buttonRef: (el: HTMLButtonElement | null) => void;
onRun: () => void;
onKeyDown: (event: ReactKeyboardEvent<HTMLButtonElement>) => void;
}
function Slice({
action,
side,
slot,
count,
x,
arm,
buttonWidth,
buttonRef,
onRun,
onKeyDown,
}: SliceProps) {
const sign = signOf(side);
const depth = count - 1 - slot;
const primary = slot === 0;
const offset = useTransform([x, arm], ([xv, av]: number[]) =>
band(xv, av, sign, count, depth, primary).offset,
);
const labelX = useTransform([x, arm], ([xv, av]: number[]) => {
const b = band(xv, av, sign, count, depth, primary);
return -sign * ((b.width - buttonWidth) / 2) * (primary ? 1 - b.arm : 1);
});
const labelOpacity = useTransform([x, arm], ([xv, av]: number[]) => {
const b = band(xv, av, sign, count, depth, primary);
return Math.min(1, Math.max(0, (b.width - LABEL_FROM) / LABEL_SPAN));
});
return (
<motion.button
ref={buttonRef}
type="button"
className={cn(
"group/slice pointer-events-auto absolute inset-y-0 w-full overflow-hidden outline-none",
side === "leading" ? "right-full" : "left-full",
)}
style={{
x: offset,
zIndex: count - slot,
backgroundColor: action.color,
color: inkOn(action.color),
}}
onClick={onRun}
onKeyDown={onKeyDown}
>
<motion.span
className={cn(
"absolute inset-y-0 flex flex-col items-center justify-center gap-1 px-1 text-xs leading-none font-medium",
side === "leading" ? "right-0" : "left-0",
)}
style={{ width: buttonWidth, x: labelX, opacity: labelOpacity }}
>
{action.icon ? (
<span aria-hidden className="flex [&_svg]:size-5">
{action.icon}
</span>
) : null}
<span className="max-w-full truncate">{action.label}</span>
<span
aria-hidden
className="pointer-events-none absolute inset-1.5 rounded-md opacity-0 ring-2 ring-current group-focus-visible/slice:opacity-100"
/>
</motion.span>
</motion.button>
);
}
function DefaultRow({ row }: { row: SwipeRow }) {
return (
<div className="flex items-center gap-3 px-4 py-3">
<div className="min-w-0 flex-1">
<p className="truncate text-sm font-medium">{row.title}</p>
{row.description ? (
<p className="mt-0.5 truncate text-sm opacity-60">{row.description}</p>
) : null}
</div>
{row.meta ? (
<span className="shrink-0 text-xs tabular-nums opacity-50">{row.meta}</span>
) : null}
</div>
);
}
function signOf(side: Side) {
return side === "leading" ? 1 : -1;
}
function band(
xv: number,
av: number,
sign: number,
count: number,
depth: number,
primary: boolean,
) {
const r = Math.max(0, sign * xv);
const armed = Math.min(1, Math.max(0, av));
const share = (r / count) * (1 - armed);
const width = primary ? r - (count - 1) * share : share;
return { offset: sign * (r - depth * share), width, arm: armed };
}
function rubber(d: number, c: number, w: number) {
if (c <= 0 || w <= 0) return 0;
return (1 - 1 / ((d * c) / w + 1)) * w;
}
function velocityOf(samples: { t: number; x: number }[], now: number) {
const recent = samples.filter((sample) => now - sample.t <= VELOCITY_WINDOW);
if (recent.length < 2) return 0;
const first = recent[0];
const last = recent[recent.length - 1];
const dt = last.t - first.t;
return dt > 0 ? ((last.x - first.x) / dt) * 1000 : 0;
}
function inkOn(color: string) {
const match = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(color.trim());
if (!match) return "#fff";
const hex =
match[1].length === 3
? match[1]
.split("")
.map((c) => c + c)
.join("")
: match[1];
const [r, g, b] = [0, 2, 4].map((i) => {
const v = parseInt(hex.slice(i, i + 2), 16) / 255;
return v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
});
const luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
return 1.05 / (luminance + 0.05) >= 3 ? "#fff" : "#0a0a0a";
}
function hintFor(leading: SwipeAction[], trailing: SwipeAction[]) {
const keys = [trailing.length ? "Left" : "", leading.length ? "Right" : ""]
.filter(Boolean)
.join(" or ");
const parts: string[] = [];
if (keys) parts.push(`Press ${keys} arrow to show actions.`);
const primary = trailing[0];
if (primary?.dismiss) parts.push(`Press Delete to ${primary.label.toLowerCase()}.`);
return parts.join(" ");
}