{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"dialog","type":"registry:block","title":"Dialog","description":"A settings danger zone whose confirmation dialog springs in, traps focus, and restores the workspace with an inline undo.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/prequel/dialog/dialog.tsx","type":"registry:component","content":"\"use client\";\n\nimport {\n  useCallback,\n  useEffect,\n  useId,\n  useRef,\n  useState,\n} from \"react\";\nimport {\n  AnimatePresence,\n  MotionConfig,\n  motion,\n  useInView,\n} from \"motion/react\";\nimport { Clock, X } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nconst EASE: [number, number, number, number] = [0.22, 1, 0.36, 1];\n\n/* ------------------------------------------------------------------ */\n/* types                                                               */\n/* ------------------------------------------------------------------ */\n\nexport type DialogProps = {\n  className?: string;\n  title?: string;\n  description?: string;\n  /** Members in the workspace, shown in the summary and the dialog. */\n  members?: number;\n  projects?: number;\n  apiKeys?: number;\n  /** Word the user has to type before deleting. Defaults to \"northwind\". */\n  confirmationWord?: string;\n};\n\n/* ------------------------------------------------------------------ */\n/* block                                                               */\n/* ------------------------------------------------------------------ */\n\nexport function Dialog({\n  className,\n  title = \"Danger zone\",\n  description = \"These actions affect everyone in Northwind Labs.\",\n  members = 8,\n  projects = 42,\n  apiKeys = 3,\n  confirmationWord = \"northwind\",\n}: DialogProps) {\n  const cardRef = useRef<HTMLDivElement | null>(null);\n  const triggerRef = useRef<HTMLButtonElement | null>(null);\n  const panelRef = useRef<HTMLDivElement | null>(null);\n  const inputRef = useRef<HTMLInputElement | null>(null);\n\n  const inView = useInView(cardRef, { once: true, margin: \"-10% 0px\" });\n  const uid = useId().replace(/:/g, \"\");\n  const titleId = `${uid}-dialog-title`;\n  const bodyId = `${uid}-dialog-body`;\n\n  const [open, setOpen] = useState(false);\n  const [confirm, setConfirm] = useState(\"\");\n  const [deleted, setDeleted] = useState(false);\n\n  const matches = confirm.trim().toLowerCase() === confirmationWord.toLowerCase();\n\n  const close = useCallback(() => {\n    setOpen(false);\n    triggerRef.current?.focus();\n  }, []);\n\n  useEffect(() => {\n    if (!open) return;\n\n    const previousOverflow = document.body.style.overflow;\n    document.body.style.overflow = \"hidden\";\n    const focusTimer = window.setTimeout(() => inputRef.current?.focus(), 80);\n\n    const onKeyDown = (event: KeyboardEvent) => {\n      if (event.key === \"Escape\") {\n        event.preventDefault();\n        close();\n        return;\n      }\n      if (event.key !== \"Tab\") return;\n\n      const panel = panelRef.current;\n      if (!panel) return;\n      const focusable = panel.querySelectorAll<HTMLElement>(\n        'button:not([disabled]), input:not([disabled]), [href], [tabindex]:not([tabindex=\"-1\"])'\n      );\n      if (focusable.length === 0) return;\n\n      const first = focusable[0];\n      const last = focusable[focusable.length - 1];\n      const active = document.activeElement;\n\n      if (!panel.contains(active)) {\n        event.preventDefault();\n        first.focus();\n      } else if (event.shiftKey && active === first) {\n        event.preventDefault();\n        last.focus();\n      } else if (!event.shiftKey && active === last) {\n        event.preventDefault();\n        first.focus();\n      }\n    };\n\n    document.addEventListener(\"keydown\", onKeyDown);\n    return () => {\n      document.body.style.overflow = previousOverflow;\n      window.clearTimeout(focusTimer);\n      document.removeEventListener(\"keydown\", onKeyDown);\n    };\n  }, [open, close]);\n\n  const openDialog = () => {\n    setConfirm(\"\");\n    setOpen(true);\n  };\n\n  const confirmDelete = () => {\n    if (!matches) return;\n    setDeleted(true);\n    setConfirm(\"\");\n    setOpen(false);\n  };\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <motion.div\n        ref={cardRef}\n        initial={{ opacity: 0, y: 16 }}\n        animate={inView ? { opacity: 1, y: 0 } : { opacity: 0, y: 16 }}\n        transition={{ duration: 0.6, ease: EASE }}\n        className={cn(\n          \"w-full rounded-xl border border-border bg-card p-5 text-card-foreground sm:p-6\",\n          className\n        )}\n      >\n        <div className=\"flex flex-wrap items-start justify-between gap-x-4 gap-y-2\">\n          <div className=\"min-w-0\">\n            <h2 className=\"text-[15px] font-semibold tracking-[-0.01em]\">{title}</h2>\n            <p className=\"mt-0.5 text-[12.5px] leading-relaxed text-muted-foreground\">\n              {description}\n            </p>\n          </div>\n          <span className=\"rounded-md border border-destructive/25 bg-destructive/10 px-2 py-1 text-[11px] font-medium text-destructive\">\n            Restricted\n          </span>\n        </div>\n\n        <dl className=\"mt-5 grid grid-cols-3 gap-px overflow-hidden rounded-lg border border-border bg-border\">\n          <div className=\"min-w-0 bg-card px-3 py-3\">\n            <dt className=\"truncate text-[11px] text-muted-foreground\">Members</dt>\n            <dd className=\"mt-0.5 text-[18px] font-semibold leading-none tracking-tight tabular-nums\">\n              {members}\n            </dd>\n          </div>\n          <div className=\"min-w-0 bg-card px-3 py-3\">\n            <dt className=\"truncate text-[11px] text-muted-foreground\">Projects</dt>\n            <dd className=\"mt-0.5 text-[18px] font-semibold leading-none tracking-tight tabular-nums\">\n              {projects}\n            </dd>\n          </div>\n          <div className=\"min-w-0 bg-card px-3 py-3\">\n            <dt className=\"truncate text-[11px] text-muted-foreground\">API keys</dt>\n            <dd className=\"mt-0.5 text-[18px] font-semibold leading-none tracking-tight tabular-nums\">\n              {apiKeys}\n            </dd>\n          </div>\n        </dl>\n\n        <div className=\"mt-5 flex flex-wrap items-start justify-between gap-3 border-t border-border pt-4\">\n          <div className=\"min-w-0 max-w-[34ch]\">\n            <p className=\"text-[13px] font-medium\">Delete workspace</p>\n            <p className=\"mt-0.5 text-[12px] leading-relaxed text-muted-foreground\">\n              Remove {projects} projects, {apiKeys} API keys and every member&apos;s access.\n            </p>\n          </div>\n          <button\n            ref={triggerRef}\n            type=\"button\"\n            aria-haspopup=\"dialog\"\n            aria-expanded={open}\n            disabled={deleted}\n            onClick={openDialog}\n            className=\"inline-flex flex-none items-center rounded-md border border-destructive/25 bg-destructive/10 px-2.5 py-1.5 text-[12px] font-medium text-destructive transition-colors hover:bg-destructive/15 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card disabled:cursor-not-allowed disabled:opacity-50\"\n          >\n            {deleted ? \"Deletion scheduled\" : \"Delete workspace\"}\n          </button>\n        </div>\n\n        <AnimatePresence>\n          {deleted ? (\n            <motion.div\n              initial={{ opacity: 0, height: 0 }}\n              animate={{ opacity: 1, height: \"auto\" }}\n              exit={{ opacity: 0, height: 0 }}\n              transition={{ duration: 0.3, ease: EASE }}\n              className=\"overflow-hidden\"\n            >\n              <div className=\"mt-3 flex flex-wrap items-center justify-between gap-3 rounded-md border border-border bg-muted/50 px-3 py-2 text-[11.5px]\">\n                <span className=\"inline-flex items-center gap-1.5 text-muted-foreground\">\n                  <Clock className=\"h-3.5 w-3.5\" aria-hidden=\"true\" />\n                  Deletion scheduled · restored for 24 hours\n                </span>\n                <button\n                  type=\"button\"\n                  onClick={() => setDeleted(false)}\n                  className=\"rounded font-medium text-foreground underline decoration-border underline-offset-2 transition-colors hover:decoration-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\"\n                >\n                  Undo\n                </button>\n              </div>\n            </motion.div>\n          ) : null}\n        </AnimatePresence>\n      </motion.div>\n\n      <AnimatePresence>\n        {open ? (\n          <motion.div\n            initial={{ opacity: 0 }}\n            animate={{ opacity: 1 }}\n            exit={{ opacity: 0 }}\n            transition={{ duration: 0.18, ease: EASE }}\n            onClick={(event) => {\n              if (event.target === event.currentTarget) close();\n            }}\n            className=\"fixed inset-0 z-50 flex items-end justify-center overflow-y-auto bg-background/85 p-4 sm:items-center sm:p-6\"\n          >\n            <motion.div\n              ref={panelRef}\n              role=\"dialog\"\n              aria-modal=\"true\"\n              aria-labelledby={titleId}\n              aria-describedby={bodyId}\n              tabIndex={-1}\n              initial={{ opacity: 0, scale: 0.96, y: 10 }}\n              animate={{ opacity: 1, scale: 1, y: 0 }}\n              exit={{ opacity: 0, scale: 0.97, y: 8 }}\n              transition={{ type: \"spring\", stiffness: 430, damping: 32, mass: 0.7 }}\n              className=\"relative w-[min(28rem,100%)] rounded-xl border border-border bg-card p-5 text-card-foreground shadow-2xl sm:p-6\"\n            >\n              <button\n                type=\"button\"\n                onClick={close}\n                aria-label=\"Close dialog\"\n                className=\"absolute right-3 top-3 grid h-7 w-7 place-items-center rounded-md text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\"\n              >\n                <X className=\"h-4 w-4\" aria-hidden=\"true\" />\n              </button>\n\n              <h2\n                id={titleId}\n                className=\"max-w-[24ch] text-[17px] font-semibold tracking-[-0.01em]\"\n              >\n                Delete Northwind Labs?\n              </h2>\n              <p\n                id={bodyId}\n                className=\"mt-2 max-w-[46ch] text-[12.5px] leading-relaxed text-muted-foreground\"\n              >\n                This removes {projects} projects, {apiKeys} API keys and every member&apos;s\n                access. Deleted workspaces can be restored for 24 hours from the audit log.\n              </p>\n\n              <label className=\"mt-5 block text-[12px] font-medium\" htmlFor={`${uid}-confirm`}>\n                Type{\" \"}\n                <span className=\"rounded bg-muted px-1 py-0.5 font-mono text-[11px]\">\n                  {confirmationWord}\n                </span>{\" \"}\n                to confirm\n              </label>\n              <input\n                ref={inputRef}\n                id={`${uid}-confirm`}\n                type=\"text\"\n                value={confirm}\n                onChange={(event) => setConfirm(event.target.value)}\n                onKeyDown={(event) => {\n                  if (event.key === \"Enter\") confirmDelete();\n                }}\n                placeholder={confirmationWord}\n                autoComplete=\"off\"\n                autoCapitalize=\"none\"\n                spellCheck={false}\n                className={cn(\n                  \"mt-1.5 h-9 w-full rounded-md border bg-background px-3 text-[13px] text-foreground outline-none transition-colors placeholder:text-muted-foreground/60 focus-visible:ring-2 focus-visible:ring-ring\",\n                  matches ? \"border-success/60\" : \"border-border\"\n                )}\n              />\n\n              <div className=\"mt-5 flex flex-wrap items-center justify-between gap-3\">\n                <span className=\"text-[11px] text-muted-foreground\">\n                  All {members} members will be notified.\n                </span>\n                <div className=\"flex items-center gap-2\">\n                  <button\n                    type=\"button\"\n                    onClick={close}\n                    className=\"rounded-md border border-border px-3 py-1.5 text-[12px] font-medium transition-colors hover:bg-muted focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card\"\n                  >\n                    Cancel\n                  </button>\n                  <button\n                    type=\"button\"\n                    disabled={!matches}\n                    onClick={confirmDelete}\n                    className=\"rounded-md bg-destructive px-3 py-1.5 text-[12px] font-medium text-background transition-colors hover:bg-destructive/90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card disabled:cursor-not-allowed disabled:opacity-40\"\n                  >\n                    Delete workspace\n                  </button>\n                </div>\n              </div>\n            </motion.div>\n          </motion.div>\n        ) : null}\n      </AnimatePresence>\n    </MotionConfig>\n  );\n}\n\nexport default Dialog;\n"}]}