Toast
A short message that appears in a corner of the screen and goes away on its own, such as "Saved".
Setup
Render <Toaster /> once, near the top of your app. It shows the toasts from
the shared toaster store, which you can call from anywhere. For a different
layout or your own store, pass toaster and a render function built from the
Toast parts.
Usage
import { Toaster, toaster } from '@leverui/components';
<Toaster />
toaster.create({
title: 'Toast Title',
description: 'Toast Description',
});
Examples
Closable Toast
Set the closable prop to true to create a closable toast.
External Close
Use the toaster.dismiss method to close a toast.
toaster.dismiss(id): Dismiss a toast by its id.toaster.dismiss(): Dismiss all toasts.
Types
Here's an example of each type of toast.
With Action
Use the action prop with a label and onClick to add an action to the toast.
When the action trigger is clicked, the toast will be closed.
Persistent Toast
Set the type prop to "loading" to create a persistent toast.
Promise
Use the toaster.promise to create a toast that resolves when the promise is resolved.
Next, you can define the toast options (title, description, etc.) for each state of the promise.
Update
Use toaster.update(...) to modify a visible toast.
Custom Duration
Use the duration prop to set the duration of the toast.
Pause and Play
Use the pause and resume methods on the toaster object to pause and play the toast.
Pausing a toast prevents it from timing out, while resuming it will reenable the timeout using the remaining duration.
Lifecycle
Use the onStatusChange prop on the toaster function to listen for changes to the toast's status.
Maximum Visible Toasts
max in createToaster limits how many toasts show at once. Extra toasts wait
in a queue until another is dismissed.
const toaster = createToaster({
max: 3,
});
Placement
Any of the four corners of the page. Pick one position and set it in
createToaster.
const toaster = createToaster({
placement: 'top-end',
});
Overlapping Toasts
Toasts stack by default. Set overlap to true to overlap them.
const toaster = createToaster({
placement: 'top-end',
overlap: true,
});
Page Idle Behavior
pauseOnPageIdle: true pauses toast timers while the page is idle or hidden. A
timed toast keeps its remaining time.
const toaster = createToaster({
pauseOnPageIdle: true,
});
Offset
offsets moves the toasts away from the screen edges. Pass one value, or one
per edge.
const toaster = createToaster({
offsets: '20px',
});
const toaster = createToaster({
offsets: { left: '20px', top: '20px', right: '20px', bottom: '20px' },
});
Guides
Store operations
createToaster returns a toast store. create returns an id. Pass it to
update, dismiss, pause, or resume.
const id = toaster.create({ title: 'Uploading', type: 'loading' });
toaster.update(id, { title: 'Uploaded', type: 'success' });
toaster.dismiss(id);
Helper methods: success, error, info, warning, loading, promise. A
toast can include an action with a label and an onClick handler.
onStatusChange follows a toast's status.
Toast.Indicator shows the default icon for success, warning, and error. For
loading, show a Spinner in your render function. The toast region is labeled
Notifications by default and uses aria-live="polite".
Props
Toaster
| Prop | Default | Type | Description |
|---|---|---|---|
toaster | — | CreateToasterReturn | Store returned by createToaster. |
children | — | (toast: Accessor<ToastOptions>) => JSX | Render one toast from its store item. |
label | Notifications | string | Accessible name of the toast region. |
as | div | Native HTML tag | HTML tag to render. |
asChild | — | Solid render function | Render your own element with the toaster's props. |
unstyled | false | boolean | Turn off the Lever UI styles. |
Root
| Prop | Default | Type | Description |
|---|---|---|---|
as | div | Native HTML tag | HTML tag to render. |
asChild | — | Solid render function | Render your own element with the part's props. |
unstyled | false | boolean | Turn off the Lever UI styles. |
Title
| Prop | Default | Type | Description |
|---|---|---|---|
as | div | Native HTML tag | HTML tag to render. |
asChild | — | Solid render function | Render your own element with the part's props. |
Description
| Prop | Default | Type | Description |
|---|---|---|---|
as | div | Native HTML tag | HTML tag to render. |
asChild | — | Solid render function | Render your own element with the part's props. |
ActionTrigger
| Prop | Default | Type | Description |
|---|---|---|---|
as | button | Native HTML tag | HTML tag to render. |
asChild | — | Solid render function | Render your own element with the part's props. |
CloseTrigger
| Prop | Default | Type | Description |
|---|---|---|---|
as | button | Native HTML tag | HTML tag to render. |
asChild | — | Solid render function | Render your own element with the part's props. |
Customize with CSS
Override the .leverui-toast__* classes and the public --leverui-*
variables. --leverui-toast-trigger-bg and --leverui-toast-border-color
restyle the action. For one toast, use class and style. unstyled turns
off the default styles. Transitions stop under reduced motion.