{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "toaster",
  "type": "registry:ui",
  "title": "<ui-toaster>",
  "description": "The corner where toasts stack (Carbon has no toast manager; Carbon's toast guidance: top right, under the header). Usually created by `toast()`; place one yourself to change `placement` or `max`. 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": [
    "lit@^3.3.0"
  ],
  "registryDependencies": [
    "@kanok-ui/core-toaster",
    "@kanok-ui/notification",
    "@kanok-ui/shared-element",
    "@kanok-ui/shared-localize",
    "@kanok-ui/tokens"
  ],
  "files": [
    {
      "path": "src/primitives/toaster/toaster.ts",
      "type": "registry:ui",
      "target": "components/ui/primitives/toaster/toaster.ts",
      "content": "import { html, css } from 'lit';\nimport { customElement, property } from 'lit/decorators.js';\nimport { UiElement } from '../shared/element.ts';\nimport { LocalizeController } from '../shared/localize.ts';\nimport '../notification/notification.ts';\nimport type { NotificationKind, UiNotification } from '../notification/notification.ts';\nimport { toastTimeout, toastsOverMax, type ToasterPlacement } from '../../core/toaster.ts';\n\nexport type { ToasterPlacement } from '../../core/toaster.ts';\n\n/**\n * The corner where toasts stack (Carbon has no toast manager; Carbon's toast guidance: top right, under\n * the header). Usually created by `toast()`; place one yourself to change `placement` or `max`.\n *\n * - One polite live region (`role=\"region\"`, \"Notifications\") that exists before any toast, so every toast\n *   is announced; the toasts inside have no live role of their own (an actionable one keeps\n *   `alertdialog` and takes focus).\n * - In the top layer (`popover=\"manual\"`, shown from the start so the live region is in place before the\n *   first toast); while a `ui-modal` is open a new toast is drawn above it — but the page outside a modal\n *   dialog is inert, so that toast can't be clicked or read by screen readers: give feedback inside the\n *   modal with an inline `ui-notification` instead (Carbon guidance).\n * - Under a `<ui-header>` it starts 16px below the 48px header; `--ui-toaster-offset` overrides.\n * - At most `max` toasts: a new one closes the oldest. Closed toasts are removed.\n *\n * @slot - `<ui-notification variant=\"toast\">` elements (added by `toast()`)\n * @cssprop --ui-toaster-offset - Distance from the top (or bottom) edge (default 16px; 64px under a ui-header)\n */\n@customElement('ui-toaster')\nexport class UiToaster extends UiElement {\n    static override readonly styles = css`\n        :host {\n            position: fixed;\n            inset: auto;\n            inset-block-start: var(--ui-toaster-offset, 16px);\n            inset-inline-end: 16px;\n            z-index: 9000;\n            display: flex;\n            flex-direction: column-reverse;\n            gap: var(--ui-space-xs);\n            margin: 0;\n            padding: 0;\n            border: 0;\n            background: transparent;\n            overflow: visible;\n            pointer-events: none;\n        }\n\n        :host([data-below-header]) {\n            inset-block-start: var(--ui-toaster-offset, 64px);\n        }\n\n        :host([placement='bottom-end']) {\n            inset-block-start: auto;\n            inset-block-end: var(--ui-toaster-offset, 16px);\n            flex-direction: column;\n        }\n\n        :host(:not(:popover-open)) {\n            display: none;\n        }\n\n        ::slotted(*) {\n            pointer-events: auto;\n        }\n    `;\n\n    /** Corner: newest toast nearest the edge. */\n    @property({ reflect: true }) placement: ToasterPlacement = 'top-end';\n\n    /** Most toasts shown at once; a new one closes the oldest. */\n    @property({ type: Number }) max = 3;\n\n    private readonly localize = new LocalizeController(this);\n\n    constructor() {\n        super();\n        // on the host, not the slot: toasts can close before the first render\n        this.addEventListener('ui-close', this.onClose);\n    }\n\n    override connectedCallback() {\n        super.connectedCallback();\n        this.setAttribute('role', 'region');\n        this.setAttribute('aria-live', 'polite');\n        this.setAttribute('aria-relevant', 'additions');\n        if (!this.hasAttribute('popover')) this.setAttribute('popover', 'manual');\n        this.toggleAttribute('data-below-header', !!document.querySelector('ui-header, [data-ui-header]')); // also the React Header\n        this.lift(false);\n    }\n\n    /** Into the top layer; again (to the top) when asked — only while a modal is open, re-showing briefly hides the region. */\n    private lift(again: boolean) {\n        if (typeof this.showPopover !== 'function') return;\n        const open = this.matches(':popover-open');\n        if (open && !again) return;\n        if (open) this.hidePopover();\n        this.showPopover();\n    }\n\n    protected override updated() {\n        this.setAttribute('aria-label', this.localize.t.common.notifications);\n    }\n\n    /** Adds `toast`, closes the oldest beyond `max`, and lifts the region above anything opened since. */\n    add(toast: UiNotification) {\n        this.append(toast);\n        const open = [...this.children].filter((t) => (t as UiNotification).open !== false) as UiNotification[];\n        for (const old of open.slice(0, toastsOverMax(open.length, this.max))) old.close('timeout');\n        this.lift(modalOpenAround(this));\n    }\n\n    override render() {\n        return html`<slot></slot>`;\n    }\n\n    private readonly onClose = (event: Event) => {\n        if (event.target !== this) (event.target as Element).remove();\n    };\n}\n\n/** A modal is open — `ui-modal` or a native modal dialog (the React Modal) — on the page or in the toaster's own shadow root (a micro app). */\nfunction modalOpenAround(el: Element): boolean {\n    const scopes = new Set<Document | ShadowRoot>([document, el.getRootNode() as Document | ShadowRoot]);\n    return [...scopes].some((scope) => !!scope.querySelector?.('ui-modal[open], dialog:modal'));\n}\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    element: UiNotification;\n    close: () => void;\n}\n\n/** The page's `<ui-toaster>`, created at the end of `<body>` when there is none. */\nexport function getToaster(): UiToaster {\n    let toaster = document.querySelector('ui-toaster');\n    if (!toaster) {\n        toaster = document.createElement('ui-toaster');\n        document.body.append(toaster);\n    }\n    return toaster;\n}\n\n/**\n * Shows a toast notification: `toast({ kind: 'success', title: 'Saved', subtitle: 'Contract RQ-12' })`.\n * Returns the element and a `close()`. A micro app in a shadow root passes its own `<ui-toaster>`.\n */\nexport function toast(options: ToastOptions, toaster: UiToaster = getToaster()): ToastHandle {\n    if (options.id) toaster.querySelector(`[data-toast-id=\"${CSS.escape(options.id)}\"]`)?.remove();\n    const kind = options.kind ?? 'info';\n    const el = document.createElement('ui-notification');\n    el.variant = 'toast';\n    el.kind = kind;\n    el.heading = options.title;\n    el.subtitle = options.subtitle ?? '';\n    el.caption = options.caption ?? '';\n    el.lowContrast = !!options.lowContrast;\n    el.timeout = toastTimeout(kind, options.timeout);\n    if (options.id) el.dataset.toastId = options.id;\n    if (options.actionLabel) {\n        el.actionLabel = options.actionLabel;\n        if (options.onAction) el.addEventListener('ui-action', options.onAction);\n    } else {\n        el.role = 'none'; // the toaster's region announces it\n    }\n    toaster.add(el);\n    return { element: el, close: () => el.close() };\n}\n\ndeclare global {\n    interface HTMLElementTagNameMap {\n        'ui-toaster': UiToaster;\n    }\n}\n"
    }
  ]
}
