ComponentCDK OverlayUpdated in v15

Toast

Short notifications that stack in a corner of the screen.

Installation

Import the class, and @use the entry point in your global styles: without it the component renders unstyled.

import { WrToast } from 'ngwr/toast';

Global config

Set defaults once with withToast() in provideWr(). Fields you omit keep the library default.

import { bootstrapApplication } from '@angular/platform-browser';
import { provideWr } from 'ngwr';
import { withToast } from 'ngwr/toast';

import { AppComponent } from './app/app';

bootstrapApplication(AppComponent, {
  providers: [
    provideWr(
      withToast({
        position: 'bottom-end',
        duration: 5000,
        showProgress: true,
        showCopy: true,
        maxStack: 5,
        labels: {
          close: 'Закрыть',
          copy: 'Копировать',
          copied: 'Скопировано',
          closeAll: 'Закрыть все',
        },
      }),
    ),
  ],
});

Show a toast

show() opens a toast and returns a ref with dismiss().

this.toast.show({
  type: 'success',
  title: 'Saved',
  message: 'Profile updated.',
});
InfoSuccessWarningDanger
toast.show(...)

Title or message alone

Either part can stand on its own. A title-only toast passes an empty message.

this.toast.show({ title: 'Saved', message: '' });
this.toast.show({ message: 'Link copied' });
Title onlyMessage only

Per-toast overrides

show() takes every field of the global config.

this.toast.show({
  message: 'Permalink copied to clipboard',
  position: 'bottom',
  showCopy: true,
  duration: 6000,
});
Show with copy button

Close all

“Close all” appears once closeAllThreshold toasts are open; in stack mode, when the stack is expanded.

Open 3 toastsDismiss all programmatically

Persistent toast

duration: 0 turns off auto-dismiss and hides the progress bar.

this.toast.show({
  type: 'danger',
  message: 'Network error',
  duration: 0, // no auto-dismiss
});
Open persistent

Layout mode

stack (default) piles toasts up and fans them out on hover; list shows a column. Set it with withToast({ mode }) or setMode().

Open 3 toasts

Position

All toasts share one stack, so a per-call position moves the whole stack.

this.toast.setPosition('bottom-end');
Fire here

Keyboard

Focus inside the stack pauses every countdown, and expands a collapsed stack so “Close all” can be reached.

Since v15, closing a toast from the keyboard moves focus to the Close button of the toast that takes its place, else the one before it. Closing the last one returns focus to where it was before it entered the stack.

Lifetime and scope

A toast outlives the component that raised it and survives navigation. Keep the ref to remove it early.

const ref = this.toast.show({ message: 'Uploading…', duration: 0 });
await this.upload();
ref.dismiss();

withToast() in a route's providers is ignored: set it at bootstrap, or pass the value per call.

Service API

NameDescriptionTypeDefault
show(options)Opens a toast and returns a handle (dismiss()).(WrToastOptions) => WrToastRef—
dismiss(id)Removes a single toast by id.(number) => void—
dismissAll()Removes every toast in the stack.() => void—
setPosition(position)Move the stack to another corner.(WrToastPosition) => void—
setMode(mode)Switch the stack layout.(WrToastMode) => void—

WrToastOptions

Per-call options for show(). Omitted fields fall back to WrToastConfig.

NameDescriptionTypeDefault
typeVisual variant.'info' | 'success' | 'warning' | 'danger''info'
titleOptional heading.string—
messagerequiredBody text. '' for a title-only toast.string—
durationAuto-dismiss after N ms. 0 disables. Falls back to global config.numberconfig.duration
dismissibleShow close (×) button.booleantrue
positionCorner of the stack. Moves the toasts already on screen too.WrToastPositionconfig.position
showProgressOverride the progress bar visibility.booleanconfig.showProgress
showCopyOverride the copy button visibility.booleanconfig.showCopy
classExtra classes on this toast's box, not the overlay pane.WrClassInput—

WrToastConfig

Set once with withToast().

NameDescriptionTypeDefault
positionDefault corner the stack renders in.WrToastPosition'top-end'
modeLayout for several toasts: a cascade or a column.WrToastMode'stack'
durationDefault auto-dismiss in ms. 0 disables.number4000
showProgressRender a countdown bar; pauses on hover.booleantrue
showCopyRender a copy-message button on each toast.booleanfalse
showCloseAllRender a "Close all" button above the stack.booleantrue
closeAllThresholdMinimum stack size before "Close all" appears.number2
maxStackMax visible toasts. Extra toasts wait in a queue. 0 = unlimited.number5
labelsUI strings. Override any one of them.{ close; copy; copied; closeAll }—

CSS variables

Set these on the component's selector or a wrapper, not on :root: the defaults are declared on ngwr/toast's own element.

VariableDefaultDeclared on
--wr-toast-accentvar(--wr-color-primary).wr-toast +1 variant override
--wr-toast-action-paddingvar(--wr-space-xs).wr-toast
--wr-toast-action-radiusvar(--wr-border-radius-sm).wr-toast
--wr-toast-actions-gapvar(--wr-space-xs).wr-toast +1 variant override
--wr-toast-bgvar(--wr-color-surface).wr-toast
--wr-toast-bordervar(--wr-border-base).wr-toast
--wr-toast-font-familyvar(--wr-font-family-base).wr-toast
--wr-toast-gapvar(--wr-space-md).wr-toast
--wr-toast-host-close-all-bordervar(--wr-border-base).wr-toast-host__close-all
--wr-toast-host-close-all-font-familyvar(--wr-font-family-base).wr-toast-host__close-all
--wr-toast-host-close-all-font-sizevar(--wr-text-xs).wr-toast-host__close-all
--wr-toast-host-close-all-font-weightvar(--wr-font-weight-medium).wr-toast-host__close-all
--wr-toast-host-close-all-line-heightvar(--wr-text-xs-leading).wr-toast-host__close-all
--wr-toast-host-close-all-padding-xvar(--wr-space-md).wr-toast-host__close-all
--wr-toast-host-close-all-padding-yvar(--wr-space-sm).wr-toast-host__close-all +1 variant override
--wr-toast-host-close-all-radiusvar(--wr-border-radius-base).wr-toast-host__close-all
--wr-toast-host-gapvar(--wr-space-sm).wr-toast-host
--wr-toast-ivar(--wr-toast-stack-index, 0) only under .wr-toast-host--stack:not(.wr-toast-host--expanded) .wr-toast — unset elsewhere.wr-toast-host--stack:not(.wr-toast-host--expanded) .wr-toast
--wr-toast-messagevar(--wr-color-on-surface-muted).wr-toast
--wr-toast-message-font-sizevar(--wr-text-sm).wr-toast
--wr-toast-message-line-heightvar(--wr-text-sm-leading).wr-toast
--wr-toast-padding-xvar(--wr-space-lg).wr-toast
--wr-toast-padding-yvar(--wr-space-md).wr-toast
--wr-toast-radiusvar(--wr-border-radius-base).wr-toast
--wr-toast-titlevar(--wr-color-on-surface).wr-toast
--wr-toast-title-font-sizevar(--wr-text-sm).wr-toast
--wr-toast-title-font-weightvar(--wr-font-weight-semibold).wr-toast
--wr-toast-title-line-heightvar(--wr-text-sm-leading).wr-toast
--wr-toast-title-margin-bottomvar(--wr-space-xs).wr-toast

Dependencies

Angular CDKportal
Built onOverlay

See also