Undo Delete Alert
Document list where deleting shows an alert with a timed undo.
"use client"
import { useEffect, useState } from "react"
import {
ArrowUUpLeftIcon,
FileTextIcon,
TrashIcon,
} from "@phosphor-icons/react"
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
import { Button } from "@/components/ui/button"
const COPY = {
heading: "Recent documents",
undo: "Undo",
remove: "Delete",
title: "Moved to trash",
seconds: 8,
empty: "Nothing here. Everything has been deleted.",
restore: "Restore all",
} as const
const DOCS = [
{ id: "d1", name: "Roadmap 2027.docx", meta: "Edited 2h ago" },
{ id: "d2", name: "Brand guidelines.pdf", meta: "Edited yesterday" },
{ id: "d3", name: "Onboarding checklist.md", meta: "Edited Mon" },
] as const
/** Document list where deleting shows an alert with a timed undo. */
export default function AlertUndoDelete01() {
const [gone, setGone] = useState<string[]>([])
const [last, setLast] = useState<string | null>(null)
const [left, setLeft] = useState(0)
useEffect(() => {
if (!last) return
if (left <= 0) {
setLast(null)
return
}
const t = window.setTimeout(() => setLeft((l) => l - 1), 1000)
return () => window.clearTimeout(t)
}, [last, left])
const visible = DOCS.filter((d) => !gone.includes(d.id))
const lastDoc = DOCS.find((d) => d.id === last)
return (
<div className="mx-auto flex w-full max-w-xl flex-col gap-3">
<div className="flex items-center justify-between">
<h3 className="text-sm font-medium">{COPY.heading}</h3>
{gone.length > 0 && (
<Button
size="xs"
variant="ghost"
onClick={() => {
setGone([])
setLast(null)
}}
>
{COPY.restore}
</Button>
)}
</div>
<ul className="divide-y rounded-2xl border bg-card">
{visible.length === 0 && (
<li className="px-4 py-8 text-center text-sm text-muted-foreground">
{COPY.empty}
</li>
)}
{visible.map((d) => (
<li key={d.id} className="flex items-center gap-3 px-4 py-3">
<FileTextIcon
aria-hidden="true"
className="size-5 text-muted-foreground"
/>
<div className="min-w-0 flex-1">
<p className="truncate text-sm font-medium">{d.name}</p>
<p className="text-xs text-muted-foreground">{d.meta}</p>
</div>
<Button
size="icon-sm"
variant="ghost"
aria-label={`${COPY.remove} ${d.name}`}
onClick={() => {
setGone((g) => [...g, d.id])
setLast(d.id)
setLeft(COPY.seconds)
}}
>
<TrashIcon />
</Button>
</li>
))}
</ul>
{lastDoc && (
<Alert>
<TrashIcon aria-hidden="true" />
<AlertTitle>{COPY.title}</AlertTitle>
<AlertDescription>
{lastDoc.name} will be removed in{" "}
<span className="tabular-nums">{left}</span>s.
</AlertDescription>
<div className="col-start-2 mt-3">
<Button
size="sm"
variant="outline"
onClick={() => {
setGone((g) => g.filter((x) => x !== lastDoc.id))
setLast(null)
}}
>
<ArrowUUpLeftIcon data-icon="inline-start" />
{COPY.undo}
</Button>
</div>
</Alert>
)}
</div>
)
}
Installation
pnpm dlx shadcn@latest add @01orbital/undo-delete-01npx shadcn@latest add @01orbital/undo-delete-01yarn dlx shadcn@latest add @01orbital/undo-delete-01bunx --bun shadcn@latest add @01orbital/undo-delete-01Make it yours
- Its words are in
COPYat the top ofcomponents/blocks/alert/undo-delete-01/index.tsx: change them there, not in the markup. DOCS, also at the top of the file, holds its lists and settings.
Built from
npm: @phosphor-icons/react. The CLI installs it for you.
Built from these parts. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:
The shadcn parts here are their Base UI versions, and this piece uses Base UI's API. A Radix version you already have won't work with it.
Last updated on