# Swipe actions

> A list row that slides over its actions: cells grow with your finger, a full swipe runs the first one, and the row folds away.

Most web lists copy swipe actions badly: a row that slides to show two buttons and nothing more. This one works the way mail apps do. Drag a row, or swipe two fingers on a trackpad, and the actions under that edge share the space it uncovers: every cell grows with the finger and its icon scales in once there is room. Let go past half the rail and the row springs open; keep going past most of the row and the first action arms, taking the whole space with its icon riding the row's edge, and letting go runs it. Actions that remove the row slide it off and fold its height away at once, and if the work fails the row unfolds, slides back and shakes. With a mouse the row's time gives way to the same actions as icons on hover, so nothing depends on the gesture.

## Install

```bash
npx shadcn@latest add @hairline/swipe-actions
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): none
- Docs and live preview: https://hairlineui.com/ui/swipe-actions
- Registry item (source): https://hairlineui.com/r/swipe-actions.json

## Usage

```tsx
import { SwipeActions } from "@/components/swipe-actions";
import { Archive, MailOpen } from "lucide-react";

<SwipeActions
  label={message.subject}
  meta={message.time}
  leading={[{ id: "read", label: "Read", icon: <MailOpen />, tone: "primary", onAction: markRead }]}
  trailing={[{ id: "archive", label: "Archive", icon: <Archive />, tone: "primary", removes: true, onAction: archive }]}
>
  <MessagePreview message={message} />
</SwipeActions>
```

## An inbox where delete can fail

Removal is optimistic: the row leaves as soon as the swipe commits. Throw from onAction to bring it back; take the item out of your list once it resolves.

```tsx
"use client";
import { SwipeActions } from "@/components/swipe-actions";
import { Mail, MailOpen, Trash2 } from "lucide-react";

export function Inbox({ messages, setMessages }) {
  return (
    <div className="overflow-hidden rounded-2xl border [&>*:last-child]:-mb-px">
      {messages.map((m) => (
        <SwipeActions
          key={m.id}
          label={m.subject}
          meta={m.time}
          leading={[{
            id: "read", label: m.unread ? "Read" : "Unread", icon: m.unread ? <MailOpen /> : <Mail />, tone: "primary",
            onAction: () => fetch("/api/messages/" + m.id, { method: "PATCH", body: JSON.stringify({ unread: !m.unread }) }),
          }]}
          trailing={[{
            id: "delete", label: "Delete", icon: <Trash2 />, tone: "destructive", removes: true,
            onAction: async () => {
              const res = await fetch("/api/messages/" + m.id, { method: "DELETE" });
              if (!res.ok) throw new Error("Could not delete");
              setMessages((list) => list.filter((x) => x.id !== m.id));
            },
          }]}
        >
          <p className="truncate text-sm font-medium">{m.from}</p>
          <p className="truncate text-xs text-muted-foreground">{m.subject}</p>
        </SwipeActions>
      ))}
    </div>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `string` | – | Names the row for screen readers and in announcements, e.g. the subject. |
| `leading` | `SwipeAction[]` | – | Under the left edge, revealed by swiping right. The first runs on a full swipe. |
| `trailing` | `SwipeAction[]` | – | Under the right edge, listed from the edge inward. The first runs on a full swipe. |
| `SwipeAction` | `{ id, label, icon, tone?, removes?, onAction }` | – | tone is "neutral" (muted), "primary" or "destructive". removes slides the row off and folds it away; onAction may return a promise and throw to undo that. |
| `meta` | `ReactNode` | – | At the row's end, e.g. the time. Gives way to the action icons on hover. |
| `enter` | `boolean` | `false` | Unfold on mount, for a row put back by an undo. |
| `divider` | `boolean` | `true` | A hairline under the row that folds away with it. Clip the last one with -mb-px on the list. |
| `disabled` | `boolean` | `false` | No gestures and no actions. |

## States

- **reveal**: After 7px of mostly horizontal movement the row follows the pointer on one transform; vertical movement is left to the page. The cells under that edge share the uncovered space equally, and each icon scales from 0.72 and fades in as its cell passes about 50px.
- **open**: Released past half the rail (76px per action), or flicked outward faster than 0.45px/ms, the row springs open; otherwise it springs closed. Tapping the row, pointing anywhere else, Escape or opening another row closes it.
- **arm**: Past 62% of the row the first action takes over the whole space in about 60ms and its icon rides the row's edge, with a 6ms vibration on phones. Coming back 24px disarms it. Releasing while armed runs it.
- **remove**: An action with removes slides the row off (critically damped) and folds its height in 380ms before onAction settles. If onAction throws, the row unfolds, springs back, shakes and its time reads Failed for 2.6s.
- **hover**: On devices with a fine pointer, hovering or focusing the row fades the time out and the actions in as 28px icon buttons over a soft edge of the row's own colour.

## What it handles

Most web lists copy swipe actions badly: a row that slides to show two buttons and nothing more. This works the way mail apps do, and has buttons for people who don’t swipe.

- **Actions that grow with your finger**: The cells under the edge share the space the row uncovers, and each icon scales in once there is room.
- **A full swipe runs the first action**: Keep going past most of the row and the first action arms, taking the whole space. Letting go runs it.
- **Trackpads work too**: A two-finger swipe on a trackpad drives the row in the same way.
- **Removal that can fail**: A removing action slides the row off and folds it away. If it fails, the row comes back, shakes and reads Failed.
- **Buttons for a mouse**: With a fine pointer, hovering or focusing the row shows the same actions as icon buttons. This is the accessible path.
- **One row open at a time**: Opening one row closes the other, and a tap on an open row closes it.

## Replaces

swipe gesture code, action cells, a full-swipe shortcut, optimistic removal with rollback, hover action buttons

## Where it fits

- **Email and inbox lists**: Swipe to archive, delete or mark as read.
- **Task and to-do lists**: Complete or snooze a task with a swipe.
- **Notification and message lists**: Quick actions on each row.
- **Order and ticket queues**: Handle many items quickly on a phone.

## Notes

- The icon buttons are the accessible actions: they are real buttons in the tab order, shown on hover or focus, with the action as their name. The swipe cells repeat them for pointers and are hidden from assistive technology.
- Results are announced in a status region: done, or failed with an invitation to try again. Escape closes an open row.
- Only one row is open at a time, as in mail apps. A tap on an open row closes it instead of following whatever is under it, and a click that ends a drag is swallowed.
- Drags and trackpad swipes write transforms only, and one frame loop runs the spring and the arming, then sleeps. Each cell is a full-height layer moved by transform, so revealing never changes layout.
- Reduced motion turns springs, folds and the shake into immediate changes; the colours, labels and announcements still say what happened.

License: MIT (https://hairlineui.com/license#components)
