Skip to main content

Toast

Preview
View as Markdown

Short messages at the edge of the screen that close on their own.

The toaster is the region the toasts appear in and holds their settings. Its toastTemplate is the template of one toast: the client clones it for every toast and fills in the title, the description and the label of the action trigger. A part keeps what the template puts into it while the toast has no text for it. An indicator shows markup for one type, and a toastTemplate with a type is the template of one type.

A toast template holds plain markup. A component that hydrates, such as ui:button, or a ui:portal inside it is cloned without its script, so it doesn't work in a toast.

Read more about installing Components and Primitives.

Render the toaster once in your page layout, with the rootId its entry file mounts:

Without content, it renders the toast template you see in the demo above, with an icon for four types. Put a ui:toaster.toastTemplate inside to use one of your own. That replaces the default template, so one of yours has no type, for the toasts of every type without a template of its own. A toast template has exactly one root element, a ui:toast.root. The entry file exports the store of the toaster as toast. Import it wherever you create a toast:

toast.create() returns the id of the toast, for toast.update() and toast.dismiss(). A toast takes a title, a description, a type, a duration, an action, closable and a priority from 1 (highest) to 8 for the queue. The store also has remove() (without the exit animation), pause() and resume(), promise(), and success(), info(), warning(), error() and loading() as shortcuts for the type.

Each button below shows a toast of another type. The toast renders it as data-type and shows the toast.indicator of its type, the others are hidden. The toaster of the first demo has an indicator with an icon for four types, an info toast shows none. When the type changes, as in a promise, the indicators change with it. Only these five types can have an indicator and a template of their own. An icon in an indicator is decoration, so give it aria-hidden="true". For screen readers, name the type in the title, or put visually hidden text such as "Error:" into the indicator. A success toast stays for 2 seconds, the others for 5 seconds. Set duration on the toast or on the toaster to change that.

Pass an action with a label and an onClick. Click "Undo" in the toast: the action runs, and the toast closes.

toast.promise() shows a loading toast until the promise settles, then turns it into the success or the error toast.

Give the toast an id, and update it while toast.isVisible() says it is still there. Click "Save draft" a few times: one toast counts the saves and shakes on every click. Screen readers announce the new description.

The toasts overlap by default and spread out while you hover or focus them. Set overlap to false to list them one below the other. This demo has a toaster of its own, with another placement and its own mount(). Give every toaster of a page its own placement: two toasters with the same one share the id of their region, and Alt+T focuses only one of them.

A toastTemplate with a type is the template of the toasts of that type, the one without a type the template of all others. When a toast changes to a type with another template, as when a promise fails, it stays in its place and takes the content of the new template:

The title of this template says "Something went wrong" while the toast has no title of its own.

Pass the data of a toast as meta, and mark the element in the template with ui:ref. A subclass of Toast fills it, and a subclass of Toaster creates that toast. This demo has a toaster of its own for the cart, and each button passes the image of its product:

Give such an element a fixed size, an image a width and a height: the toaster measures a toast when its content changes, not when an image loads.

A type change can swap every part of a toast except its root for the ones of another template. Query the parts in render(), as the cart toast does, and don't keep them or add listeners to them in init(). To animate the swap, override replaceContent() and call super.replaceContent() inside.

The client places every toast through CSS variables on its root, so your CSS has to use them: --x and --y for translate, --scale for scale, and --z-index, --height and --opacity. Transition them, and the toasts slide in, pile up and spread out. A closing toast has data-state="closed", and a toast behind the front one has data-sibling:

Write a component of your own that renders the flash messages as JSON and marks the <script> with ui:ref. f:page.footerData puts it at the end of the page, apart from the markup of your plugin:

Components/FlashMessages/FlashMessages.html:

Use it where your plugin template has f:flashMessages. It reads the same queue, the one of the plugin by default:

f:format.json escapes < and >, so a message cannot end the <script> element. The entry file finds the <script> by its ref and shows the messages with the toaster. The keys of types are the values of ContextualFeedbackSeverity:

Components/FlashMessages/FlashMessages.entry.ts:

An action you call with extbase.post() answers with JSON, so nothing renders its flash messages. Send them along in the response and flush the queue, otherwise they show up on the next page load:

The following tables cover the available props of the Fluid Primitives. For a full list of available client side props and methods, see the Zag.js Machine API.

The region the toasts appear in, with their settings. Renders a <div> element.

NameDescriptionRequiredDefault
placementEnum\ToastPlacement
Where on the screen the toasts appear.
No
BottomEnd
overlapboolean
Whether the toasts pile up on top of each other and spread out on hover or focus. Set it to `false` to list them one below the other.
No
true
maxinteger
How many toasts are visible at once. More toasts wait in a queue, the most important first. When they overlap, each toast behind the front one is 5% smaller than the one before, so far more than 10 turn the pile into an hourglass.
No
10
gapinteger
The space between the toasts, in pixels.
No
16
offsetsstring|array
The distance from the edges of the viewport, a CSS length or an array with one for each of `top`, `right`, `bottom` and `left`.
No
'1rem'
durationinteger
How long a toast stays, in milliseconds. Without it, the type of the toast decides: 2 seconds for `success`, 5 seconds for the others, and a `loading` toast stays until it is updated.
No
-
removeDelayinteger
How long a dismissed toast stays in the DOM, in milliseconds, so its exit animation can play.
No
200
pauseOnPageIdleboolean
Whether the toasts wait while the browser tab is in the background.
No
true
hotkeystring[]
The keys that move the focus to the toasts, as `KeyboardEvent` modifier properties and codes, `altKey` and `KeyT` (Alt+T) by default.
No
-
translationsarray
Localized labels: `groupLabel` names the region of the toasts for screen readers, `closeTriggerLabel` the close trigger of each toast. Use `f:translate` for per-template localization overrides when needed.
No
-
attributesarray
Additional attributes that should be rendered on the component where ui:attributes is used.
No
[]
rootIdstring
The root ID of the component, used for hydration and identification.
No
-
idsarray
The IDs of of the component parts for composition.
No
[]
autoMountboolean
Whether the client initializes the component on its own. Set it to false to mount it yourself with `mount()`
No
true
providerstring
The `id` of the `.provider` this root takes its `rootId` and props from.
No
-
classstring
The CSS class(es) to be applied to the component.
No
-

The template of one toast, with a toast.root as its root element, or of the toasts of one type. Renders a <template> element.

NameDescriptionRequiredDefault
typeEnum\ToastType
The type of toast this template is for. A toast takes the template of its type, or the one without a type.
No
-

The following tables cover the available props of the Fluid Primitives. For a full list of available client side props and methods, see the Zag.js Machine API.

One toast, the root element of the toast template. Renders a <div> element.

NameDescriptionRequiredDefault
attributesarray
Additional attributes that should be rendered on the component where ui:attributes is used.
No
[]
asChildboolean
If true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props.
No
-
rootIdstring
The root ID of the component, used for hydration and identification.
No
-
idsarray
The IDs of of the component parts for composition.
No
[]
autoMountboolean
Whether the client initializes the component on its own. Set it to false to mount it yourself with `mount()`
No
true
providerstring
The `id` of the `.provider` this root takes its `rootId` and props from.
No
-
classstring
The CSS class(es) to be applied to the component.
No
-

Rendered data attributes

AttributeDescription
data-toast-root<uid>
data-state"open" | "closed"
data-typeThe type of the item
data-placementThe placement of the toast
data-align
data-sideThe side of the trigger that the toast is positioned on
data-mountedPresent when mounted
data-pausedPresent when paused
data-first
data-sibling
data-stack
data-overlapPresent when overlapping

Markup for one type of toast, such as its icon, hidden while the toast has another type. Renders a <div> element.

NameDescriptionRequiredDefault
typeEnum\ToastType
The type of toast the indicator is shown for.
Yes
-
attributesarray
Additional attributes that should be rendered on the component where ui:attributes is used.
No
[]
asChildboolean
If true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props.
No
-
classstring
The CSS class(es) to be applied to the component.
No
-

The title of the toast, or what the template puts into it while the toast has none. Hidden when both are empty. Renders a <div> element.

NameDescriptionRequiredDefault
attributesarray
Additional attributes that should be rendered on the component where ui:attributes is used.
No
[]
asChildboolean
If true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props.
No
-
classstring
The CSS class(es) to be applied to the component.
No
-

The description of the toast, or what the template puts into it while the toast has none. Hidden when both are empty. Renders a <div> element.

NameDescriptionRequiredDefault
attributesarray
Additional attributes that should be rendered on the component where ui:attributes is used.
No
[]
asChildboolean
If true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props.
No
-
classstring
The CSS class(es) to be applied to the component.
No
-

Runs the action of the toast and closes it, hidden when the toast has no action. Renders a <button> element.

NameDescriptionRequiredDefault
attributesarray
Additional attributes that should be rendered on the component where ui:attributes is used.
No
[]
asChildboolean
If true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props.
No
-
classstring
The CSS class(es) to be applied to the component.
No
-

Closes the toast, hidden when it is not closable. Renders a <button> element.

NameDescriptionRequiredDefault
attributesarray
Additional attributes that should be rendered on the component where ui:attributes is used.
No
[]
asChildboolean
If true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props.
No
-
classstring
The CSS class(es) to be applied to the component.
No
-
NameTypeDescription
getCount() => numberThe total number of toasts
getToasts() => ToastProps[]The toasts
subscribe(callback: (toasts: Options<O>[]) => void) => VoidFunctionSubscribe to the toast group
getGroupState() => GroupStateReturns the state of the group