Toast
A short message about something that happened, in a corner, for a few seconds.
Installation
pnpm dlx @human-kit/design add toastnpx @human-kit/design add toastyarn dlx @human-kit/design add toastbunx @human-kit/design add toastThe 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.Rootor anAlert.Root. A toast is for an error of the background, such as a request that will be tried again. - Give
typefor the mark:success,error,warning,infoorloading. 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.Provideraround the app, in the root layout. Any page under it reaches the manager withuseToastManager().
Accessibility
- Each toast is announced in a live region: a polite
role="status"by default, an assertiverole="alert"withpriority: "high". Usehighonly for what cannot wait. - The stack is a
role="region"landmark.F6moves 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.Escapecloses 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
| Prop | Type | Default |
|---|---|---|
timeout | number How long a toast stays, in milliseconds. | 5000 |
limit | number How many toasts show at once. The older ones wait behind, with their timers stopped. | 3 |
manager | ToastManager The manager of the list. Bind it to use it in the same component, or give one to share a list between two toasters. | — |
closeLabel | string The name of the close button, read aloud. | 'Close' |
children | Snippet The app. Anything under it reaches the manager with | — |
It draws the stack in the bottom end corner of the screen, over the page.
ToastManager
| Prop | Type | Default |
|---|---|---|
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 | — |
What useToastManager() returns, or bind:manager gives. It also has toasts, the list, newest first.
ToastOptions
| Prop | Type | Default |
|---|---|---|
title | string What happened. It is read first. | — |
description | string One line of detail, under the title. | — |
type | 'success' | 'error' | 'warning' | 'info' | 'loading' The mark beside the words. A | — |
priority | 'low' | 'high'
| 'low' |
timeout | number How long this toast stays, over the toaster timeout. | — |
data | { action?: { label: string; onClick: () => void } } One button that acts on the message, under the text. | — |
id | string An id of your own, to update the toast later. | — |
onClose | () => void Called when the toast starts to close. | — |
What add() takes.