Toast and Toaster
Brief notices about something that happened, on Base UI Toast. They appear in a corner, are announced politely by screen readers, and go away on their own unless you keep them.
import { Toaster, toast, toastError, toastLoading, toastPromise, toastUpdate } from '@blocks/design-system'Set up once
Render Toaster once, near the root of the app. Every helper adds to the same manager, so there is exactly one place notices appear.
// app/layout.tsx: render the Toaster once, next to the page.
import { Toaster } from '@blocks/design-system'
import '@blocks/design-system/styles.css'
import type { ReactNode } from 'react'
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html lang="en">
<body>
{children}
<Toaster />
</body>
</html>
)
}The helpers are client-only: call them from event handlers and effects in client components. A Server Component that calls one gets an error instead of a notice that never shows.
One notice per task
Show a loading notice with an id, then change that same notice with toastUpdate when the work finishes. Pass type to turn it into a success or error. toastPromise does this for a promise.
Source · toast/save-station.tsx
'use client'
import { Button, Stack, toastDismiss, toastError, toastLoading, toastPromise, toastUpdate } from '@blocks/design-system'
// Stands in for a request; resolves or rejects after a moment.
const request = (ok: boolean) =>
new Promise<string>((resolve, reject) => setTimeout(() => (ok ? resolve('Mount Hood') : reject(new Error('timeout'))), 1000))
export default function SaveStation() {
async function save() {
// One notice that changes, rather than a new one per step.
const id = toastLoading('Saving Mount Hood…', { id: 'save-station' })
try {
const name = await request(true)
toastUpdate(id, `Saved ${name}`, { type: 'success', timeout: 4000 })
} catch {
toastUpdate(id, 'Couldn’t save Mount Hood', { type: 'error', timeout: 0 })
}
}
function sync() {
toastPromise(request(false), {
loading: 'Syncing readings…',
success: 'Readings synced',
error: 'Sync failed. Try again in a minute.',
})
}
return (
<Stack direction="row" gap="sm" wrap="wrap">
<Button type="button" variant="primary" onClick={save}>
Save station
</Button>
<Button type="button" onClick={sync}>
Sync readings
</Button>
<Button type="button" variant="ghost" onClick={() => toastError('Station is offline', { timeout: 0 })}>
Report a problem
</Button>
<Button type="button" variant="link" onClick={() => toastDismiss()}>
Dismiss all
</Button>
</Stack>
)
}Tones and options
toast, toastSuccess, toastError, toastInfo, toastWarning and toastLoading set the type, color and icon; toasts.success and friends are the same helpers grouped. Options: description, timeout in milliseconds (0 keeps the notice until it’s dismissed; loading notices never time out), onClose and id. toastDismiss(id) closes one notice and toastDismiss() closes them all. toastManager is the Base UI manager underneath.
Source · toast/tones.tsx
'use client'
import { Button, Stack, toast, toastInfo, toastManager, toasts, toastSuccess, toastWarning } from '@blocks/design-system'
export default function Tones() {
return (
<Stack direction="row" gap="sm" wrap="wrap">
<Button type="button" size="sm" onClick={() => toast('Station added to your list')}>
Default
</Button>
<Button type="button" size="sm" onClick={() => toastSuccess('Readings exported')}>
Success
</Button>
<Button type="button" size="sm" onClick={() => toastInfo('New readings every hour')}>
Info
</Button>
<Button type="button" size="sm" onClick={() => toastWarning('Battery low at Alta', { description: 'Readings may pause overnight.' })}>
Warning
</Button>
{/* The same helpers, grouped: toasts.success, toasts.error, toasts.promise… */}
<Button type="button" size="sm" onClick={() => toasts.info('Grouped helpers work too')}>
toasts.info
</Button>
{/* The underlying Base UI manager, for anything the helpers don't cover. */}
<Button type="button" size="sm" variant="ghost" onClick={() => toastManager.close()}>
Close all
</Button>
</Stack>
)
}Props
Toaster has no props of its own.
Accessibility
- The notices live in a region named “Notifications” that is announced politely, so they don’t interrupt.
- Keyboard users can focus a notice and press Escape to dismiss it; pointer users have a Dismiss button.
- Notices disappear: don’t put the only copy of important information or the only way to undo something in one. Errors that need action should use
timeout: 0or appear on the page.