{ "$schema": "https://ui.shadcn.com/schema/registry-item.json", "name": "confirm-action", "type": "registry:ui", "title": "Confirm action", "description": "Hold or type an exact phrase to confirm a destructive action, with busy and failure feedback.", "dependencies": ["@fungi.computer/stipe", "react"], "registryDependencies": [ "@shiit/button", "@shiit/dialog", "@shiit/field", "@shiit/input", "@shiit/undo-action" ], "files": [ { "path": "../src/components/confirm-action.tsx", "type": "registry:ui", "target": "@ui/confirm-action.tsx", "content": "import { useRef, useState } from \"react\";\n\nimport { Button, type ButtonSize, type ButtonVariant } from \"./button.js\";\nimport {\n Dialog,\n DialogClose,\n DialogContent,\n DialogDescription,\n DialogHeader,\n DialogTitle,\n DialogTrigger,\n} from \"./dialog.js\";\nimport { Field, FieldError, Form, FormFooter } from \"./field.js\";\nimport { HoldButton } from \"./hold-button.js\";\nimport { Input } from \"./input.js\";\n\n/** A deliberate hold, or an exact phrase before a destructive action. */\nexport type ConfirmActionProps = {\n /** The action's trigger label and dialog title. */\n label: string;\n /** The destructive key's label (and the hold gesture's instruction). */\n confirmLabel: string;\n /** Resolve on success; reject on failure. The component prevents concurrent calls. */\n onConfirm: () => Promise;\n /** Caller-owned, safe-to-display failure copy; rejection details are never displayed. */\n error?: string;\n /** The trigger key's look; destructive by default, quiet where a toolbar needs it. */\n variant?: ButtonVariant;\n /** The trigger key's size. */\n size?: ButtonSize;\n} & ({ mode: \"hold\"; phrase?: never } | { mode: \"type\"; phrase: string });\n\n/**\n * Confirms once per gesture, owns pending/result feedback, and keeps failed\n * confirmations available for retry. Type mode closes on success and restores\n * focus through Dialog. A pending action cannot be dismissed or cancelled.\n */\nexport function ConfirmAction({\n label,\n confirmLabel,\n onConfirm,\n error,\n variant = \"destructive\",\n size,\n mode,\n phrase,\n}: ConfirmActionProps) {\n const [open, setOpen] = useState(false);\n const [typed, setTyped] = useState(\"\");\n const [state, setState] = useState<\"idle\" | \"busy\" | \"done\" | Error>(\"idle\");\n const pending = useRef(false);\n const input = useRef(null);\n const busy = state === \"busy\";\n const matches = mode === \"hold\" || (phrase.length > 0 && typed === phrase);\n const message = busy\n ? undefined\n : error !== undefined && error !== \"\"\n ? error\n : state instanceof Error\n ? state.message\n : undefined;\n const result = message ? \"failed\" : state === \"done\" ? \"done\" : undefined;\n\n async function confirm() {\n if (pending.current || !matches) return;\n pending.current = true;\n setState(\"busy\");\n try {\n await onConfirm();\n setState(\"done\");\n setOpen(false);\n setTyped(\"\");\n } catch (cause) {\n setState(\n new Error(\"Could not confirm the action. Try again.\", { cause }),\n );\n } finally {\n pending.current = false;\n }\n }\n\n if (mode === \"hold\") {\n return (\n
\n void confirm()}\n >\n {label}\n \n {message}\n
\n );\n }\n\n return (\n {\n if (pending.current) {\n details.cancel();\n return;\n }\n setOpen(next);\n setTyped(\"\");\n setState(\"idle\");\n }}\n >\n \n }\n >\n {label}\n \n \n {\n event.preventDefault();\n void confirm();\n }}\n >\n \n {label}\n \n Type the phrase exactly to confirm.\n \n \n \n {\n setTyped(event.currentTarget.value);\n }}\n />\n \n \n } disabled={busy}>\n Cancel\n \n \n {confirmLabel}\n \n \n \n \n \n );\n}\n" } ], "css": { "@import \"@fungi.computer/stipe/styles.css\"": {} } }