{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "confirm-delete-with-undo",
  "title": "Confirm Delete With Undo",
  "description": "Delete without a dialog and make it safe with an announced Undo bar: the item goes at once and the delete is committed only after the undo window passes.",
  "registryDependencies": [
    "button"
  ],
  "files": [
    {
      "path": "registry/patterns/confirm-delete-with-undo/confirm-delete-with-undo.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { Button } from \"@/components/ui/button\"\n\nexport interface ConfirmDeleteWithUndoProps {\n  /** Label of the delete button. */\n  children: React.ReactNode\n  /** Remove it from the screen now (optimistic). */\n  onDelete: () => void\n  /** Put it back; the delete was undone. */\n  onUndo: () => void\n  /** The undo window passed: really delete it on the server. */\n  onCommit: () => void | Promise<void>\n  /** What was deleted, for the message. */\n  noun?: string\n  /** Milliseconds the undo bar stays. Default 6000. */\n  timeout?: number\n}\n\n/**\n * Delete without a dialog and make it safe with undo instead. The item is\n * removed from the UI at once, an announced bar offers Undo for a few seconds,\n * and only then is the delete committed. Faster than a confirmation and\n * kinder, as long as the delete really can wait.\n */\nexport function ConfirmDeleteWithUndo({\n  children,\n  onDelete,\n  onUndo,\n  onCommit,\n  noun = \"Item\",\n  timeout = 6000,\n}: ConfirmDeleteWithUndoProps) {\n  const [pendingUndo, setPendingUndo] = React.useState(false)\n  const commit = React.useRef(onCommit)\n  commit.current = onCommit\n\n  React.useEffect(() => {\n    if (!pendingUndo) return\n    const timer = window.setTimeout(() => {\n      setPendingUndo(false)\n      void commit.current()\n    }, timeout)\n    return () => window.clearTimeout(timer)\n  }, [pendingUndo, timeout])\n\n  return (\n    <div className=\"grid gap-2\">\n      {pendingUndo ? null : (\n        <Button\n          variant=\"destructive\"\n          onClick={() => {\n            onDelete()\n            setPendingUndo(true)\n          }}\n        >\n          {children}\n        </Button>\n      )}\n      <div role=\"status\" aria-live=\"polite\">\n        {pendingUndo ? (\n          <div className=\"bg-muted flex items-center justify-between gap-3 rounded-md border px-3 py-2 text-sm\">\n            <span>{noun} deleted.</span>\n            <Button\n              size=\"sm\"\n              variant=\"outline\"\n              // eslint-disable-next-line jsx-a11y/no-autofocus -- Undo is the one useful target right after a delete\n              autoFocus\n              onClick={() => {\n                setPendingUndo(false)\n                onUndo()\n              }}\n            >\n              Undo\n            </Button>\n          </div>\n        ) : null}\n      </div>\n    </div>\n  )\n}\n",
      "type": "registry:block"
    }
  ],
  "type": "registry:block"
}