{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "confirm-dialog",
  "title": "ConfirmDialog",
  "description": "Asks the user to confirm an action, with Cancel focused first.",
  "registryDependencies": [
    "@ouro/button",
    "@ouro/dialog",
    "@ouro/lib-dev",
    "@ouro/lib-env",
    "@ouro/lib-open-mode",
    "@ouro/lib-tones",
    "@ouro/lib-use-dev-messages"
  ],
  "files": [
    {
      "path": "registry/ouro/components/ouro/dialog/confirm-dialog.tsx",
      "content": "\"use client\";\n\nimport { useEffect, useRef, useState, type JSX, type ReactElement, type ReactNode, type RefObject } from \"react\";\nimport { devWarn } from \"@/registry/ouro/lib/ouro/dev\";\nimport { checkOpenMode, type TriggerOrOpen } from \"@/registry/ouro/lib/ouro/open-mode\";\nimport { toneWarning } from \"@/registry/ouro/lib/ouro/tones\";\nimport { useDevWarnings } from \"@/registry/ouro/lib/ouro/use-dev-messages\";\nimport { Button } from \"@/registry/ouro/components/ouro/button/button\";\nimport {\n  AlertDialogRoot,\n  DialogBackdrop,\n  DialogClose,\n  DialogDescription,\n  DialogFooter,\n  DialogPopupUnchecked,\n  DialogPortal,\n  DialogTitle,\n  DialogTrigger,\n  DialogViewport,\n} from \"@/registry/ouro/components/ouro/dialog/dialog-parts\";\n\n/** Props shared by both ways of opening a ConfirmDialog. */\nexport interface ConfirmDialogBaseProps {\n  /** The question, which names the dialog: `title=\"Delete recipe?\"`. */\n  title: string;\n  /** What happens if the user confirms: `description=\"This can't be undone.\"`. */\n  description?: ReactNode;\n  /** The confirm button's label: the action, not \"OK\" (`confirmLabel=\"Delete\"`). */\n  confirmLabel: string;\n  /** The cancel button's label. Cancel has focus when the dialog opens. @default \"Cancel\" */\n  cancelLabel?: string | undefined;\n  /** The confirm button's color role. Use `destructive` for destructive actions. @default \"primary\" */\n  tone?: \"primary\" | \"destructive\" | (string & { 'danger is \"destructive\"; accent is \"primary\"': never }) | undefined;\n  /**\n   * Runs the action. Return a promise to keep the dialog open while it runs: the\n   * confirm button shows `loading` until it settles. When it resolves the dialog\n   * closes; when it rejects (or `onConfirm` throws) the dialog stays open so the\n   * user can retry or cancel. Cancel and Escape still close it while pending, so a\n   * stuck action never traps the user, and the result is then ignored. Cancel does\n   * not stop an action that has already started; make `onConfirm` cancellable or\n   * idempotent if that matters.\n   *\n   * If the action removes the trigger (a delete), pass `finalFocus` to what\n   * replaces it, such as the next item or the page heading.\n   * @claim \"Cancel and Escape still close it\" registry/ouro/components/ouro/dialog/confirm-dialog.test.tsx verify:testExists(registry/ouro/components/ouro/dialog/confirm-dialog.test.tsx::ConfirmDialog > ignores a promise that settles after the user cancelled)\n   * @claim \"Cancel and Escape still close it\" registry/ouro/components/ouro/dialog/confirm-dialog.test.tsx verify:testExists(registry/ouro/components/ouro/dialog/confirm-dialog.test.tsx::ConfirmDialog > closes from Cancel without confirming, and takes cancelLabel)\n   */\n  onConfirm: () => void | Promise<void>;\n  /**\n   * Where focus goes after a confirmed action closes the confirmation. Any other close\n   * (Cancel, Escape, or your app setting `open` to false) returns focus to the trigger,\n   * even after an earlier confirm. When `onConfirm` removes the trigger,\n   * point this at what replaces it (give a heading `tabIndex={-1}`):\n   * `finalFocus={headingRef}`.\n   */\n  finalFocus?: RefObject<HTMLElement | null> | undefined;\n\n  /** Not a prop. Ouro UI uses `open` with `onOpenChange`: `<ConfirmDialog open={open} onOpenChange={setOpen}>`. */\n  isOpen?: { 'Use <ConfirmDialog open={open} onOpenChange={setOpen}>': never };\n  /** Not a prop. `onOpenChange(false)` fires on close: `<ConfirmDialog open={open} onOpenChange={setOpen}>`. */\n  onClose?: { 'Use <ConfirmDialog open={open} onOpenChange={setOpen}>': never };\n  /** Not a prop. Cancel calls `onOpenChange(false)`: `<ConfirmDialog open={open} onOpenChange={setOpen}>`. */\n  onCancel?: { 'Use <ConfirmDialog open={open} onOpenChange={setOpen}>': never };\n  /** Not a prop. Ouro UI uses `tone`: `<ConfirmDialog tone=\"destructive\">`. */\n  destructive?: { 'Use <ConfirmDialog tone=\"destructive\">': never };\n  /** Not a prop. Ouro UI uses `confirmLabel`: `<ConfirmDialog confirmLabel=\"Delete\">`. */\n  confirmText?: { 'Use <ConfirmDialog confirmLabel=\"Delete\">': never };\n  /** Not a prop. Ouro UI uses `cancelLabel`: `<ConfirmDialog cancelLabel=\"Keep\">`. */\n  cancelText?: { 'Use <ConfirmDialog cancelLabel=\"Keep\">': never };\n  /** Not a prop. Ouro UI uses `tone`: `<ConfirmDialog tone=\"destructive\">`. */\n  variant?: { 'Use <ConfirmDialog tone=\"destructive\">': never };\n}\n\n/** An uncontrolled ConfirmDialog: `trigger` opens it. */\nexport type ConfirmDialogWithTriggerProps = ConfirmDialogBaseProps & {\n  /** The element that opens the confirmation, usually a Button: `trigger={<Button tone=\"destructive\">Delete</Button>}`. */\n  trigger: ReactElement;\n  /** Not with `trigger`. Remove `trigger` to control the confirmation with `open` and `onOpenChange`. */\n  open?: TriggerOrOpen;\n  /** Not with `trigger`. Remove `trigger` to control the confirmation with `open` and `onOpenChange`. */\n  onOpenChange?: never;\n};\n\n/** A controlled ConfirmDialog: you own `open` and update it in `onOpenChange`. */\nexport type ConfirmDialogControlledProps = ConfirmDialogBaseProps & {\n  /** Whether the confirmation is open. */\n  open: boolean;\n  /** Called with `false` when the user cancels (Cancel or Escape) and when `onConfirm` finishes. */\n  onOpenChange: (open: boolean) => void;\n  /** Not with `open`. Remove `open` and `onOpenChange` to let a trigger open the confirmation. */\n  trigger?: never;\n};\n\n/** Props for {@link ConfirmDialog}: either `trigger`, or `open` with `onOpenChange`. */\nexport type ConfirmDialogProps = ConfirmDialogWithTriggerProps | ConfirmDialogControlledProps;\n\n/**\n * Asks the user to confirm an action, with Cancel focused first. It is always\n * a centered dialog, on phones too, and an outside press does not dismiss it.\n * The actions stack full width on phones, the confirm action at the bottom,\n * nearest the thumb.\n *\n * Use `tone=\"destructive\"` for destructive actions, and return a promise from\n * `onConfirm` to show progress until the action finishes. Cancel and Escape close\n * the dialog even while the action runs, and its result is then ignored. Cancel does\n * not stop an action that has already started; make `onConfirm` cancellable or\n * idempotent if that matters. Use `Dialog` instead when the user has more to do\n * than confirm or cancel.\n *\n * It stacks above the page at `--ouro-layer`, a z-index of 50 by default (from\n * ouro-base.css). If your app has fixed bars above z-50, raise it in a `:root` rule\n * of your own after the Ouro tokens, outside any `@layer`: `:root { --ouro-layer: 60; }`.\n * Don't edit the `--ouro-layer: 50` line shadcn wrote; `shadcn add` resets it.\n *\n * Testing accessibility: in WebKit, Base UI's focus guards take `role=\"button\"`\n * without a name so VoiceOver's cursor stays trapped. Exclude them:\n * `new AxeBuilder({ page }).exclude(\"[data-base-ui-focus-guard]\")`.\n *\n * @group Overlays\n * @see https://ui.ouroboros.bot/docs/confirm-dialog\n *\n * @example\n * <ConfirmDialog\n *   title=\"Delete recipe?\"\n *   description=\"This can't be undone.\"\n *   confirmLabel=\"Delete\"\n *   tone=\"destructive\"\n *   onConfirm={() => deleteRecipe(id)}\n *   trigger={<Button tone=\"destructive\" variant=\"soft\">Delete</Button>}\n * />\n * @claim \"It stacks above the page at\" registry/ouro/components/ouro/dialog/dialog.test.tsx verify:testExists(registry/ouro/components/ouro/dialog/dialog.test.tsx::Dialog stacking above the app's fixed bars > puts the portal on the --ouro-layer level, as its own stacking context)\n * @claim \"It stacks above the page at\" registry/ouro/theme/ouro-base.css verify:fileLine(registry/ouro/theme/ouro-base.css, /^\\s+--ouro-layer:\\s50;/)\n * @claim \"Testing accessibility: in WebKit, Base UI's\" test/axe-exclude.test.tsx verify:testExists(test/axe-exclude.test.tsx::matches the open dialog's focus guards, and with them excluded the accessibility gate finds nothing)\n * @claim \"It is always a centered dialog\" registry/ouro/components/ouro/dialog/confirm-dialog.test.tsx verify:testExists(registry/ouro/components/ouro/dialog/confirm-dialog.test.tsx::ConfirmDialog > does not close on an outside press)\n * @claim \"It is always a centered dialog\" registry/ouro/components/ouro/dialog/confirm-dialog.test.tsx verify:testExists(registry/ouro/components/ouro/dialog/confirm-dialog.test.tsx::ConfirmDialog > stays centered on a phone with side margins and stacks its actions with confirm at the bottom)\n */\nexport function ConfirmDialog(props: ConfirmDialogProps): JSX.Element {\n  const {\n    title,\n    description,\n    confirmLabel,\n    cancelLabel = \"Cancel\",\n    tone: toneProp = \"primary\",\n    onConfirm,\n    finalFocus,\n    trigger,\n    open,\n    onOpenChange,\n    // Guard props (hint types), dropped here so they never reach the DOM.\n    isOpen,\n    onClose,\n    onCancel,\n    destructive,\n    confirmText,\n    cancelText,\n    variant,\n  } = props as ConfirmDialogBaseProps & {\n    trigger?: ReactElement;\n    open?: boolean;\n    onOpenChange?: (open: boolean) => void;\n  };\n\n  // A tone that is not one of the confirm tones (a type error) warns and renders primary.\n  const tone = toneProp === \"destructive\" ? \"destructive\" : \"primary\";\n\n  checkOpenMode(\"ConfirmDialog\", \"confirmation\", trigger !== undefined, open, onOpenChange);\n\n  useDevWarnings(\n    \"ConfirmDialog\",\n    process.env.NODE_ENV !== \"production\"\n      ? () => {\n          const controlFix = \"<ConfirmDialog open={open} onOpenChange={setOpen}>\";\n          return [\n            !title && [\"title is required; it names the confirmation for screen readers\", '<ConfirmDialog title=\"Delete recipe?\">'],\n            tone !== toneProp && toneWarning(\"ConfirmDialog\", String(toneProp), [\"primary\", \"destructive\"]),\n            isOpen !== undefined && [\"isOpen is not a prop\", controlFix],\n            onClose !== undefined && [\"onClose is not a prop\", controlFix],\n            onCancel !== undefined && [\"onCancel is not a prop; Cancel calls onOpenChange(false)\", controlFix],\n            destructive !== undefined && [\"destructive is not a prop\", '<ConfirmDialog tone=\"destructive\">'],\n            confirmText !== undefined && [\"confirmText is not a prop\", '<ConfirmDialog confirmLabel=\"Delete\">'],\n            cancelText !== undefined && [\"cancelText is not a prop\", '<ConfirmDialog cancelLabel=\"Keep\">'],\n            variant !== undefined && [\"variant is not a prop\", '<ConfirmDialog tone=\"destructive\">'],\n          ];\n        }\n      : /* istanbul ignore next @preserve -- production only */ undefined,\n  );\n\n  const [ownOpen, setOwnOpen] = useState(false);\n  const [pending, setPending] = useState(false);\n  // Each open, close and confirm bumps the attempt, so a promise that settles\n  // after the user cancelled does not close a dialog they have since reopened.\n  const attempt = useRef(0);\n  const warnKey = useRef({});\n  const cancelRef = useRef<HTMLButtonElement>(null);\n\n  // Whether the last close came from a confirmed action. Only then does focus go to\n  // `finalFocus`; Cancel and Escape leave the trigger in place, so focus returns to it.\n  const confirmed = useRef(false);\n\n  const controlled = open !== undefined;\n  const shown = controlled ? open : ownOpen;\n  // Every open starts unconfirmed, however it opened. An app that sets `open` itself\n  // never calls setOpen, so without this a close the app makes after reopening a\n  // confirmed dialog would still send focus to `finalFocus`.\n  useEffect(() => {\n    if (shown) confirmed.current = false;\n  }, [shown]);\n\n  const setOpen = (next: boolean, afterConfirm = false) => {\n    confirmed.current = afterConfirm;\n    attempt.current++;\n    setPending(false);\n    if (controlled) onOpenChange!(next);\n    else setOwnOpen(next);\n  };\n\n  const confirm = () => {\n    let result: void | Promise<void>;\n    try {\n      result = onConfirm();\n    } catch (error) {\n      // A synchronous throw takes the rejection path: the dialog stays open and the error is reported.\n      result = Promise.reject(error);\n    }\n    if (!result) {\n      setOpen(false, true);\n      return;\n    }\n    const current = ++attempt.current;\n    setPending(true);\n    result.then(\n      () => {\n        if (attempt.current === current) setOpen(false, true);\n      },\n      (error: unknown) => {\n        // A rejection from an attempt the user already left (Cancel, Escape, a newer\n        // confirm) must not clear a newer confirm's busy state; it is still reported.\n        const stale = attempt.current !== current;\n        if (!stale) setPending(false);\n        /* istanbul ignore else @preserve -- the else path exists only in production bundles; scripts/dev-strip.test.mjs covers it */\n        if (process.env.NODE_ENV !== \"production\") {\n          const message = error instanceof Error ? error.message : String(error);\n          devWarn(\n            \"ConfirmDialog\",\n            stale\n              ? `onConfirm rejected with \"${message}\" after the user had left that confirmation`\n              : `onConfirm rejected with \"${message}\", so the dialog stayed open`,\n            \"onConfirm={() => remove().catch((error) => { setError(error.message); throw error; })} description={error}\",\n            warnKey.current,\n          );\n        }\n      },\n    );\n  };\n\n  return (\n    <AlertDialogRoot open={shown} onOpenChange={(next) => setOpen(next)}>\n      {trigger !== undefined && <DialogTrigger render={trigger} />}\n      <DialogPortal>\n        <DialogBackdrop />\n        <DialogViewport>\n          <DialogPopupUnchecked\n            size=\"sm\"\n            initialFocus={cancelRef}\n            {...(finalFocus\n              ? { finalFocus: () => (confirmed.current ? (finalFocus.current ?? true) : true) }\n              : {})}\n            data-ouro-confirm=\"\"\n          >\n            <div data-ouro-dialog-header=\"\" className=\"box-border grid shrink-0 gap-1 px-5 pt-5 pb-2\">\n              <DialogTitle>{title}</DialogTitle>\n              {description !== undefined && <DialogDescription>{description}</DialogDescription>}\n            </div>\n            <DialogFooter>\n              <DialogClose\n                ref={cancelRef}\n                render={\n                  <Button variant=\"outline\" tone=\"secondary\" data-ouro-control=\"confirm-dialog\" data-ouro-part=\"cancel\">\n                    {cancelLabel}\n                  </Button>\n                }\n              />\n              <Button tone={tone} loading={pending} onClick={confirm} data-tone={tone} data-ouro-control=\"confirm-dialog\" data-ouro-part=\"confirm\">\n                {confirmLabel}\n              </Button>\n            </DialogFooter>\n          </DialogPopupUnchecked>\n        </DialogViewport>\n      </DialogPortal>\n    </AlertDialogRoot>\n  );\n}\n",
      "type": "registry:ui",
      "target": "@components/ouro/dialog/confirm-dialog.tsx"
    }
  ],
  "docs": "Asks the user to confirm an action, with Cancel focused first. Import: import { ConfirmDialog } from \"@/components/ouro/dialog/confirm-dialog\", with @/components replaced by your components alias (aliases.components in components.json, such as ~/components). Also installed: button, dialog, sheet, the helpers they import in lib/ouro. Needs ouro-base in your stylesheet: add that item once per app, from the same place as this one, if you have not. Docs: https://ui.ouroboros.bot/docs/confirm-dialog.md Ouro UI v1.3.257.",
  "type": "registry:ui"
}