{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tw-toaster",
  "type": "registry:ui",
  "title": "react-tw/toaster.tsx",
  "description": "Shows a toast in the `<Toaster>`: `toast({ kind: 'success', title: 'Saved', subtitle: 'Contract RQ-12' })`. Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [
    "@purpurabog/ui@^2.3.0"
  ],
  "registryDependencies": [
    "@kanok-ui/core-notification",
    "@kanok-ui/core-toaster",
    "@kanok-ui/tw-i18n",
    "@kanok-ui/tw-notification",
    "@kanok-ui/tw-refs",
    "@kanok-ui/tw-theme",
    "@kanok-ui/tw-utils"
  ],
  "files": [
    {
      "path": "src/react-tw/toaster.tsx",
      "type": "registry:ui",
      "target": "components/ui/react-tw/toaster.tsx",
      "content": "import { useLayoutEffect, useRef, useState, type CSSProperties, type ComponentProps } from 'react';\nimport type { NotificationKind } from '../core/notification.ts';\nimport { ToastQueue, toastTimeout, type ToasterPlacement } from '../core/toaster.ts';\nimport { Toaster as ToasterPrimitive } from '@purpurabog/ui/headless';\nimport { useTranslation } from './lib/i18n.ts';\nimport { cn } from './lib/utils.ts';\nimport { Notification } from './notification.tsx';\nimport { useMergedRefs } from './lib/refs.ts';\n\nexport type { ToasterPlacement } from '../core/toaster.ts';\n\nexport interface ToastOptions {\n    /** Carbon notification kind (default `info`). */\n    kind?: NotificationKind;\n    title: string;\n    subtitle?: string;\n    caption?: string;\n    /**\n     * Auto-close after this many ms (paused while hovered or focused). Default: 5000 for info / success,\n     * 0 (stays until closed) for error / warning — those need reading.\n     */\n    timeout?: number;\n    /** Adds an action button (Carbon actionable toast; it never closes on its own). */\n    actionLabel?: string;\n    onAction?: () => void;\n    lowContrast?: boolean;\n    /** A toast with the same id that is still open is replaced instead of stacking a duplicate. */\n    id?: string;\n}\n\nexport interface ToastHandle {\n    /** Closes the toast (nothing happens when it is already gone). */\n    close: () => void;\n}\n\n/** The page's toasts; the mounted `<Toaster>` shows them. */\nconst queue = new ToastQueue<ToastOptions>();\n\n/**\n * Shows a toast in the `<Toaster>`: `toast({ kind: 'success', title: 'Saved', subtitle: 'Contract RQ-12' })`.\n * Returns a handle to close it.\n */\nexport function toast(options: ToastOptions): ToastHandle {\n    const key = queue.add(options, options.id);\n    return { close: () => queue.remove(key) };\n}\n\n/** Removes every toast (e.g. on sign-out). */\ntoast.clear = () => queue.clear();\n\nexport interface ToasterProps extends Omit<ComponentProps<'section'>, 'placement' | 'max' | 'offset' | 'lang' | 'className'> {\n    /** Corner: newest toast nearest the edge. */\n    placement?: ToasterPlacement;\n    /** Most toasts shown at once; a new one closes the oldest. */\n    max?: number;\n    /** Distance from the top (or bottom) edge (default 16px; 64px under a `<ui-header>` or `<Header>`); sets `--ui-toaster-offset`. */\n    offset?: string;\n    lang?: string;\n    className?: string;\n}\n\n/**\n * The corner where toasts stack, as `<ui-toaster>`; mount one per app (`toast()` fills it). One polite\n * \"Notifications\" region that exists before any toast, so every toast is announced (the toasts have no\n * live role of their own; an actionable one keeps `alertdialog` and takes focus). In the top layer from\n * the start; a toast added while a modal is open lifts the region above the modal — but outside a modal\n * the page is inert, so give feedback inside a modal with an inline Notification (Carbon guidance).\n */\nexport function Toaster({ placement = 'top-end', max = 3, offset, lang, className, ref, ...rest }: Readonly<ToasterProps>) {\n    const t = useTranslation(lang).common;\n    useLayoutEffect(() => {\n        queue.max = max;\n    }, [max]);\n    const toasts = ToasterPrimitive.useToasts(queue);\n    const region = useRef<HTMLElement>(null);\n    const regionRef = useMergedRefs(region, ref);\n\n    // under a header (ui-header or the React Header, on the page or in the toaster's own shadow root) it starts below it\n    const [belowHeader, setBelowHeader] = useState(false);\n    useLayoutEffect(() => {\n        const el = region.current;\n        if (!el) return;\n        const scopes = new Set<Document | ShadowRoot>([document, el.getRootNode() as Document | ShadowRoot]);\n        setBelowHeader([...scopes].some((scope) => !!scope.querySelector?.('ui-header, [data-ui-header]')));\n    }, []);\n\n    return (\n        <ToasterPrimitive.Root {...rest}\n            ref={regionRef}\n            queue={queue}\n            aria-label={t.notifications}\n            style={{ ...rest.style, ...(offset ? ({ '--ui-toaster-offset': offset } as CSSProperties) : undefined) }}\n            className={cn(\n                'pointer-events-none fixed inset-auto end-4 z-[9000] m-0 flex gap-2 overflow-visible border-0 bg-transparent p-0 not-open:hidden',\n                'top-[var(--ui-toaster-offset,16px)] flex-col-reverse',\n                belowHeader && 'top-[var(--ui-toaster-offset,64px)]',\n                placement === 'bottom-end' && 'top-auto bottom-[var(--ui-toaster-offset,16px)] flex-col',\n                className,\n            )}\n        >\n            {toasts.map(({ key, data }) => (\n                <Notification\n                    key={key}\n                    variant=\"toast\"\n                    kind={data.kind ?? 'info'}\n                    title={data.title}\n                    subtitle={data.subtitle}\n                    caption={data.caption}\n                    lowContrast={data.lowContrast}\n                    timeout={toastTimeout(data.kind ?? 'info', data.timeout)}\n                    actionLabel={data.actionLabel}\n                    onAction={data.onAction}\n                    role={data.actionLabel ? undefined : 'none'}\n                    onClose={() => queue.remove(key)}\n                    lang={lang}\n                    className=\"pointer-events-auto\"\n                />\n            ))}\n        </ToasterPrimitive.Root>\n    );\n}\n"
    }
  ]
}
