Swipe row
iOS trailing swipe actions for a list row on touch screens.
The gesture exists only where the main pointer is coarse (a touch screen): a swipe to the left uncovers the actions, a tap elsewhere or a scroll covers them again, and a swipe across more than 60% of the row runs the last action. With a mouse or trackpad SwipeRow draws its children and nothing else, so the row has to offer the same actions itself, as buttons or in a menu. To try the example, open this page on a phone or turn on touch emulation. Give the row its background through className: the sliding content paints over the actions with it.
Trailing actions
Release 4.2.0 is ready to ship
All 214 checks passed
INV-1040 is overdue
It was due on 15 November
You were added to Atlas redesign
As a reviewer
"use client"
import { useRef, useState } from "react"
import { ArchiveIcon, BinIcon, PinIcon } from "@keyline-icons/react"
import { Button } from "@/components/ui/button"
import { SwipeRow, type SwipeAction } from "@/components/ui/swipe-row"
const inbox = [
{ id: "n1", title: "Release 4.2.0 is ready to ship", detail: "All 214 checks passed" },
{ id: "n2", title: "INV-1040 is overdue", detail: "It was due on 15 November" },
{ id: "n3", title: "You were added to Atlas redesign", detail: "As a reviewer" },
]
export default function SwipeRowTrailingActions() {
const [rows, setRows] = useState(inbox)
const [pinned, setPinned] = useState<string[]>([])
const restore = useRef<HTMLButtonElement>(null)
return (
<div className="flex w-full max-w-md flex-col items-start gap-4">
<ul className="w-full divide-y divide-border overflow-hidden rounded-xl border border-border">
{rows.map((row) => {
const isPinned = pinned.includes(row.id)
// A removed row takes its buttons with it, so focus moves to the way back.
const remove = () => {
setRows(rows.filter((other) => other.id !== row.id))
restore.current?.focus()
}
// A full swipe runs the last action.
const actions: SwipeAction[] = [
{
label: `${isPinned ? "Unpin" : "Pin"} ${row.title}`,
icon: <PinIcon />,
tone: "primary",
onAction: () => setPinned(isPinned ? pinned.filter((id) => id !== row.id) : [...pinned, row.id]),
},
{ label: `Archive ${row.title}`, icon: <ArchiveIcon />, onAction: remove },
{ label: `Delete ${row.title}`, icon: <BinIcon />, tone: "destructive", onAction: remove },
]
return (
<li key={row.id}>
<SwipeRow className="bg-background" actions={actions}>
<div className="flex items-center px-4 py-3">
<div className="min-w-0 flex-1">
<p className="truncate font-medium">{row.title}</p>
<p className="truncate text-sm text-muted-foreground">
{isPinned ? "Pinned. " : null}
{row.detail}
</p>
</div>
{/* With a mouse or trackpad SwipeRow draws its children only: the row offers the same actions itself. */}
{actions.map((action) => (
<Button
key={action.label}
size="icon"
variant="ghost"
className="pointer-coarse:hidden"
aria-label={action.label}
onClick={action.onAction}
>
{action.icon}
</Button>
))}
</div>
</SwipeRow>
</li>
)
})}
{rows.length === 0 ? <li className="px-4 py-3 text-muted-foreground">Nothing left in the inbox</li> : null}
</ul>
<Button ref={restore} variant="outline" onClick={() => setRows(inbox)}>
Bring the rows back
</Button>
</div>
)
}
Installation
npx shadcn@latest add @khaeli/swipe-rowbunx --bun shadcn@latest add @khaeli/swipe-rowInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
Keyboard
Tab- On a touch screen with a keyboard: moves to each action in turn. Focus on an action slides the row open to show it, and leaving the last one closes the row.
EnterSpace- Run the focused action and close the row.
Accessible name
- Each action is a button named by its
label; theiconis all that shows. Put the row in the label (Archive INV-1040 is overdue), since every row has the same actions. - The actions differ by icon as well as colour:
primary,neutralanddestructiveare never the only cue. - With a mouse, a trackpad or a keyboard on a desktop there are no swipe actions to reach. The buttons the row offers in their place need the same names.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/swipe-row.json