Todae

Todae

Toast

A short message about something that just happened, which leaves by itself. Toasts live in a store you make with createToaster(), so any code can show one, and appear in one Toast.Region near the root of your app, which takes the theme where it is rendered.

On this page
import {
  Button,
  Dialog,
  Stack,
  Toast,
  createToaster,
  type ToastLayout,
  type ToastTone,
} from '@tounsoo/todae';
import { useState } from 'react';

// Make the store once, at module scope, and share it across the app.
const toaster = createToaster<{ onUndo?: () => void }>();

const MESSAGES: Record<ToastTone, string> = {
  info: 'Draft synced',
  success: 'Changes saved',
  warning: 'You are offline',
  critical: 'Payment failed',
};

Toasts over a dialog

Toasts already showing moved in front of this dialog. New ones land there too.

toaster.add() returns the toast's id. Pass that id to update() to change the toast while it shows, such as from "Uploading" to "Uploaded" (give a toast that waits on work duration: Infinity), or to dismiss() to close it. Adding a toast with an id the toaster already has replaces that toast in place. Give createToaster<YourFields>() a type to put fields of your own on each toast and read them in the render function.

const id = toaster.add({ title: 'Uploading report.pdf', duration: Infinity });
await upload(file);
toaster.update(id, { tone: 'success', title: 'Uploaded report.pdf', duration: 5000 });

Todae ships no icons. Toast.Icon is a slot for one from your own set, as in Alert.

Toasts and modal dialogs

The toasts sit in a popover="manual" element that the region creates, so they are in the top layer above the page. A modal <dialog> is in the top layer too, and it makes everything outside it inert: a toast outside an open dialog can't be clicked or focused, and screen readers can't find it, even when it is drawn on top. Showing the popover again doesn't change that.

So while a Dialog or BottomSheet is open, the region moves inside it and shows itself again. Toasts shown before the dialog opened and toasts shown while it is open both sit in front of the dialog and its backdrop, and stay usable. When the dialog has closed, the region moves back. While a modal is open, the toasts sit inside it, so they take its theme. With nested dialogs, it follows the one in front.

The region knows about a modal from Todae's modal stack. Dialog and BottomSheet join it by themselves. For a modal <dialog> of your own, call useModalLayer. Toasts stay inert under a modal that is not in the stack.

Each new toast shows the region again, so it lands in front of a Popover or Menu opened since the last one. Clicking a toast closes an open popover that closes on outside clicks, as any click outside it does.

Layouts

layout="list" shows each toast in full. layout="stack" puts the toasts behind the newest one, peeking out by toast.peek, and spreads them out while the pointer is over them or keyboard focus is inside. A tap with a finger, or a pen that isn't hovering, spreads them until a press outside the region or until no toast is left. The newest toast sits nearest the edge placement names.

In a list in Chrome, a toast that leaves also gives back its height and the gap beside it over toast.motion.duration.exit, so the toasts behind it slide into place; other browsers, and reduced motion, move them once it is gone.

limit caps how many toasts show at once. Toasts added past it wait their turn and show as others leave.

The region sets these variables on each li for your own stack styles: --toast-index (0 for the newest), --toast-offset (the height of the toasts in front of it), and on the list --toast-front-height and --toast-count.

Timing

A toast dismisses itself after duration: 5000 milliseconds unless createToaster() or add() says otherwise. Infinity keeps it until it is dismissed. Every timer pauses while the pointer is over the region, while keyboard focus is inside it, while a finger or a pen that isn't hovering presses it, from a tap on it until a press outside, and while the page is hidden.

A toast can leave before someone reaches it, so it should not be the only way to do something. Offer Undo somewhere else too, or give a toast with an action duration: Infinity. Toast.Action dismisses its toast after onClick; call event.preventDefault() to keep it. An action that goes somewhere, such as View, is a link: pass render, as Button takes it. An action that opens a Dialog leaves it nothing to return focus to once the toast is gone, so pass the Dialog a restoreFocusRef.

Keyboard and screen readers

  • F8 moves focus to the region while a toast shows, and Tab goes on into the toasts. Change it with hotkey, such as hotkey="Alt+T", or turn it off with hotkey={null}.
  • Escape on a toast dismisses it, not the dialog behind it. Focus stays in the region while other toasts remain, and goes back where it came from once none is left.
  • The toasts are a list inside a live region that exists before the first toast, so screen readers read each new one politely, once. That includes critical toasts: a message that must interrupt belongs in an Alert.
  • The region is named "Notifications" and Toast.Close is named "Dismiss" unless its text names it. Both come from the notifications and dismiss strings of StringsProvider, English unless you set them; a non-empty aria-label still wins.

Icons

An svg or img directly inside Toast.Icon takes the toast.icon.size token. Toast.Close with no content shows the close glyph: a cross, unless a GlyphsProvider sets one. To use a different icon for one close, put it in a Button.Icon.

import { CircleCheck, X } from 'lucide-react';

<Toast.Root>
  <Toast.Icon>
    <CircleCheck />
  </Toast.Icon>
  <Toast.Title>{toast.title}</Toast.Title>
  <Toast.Close>
    <Button.Icon>
      <X />
    </Button.Icon>
  </Toast.Close>
</Toast.Root>

Accessible composition

Todae handles: The keyboard and screen reader model above. The region is a section named by the notifications string, and each toast is an li in an ol with aria-live="polite". Toast.Icon is aria-hidden, Toast.Close falls back to the dismiss string, and Toast.Action dismisses its toast after onClick. Timers pause while keyboard focus is inside the region. While a modal in Todae's modal stack is open, the region moves into it so the toasts stay reachable.

You supply: Text that stands on its own when read once: say what happened in Toast.Title, and put the tone in words when it matters, such as "Upload failed", since tone only sets data-tone for color and the icon is hidden. Action labels that say what they do, such as "Undo delete". A restoreFocusRef for a Dialog that an action opens.

Watch for: Do not add role="alert" or aria-live to Toast.Root: the region already reads each toast, and a second live region can read it twice. Do not carry the tone by color or icon alone. A modal <dialog> of your own needs useModalLayer, or the toasts are inert under it.

When to use

When: Confirming an action that already happened, such as saved, sent or copied, or a background event that needs no answer.

When not: A message that stays until it is fixed → Alert. An error on a field → ErrorMessage. A choice that blocks the task → Dialog. A toast holds text and buttons, with their tooltips, not a menu, popover or dialog of its own: Escape inside it dismisses the toast.

Props

Toast.Region props
NameTypeDescription
toaster RequiredToaster<T>

The store from createToaster().

children Required(toast: ToastRecord<T>) => React.ReactNode

Renders one toast, usually a Toast.Root.

placement"top-start"| "top-center"| "top-end"| "bottom-start"| "bottom-center"| "bottom-end"

Corner or edge of the viewport. The newest toast sits nearest the edge.

Default: "bottom-end"

layout"list" | "stack"

list shows every toast in full. stack collapses them behind the newest and spreads them while the pointer is over them or keyboard focus is inside, and from a tap with a finger or a pen that isn't hovering until a press outside or no toast is left.

Default: "list"

limitnumber

How many toasts show at once. Later ones wait their turn.

Default: 3

hotkeystring | null

Key that moves focus to the toasts, such as "F8" or "Alt+T". Modifiers are Alt (or Option), Ctrl, Meta (or Cmd) and Shift. null turns it off.

Default: "F8"

aria-labelstring

Name of the region landmark. Default is the notifications string from StringsProvider.

Default: "Notifications"

Toast.Root props
NameTypeDescription
tone"info"| "success"| "warning"| "critical"

Colors from color.status.<tone> through the toast contract tokens.

Default: the toast's own tone

childrenReact.ReactNode

Toast parts, or any content.

Toast.Icon props
NameTypeDescription
childrenReact.ReactNode

Icon graphic from your own icon set.

Toast.Title props
NameTypeDescription
childrenReact.ReactNode

Title text.

Toast.Description props
NameTypeDescription
childrenReact.ReactNode

Description content.

Toast.Action props
NameTypeDescription
variant"primary"| "secondary"| "ghost"| "critical"

Button variant.

Default: "secondary"

onClickReact.MouseEventHandler<HTMLButtonElement>

Runs the action. Call event.preventDefault() to keep the toast open.

render(props: ButtonRenderProps) => React.ReactElement

Draws the action on a link, such as View, as Button.Root's render does.

Toast.Close props
NameTypeDescription
aria-labelstring

Accessible name. Text of two or more letters or digits names the button by itself. Otherwise the dismiss string from StringsProvider names it, Dismiss in English.

Default: "Dismiss" when its text, not counting aria-hidden or SVG parts, has fewer than two letters or digits

onClickReact.MouseEventHandler<HTMLButtonElement>

Runs before the toast is dismissed. Call event.preventDefault() to keep it open.

createToaster props
NameTypeDescription
durationnumber

Default milliseconds before a toast dismisses itself. Infinity keeps toasts until dismissed.

Default: 5000