Toast
A succinct message that is displayed temporarily.
Interactive
Features
Multiple variants (default, success, error, warning, info). Auto-dismiss with configurable duration. Swipe to dismiss support. Action buttons. Multiple toast positioning options. Accessible announcements.
Installation
Install the component from your command line.
ng g @ng-cn/core:c toastAnatomy
Import all parts and piece them together.
import {
Toaster,
ToastService
} from '@/ui/toast';
// Or individual components:
import {
Toast,
ToastTitle,
ToastDescription,
ToastAction
} from '@/ui/toast';<!-- Add Toaster to your app root -->
<Toaster />
<!-- Use ToastService to show toasts -->
// this.toastService.success({ title: 'Success!' });API Reference
ToastService
Service for managing toast notifications.
| Prop | Type | Default |
|---|---|---|
| toasts | Signal<Toast[]> | — |
| Reactive list of active toasts. |
Toaster
Container component that renders all active toasts.
| Prop | Type | Default |
|---|---|---|
| position | 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right' | 'bottom-right' |
| Position of the toast container. | ||
| class | string | — |
| Additional CSS classes to apply. |
Toast
Individual toast notification component.
| Prop | Type | Default |
|---|---|---|
| variant | 'default' | 'success' | 'error' | 'warning' | 'info' | 'default' |
| Visual variant of the toast. | ||
| showIcon | boolean | true |
| Whether to display the variant icon. | ||
| isVisible | boolean | true |
| Controls animation state. | ||
| class | string | — |
| Additional CSS classes to apply. |
Data Attributes
| Attribute | Values |
|---|---|
| [data-state] | "open" | "closed" |
| [data-swipe] | "start" | "move" | "cancel" | "end" |
CSS Variables
| Variable | Description |
|---|---|
| --radix-toast-swipe-move-x | Swipe position during drag. |
| --radix-toast-swipe-end-x | Final swipe position. |
ToastTitle
Title text for a toast notification.
| Prop | Type | Default |
|---|---|---|
| class | string | — |
| Additional CSS classes to apply. |
ToastDescription
Description text for a toast notification.
| Prop | Type | Default |
|---|---|---|
| class | string | — |
| Additional CSS classes to apply. |
ToastAction
Action button displayed within a toast.
| Prop | Type | Default |
|---|---|---|
| altText | string | — |
| Alt text for accessibility within destructive toasts. | ||
| class | string | — |
| Additional CSS classes to apply. |
Examples
Basic
Show a simple toast notification.
// In your component:
toastService = inject(ToastService);
showToast() {
this.toastService.toast({
title: 'Scheduled: Catch up',
description: 'Friday, February 10, 2023 at 5:57 PM',
});
}
// In template:
<Button (click)="showToast()">Show Toast</Button>With variants
Different toast variants for different message types.
// Success
this.toastService.success({
title: 'Success!',
description: 'Your changes have been saved.',
});
// Error
this.toastService.error({
title: 'Error',
description: 'Something went wrong.',
});
// Warning
this.toastService.warning({
title: 'Warning',
description: 'This action cannot be undone.',
});
// Info
this.toastService.info({
title: 'Info',
description: 'New features are available.',
});With action
Toast with an action button.
this.toastService.error({
title: 'Uh oh! Something went wrong.',
description: 'There was a problem with your request.',
action: {
label: 'Try again',
onClick: () => this.retryRequest(),
},
});Persistent toast
Toast that does not auto-dismiss.
const id = this.toastService.info({
title: 'Uploading...',
description: 'Please wait while we upload your file.',
duration: 0, // Won't auto-dismiss
});
// Later, dismiss manually
this.toastService.dismiss(id);Custom position
Position toasts at different locations.
<Toaster position="top-center" />Accessibility
Adheres to the Alert WAI-ARIA design pattern .
Keyboard Interactions
| Key | Description |
|---|---|
| Escape | Closes the focused toast. |
| Tab | Moves focus to the action button or close button. |