/** * A single toast, returned by {@link Toast#show}. */ export class ToastItem { /** * @param {Toast} region Owning region. * @param {HTMLElement} element Item node. * @param {number} timeout Auto-dismiss delay in ms; `0` disables it. * @param {boolean} dismissible Whether the item has a dismiss button. */ constructor(region: Toast, element: HTMLElement, timeout: number, dismissible: boolean); /** @type {Toast} */ _region: Toast; /** @type {HTMLElement} */ _element: HTMLElement; /** @type {number} */ timeout: number; /** @type {boolean} */ dismissible: boolean; /** @type {boolean} Whether the item is currently visible. */ _open: boolean; /** @type {boolean} Whether the item has already been dismissed or dropped. */ _done: boolean; /** @type {HTMLElement | null} Element focus came from before entering this toast (restored on dismiss). */ _returnTo: HTMLElement | null; /** @type {boolean} Whether the pointer is over the item. */ _hovered: boolean; /** @type {boolean} Whether focus is inside the item. */ _focused: boolean; /** @type {ReturnType|null} */ _timerId: ReturnType | null; /** @type {number} Milliseconds left before auto-dismiss. */ _remaining: number; /** @type {number} Timestamp the running timer started at. */ _startedAt: number; /** @type {ItemListener[]} */ _listeners: ItemListener[]; /** * The item node. It is only connected while the toast is visible. * * @returns {HTMLElement} The element. */ get element(): HTMLElement; /** * Whether the toast is visible. Queued toasts are not open yet. * * @returns {boolean} `true` while visible. */ get isOpen(): boolean; /** * Dismisses the toast. Queued toasts are simply dropped. * * @param {ToastReason} [reason] Why the toast is closing. * @returns {void} */ dismiss(reason?: ToastReason): void; } /** * Toast region component. * * @augments IvComponent */ export class Toast extends IvComponent { /** @type {Readonly} */ static defaults: Readonly; /** * Returns the toast region attached to `el`, if any. * * @param {Element} el Host element. * @returns {Toast|undefined} The instance or `undefined`. */ static get(el: Element): Toast | undefined; /** * Returns the existing instance or creates one. * * @param {Element} el Host element. * @param {Partial} [options] Options passed in JavaScript. * @returns {Toast} The instance. */ static getOrCreate(el: Element, options?: Partial): Toast; /** * Instantiates every `[data-iv-component="toast"]` inside `root`. * * @param {ParentNode} [root] Subtree to scan. * @returns {Toast[]} Newly created instances. */ static initAll(root?: ParentNode): Toast[]; /** * @param {Element} el The region element. * @param {Partial} [options] Options passed in JavaScript. */ constructor(el: Element, options?: Partial); /** * Resolved options. * * @returns {Readonly} The options. */ get options(): Readonly; /** * Visible toasts, oldest first. * * @returns {ToastItem[]} The visible items. */ get items(): ToastItem[]; /** @type {ToastItem[]} Visible items, oldest first. */ _items: ToastItem[]; /** @type {ToastItem[]} Items waiting for a free slot. */ _queue: ToastItem[]; /** @type {boolean} Whether this instance added role="region" to the element. */ _addedRole: boolean; /** @type {boolean} Whether this instance added aria-live to the region. */ _addedLive: boolean; /** * Class name for the resolved placement; an invalid value falls back to the * default corner. * * @returns {string} The placement class. */ get _placementClassName(): string; /** @type {boolean} Whether this instance added the placement class. */ _addedPlacementClass: boolean | undefined; /** @type {boolean} Whether this instance added the popover attribute. */ _addedPopover: boolean | undefined; /** @type {boolean} Whether the region is currently shown in the top layer. */ _layerOpen: boolean | undefined; /** * Shows a toast described by the `data-iv-*` attributes of a trigger element. * * Every value is text: it reaches the item through `textContent`, never as * markup. An unknown `data-iv-variant` falls back to `info` and a * non-numeric `data-iv-timeout` to the default of the region. * * @param {Element} trigger Element carrying the `data-iv-*` description. * @returns {ToastItem} The item, visible or queued. */ showFromTrigger(trigger: Element): ToastItem; /** * Moves the region to the top layer before the first visible item, so a * toast is reachable above a modal dialog. No-op without popover support. * * @returns {void} */ _showLayer(): void; /** * Leaves the top layer once nothing is visible or queued. * * @returns {void} */ _hideLayer(): void; /** * Maximum number of visible toasts, never below one. * * @returns {number} The limit. */ get _max(): number; /** * Shows a toast, or queues it when `max` toasts are already visible. * * @param {ToastShowOptions} opts Toast content and behaviour. * @returns {ToastItem} The item, visible or queued. */ show(opts: ToastShowOptions): ToastItem; /** * Builds the item node. Text is written with `textContent`, never as HTML. * * @param {{ message: string, title: string, variant: string, dismissible: boolean }} parts Content. * @returns {HTMLElement} The item node. */ _createNode({ message, title, variant, dismissible }: { message: string; title: string; variant: string; dismissible: boolean; }): HTMLElement; /** * Inserts an item and starts its timer. * * The node is inserted before `iv:open` is emitted so the event bubbles to * `document`; a cancelled `iv:open` removes it again in the same task, so * nothing is ever painted. * * @param {ToastItem} item Item to show. * @returns {void} */ _present(item: ToastItem): void; /** * Attaches the listeners of an item. They are removed when it is dismissed. * * @param {ToastItem} item Visible item. * @returns {void} */ _bindItem(item: ToastItem): void; /** * Removes the listeners of an item. * * @param {ToastItem} item Item to unbind. * @returns {void} */ _unbindItem(item: ToastItem): void; /** * Starts (or restarts) the auto-dismiss timer of an item. * * @param {ToastItem} item Visible item. * @returns {void} */ _startTimer(item: ToastItem): void; /** * Pauses the timer, keeping the time left. * * @param {ToastItem} item Visible item. * @returns {void} */ _pauseTimer(item: ToastItem): void; /** * Resumes a paused timer with the time left. * * @param {ToastItem} item Visible item. * @returns {void} */ _resumeTimer(item: ToastItem): void; /** * Dismisses a visible item or drops a queued one. * * `iv:closed` is emitted while the node is still connected, so the event * reaches listeners on `document`; the node is removed right after. * * @param {ToastItem} item Item to dismiss. * @param {ToastReason} reason Why the toast is closing. * @returns {void} */ _dismiss(item: ToastItem, reason: ToastReason): void; /** * Shows queued items while there is room for them. * * @returns {void} */ _flushQueue(): void; /** * Dismisses every visible toast and empties the queue. * * @returns {void} */ clear(): void; } export type ToastOptions = { /** * Corner the region is anchored to. */ placement: "bottom-end" | "bottom-start" | "top-end" | "top-start"; /** * Maximum number of items visible at the same time. */ max: number; /** * Accessible name of the dismiss button of every item. */ dismissText: string; }; export type ToastShowOptions = { /** * Message text. */ message: string; /** * Optional title text. */ title?: string | undefined; /** * Tone of the toast. Defaults to `"info"`. */ variant?: "info" | "success" | "warning" | "danger" | undefined; /** * Auto-dismiss delay in ms; `0` disables it. Defaults to 6000. */ timeout?: number | undefined; /** * Whether a dismiss button is rendered. Defaults to `true`. */ dismissible?: boolean | undefined; }; export type ToastReason = "api" | "timeout" | "trigger" | "escape"; /** * A listener attached to an item node. */ export type ItemListener = { /** * Event type. */ type: string; /** * Listener. */ handler: EventListener; }; import { IvComponent } from "../core/component.js";