Toast
Brief, non-blocking notifications. Place one toaster per page and call `show()`.
Example
<focsd-button id="toast-demo">Save draft</focsd-button><focsd-button id="toast-demo-sticky">Show sticky toast</focsd-button><focsd-toaster id="toaster-demo"></focsd-toaster>
<script type="module">const toaster = document.getElementById('toaster-demo');document.getElementById('toast-demo').addEventListener('click', () => { toaster.show({ message: 'Draft saved', tone: 'positive', action: { label: 'Undo', onClick: () => toaster.show({ message: 'Draft restored' }) } });});document.getElementById('toast-demo-sticky').addEventListener('click', () => { toaster.show({ message: 'Sync paused while offline', tone: 'caution', duration: 0 });});</script>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-toaster.js';</script>
<focsd-toaster></focsd-toaster>import { Toaster } from '@focsd-dev/react';
export function Example() { return <Toaster></Toaster>;}<script setup>import '@focsd-dev/components/focsd-toaster.js';</script>
<template> <focsd-toaster></focsd-toaster></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-toaster.js';
// template<focsd-toaster></focsd-toaster><script> import '@focsd-dev/components/focsd-toaster.js';</script>
<focsd-toaster></focsd-toaster>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
placement | placement | 'bottom-end' | 'bottom-start' | 'top-end' | 'top-start' | bottom-end |
JavaScript properties
| Property | Type | Description |
|---|---|---|
show(options) | { message, tone?, action?: { label, onClick }, duration? = 6000 } | Adds a toast and returns its id. duration: 0 keeps it until dismissed. |
dismiss(id) | number | Removes a toast. |
Methods
| Method | Description |
|---|---|
show({ message, tone = 'neutral', action, duration = DEFAULT_DURATION }, options) | Show a toast. Returns its id. duration: 0 keeps it until dismissed. |
dismiss(id) | Dismiss the toast with this id. |
CSS parts
Style the inside of the component with ::part(), for example focsd-toaster::part(action).
| Part | Description |
|---|---|
action | The action element. |
dismiss | The dismiss element. |
list | The list element. |
message | The message element. |
region | The region element. |
toast | The toast element. |
Accessibility
- The toaster is a polite
role="status"live region that exists before toasts arrive, so every message is announced. - Toasts never take focus. Timers pause while hovered or focused (WCAG 2.2.1); each toast has a "Dismiss notification" button.
- Do not put the only path to an action in a toast.
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.