Toast

A short message that appears for a few seconds and does not block the page.

Import

import { ToastProvider, useToast, createToaster } from '@matix/components/toast';

Usage

Render ToastProvider once, near the root of your app. Call useToast() in any component inside it.

The newest toast is at the front. Older toasts stack behind it. If you move the pointer over the stack or focus a toast, the stack expands and the timers stop.

Variants

variant adds an icon in the semantic color. The toast surface stays the same.

Action

action adds one button. The toast closes after the button runs onClick. To keep the toast open, call event.preventDefault() in onClick.

Promise

promise() shows a loading toast. When the promise settles, the same toast changes to a success or error toast. promise() returns the promise you pass in.

Update and dismiss

toast() returns an id. Use the id to change or close the toast. If update() sets a new duration, the close timer starts again.

const { toast, update, dismiss } = useToast();

const id = toast({ title: 'Uploading…', variant: 'loading', duration: Infinity });
update(id, { title: 'Upload complete', variant: 'success', duration: 5000 });
dismiss(id);

Duration

A toast closes after 5 seconds. Set duration on ToastProvider to change the default for all toasts. Set duration on one toast to override it. Infinity keeps the toast open until the user closes it. If a toast has an action, such as "Undo", give it a longer duration or Infinity. Keyboard and screen-reader users need more time to reach the action.

<ToastProvider duration={8000}>…</ToastProvider>;

toast({ title: 'Connection lost', variant: 'error', duration: Infinity });

Positions

Outside React

createToaster() returns the same methods as useToast(). Use it in code that is not a component, for example a fetch helper. Pass it to ToastProvider.

// toaster.ts
export const toaster = createToaster();

// app.tsx
<ToastProvider toaster={toaster}>
  <App />
</ToastProvider>;

// api.ts
toaster.toast({ title: 'Session expired', variant: 'warning' });

API Reference

ToastProvider

PropTypeDefaultDescription
positionToastPosition'bottom-end'top-start, top-center, top-end, or the same on bottom. start and end flip in right-to-left pages. left and right stay physical
maxToastsnumber3Toasts in the stack. A new toast closes the oldest one.
durationnumber5000Default time in ms before a toast closes
toasterToaster—A toaster from createToaster()
labels{ region?: string; close?: string }—Screen-reader text of the region and the close button, for translation
icons{ info?, success?, warning?, error?, loading?, close? }—Replaces the built-in icons. See the Icons guide
childrenReactNode—App content

useToast and createToaster

Both return a Toaster:

MethodDescription
toast(options)Opens a toast and returns its id
update(id, options)Changes an open toast
dismiss(id)Closes a toast
promise(promise, states)Shows loading, then success or error from the promise

ToastOptions

PropTypeDefaultDescription
titleReactNode—The main message
descriptionReactNode—More detail, in muted text
variant'default' | 'info' | 'success' | 'warning' | 'error' | 'loading''default'Sets the icon
durationnumber5000Time in ms before the toast closes
action{ label: ReactNode; onClick: (event) => void }—One button, for example "Undo"
priority'low' | 'high''low'high interrupts the screen reader
idstring—Reuse an id to replace a toast that is open
onClose() => void—Runs when the toast starts to close

Accessibility

  • The toasts sit in a region named "Notifications". Screen readers announce the title and description of each new toast. With priority: 'high', the announcement interrupts the screen reader.
  • A new toast does not move focus. Press F6 to move focus to the toasts. Press F6 again to go back.
  • The timers stop while the pointer is on the stack or focus is in a toast.
  • Press Esc to close the focused toast. You can also swipe a toast toward the nearest edge, or use the close button.
  • If the user sets prefers-reduced-motion, toasts fade in and out and do not slide.