Toast
A succinct message that is displayed temporarily.
Usage
import { Component, ViewEncapsulation, inject } from '@angular/core';import { ScButton, ScToaster } from '@semantic-components/ui';
/** A toast from code: its title, its description and an Undo action. */@Component({ selector: 'app-toast-usage-demo', imports: [ScButton], template: ` <button scButton variant="outline" (click)="show()">Show Toast</button> `, host: { class: 'flex w-full justify-center' }, encapsulation: ViewEncapsulation.None,})export class ToastUsageDemo { private readonly toaster = inject(ScToaster);
private show(): void { this.toaster.show({ title: 'Event created', description: 'Sunday, December 3 at 9:00 AM', // Undo it here: the toast closes itself. action: { label: 'Undo', onClick: () => undefined }, }); }}Anatomy
Toasts are shown from code, with ScToaster. How one looks is written once, in your app: an ng-template whose root is a scToast, its parts inside, from the toast's data. The toaster renders it for each toast, in a stack of its own.
import { ScToaster, ScToastTemplate, ScToast, ScToastIcon, ScToastTitle, ScToastDescription, ScToastAction, ScToastClose,} from '@semantic-components/ui';<!-- once, in your app --><ng-template scToastTemplate let-toast label="…"> <div scToast> <span scToastIcon>…</span> <div class="flex min-w-0 flex-1 flex-col gap-1"> <div scToastTitle>{{ toast.title }}</div> <div scToastDescription>{{ toast.description }}</div> </div> <button scToastAction>{{ toast.action.label }}</button> <button scToastClose>…</button> </div></ng-template>
<!-- anywhere -->inject(ScToaster).show({ title: '…', description: '…' });Accessibility
- The stack is one polite live region: each new toast is announced as it appears, without interrupting.
- A toast's timer pauses while its stack is hovered or focused, so it can be read and its action reached; a duration of 0 keeps it until closed.
- The toaster's hotkey moves focus to the toasts, and Escape back; write the keys in the template's label, the region's name.
- The close button is icon-only: name it (a sr-only span). Swiping a toast sideways closes it too.
- Positions follow the page's direction: start is the right in Arabic or Hebrew.
Examples
Types
import { Component, ViewEncapsulation, inject } from '@angular/core';import { ScButton, type ScToastConfig, ScToaster,} from '@semantic-components/ui';
/** A variant for each kind of message: your template gives each its icon. */@Component({ selector: 'app-types-toast-demo',Promise
import { Component, ViewEncapsulation, inject } from '@angular/core';import { ScButton, ScToaster } from '@semantic-components/ui';
/** One toast while a promise runs, then its result: loading, success or error. */@Component({ selector: 'app-promise-toast-demo', imports: [ScButton], template: ` <button scButton variant="outline" (click)="create()">Create Event</button> `,Position
import { Component, ViewEncapsulation, inject } from '@angular/core';import { ScButton, type ScToastPosition, ScToaster,} from '@semantic-components/ui';
/** Each toast at its own position: the others stay where they are. */@Component({ selector: 'app-position-toast-demo',Custom Duration
import { Component, ViewEncapsulation, inject } from '@angular/core';import { ScButton } from '@semantic-components/ui';import { ScToaster } from '@semantic-components/ui';
/** A duration of its own: this one stays 10 seconds (0 keeps it until closed). */@Component({ selector: 'app-duration-toast-demo', imports: [ScButton], template: ` <button scButton variant="outline" (click)="showToast()">Show Toast</button>Update
import { Component, ViewEncapsulation, inject, signal } from '@angular/core';import { ScButton, ScToaster } from '@semantic-components/ui';
/** One toast, changed in place: a loading toast becomes a success one. */@Component({ selector: 'app-update-toast-demo', imports: [ScButton], template: ` <div class="flex gap-2"> <button scButton variant="outline" (click)="start()">Start upload</button>Stacked
import { Component, ViewEncapsulation, inject } from '@angular/core';import { ScButton, ScToaster } from '@semantic-components/ui';
/** Toasts stack, the newest in front; hover or focus them to spread them out. */@Component({ selector: 'app-stacked-toast-demo', imports: [ScButton], template: ` <button scButton variant="outline" (click)="showThree()"> Show three toastsNo right-to-left example here: the toasts are one stack for the whole app, and follow the page's direction (dir on the html element) — not a dir="rtl" around an example, as other components do. In an Arabic or Hebrew page their row is mirrored and start is the right.
API reference
Each part takes class, merged over the style (cn: a later class wins).
| Part | Selector | Description |
|---|---|---|
ScToaster | service | show(config) returns its id; update(id, config), promise(promise, config), dismiss(id), dismissAll(); toasts. A config: title, description, variant (default, success, info, warning, error, loading), action { label, onClick }, duration (0 keeps it), position. Its settings in provideSemanticComponents({ toaster }): position, duration, visibleToasts, maxToasts, layout, hotkey. |
ScToastTemplate | ng-template[scToastTemplate] | How a toast looks, written once in your app; let-toast is the toast's data. label names the toasts' region (write the hotkey in it). |
ScToast | div[scToast] | The toast, the template's root: the style's look, data-variant, its place in the stack (the newest in front, spread out while hovered or focused); it slides in and out, and goes when swiped sideways. |
ScToastIcon | [scToastIcon] | Your icon, hidden from screen readers: in its variant's colour; it spins while loading. |
ScToastTitle, ScToastDescription | [scToastTitle], [scToastDescription] | The title, and the description under it, muted; both in the variant's colour for success, warning and error. |
ScToastAction | button[scToastAction] | Runs the toast's action, then closes it: the style's button (outline, sm; variant, size). Write its text (toast.action.label). |
ScToastClose | button[scToastClose] | Closes the toast: the style's button (ghost, icon-sm; variant, size), at the end of the row, muted until hovered. Write its icon and name it. |