{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "button-delete-undo",
  "type": "registry:component",
  "title": "Button Delete Undo",
  "description": "Deleting blurs the button away and slides in a toast with a draining bar, an Undo button and a Cmd+Z shortcut. If the window runs out the deletion is committed.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "buttons",
    "delete",
    "undo",
    "toast",
    "button",
    "shortcut",
    "soft delete"
  ],
  "docs": "https://hyperblur.dev/effects/button-delete-undo",
  "files": [
    {
      "path": "registry/hyperblur/button-delete-undo.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/button-delete-undo.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface ButtonDeleteUndoProps\n  extends Omit<React.HTMLAttributes<HTMLSpanElement>, \"children\"> {\n  children?: React.ReactNode;\n  message?: React.ReactNode;\n  undoLabel?: string;\n  restoredLabel?: React.ReactNode;\n  committedLabel?: React.ReactNode;\n  undoWindow?: number;\n  accent?: string;\n  resetAfter?: number;\n  disabled?: boolean;\n  onDelete?: () => void;\n  onUndo?: () => void;\n  onCommit?: () => void;\n}\n\nconst css = `\n.hb-xd6 {\n  --hb-xd6-p: 1;\n  position: relative;\n  display: inline-grid;\n  justify-items: start;\n  align-items: center;\n  box-sizing: border-box;\n  font-family: inherit;\n  font-size: 0.8125rem;\n  color: #ededed;\n}\n.hb-xd6 *, .hb-xd6 *::before, .hb-xd6 *::after { box-sizing: border-box; }\n.hb-xd6-btn, .hb-xd6-toast { grid-area: 1 / 1; }\n.hb-xd6-btn {\n  display: inline-flex;\n  align-items: center;\n  gap: 0.55em;\n  height: 2.5rem;\n  margin: 0;\n  padding: 0 1.05em 0 0.9em;\n  font: inherit;\n  font-weight: 600;\n  color: color-mix(in srgb, var(--hb-xd6-c) 45%, #fff);\n  white-space: nowrap;\n  background: linear-gradient(180deg, color-mix(in srgb, var(--hb-xd6-c) 14%, #151515), color-mix(in srgb, var(--hb-xd6-c) 7%, #101010));\n  border: 1px solid color-mix(in srgb, var(--hb-xd6-c) 30%, #262626);\n  border-radius: 10px;\n  box-shadow: inset 0 1px 0 rgba(255,255,255,0.05), 0 1px 2px rgba(0,0,0,0.5);\n  cursor: pointer;\n  -webkit-tap-highlight-color: transparent;\n  transform-origin: left center;\n  transition: opacity 0.25s ease, transform 0.45s cubic-bezier(0.3, 1.3, 0.5, 1), filter 0.3s ease, background 0.2s ease, border-color 0.2s ease;\n}\n.hb-xd6-btn:hover:not(:disabled) { border-color: color-mix(in srgb, var(--hb-xd6-c) 55%, #262626); background: linear-gradient(180deg, color-mix(in srgb, var(--hb-xd6-c) 20%, #151515), color-mix(in srgb, var(--hb-xd6-c) 10%, #101010)); }\n.hb-xd6-btn:active:not(:disabled) { transform: scale(0.97); }\n.hb-xd6-btn:focus-visible, .hb-xd6-undo:focus-visible { outline: 2px solid var(--hb-xd6-c); outline-offset: 2px; }\n.hb-xd6-btn:disabled { opacity: 0.45; cursor: not-allowed; }\n.hb-xd6[data-phase=\"pending\"] .hb-xd6-btn,\n.hb-xd6[data-phase=\"committed\"] .hb-xd6-btn {\n  opacity: 0;\n  transform: scale(0.86) translateY(4px);\n  filter: blur(3px);\n  pointer-events: none;\n  visibility: hidden;\n  transition: opacity 0.2s ease, transform 0.3s ease, filter 0.3s ease, visibility 0s linear 0.3s;\n}\n.hb-xd6-toast {\n  position: relative;\n  display: flex;\n  align-items: center;\n  gap: 10px;\n  height: 2.75rem;\n  padding: 0 6px 0 8px;\n  white-space: nowrap;\n  background: linear-gradient(180deg, #1c1c1c, #141414);\n  border: 1px solid #2c2c2c;\n  border-radius: 12px;\n  box-shadow: inset 0 1px 0 rgba(255,255,255,0.06), 0 12px 30px -12px rgba(0,0,0,0.9), 0 2px 6px rgba(0,0,0,0.5);\n  overflow: hidden;\n  opacity: 0;\n  visibility: hidden;\n  transform: translateY(10px) scale(0.94);\n  transform-origin: left center;\n  transition: opacity 0.2s ease, transform 0.5s cubic-bezier(0.2, 1.3, 0.4, 1), visibility 0s linear 0.5s;\n}\n.hb-xd6[data-phase=\"pending\"] .hb-xd6-toast,\n.hb-xd6[data-phase=\"committed\"] .hb-xd6-toast,\n.hb-xd6[data-phase=\"restored\"] .hb-xd6-toast {\n  opacity: 1;\n  visibility: visible;\n  transform: none;\n  transition: opacity 0.2s ease 0.05s, transform 0.5s cubic-bezier(0.2, 1.3, 0.4, 1) 0.05s, visibility 0s;\n}\n.hb-xd6-ico {\n  display: grid;\n  place-items: center;\n  flex: none;\n  width: 26px;\n  height: 26px;\n  border-radius: 8px;\n  color: color-mix(in srgb, var(--hb-xd6-c) 70%, #fff);\n  background: color-mix(in srgb, var(--hb-xd6-c) 16%, transparent);\n  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--hb-xd6-c) 30%, transparent);\n  transition: color 0.3s ease, background 0.3s ease;\n}\n.hb-xd6[data-phase=\"restored\"] .hb-xd6-ico { color: #86efac; background: rgba(34,197,94,0.14); box-shadow: inset 0 0 0 1px rgba(34,197,94,0.3); }\n.hb-xd6[data-phase=\"restored\"] .hb-xd6-ico svg { animation: hb-xd6-rewind 0.6s cubic-bezier(0.3, 0.9, 0.3, 1); }\n.hb-xd6[data-phase=\"committed\"] .hb-xd6-ico { color: #8f8f8f; background: #1f1f1f; box-shadow: inset 0 0 0 1px #2a2a2a; }\n.hb-xd6-msg { display: flex; flex-direction: column; gap: 1px; line-height: 1.2; padding-right: 4px; }\n.hb-xd6-msg small { font-size: 0.68rem; color: #8f8f8f; font-variant-numeric: tabular-nums; }\n.hb-xd6-undo {\n  display: inline-flex;\n  align-items: center;\n  gap: 7px;\n  height: 30px;\n  margin: 0 0 0 auto;\n  padding: 0 7px 0 11px;\n  font: inherit;\n  font-weight: 600;\n  color: #fff;\n  background: #262626;\n  border: 1px solid #333;\n  border-radius: 8px;\n  cursor: pointer;\n  transition: background 0.2s ease, transform 0.15s ease;\n}\n.hb-xd6-undo:hover { background: #303030; }\n.hb-xd6-undo:active { transform: scale(0.96); }\n.hb-xd6-undo kbd {\n  padding: 1px 5px;\n  font-family: var(--font-mono), ui-monospace, monospace;\n  font-size: 0.66rem;\n  font-weight: 500;\n  color: #a1a1a1;\n  background: #171717;\n  border: 1px solid #333;\n  border-bottom-width: 2px;\n  border-radius: 5px;\n}\n.hb-xd6-bar {\n  position: absolute;\n  left: 0;\n  right: 0;\n  bottom: 0;\n  height: 2px;\n  background: linear-gradient(90deg, color-mix(in srgb, var(--hb-xd6-c) 60%, transparent), var(--hb-xd6-c));\n  transform: scaleX(var(--hb-xd6-p));\n  transform-origin: left;\n  box-shadow: 0 0 8px var(--hb-xd6-c);\n}\n.hb-xd6[data-phase=\"committed\"] .hb-xd6-bar, .hb-xd6[data-phase=\"restored\"] .hb-xd6-bar { opacity: 0; }\n.hb-xd6-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }\n@keyframes hb-xd6-rewind { from { transform: rotate(360deg); } to { transform: rotate(0); } }\n@media (prefers-reduced-motion: reduce) {\n  .hb-xd6-btn, .hb-xd6-toast, .hb-xd6[data-phase] .hb-xd6-btn, .hb-xd6[data-phase] .hb-xd6-toast { transition: none; filter: none; }\n  .hb-xd6[data-phase=\"restored\"] .hb-xd6-ico svg { animation: none; }\n}\n`;\n\ntype Phase = \"idle\" | \"pending\" | \"committed\" | \"restored\";\n\nexport function ButtonDeleteUndo({\n  children = \"Delete 3 emails\",\n  message = \"3 emails deleted\",\n  undoLabel = \"Undo\",\n  restoredLabel = \"Emails restored\",\n  committedLabel = \"Deleted permanently\",\n  undoWindow = 5000,\n  accent = \"#ef4444\",\n  resetAfter = 2200,\n  disabled = false,\n  onDelete,\n  onUndo,\n  onCommit,\n  className = \"\",\n  style,\n  ...rest\n}: ButtonDeleteUndoProps) {\n  const rootRef = React.useRef<HTMLSpanElement>(null);\n  const btnRef = React.useRef<HTMLButtonElement>(null);\n  const undoRef = React.useRef<HTMLButtonElement>(null);\n  const raf = React.useRef(0);\n  const elapsed = React.useRef(0);\n  const latest = React.useRef({ onDelete, onUndo, onCommit });\n  const [phase, setPhase] = React.useState<Phase>(\"idle\");\n  const [left, setLeft] = React.useState(Math.ceil(undoWindow / 1000));\n\n  React.useEffect(() => {\n    latest.current = { onDelete, onUndo, onCommit };\n  }, [onDelete, onUndo, onCommit]);\n\n  const undo = React.useCallback(() => {\n    cancelAnimationFrame(raf.current);\n    setPhase(\"restored\");\n    latest.current.onUndo?.();\n  }, []);\n\n  React.useEffect(() => {\n    if (phase !== \"pending\") return;\n    elapsed.current = 0;\n    let last = 0;\n    const step = (t: number) => {\n      const dt = last ? Math.min(64, t - last) : 0;\n      last = t;\n      elapsed.current += dt;\n      const p = Math.max(0, 1 - elapsed.current / undoWindow);\n      rootRef.current?.style.setProperty(\"--hb-xd6-p\", p.toFixed(4));\n      setLeft(Math.max(0, Math.ceil((undoWindow - elapsed.current) / 1000)));\n      if (p <= 0) {\n        setPhase(\"committed\");\n        latest.current.onCommit?.();\n        return;\n      }\n      raf.current = requestAnimationFrame(step);\n    };\n    raf.current = requestAnimationFrame(step);\n    const onKey = (e: KeyboardEvent) => {\n      if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === \"z\") {\n        e.preventDefault();\n        undo();\n      }\n    };\n    window.addEventListener(\"keydown\", onKey);\n    return () => {\n      cancelAnimationFrame(raf.current);\n      window.removeEventListener(\"keydown\", onKey);\n    };\n  }, [phase, undoWindow, undo]);\n\n  React.useEffect(() => {\n    if (phase === \"restored\") {\n      const t = window.setTimeout(() => {\n        setPhase(\"idle\");\n        requestAnimationFrame(() => btnRef.current?.focus({ preventScroll: true }));\n      }, 1300);\n      return () => window.clearTimeout(t);\n    }\n    if (phase === \"committed\" && resetAfter > 0) {\n      const t = window.setTimeout(() => setPhase(\"idle\"), resetAfter);\n      return () => window.clearTimeout(t);\n    }\n  }, [phase, resetAfter]);\n\n  const restoring = phase === \"restored\";\n  const committed = phase === \"committed\";\n\n  return (\n    <span\n      ref={rootRef}\n      className={`hb-xd6 ${className}`.trim()}\n      data-phase={phase}\n      style={{ \"--hb-xd6-c\": accent, ...style } as React.CSSProperties}\n      {...rest}\n    >\n      <style href=\"hb-button-delete-undo\" precedence=\"default\">{css}</style>\n      <button\n        ref={btnRef}\n        type=\"button\"\n        className=\"hb-xd6-btn\"\n        disabled={disabled}\n        tabIndex={phase === \"idle\" ? 0 : -1}\n        onClick={() => {\n          if (phase !== \"idle\") return;\n          rootRef.current?.style.setProperty(\"--hb-xd6-p\", \"1\");\n          setLeft(Math.ceil(undoWindow / 1000));\n          setPhase(\"pending\");\n          latest.current.onDelete?.();\n          requestAnimationFrame(() => undoRef.current?.focus({ preventScroll: true }));\n        }}\n      >\n        <svg viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n          <path d=\"M4 7h16M9 7V4.5h6V7M6 7l1 13h10l1-13\" />\n        </svg>\n        {children}\n      </button>\n      <span className=\"hb-xd6-toast\" aria-hidden={phase === \"idle\"}>\n        <span className=\"hb-xd6-ico\" aria-hidden=\"true\">\n          {restoring ? (\n            <svg viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2.2\" strokeLinecap=\"round\" strokeLinejoin=\"round\"><path d=\"M4 12a8 8 0 1 0 2.5-5.8\" /><path d=\"M4 4v4.5h4.5\" /></svg>\n          ) : (\n            <svg viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2.2\" strokeLinecap=\"round\" strokeLinejoin=\"round\"><path d=\"M4 7h16M9 7V4.5h6V7M6 7l1 13h10l1-13\" /></svg>\n          )}\n        </span>\n        <span className=\"hb-xd6-msg\">\n          <span>{restoring ? restoredLabel : committed ? committedLabel : message}</span>\n          {phase === \"pending\" && <small>{`Undo within ${left}s`}</small>}\n        </span>\n        {phase === \"pending\" && (\n          <button ref={undoRef} type=\"button\" className=\"hb-xd6-undo\" onClick={undo}>\n            {undoLabel}\n            <kbd aria-hidden=\"true\">⌘Z</kbd>\n          </button>\n        )}\n        <span className=\"hb-xd6-bar\" aria-hidden=\"true\" />\n      </span>\n      <span className=\"hb-xd6-sr\" role=\"status\" aria-live=\"polite\">\n        {phase === \"pending\" ? message : restoring ? restoredLabel : committed ? committedLabel : \"\"}\n      </span>\n    </span>\n  );\n}\n"
    }
  ]
}