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

Code
import { Toaster, toaster } from '@leverui/components';
Code
<Toaster />
Code
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.

Code
const toaster = createToaster({
  max: 3,
});

Placement

Any of the four corners of the page. Pick one position and set it in createToaster.

Code
const toaster = createToaster({
  placement: 'top-end',
});

Overlapping Toasts

Toasts stack by default. Set overlap to true to overlap them.

Code
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.

Code
const toaster = createToaster({
  pauseOnPageIdle: true,
});

Offset

offsets moves the toasts away from the screen edges. Pass one value, or one per edge.

Code
const toaster = createToaster({
  offsets: '20px',
});
Code
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.

Code
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

PropDefaultTypeDescription
toaster—CreateToasterReturnStore returned by createToaster.
children—(toast: Accessor<ToastOptions>) => JSXRender one toast from its store item.
labelNotificationsstringAccessible name of the toast region.
asdivNative HTML tagHTML tag to render.
asChild—Solid render functionRender your own element with the toaster's props.
unstyledfalsebooleanTurn off the Lever UI styles.

Root

PropDefaultTypeDescription
asdivNative HTML tagHTML tag to render.
asChild—Solid render functionRender your own element with the part's props.
unstyledfalsebooleanTurn off the Lever UI styles.

Title

PropDefaultTypeDescription
asdivNative HTML tagHTML tag to render.
asChild—Solid render functionRender your own element with the part's props.

Description

PropDefaultTypeDescription
asdivNative HTML tagHTML tag to render.
asChild—Solid render functionRender your own element with the part's props.

ActionTrigger

PropDefaultTypeDescription
asbuttonNative HTML tagHTML tag to render.
asChild—Solid render functionRender your own element with the part's props.

CloseTrigger

PropDefaultTypeDescription
asbuttonNative HTML tagHTML tag to render.
asChild—Solid render functionRender 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.