Skip to the content
human-kit design
35 — Overlays

Toast

A short message about something that happened, in a corner, for a few seconds.

Installation

pnpm dlx @human-kit/design add toast
npx @human-kit/design add toast
yarn dlx @human-kit/design add toast
bunx @human-kit/design add toast

The command copies the files to src/lib/components/ui/toast. It also installs the packages that the files import.

Usage

<!-- src/routes/+layout.svelte -->
<script lang="ts">
	import * as Toast from '$lib/components/ui/toast';

	let { children } = $props();
</script>

<Toast.Provider>
	{@render children()}
</Toast.Provider>

<!-- Any page under it -->
<script lang="ts">
	import { useToastManager } from '$lib/components/ui/toast';

	const toasts = useToastManager();
</script>

<Button.Root onclick={() => toasts.add({ type: 'success', title: 'Changes saved' })}>Save</Button.Root>

Examples

A task that takes a while

manager.promise() shows a loading toast while the promise runs, and turns it into success or error when it settles. The success can read the value the promise gave.

Guidelines

  • Use a toast to confirm that something happened away from where the reader looks: saved, sent, archived. It goes by itself, so never put in it what the reader must read.
  • For an error the reader has to fix, show it next to what failed, in a Field.Root or an Alert.Root. A toast is for an error of the background, such as a request that will be tried again.
  • Give type for the mark: success, error, warning, info or loading. Without one the toast has no mark, for a plain notice.
  • Offer one action at most, through data.action, and only one that can wait: "Undo", "View". The toast closes after the press.
  • Keep the title to the fact, "Note archived", and the description to one line of detail.
  • Put one Toast.Provider around the app, in the root layout. Any page under it reaches the manager with useToastManager().

Accessibility

  • Each toast is announced in a live region: a polite role="status" by default, an assertive role="alert" with priority: "high". Use high only for what cannot wait.
  • The stack is a role="region" landmark. F6 moves the focus into it from anywhere on the page, and back.
  • Each toast is a non-modal role="dialog", named by its title and described by its text. Escape closes the focused one.
  • The timers stop while the pointer rests on the stack, while the focus is in it, and while the tab is hidden, so a toast does not go while it is read.
  • The close button is named by closeLabel. Translate it with the rest of the app.

API reference

Toast.Provider

PropTypeDefault
timeoutnumber

How long a toast stays, in milliseconds. 0 keeps each toast until it is closed.

5000
limitnumber

How many toasts show at once. The older ones wait behind, with their timers stopped.

3
managerToastManager

The manager of the list. Bind it to use it in the same component, or give one to share a list between two toasters.

—
closeLabelstring

The name of the close button, read aloud.

'Close'
childrenSnippet

The app. Anything under it reaches the manager with useToastManager().

—

It draws the stack in the bottom end corner of the screen, over the page.

ToastManager

PropTypeDefault
add(options: ToastOptions) => string

Shows a toast and returns its id. With the id of a toast on the screen, it updates that one.

—
update(id: string, changes) => void

Changes the fields of a toast that it names.

—
close(id?: string) => void

Closes one toast, or all of them with no id.

—
promise(promise, { loading, success, error }) => Promise

Shows a loading toast that turns into success or error with the promise. Each is a title, the options, or a function of the result.

—

What useToastManager() returns, or bind:manager gives. It also has toasts, the list, newest first.

ToastOptions

PropTypeDefault
titlestring

What happened. It is read first.

—
descriptionstring

One line of detail, under the title.

—
type'success' | 'error' | 'warning' | 'info' | 'loading'

The mark beside the words. A loading toast stays until an update, since its timer cannot know when the task ends.

—
priority'low' | 'high'

high interrupts the screen reader, and makes the toast an alertdialog.

'low'
timeoutnumber

How long this toast stays, over the toaster timeout. 0 keeps it.

—
data{ action?: { label: string; onClick: () => void } }

One button that acts on the message, under the text.

—
idstring

An id of your own, to update the toast later.

—
onClose() => void

Called when the toast starts to close.

—

What add() takes.