This documentation is under construction. Pages can be missing or change at any time. Follow it on GitHub.

Skip to content

Toast

Generates toast notifications.

This example sends events to the server: PHP runs in your browser.

Examples#

Anchored toasts#

Toasts can be anchored to a specific element: give the toast the anchor option with the id of the element. This is useful for showing contextual feedback like transient "Copied" toasts that appear near the button that triggered the action.

Anchored toasts should be rendered in a separate ToastViewport from stacked toasts: an anchored viewport with a name, and the viewport option of the toast. The anchored viewport places each toast on its side, with its sideOffset.

Custom position#

The position of the toasts is controlled by the position prop of ToastViewport: bottom-right (the default) or top-center. A toast at the top closes with a swipe up. The following shows a top-center position.

This example sends events to the server: PHP runs in your browser.

Undo action#

When adding a toast, the action option gives the label of an action button inside of it. A click on the button closes the toast and calls the onCall handler of the viewport, with the action, the id and the data of the toast in $e->detail: this enables the ability to undo an action associated with the toast.

This example sends events to the server: PHP runs in your browser.

Promise#

An asynchronous toast can be created with three possible states: loading, success, and error. The type string matches these states to change the styling.

Here, the button has data-phem-toast-show with the detail of a loading toast: it shows at the click, before the server answers. The handler answers after two seconds with a success or an error toast with the same id, which updates the loading toast.

This example sends events to the server: PHP runs in your browser.

Custom#

A toast with custom data can be created by passing an array to the data option. Each ToastData part of the template of the viewport shows the value of its name.

This example sends events to the server: PHP runs in your browser.

Deduplicated toast#

When you show the same toast by id, the toast on the screen updates, and its data-update-key attribute counts the updates. Its data-updated attribute comes again with each update, so a CSS animation on [data-updated] plays again while the same toast stays on the screen.

This example sends events to the server: PHP runs in your browser.

Varying heights#

Toasts with varying heights are stacked by clamping every toast's height to the frontmost toast at index 0 using the --toast-frontmost-height CSS variable, while the data-behind attribute hides the content of the toasts behind it. Avoid sizing ToastContent to the root's height (such as height: 100%), as resizing it alongside the root cancels the root's height transition.

This example sends events to the server: PHP runs in your browser.

API reference#

Import the components of the family:

use Phem\Ui\ToastViewport;
use function Phem\Ui\{ToastRoot, ToastContent, ToastTitle, ToastDescription, ToastAction, ToastClose, ToastArrow, ToastData};

ToastViewport#

The place of the toasts: Base UI Toast.Viewport, on the phem-toast controller. Put one in the layout of the app. A handler shows a toast with $reply->toast('Saved', ToastType::Success), and a toast with a title with $reply->toast('The file is ready.', title: 'Export complete').

Each toast is a copy of the template: a stack in the corner of the viewport that expands on hover, and closes with a swipe. The template has the parts of Base UI: ToastRoot, ToastContent (its text hides behind the last toast, until the stack expands), ToastTitle, ToastDescription, ToastAction (removed when the toast has no action) and ToastClose. Give your own parts as the children for another template.

position places the stack: at the bottom right (the default), or at the top center (a swipe up closes a toast). With anchored, each toast shows next to the element of its anchor, as Base UI Toast.Positioner, on the side with the sideOffset. Its default template has no arrow, as the Base UI demo: put a ToastArrow in your template to point to the anchor. A name takes only the toasts with this viewport: $reply->toast('Copied', anchor: 'copy', viewport: 'anchored').

PropTypeDefault
name?stringnull
position'bottom-right' | 'top-center'ToastPosition::BottomRight
anchoredboolfalse
side'top' | 'bottom' | 'left' | 'right' | 'inline-start' | 'inline-end'Side::Top
sideOffsetint0
limitint3
durationint5000
closeLabelstring'Dismiss'
children?Htmlnull
...attrsarray[]

ToastRoot#

A toast: Base UI Toast.Root. Its look follows the position and the anchored of the viewport.

PropTypeDefault
children?Htmlnull
...restmixed—

ToastContent#

The content of a toast: its text hides behind the newest toast, until the stack expands.

PropTypeDefault
children?Htmlnull
...restmixed—

ToastTitle#

The title of a toast: the controller removes it from a toast with no title.

PropTypeDefault
...restmixed—

ToastDescription#

The message of a toast.

PropTypeDefault
...restmixed—

ToastAction#

The action button of a toast, with the label of its action: a click on it calls the onCall handler of the viewport, and closes the toast. A toast with no action has no action button.

PropTypeDefault
...restmixed—

ToastClose#

The button that closes the toast.

PropTypeDefault
children?Htmlnull
...restmixed—

ToastArrow#

The arrow of an anchored toast: it points to the anchor.

PropTypeDefault
...restmixed—

ToastData#

The text of the data field name of the toast. With no value, the controller removes it.

PropTypeDefault
name (required)string—
...restmixed—

The source of the examples: examples/ui/Toast.phem.

Phem
Esc
↑↓ to navigate↵ to openEsc to close