Fragiola

API: components

Toast

Notifications on a toast queue outside every framework, announced through live regions, with an F6 landmark, pausing, a limit, promises and swipe.

Brief notifications that appear in a stack, announce themselves to assistive technology and close on their own after a timeout. The toasts live in a toast queue outside every framework (createToastManager, from the toolkit's @fragiola/primitives/toast): a plain module can create it, import it anywhere and add toasts with no component, and the Provider renders from it. The APG has no toast pattern: the announcements follow WAI-ARIA live regions, the keyboard follows Base UI (an F6 landmark, Escape on a toast).

Notification stack
Gallery

Behaviour

  • The queue: add, update, close, promise (a loading toast that becomes success or error), newest first. The newest limit toasts show; the older ones are queued (data-limited, inert) and show again as the newer ones close.
  • Timers: each toast closes timeout ms after it shows (its own, else the queue's; 0 never). A queued toast's timer waits until it shows; a loading toast has none.
  • Pausing: every timer pauses while the pointer is over the Viewport, while the keyboard is in it, while the page is hidden and while a toast is swiped; each keeps what it had left.
  • Announcements: two live regions inside the Viewport, polite and assertive, hold the title and description of each shown toast by its priority, appended in the order they show; an update (a promise's result) changes the text in place. The toasts themselves are not live, so nothing is announced twice.
  • Keyboard: F6 focuses the Viewport (a region landmark the app names), F6 or Shift+Tab on it go back; each toast is a tab stop; Escape closes the focused toast, and focus moves to the next one, else back where it was.
  • Swipe: a drag along the Root's swipeDirection (mouse, pen or touch) moves it (data-swiping, --toast-swipe-movement-x / -y); past half its size, or a flick, it closes.
  • Stacking: --toast-index, --toast-offset-y, --toast-height and the Viewport's --toast-frontmost-height let a stylesheet stack the toasts collapsed or expanded (data-expanded while hovered or focused).

Anatomy

partelementdata-primitives-partrole
Providernonegives its parts the queue
Portalnonemoves the Viewport to the end of body
Viewportdivtoast-viewportthe region landmark of the toasts, with the live regions (toast-announcer, toast-announcement) first inside it
Rootdivtoast-rootone toast
Titleh2toast-titleits name (default: the toast's title)
Descriptionptoast-descriptionits description (default: the toast's description)
Actionbuttontoast-actionan action the app wires
Closebuttontoast-closecloses the toast

Usage

import { createToastManager, Toast } from "@fragiola/primitives-react/toast";

// a plain module: import it anywhere and call notifications.add(...)
export const notifications = createToastManager();

function Toasts() {
    const { toasts } = Toast.useToastManager();
    return toasts.map((toast) => (
        <Toast.Root key={toast.id} toast={toast}>
            <Toast.Title />
            <Toast.Description />
            <Toast.Close>Dismiss</Toast.Close>
        </Toast.Root>
    ));
}

<Toast.Provider toastManager={notifications}>
    <App />
    <Toast.Portal>
        <Toast.Viewport aria-label="Notifications">
            <Toasts />
        </Toast.Viewport>
    </Toast.Portal>
</Toast.Provider>;

The queue. createToastManager({ limit, timeout }) returns a ToastManager: add(options) (returns the id; an open toast's id updates it), update(id, updates), close(id?), promise(promise, { loading, success, error }), toasts(), subscribe(listener). React's Toast.useToastManager(), Vue's useToastManager() and Angular's injectToastManager() give a component the Provider's queue, with its toasts as the framework's reactive value.

optiontypedefaultdescription
idstringgeneratedthe toast's id; adding an open one's id updates it
title, descriptionstringwhat it says, and what is announced
typestringfree-form (data-type); loading never closes by itself
timeoutnumberthe queue'sms before it closes; 0 never
priority"low" | "high""low"announced politely, or assertively (an alertdialog)
dataDatathe app's own data
onClose, onRemove() => voidonce it closes; once it left the list after its exit

Props

Every part also takes what the primitive contract gives: render (Vue: as and the render slot), a class and a style, a ref, and any attribute, which it forwards. The names below are React's; Vue and Angular bind them the same way.

Provider

proptypedefaultdescription
toastManagerToastManagera queue of its owna queue the app created, for instance in a plain module
limitnumber3how many toasts show at once (given to a toastManager only when set)
timeoutnumber5000the default ms before a toast closes (idem)

Portal

proptypedefaultdescription
container (react, vue)Elementdocument.bodywhere the Viewport goes
options (angular)Element | { container? }{}the container: *toastPortal="element"

Viewport

No props of its own; the app names it (aria-label).

Root

proptypedefaultdescription
toastToastObjectthe toast of the queue it renders
swipeDirection"up" | "down" | "left" | "right""right"the edge a swipe dismisses it towards

Title

proptypedefaultdescription
id (angular)stringgeneratedthe id the Root's aria-labelledby points at; React and Vue take it as a plain attribute

Description

proptypedefaultdescription
id (angular)stringgeneratedthe id the Root's aria-describedby points at; React and Vue take it as a plain attribute

Action

No props of its own.

Close

No props of its own.

State and part attributes

partattributes
Viewportrole="region", tabindex="-1", data-expanded, --toast-frontmost-height
live regionsaria-live="polite" / "assertive", aria-atomic="false", aria-relevant="additions text", data-priority; visually hidden
Rootrole="dialog" (alertdialog for a high priority), aria-modal="false", aria-labelledby, aria-describedby, tabindex="0", data-type, data-expanded, data-limited and inert, data-swiping, data-swipe-direction, data-starting-style, data-ending-style; --toast-index, --toast-offset-y, --toast-height, --toast-swipe-movement-x, --toast-swipe-movement-y
Title, Descriptionan id, data-type
Action, Closetype="button", data-type

Keyboard

keyaction
F6focuses the Viewport; F6 again (or Shift+Tab on it) goes back
Tabthrough the toasts and their buttons
Escapecloses the focused toast

Exports

entryexports
@fragiola/primitives-react/toastToast, createToastManager, useToastManager, UseToastManagerValue, ToastProviderProps, ToastPortalProps, ToastRootProps, ToastRootState, ToastViewportState, ToastManager, ToastManagerOptions, ToastObject, ToastOptions, ToastPriority, ToastPromiseOptions, ToastUpdate, SwipeDirection, OverlayPortalProps, RenderProp
@fragiola/primitives-vue/toastToast, createToastManager, useToastManager, UseToastManagerValue, ToastProviderProps, ToastPortalProps, ToastRootProps, ToastRootState, ToastViewportState, ToastManager, ToastManagerOptions, ToastObject, ToastOptions, ToastPriority, ToastPromiseOptions, ToastUpdate, SwipeDirection, RenderSlotProps
@fragiola/primitives-angular/toastToastProvider, ToastPortal, ToastViewport, ToastRoot, ToastTitle, ToastDescription, ToastAction, ToastClose, createToastManager, injectToastManager, InjectedToastManager, ToastRootState, ToastViewportState, ToastManager, ToastManagerOptions, ToastObject, ToastOptions, ToastPriority, ToastPromiseOptions, ToastUpdate, SwipeDirection, OverlayPortalOptions