Primitives
A toast is a non-modal, unobtrusive window element used to display brief, auto-expiring messages to the user.
import { Component } from "@angular/core";
import { NgpToast } from "ng-primitives/toast";
@Component({
selector: "app-toast",
imports: [NgpToast],
template: `
<button class="toast-trigger" (click)="toast.show()" ngpButton>
Show Toast
</button>
<ng-template #toast="ngpToast" ngpToast let-dismiss="dismiss">
<div class="toast">
<p class="toast-title">This is a toast message</p>
<p class="toast-description">It will disappear in 3 seconds</p>
<button class="toast-dismiss" (click)="dismiss()" ngpButton>
Dismiss
</button>
</div>
</ng-template>
`,
styles: `
.toast-trigger {
padding-left: 1rem;
padding-right: 1rem;
border-radius: 0.5rem;
color: var(--ngp-text-primary);
border: none;
outline: none;
height: 2.5rem;
font-weight: 500;
background-color: var(--ngp-background);
transition: background-color 300ms cubic-bezier(0.4, 0, 0.2, 1);
box-shadow: var(--ngp-button-shadow);
}
.toast-trigger[data-hover] {
background-color: var(--ngp-background-hover);
}
.toast-trigger[data-focus-visible] {
outline: 2px solid var(--ngp-focus-ring);
outline-offset: 2px;
}
.toast-trigger[data-press] {
background-color: var(--ngp-background-active);
}
.toast {
position: fixed;
display: inline-grid;
background: var(--ngp-background);
box-shadow: var(--ngp-shadow);
border: 1px solid var(--ngp-border);
padding: 12px 16px;
opacity: 0;
transition: all 0.4s cubic-bezier(0.215, 0.61, 0.355, 1);
border-radius: 8px;
z-index: 9999;
grid-template-columns: 1fr auto;
grid-template-rows: auto auto;
column-gap: 12px;
align-items: center;
}
.toast-title {
color: var(--ngp-text-primary);
font-size: 0.75rem;
font-weight: 600;
margin: 0;
grid-column: 1 / 2;
grid-row: 1;
line-height: 16px;
}
.toast-description {
font-size: 0.75rem;
margin: 0;
color: var(--ngp-text-secondary);
grid-column: 1 / 2;
grid-row: 2;
line-height: 16px;
}
.toast-dismiss {
background: var(--ngp-background-inverse);
color: var(--ngp-text-inverse);
border: none;
border-radius: 8px;
padding: 4px 8px;
font-weight: 600;
font-size: 12px;
cursor: pointer;
grid-column: 2 / 3;
grid-row: 1 / 3;
max-height: 27px;
}
.toast[data-toast="visible"] {
opacity: 1;
}
.toast[data-position="end"] {
right: 16px;
}
.toast[data-position="start"] {
left: 16px;
}
.toast[data-gravity="top"] {
top: -150px;
}
.toast[data-gravity="bottom"] {
bottom: -150px;
}
.toast[data-position="center"] {
margin-left: auto;
margin-right: auto;
left: 0;
right: 0;
max-width: fit-content;
}
`,
})
export default class ToastExample {}
Import the Toast primitives from ng-primitives/toast
.
import { NgpToast } from 'ng-primitives/toast';
Assemble the toast directives in your template.
<button (click)="toast.show()">Show Toast</button>
<ng-template ngpToast #toast="ngpToast">
<div>...</div>
</ng-template>
To show a toast, call the show
method on the ngpToast
directive.
Create a toast component that uses the NgpToast
directive.
import { Component } from '@angular/core';
import { Toast } from './toast';
@Component({
selector: 'app-root',
imports: [Toast],
template: `
<button class="toast-trigger" (click)="toast.show()" ngpButton>Show Toast</button>
<app-toast
#toast
header="This is a toast message"
description="It will disappear in 3 seconds"
/>
`,
styles: `
.toast-trigger {
padding-left: 1rem;
padding-right: 1rem;
border-radius: 0.5rem;
color: var(--ngp-text-primary);
border: none;
outline: none;
height: 2.5rem;
font-weight: 500;
background-color: var(--ngp-background);
transition: background-color 300ms cubic-bezier(0.4, 0, 0.2, 1);
box-shadow: var(--ngp-button-shadow);
}
.toast-trigger[data-hover] {
background-color: var(--ngp-background-hover);
}
.toast-trigger[data-focus-visible] {
outline: 2px solid var(--ngp-focus-ring);
outline-offset: 2px;
}
.toast-trigger[data-press] {
background-color: var(--ngp-background-active);
}
`,
})
export default class App {}
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.componentSuffix
: The suffix to apply to the generated component class name.fileSuffix
: The suffix to apply to the generated component file name. Defaults to component
.exampleStyles
: Whether to include example styles in the generated component file. Defaults to true
.The following directives are available to import from the ng-primitives/toast
package:
The duration the toast will display for before it is automatically dismissed in milliseconds.
The direction the toast will appear from.
The position the toast will on the horizontal axis.
Whether the automatic dismissal of the toast should be paused when the user hovers over it.
Whether the toast should be announced to assistive technologies.
[ngpToast]
ngpToast
The following data attributes are applied to the first child of the ngpToast
ng-template:
Attribute | Description | Value |
---|---|---|
data-toast |
The visible state of the toast. | visible | hidden |
data-position |
The position of the toast. | start | center | end |
data-gravity |
The gravity of the toast. | top | bottom |
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({
duration: 5000,
position: 'center',
gravity: 'top',
stopOnHover: false,
ariaLive: 'assertive',
gap: 16,
}),
],
});
The duration in milliseconds that the toast will be visible.
The position of the toast.
The gravity of the toast. This will determine the location the toast will slide in and out.
Whether the toast should stop the timer when hovered over. Once the mouse leaves the toast, the timer will restart.
The `aria-live` attribute value for the toast. This will determine how the toast will be read by screen readers.
The gap between each toast.
Copyright © 2025 Angular Primitives