{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "toast",
  "type": "registry:ui",
  "title": "Toast",
  "description": "Toasts from anywhere in the app: neutral, info and danger tones, an optional Undo action, polite and assertive live regions.",
  "dependencies": [
    "lucide-react@^1.49.0"
  ],
  "registryDependencies": [
    "https://ui.klarhimmel.com/r/theme.json",
    "https://ui.klarhimmel.com/r/utils.json"
  ],
  "files": [
    {
      "path": "src/ui/toast.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/ui/toast.tsx",
      "content": "import { useEffect, useId, useState, useSyncExternalStore } from 'react';\nimport { createPortal } from 'react-dom';\nimport { CircleAlert, Info, X } from 'lucide-react';\nimport { ThemeRootScope, useThemeRoot } from '../lib/theme-root';\nimport { cn } from '../lib/utils';\n\nexport type ToastTone = 'neutral' | 'info' | 'danger';\nexport interface ToastAction { label: string; onClick: () => void }\ninterface ToastItem { id: number; title: string; description?: string; tone: ToastTone; action?: ToastAction; duration: number }\nexport interface ToastOptions {\n  description?: string;\n  tone?: ToastTone;\n  /** One next step, like Undo. */\n  action?: ToastAction;\n  /** Milliseconds before it goes away (default 5000, or until dismissed when there is an action, so a keyboard user can reach it). 0 keeps it until dismissed. It never leaves while hovered or focused. */\n  duration?: number;\n}\n\nlet items: ToastItem[] = [];\nconst subs = new Set<() => void>();\nconst emit = () => subs.forEach((f) => f());\nlet n = 0;\n\n// Only one Toaster renders, so two mounted (a layout and a page) do not show every toast twice.\nconst owners: string[] = [];\nconst register = (id: string) => {\n  owners.push(id);\n  emit();\n  return () => {\n    owners.splice(owners.indexOf(id), 1);\n    emit();\n  };\n};\n\nexport function dismissToast(id: number) {\n  items = items.filter((t) => t.id !== id);\n  emit();\n}\nexport function clearToasts() {\n  items = [];\n  emit();\n}\n\n/**\n * Show a toast from anywhere (mount one <Toaster /> in the app first). At most three are kept; a toast of the same\n * tone replaces the previous one, unless it has an action. Returns the id for dismissToast.\n */\nexport function toast(title: string, opts: ToastOptions = {}): number {\n  const id = ++n;\n  const tone = opts.tone ?? 'neutral';\n  const keep = items.filter((t) => t.action || t.tone !== tone);\n  items = [...keep.slice(-2), { id, title, description: opts.description, tone, action: opts.action, duration: opts.duration ?? (opts.action ? 0 : 5000) }];\n  emit();\n  return id;\n}\n\nconst icons = { neutral: Info, info: Info, danger: CircleAlert };\nconst iconTone = { neutral: 'text-kh-muted', info: 'text-kh-accent-ink', danger: 'text-kh-danger' };\n\nfunction Toast({ t, dismissLabel }: { t: ToastItem; dismissLabel: string }) {\n  const [held, setHeld] = useState(false);\n  useEffect(() => {\n    if (held || !t.duration) return;\n    const timer = setTimeout(() => dismissToast(t.id), t.duration);\n    return () => clearTimeout(timer);\n  }, [held, t.id, t.duration]);\n  const Icon = icons[t.tone];\n  return (\n    <div\n      onMouseEnter={() => setHeld(true)}\n      onMouseLeave={() => setHeld(false)}\n      onFocus={() => setHeld(true)}\n      onBlur={() => setHeld(false)}\n      onKeyDown={(e) => e.key === 'Escape' && dismissToast(t.id)}\n      className=\"pointer-events-auto flex w-full max-w-sm items-start gap-3 rounded-kh-lg border border-kh-line bg-kh-surface p-3 pr-2 text-kh-ink shadow-kh-3\"\n    >\n      <Icon aria-hidden className={cn('mt-0.5 size-5 shrink-0', iconTone[t.tone])} />\n      <div className=\"min-w-0 flex-1 py-0.5\">\n        <p className=\"break-words text-kh-small font-medium\">{t.title}</p>\n        {t.description && <p className=\"break-words text-kh-small text-kh-muted\">{t.description}</p>}\n      </div>\n      {t.action && (\n        <button\n          type=\"button\"\n          onClick={() => {\n            t.action!.onClick();\n            dismissToast(t.id);\n          }}\n          className=\"relative my-auto shrink-0 rounded-kh-sm px-1 text-kh-small font-medium text-kh-accent-ink underline underline-offset-4 after:absolute after:-inset-2 after:content-[''] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-(--kh-focus)\"\n        >\n          {t.action.label}\n        </button>\n      )}\n      <button\n        type=\"button\"\n        aria-label={dismissLabel}\n        onClick={() => dismissToast(t.id)}\n        className=\"relative flex size-9 shrink-0 items-center justify-center rounded-kh text-kh-muted after:absolute after:-inset-1 after:content-[''] hover:bg-kh-surface-2 hover:text-kh-ink focus-visible:outline-2 focus-visible:outline-offset-0 focus-visible:outline-(--kh-focus)\"\n      >\n        <X aria-hidden className=\"size-4\" />\n      </button>\n    </div>\n  );\n}\n\nfunction Region({ label, dismissLabel }: { label: string; dismissLabel: string }) {\n  const id = useId();\n  const root = useThemeRoot();\n  useEffect(() => register(id), [id]);\n  const list = useSyncExternalStore(\n    (f) => (subs.add(f), () => subs.delete(f)),\n    () => items,\n    () => items,\n  );\n  const first = useSyncExternalStore(\n    (f) => (subs.add(f), () => subs.delete(f)),\n    () => owners[0] ?? '',\n    () => '',\n  );\n  if (first !== id) return null;\n  // The two live regions exist, empty, before any toast is put in them: that is what makes screen readers announce.\n  const view = (\n    <div role=\"region\" aria-label={label} className=\"pointer-events-none fixed inset-x-0 bottom-0 z-50 flex flex-col items-center gap-2 p-4 text-kh-body\">\n      <div role=\"status\" className=\"flex w-full flex-col items-center gap-2\">\n        {list.filter((t) => t.tone !== 'danger').map((t) => <Toast key={t.id} t={t} dismissLabel={dismissLabel} />)}\n      </div>\n      <div role=\"alert\" className=\"flex w-full flex-col items-center gap-2\">\n        {list.filter((t) => t.tone === 'danger').map((t) => <Toast key={t.id} t={t} dismissLabel={dismissLabel} />)}\n      </div>\n    </div>\n  );\n  return root ? createPortal(view, root) : view;\n}\n\n/**\n * Mount once near the root of the app (a second one renders nothing). Toasts stack at the bottom, in a labelled region\n * with a polite and an assertive live area. It portals into the theme root, so it keeps the tokens wherever it is mounted.\n */\n/** `dismissLabel`: each toast's close button name for screen readers, in the page's language ('Stäng'). */\nexport function Toaster({ label = 'Notifications', dismissLabel = 'Dismiss' }: { label?: string; dismissLabel?: string }) {\n  return (\n    <ThemeRootScope>\n      <Region label={label} dismissLabel={dismissLabel} />\n    </ThemeRootScope>\n  );\n}\n"
    }
  ]
}
