Primitives
A toast is a non-modal, unobtrusive window element used to display brief, auto-expiring messages to the user.
Import the Toast primitives from ng-primitives/toast.
import { NgpToast } from 'ng-primitives/toast';
Assemble the toast directives in your template.
<ng-template #toast>
<div ngpToast>...</div>
</ng-template>
To show a toast, inject the NgpToastManager service and call the show method passing the toast template or a component
class that uses the NgpToast directive as a Host Directive.
import { NgpToastManager } from 'ng-primitives/toast';
export class MyComponent {
private readonly toastManager = inject(NgpToastManager);
showToast(): void {
this.toastManager.show(toastTemplate);
// or
this.toastManager.show(MyToastComponent);
}
}
Create a toast component that uses the NgpToast directive.
Generate a reusable toast component using the Angular CLI.
ng g ng-primitives:primitive toast
path: The path at which to create the component file.prefix: The prefix to apply to the generated component selector.component-suffix: The suffix to apply to the generated component class name.file-suffix: The suffix to apply to the generated component file name. Defaults to component.styles: How component styles should be generated. css (default) includes the full example styles; unstyled omits them entirely so you can style the component yourself.example-styles (deprecated): still supported for compatibility - true maps to styles: css, false maps to styles: unstyled.Here are some additional examples of how to use the Toast primitive.
The sequential mode prevents background toasts from auto-closing. Only the front-most toast's timer runs, and when that toast is dismissed, the timer starts on the next toast in the stack.
This is useful when you have multiple notifications in quick succession (e.g., health monitoring of external services) where you want to ensure users can see all notifications before they auto-close.
Pass sequential: true when calling show, or set it globally via provideToastConfig to make it the default for every toast.
Persistent toasts do not auto-dismiss. They remain visible until the user dismisses them (by swiping when dismissible is enabled) or until they are dismissed programmatically — either via the returned NgpToastRef or via the manager (toastManager.toasts() + toastManager.dismiss()). This is useful for notification centers and other surfaces where messages must stay until acknowledged.
Pass persistent: true when calling show, or set it globally via provideToastConfig to make it the default for every toast.
Animate a progress bar over --ngp-toast-duration and pause it on data-paused to keep it in step with the timer. To read the time left in code, call remaining() on the NgpToast instance.
Change a toast after it is shown, for example to count uploaded files and then show the result. show() returns an NgpToastRef, whose update() takes a new context, duration or persistent. The toast keeps its place in the stack and does not replay its enter animation. Its timer restarts with the new duration, or the current one, so the new content stays long enough to read. A persistent toast keeps its timer stopped until an update sets persistent: false. While the toast is paused, by hover or sequential, the restarted timer waits for the pause to end.
const ref = this.toastManager.show(UploadToast, {
persistent: true,
context: { label: 'Uploading 1 of 3' },
});
ref.update({ context: { label: 'Uploading 2 of 3' } });
ref.update({ context: { label: 'Files uploaded' }, persistent: false, duration: 5000 });
A template reads the new context through let-context="context". A component reads it with injectToastContextSignal(); injectToastContext() keeps the value passed to show().
The toast keeps its element, so the container's polite live region reads the changed text. A progress bar that animates over --ngp-toast-duration does not restart on its own: render it inside a block keyed on the toast's restarts(), as below.
The following directives are available to import from the ng-primitives/toast package:
You can configure the default options for all toasts in your application by using the provideToastConfig function in a providers array.
import { provideToastConfig } from 'ng-primitives/toast';
bootstrapApplication(AppComponent, {
providers: [
provideToastConfig({
placement: 'top-end',
duration: 5000,
offsetTop: 16,
offsetBottom: 16,
offsetLeft: 16,
offsetRight: 16,
dismissible: true,
maxToasts: 3,
zIndex: 9999999,
swipeDirections: ['left', 'right'],
ariaLive: 'assertive',
gap: 16,
persistent: false,
}),
],
});
For screen reader announcements, ensure your toast container includes role="status" or role="alert" with aria-live="polite" so that new toasts are announced to assistive technology users.
In the browser, NgpToastManager adds an empty live region for every placement when it is first injected, once the page has a <body> (before that, they are added with the first toast). Screen readers can skip a toast added in the same task as its live region, so inject the manager before the first toast is due, for example in the root component, rather than right where the toast is shown.
When the toast that holds keyboard focus closes, focus moves to the next older toast at the same placement, or the newer one if it was the oldest. With no other toast at that placement, or after a pointer interaction, focus returns to the element focused before it entered the toasts.
Copyright © 2026 Angular Primitives
This site is powered by Netlify