{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "alert-dialog",
  "title": "Alert Dialog",
  "description": "A question that has to be answered before anything else, with focus on the safe answer, a typed confirmation for the worst cases and a bottom sheet on phones.",
  "dependencies": [
    "radix-ui",
    "lucide-react"
  ],
  "registryDependencies": [
    "utils",
    "https://miseui.junsu4621.workers.dev/r/button.json",
    "https://miseui.junsu4621.workers.dev/r/input.json"
  ],
  "files": [
    {
      "path": "registry/miseui/ui/alert-dialog.tsx",
      "content": "import type * as React from \"react\"\r\nimport { useContext, useMemo, useRef } from \"react\"\r\nimport { AlertDialog as AlertDialogPrimitive } from \"radix-ui\"\r\nimport { AlertTriangle } from \"lucide-react\"\r\nimport { AlertDialogConfirmField, ConfirmContext, ConfirmScope, RootContext } from \"@/registry/miseui/ui/alert-dialog-confirm\"\r\nimport {\r\n  BUTTON_FIT_CLASS,\r\n  COMPACT_ACTION_TONE,\r\n  COMPACT_BUTTON_CLASS,\r\n  FOOTER_CLASS,\r\n  HEADER_CLASS,\r\n  MATERIALIZE_CLASS,\r\n  OVERLAY_CLASS,\r\n  PLAIN_MOTION_CLASS,\r\n  SHELL_CLASS,\r\n  VARIANT_SHELL_CLASS,\r\n  matchesConfirmation,\r\n  type AlertDialogEffect,\r\n  type AlertDialogTone,\r\n  type AlertDialogVariant,\r\n} from \"@/registry/miseui/ui/alert-dialog-classes\"\r\nimport { buttonVariants } from \"@/registry/miseui/ui/button\"\r\nimport { cn } from \"@/registry/miseui/lib/utils\"\r\n\r\ntype AlertDialogProps = React.ComponentProps<typeof AlertDialogPrimitive.Root> & {\r\n  /** `plain` a calm question, `destructive` a warning mark and a red action, `typed` a word must be typed first, `compact` a small centred alert with joined buttons. */\r\n  variant?: AlertDialogVariant\r\n  /** `materialize` brings the surface into focus with scale, blur and opacity together. */\r\n  effect?: AlertDialogEffect\r\n  /** Colours the action. `destructive` and `typed` default to destructive, the others to neutral. */\r\n  tone?: AlertDialogTone\r\n  /** With `typed`: what has to be typed before the action unlocks. */\r\n  confirmWord?: string\r\n}\r\n\r\nfunction AlertDialog({ variant = \"plain\", effect, tone, confirmWord, ...props }: AlertDialogProps) {\r\n  const resolvedTone = tone ?? (variant === \"destructive\" || variant === \"typed\" ? \"destructive\" : \"neutral\")\r\n  const context = useMemo(() => ({ variant, effect, tone: resolvedTone, confirmWord }), [variant, effect, resolvedTone, confirmWord])\r\n  return (\r\n    <RootContext.Provider value={context}>\r\n      <AlertDialogPrimitive.Root data-slot=\"alert-dialog\" {...props} />\r\n    </RootContext.Provider>\r\n  )\r\n}\r\n\r\nfunction AlertDialogTrigger(props: React.ComponentProps<typeof AlertDialogPrimitive.Trigger>) {\r\n  return <AlertDialogPrimitive.Trigger data-slot=\"alert-dialog-trigger\" {...props} />\r\n}\r\n\r\nfunction AlertDialogContent({ className, children, onOpenAutoFocus, ...props }: React.ComponentProps<typeof AlertDialogPrimitive.Content>) {\r\n  const { variant, effect, tone } = useContext(RootContext)\r\n  const inputRef = useRef<HTMLInputElement>(null)\r\n  return (\r\n    <AlertDialogPrimitive.Portal>\r\n      <AlertDialogPrimitive.Overlay data-slot=\"alert-dialog-overlay\" className={OVERLAY_CLASS} />\r\n      <AlertDialogPrimitive.Content\r\n        data-slot=\"alert-dialog-content\"\r\n        data-variant={variant}\r\n        data-effect={effect}\r\n        data-tone={tone}\r\n        // Radix focuses Cancel, the safe answer. Typing is just as safe and is the only way forward, so `typed` starts there.\r\n        onOpenAutoFocus={(event) => {\r\n          onOpenAutoFocus?.(event)\r\n          if (event.defaultPrevented || variant !== \"typed\" || !inputRef.current) return\r\n          event.preventDefault()\r\n          inputRef.current.focus()\r\n        }}\r\n        className={cn(SHELL_CLASS, VARIANT_SHELL_CLASS[variant], effect === \"materialize\" ? MATERIALIZE_CLASS : PLAIN_MOTION_CLASS, className)}\r\n        {...props}\r\n      >\r\n        <ConfirmScope inputRef={inputRef}>{children}</ConfirmScope>\r\n      </AlertDialogPrimitive.Content>\r\n    </AlertDialogPrimitive.Portal>\r\n  )\r\n}\r\n\r\nfunction AlertDialogHeader({ className, children, ...props }: React.ComponentProps<\"div\">) {\r\n  const { variant } = useContext(RootContext)\r\n  return (\r\n    <div data-slot=\"alert-dialog-header\" className={cn(HEADER_CLASS[variant], className)} {...props}>\r\n      {variant === \"destructive\" ? (\r\n        <>\r\n          <span aria-hidden=\"true\" data-slot=\"alert-dialog-mark\" className=\"flex size-10 shrink-0 items-center justify-center rounded-full bg-destructive/10 text-destructive dark:bg-destructive/15\">\r\n            <AlertTriangle className=\"size-5\" />\r\n          </span>\r\n          <div className=\"grid min-w-0 gap-2\">{children}</div>\r\n        </>\r\n      ) : (\r\n        children\r\n      )}\r\n    </div>\r\n  )\r\n}\r\n\r\nfunction AlertDialogTitle({ className, ...props }: React.ComponentProps<typeof AlertDialogPrimitive.Title>) {\r\n  return <AlertDialogPrimitive.Title data-slot=\"alert-dialog-title\" className={cn(\"text-[15px] leading-6 font-semibold text-foreground\", className)} {...props} />\r\n}\r\n\r\nfunction AlertDialogDescription({ className, ...props }: React.ComponentProps<typeof AlertDialogPrimitive.Description>) {\r\n  return <AlertDialogPrimitive.Description data-slot=\"alert-dialog-description\" className={cn(\"text-sm leading-5 text-muted-foreground\", className)} {...props} />\r\n}\r\n\r\nfunction AlertDialogFooter({ className, ...props }: React.ComponentProps<\"div\">) {\r\n  const { variant } = useContext(RootContext)\r\n  return <div data-slot=\"alert-dialog-footer\" className={cn(FOOTER_CLASS[variant], className)} {...props} />\r\n}\r\n\r\n/** To keep the dialog open during async work, call `event.preventDefault()` in onClick and close it yourself. */\r\nfunction AlertDialogAction({ className, disabled, ...props }: React.ComponentProps<typeof AlertDialogPrimitive.Action>) {\r\n  const { variant, tone, confirmWord } = useContext(RootContext)\r\n  const confirm = useContext(ConfirmContext)\r\n  const locked = variant === \"typed\" && confirmWord !== undefined && !matchesConfirmation(confirm?.typed ?? \"\", confirmWord)\r\n  const look =\r\n    variant === \"compact\"\r\n      ? cn(COMPACT_BUTTON_CLASS, COMPACT_ACTION_TONE[tone])\r\n      : cn(\r\n          buttonVariants({ variant: tone === \"destructive\" ? \"destructive\" : \"default\" }),\r\n          BUTTON_FIT_CLASS,\r\n          // The dark error colour is too light for the white text the button brings.\r\n          tone === \"destructive\" && \"dark:text-background\",\r\n        )\r\n  return <AlertDialogPrimitive.Action data-slot=\"alert-dialog-action\" data-tone={tone} disabled={disabled || locked} className={cn(look, className)} {...props} />\r\n}\r\n\r\nfunction AlertDialogCancel({ className, ...props }: React.ComponentProps<typeof AlertDialogPrimitive.Cancel>) {\r\n  const { variant } = useContext(RootContext)\r\n  const look = variant === \"compact\" ? cn(COMPACT_BUTTON_CLASS, \"text-foreground\") : cn(buttonVariants({ variant: \"outline\" }), BUTTON_FIT_CLASS)\r\n  return <AlertDialogPrimitive.Cancel data-slot=\"alert-dialog-cancel\" className={cn(look, className)} {...props} />\r\n}\r\n\r\nexport {\r\n  AlertDialog,\r\n  AlertDialogTrigger,\r\n  AlertDialogContent,\r\n  AlertDialogHeader,\r\n  AlertDialogTitle,\r\n  AlertDialogDescription,\r\n  AlertDialogConfirmField,\r\n  AlertDialogFooter,\r\n  AlertDialogAction,\r\n  AlertDialogCancel,\r\n  matchesConfirmation,\r\n  type AlertDialogProps,\r\n  type AlertDialogVariant,\r\n  type AlertDialogEffect,\r\n  type AlertDialogTone,\r\n}\r\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/miseui/ui/alert-dialog-classes.ts",
      "content": "import { cn } from \"@/registry/miseui/lib/utils\"\n\ntype AlertDialogVariant = \"plain\" | \"destructive\" | \"typed\" | \"compact\"\ntype AlertDialogEffect = \"materialize\"\ntype AlertDialogTone = \"neutral\" | \"destructive\"\n\n/**\n * Below sm the surface is a sheet on the bottom edge, where a thumb can reach the answer. It has no drag handle:\n * this question has to be answered, so it must not be swipeable away.\n */\nconst SHELL_CLASS = cn(\n  \"fixed inset-x-0 bottom-0 z-50 grid max-h-[85dvh] overflow-y-auto rounded-t-xl border-t bg-background shadow-lg outline-none\",\n  \"sm:inset-x-auto sm:top-1/2 sm:bottom-auto sm:left-1/2 sm:w-full sm:-translate-x-1/2 sm:-translate-y-1/2 sm:rounded-lg sm:border\",\n)\n\nconst VARIANT_SHELL_CLASS: Record<AlertDialogVariant, string> = {\n  plain: \"gap-4 p-6 pb-[calc(1.5rem+env(safe-area-inset-bottom))] sm:max-w-md sm:pb-6\",\n  destructive: \"gap-4 p-6 pb-[calc(1.5rem+env(safe-area-inset-bottom))] sm:max-w-md sm:pb-6\",\n  typed: \"gap-4 p-6 pb-[calc(1.5rem+env(safe-area-inset-bottom))] sm:max-w-md sm:pb-6\",\n  compact: \"pb-[env(safe-area-inset-bottom)] sm:max-w-xs sm:pb-0\",\n}\n\nconst PLAIN_MOTION_CLASS = [\n  \"duration-(--duration-base) ease-(--ease-mise)\",\n  \"data-[state=open]:animate-in data-[state=closed]:animate-out fade-in-0 fade-out-0\",\n  \"max-sm:slide-in-from-bottom max-sm:slide-out-to-bottom sm:zoom-in-95 sm:zoom-out-95\",\n  \"max-sm:motion-reduce:slide-in-from-bottom-0 max-sm:motion-reduce:slide-out-to-bottom-0 motion-reduce:zoom-in-100 motion-reduce:zoom-out-100\",\n]\n\n/** Radix exposes no origin for a dialog, since it has no anchor. The enter and exit values stay unqualified so the media overrides win on source order. */\nconst MATERIALIZE_CLASS = [\n  \"duration-(--duration-slow) ease-(--ease-mise-out) will-change-[transform,opacity,filter]\",\n  \"data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:duration-(--duration-base)\",\n  \"fade-in-0 fade-out-0 blur-in-md blur-out-md\",\n  \"max-sm:slide-in-from-bottom max-sm:slide-out-to-bottom sm:zoom-in-90 sm:zoom-out-90\",\n  \"max-sm:motion-reduce:slide-in-from-bottom-0 max-sm:motion-reduce:slide-out-to-bottom-0 motion-reduce:zoom-in-100 motion-reduce:zoom-out-100\",\n  \"motion-reduce:blur-in-0 motion-reduce:blur-out-0\",\n  \"[@media(prefers-reduced-transparency:reduce)]:blur-in-0 [@media(prefers-reduced-transparency:reduce)]:blur-out-0\",\n]\n\nconst OVERLAY_CLASS = cn(\n  \"fixed inset-0 z-50 bg-background/80 backdrop-blur-sm\",\n  \"duration-(--duration-base) ease-(--ease-mise)\",\n  \"data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=closed]:animate-out data-[state=closed]:fade-out-0\",\n)\n\nconst HEADER_CLASS: Record<AlertDialogVariant, string> = {\n  plain: \"grid gap-2\",\n  destructive: \"flex items-start gap-4\",\n  typed: \"grid gap-2\",\n  compact: \"grid gap-1 p-6 text-center\",\n}\n\nconst FOOTER_CLASS: Record<AlertDialogVariant, string> = {\n  plain: \"flex flex-col-reverse gap-2 sm:flex-row sm:justify-end\",\n  destructive: \"flex flex-col-reverse gap-2 sm:flex-row sm:justify-end\",\n  typed: \"flex flex-col-reverse gap-2 sm:flex-row sm:justify-end\",\n  compact: \"grid grid-cols-2 divide-x border-t\",\n}\n\nconst BUTTON_FIT_CLASS = \"max-sm:h-10 max-sm:w-full pointer-coarse:h-10\"\n\nconst COMPACT_BUTTON_CLASS = cn(\n  \"h-12 w-full text-[15px] outline-none select-none\",\n  \"transition-colors duration-(--duration-fast) ease-(--ease-mise) hover:bg-accent active:bg-accent/70 active:duration-0\",\n  \"focus-visible:ring-[3px] focus-visible:ring-ring/50 focus-visible:ring-inset\",\n  \"disabled:pointer-events-none disabled:opacity-50\",\n)\n\nconst COMPACT_ACTION_TONE: Record<AlertDialogTone, string> = {\n  neutral: \"font-semibold text-foreground\",\n  destructive: \"font-semibold text-destructive\",\n}\n\n/** Only the outer spaces go: a pasted word often brings them along. Case stays strict, as this is deliberate friction. */\nfunction matchesConfirmation(typed: string, word: string) {\n  return typed.trim() === word.trim() && word.trim() !== \"\"\n}\n\nexport {\n  BUTTON_FIT_CLASS,\n  COMPACT_ACTION_TONE,\n  COMPACT_BUTTON_CLASS,\n  FOOTER_CLASS,\n  HEADER_CLASS,\n  MATERIALIZE_CLASS,\n  OVERLAY_CLASS,\n  PLAIN_MOTION_CLASS,\n  SHELL_CLASS,\n  VARIANT_SHELL_CLASS,\n  matchesConfirmation,\n  type AlertDialogEffect,\n  type AlertDialogTone,\n  type AlertDialogVariant,\n}\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/miseui/ui/alert-dialog-confirm.tsx",
      "content": "import type * as React from \"react\"\nimport { createContext, useContext, useId, useMemo, useState } from \"react\"\nimport { matchesConfirmation, type AlertDialogEffect, type AlertDialogTone, type AlertDialogVariant } from \"@/registry/miseui/ui/alert-dialog-classes\"\nimport { Input } from \"@/registry/miseui/ui/input\"\nimport { cn } from \"@/registry/miseui/lib/utils\"\n\ninterface RootContextValue {\n  variant: AlertDialogVariant\n  effect: AlertDialogEffect | undefined\n  tone: AlertDialogTone\n  confirmWord: string | undefined\n}\n\nconst RootContext = createContext<RootContextValue>({ variant: \"plain\", effect: undefined, tone: \"neutral\", confirmWord: undefined })\n\ninterface ConfirmContextValue {\n  typed: string\n  setTyped: (value: string) => void\n  inputRef: React.RefObject<HTMLInputElement | null>\n}\n\nconst ConfirmContext = createContext<ConfirmContextValue | null>(null)\n\n/**\n * Rendered inside the Radix content because that is the part that unmounts on close; a wrapper around it stays\n * mounted, and a reopened dialog would start already unlocked.\n */\nfunction ConfirmScope({ inputRef, children }: { inputRef: React.RefObject<HTMLInputElement | null>; children: React.ReactNode }) {\n  const [typed, setTyped] = useState(\"\")\n  const confirm = useMemo(() => ({ typed, setTyped, inputRef }), [typed, inputRef])\n  return <ConfirmContext.Provider value={confirm}>{children}</ConfirmContext.Provider>\n}\n\ntype AlertDialogConfirmFieldProps = Omit<React.ComponentProps<\"input\">, \"value\" | \"onChange\" | \"ref\"> & {\n  /** Replaces the sentence above the field. */\n  label?: React.ReactNode\n}\n\n/** The field of the `typed` variant. Enter runs the action once the word matches. */\nfunction AlertDialogConfirmField({ label, className, onKeyDown, ...props }: AlertDialogConfirmFieldProps) {\n  const { confirmWord = \"\" } = useContext(RootContext)\n  const confirm = useContext(ConfirmContext)\n  const id = useId()\n  return (\n    <div data-slot=\"alert-dialog-confirm\" className={cn(\"grid gap-2\", className)}>\n      <label htmlFor={id} className=\"text-[13px] leading-5 text-muted-foreground\">\n        {label ?? (\n          <>\n            Type <strong className=\"font-medium break-all text-foreground select-all\">{confirmWord}</strong> to confirm\n          </>\n        )}\n      </label>\n      <Input\n        id={id}\n        ref={confirm?.inputRef}\n        value={confirm?.typed ?? \"\"}\n        // A phone keyboard would capitalise the first letter and the word would never match.\n        autoCapitalize=\"off\"\n        autoCorrect=\"off\"\n        autoComplete=\"off\"\n        spellCheck={false}\n        className=\"pointer-coarse:h-10\"\n        onChange={(event) => confirm?.setTyped(event.target.value)}\n        onKeyDown={(event) => {\n          onKeyDown?.(event)\n          if (event.key !== \"Enter\" || !matchesConfirmation(confirm?.typed ?? \"\", confirmWord)) return\n          event.preventDefault()\n          event.currentTarget.closest(\"[role='alertdialog']\")?.querySelector<HTMLButtonElement>(\"[data-slot='alert-dialog-action']\")?.click()\n        }}\n        {...props}\n      />\n    </div>\n  )\n}\n\nexport { AlertDialogConfirmField, ConfirmContext, ConfirmScope, RootContext, type AlertDialogConfirmFieldProps }\n",
      "type": "registry:ui"
    }
  ],
  "categories": [
    "overlays"
  ],
  "type": "registry:ui"
}