Skip to content
TopClass UI

Toast

A short notice that something happened. It shows in a corner, leaves by itself, and always has an x, because a swipe is a drag.

client - size not measured yet

Toast

client - size not measured yet

A short notice that something happened. It shows in a corner, leaves by itself, and always has an x, because a swipe is a drag.

Install

Try it

tone
duration
priority

The code

toast.tsx
'use client'

import { Button } from 'topclassui/button'
import { toast } from 'topclassui/toast'
import type { ToastDesignProps } from 'topclassui/toast'

/** The docs stage: the Try it controls set the options of the toast the press adds. */
export default function Demo(props: ToastDesignProps = {}) {
  return (
    <Button
      variant="outline"
      onClick={() => { toast.add({ ...props, title: 'Project created', description: 'It is ready for its first deploy.' }) }}
    >
      Show a toast
    </Button>
  )
}

TopClassProvider mounts the one viewport, so there is nothing to mount; toast.add works from any browser module, inside React or not.

Every state

11 states

Import from the package

topclassui/toast
import { Toast } from 'topclassui/toast'

Install once:npm i topclassui@beta zod

Examples

  • toast is four plain functions: add, update, close and promise. The table lists the options of toast.add, not props; it also takes a required title, a description and one action.
  • duration is clamped to 0 to 600000 ms, and 0 stays until it is closed. Left out, the provider's applies: 5000 unless TopClassProvider sets another.
  • The action does not close the toast: call toast.close(id) in its handler, with the id add returned. toast.close() with no id closes them all.
  • toast.promise shows one toast: a spinner, then the success or the error icon and text. It returns the promise, rejection included, so catch it.
  • Placement, limit and duration are set once on the provider. The newest toast is the nearest the edge, and three show at once; the next appears when one closes.
with-description
basic
tones
with-action
promise
sticky
high-priority
many
over-dialog
long-content
In Arabic, right to left

Props

The props of Toast
PropTypeDefaultLimitsDescription
durationnumber, min 0, max 600000-min 0, max 600000toast.add: milliseconds before it closes itself, clamped; 0 stays until closed; left out, the provider duration applies.
prioritylow | high"low"-toast.add: high is announced at once, as an alert; low waits for the reader to finish.
toneprimary | neutral | success | warning | danger | info"neutral"-toast.add: what the toast means. success, warning, danger and info draw an icon; neutral and primary none.

Keyboard and accessibility

The keyboard of Toast
KeysWhat it does
F6Moves focus to the toasts.
TabGoes to the toast, its action, its x, then back where you were.
EscapeCloses the toast that holds focus. Closing the last one returns focus where F6 found it.
  • The viewport is a polite live region named by labels.toast.region. priority="high" is read at once, as an alert: keep it for what cannot wait.
  • Every toast ends with an x, and nothing removes it: a swipe is a drag, and a drag needs a second way (WCAG 2.5.7).
  • The timers pause while the pointer is over the toasts, while one holds focus, and while the window is in the background. Over a modal Dialog, Tab stays in the dialog and F6 reaches the toast.

Right to left

bottom-end, the default, is the bottom right in English and the bottom left in Arabic, and the sideways swipe follows the edge the toast sits at. An Arabic island on an English page shows its toasts in the page's corner, in the page's language.

Server and client

A client component: it renders on the server and hydrates in the browser.

Files: index.tsx, client.tsx.

On the chip: client - size not measured yet.