Toast
A transient notification region driven imperatively by a Toaster.
Usage
Use standalone, driven by a Toaster from createToaster().
<Toast {toaster} {...props}>
{#snippet children(item)}{item.message}{/snippet}
</Toast>Anatomy
The class attribute values represent part names, and conditional indicates elements that are rendered conditionally. Other attributes represent their corresponding props.
<div class="whole" popover="manual">
{#each toaster.toasts as item (item.id)}
<div class="middle" animate:flip={{ duration: motion }}>
<div class="main" role aria-label>
{children(item, variant)}
</div>
</div>
{/each}
</div>Props
| Name | Type | Default | Description |
|---|---|---|---|
toaster* | Toaster | - | A Toaster from createToaster() used to drive the component imperatively |
children* | Snippet | - | Renders each ToastItem |
motion | number | 200 | Flip animation duration |
styling | SVSClass | - | Styling override |
variant | SVSVariant | NEUTRAL | Variant |
* required.
assertive: true on createToaster() or Toaster.add() switches toast items from polite to assertive announcement.
Styling
To learn more, see Styling.
Variant Management
While a toast is visible, the component variant is applied; while it animates out, inactive is applied.
Default Class Name
svs-toast
Accessibility
Keyboard Interactions
| Key | Description |
|---|---|
F6 | When the toast region is open and the key event is not composed, moves focus to the toast region |
ARIA Roles and Attributes
The component applies these automatically:
- Toast region (
whole):role="region",tabindex="-1", andaria-labelwith the current notification count. - Toast item (
main):role="status"by default for polite announcement, orrole="alert"whenassertiveis true, plustabindex="0"andaria-labelderived from the toast type ("<type> message") or"message".
No separate label prop is required for the generated region or toast item names. Provide meaningful rendered toast content through children, because the generated item label describes the message category, not the full message body.
Exports
Types
interface ToasterOptions {
duration?: number;
assertive?: boolean;
}
interface ToastAddOptions {
type?: string;
duration?: number;
assertive?: boolean;
}
interface ToastItem {
readonly id: string;
readonly message: string;
readonly type: string;
readonly assertive: boolean;
readonly visible: boolean;
}
interface ToastProps {
toaster: Toaster;
children: Snippet<[ToastItem, string]>; // Snippet<[item,variant]>
motion?: number; // (200) flip animation duration
styling?: SVSClass;
variant?: SVSVariant; // (VARIANT.NEUTRAL)
}
type ToastReqdProps = "toaster" | "children";
type ToastBindProps = never;Others
// Creates an isolated toast store to pass to <Toast> and drive imperatively.
function createToaster(options?: ToasterOptions): Toaster;
class Toaster {
readonly toasts: ToastItem[]; // reactive list of current toasts
add(message: string, options?: ToastAddOptions): string; // enqueue a toast; options include type/duration/assertive; returns its id
dismiss(id: string): void; // start the hide animation (auto-removes afterward)
remove(id: string): void; // remove immediately without animation
clear(): void; // dismiss all toasts
pause(id: string): void; // pause the auto-dismiss timer (e.g. on hover)
resume(id: string, extend?: boolean): void; // restart the auto-dismiss timer; extend lengthens the duration
}Examples
The example code uses Tailwind CSS along with SVSClass.