Skip to content
Slotted

Toast

A short message that appears over the page, is announced, and leaves on its own or when it is dismissed.

Installation

The packages are a 0.x beta on npm, and their public API changes between beta releases without deprecation. One line installs the binding and the default theme.

bash
pnpm add @slotted/react @slotted/theme @slotted/themes
tsx
import { Toast, ToastActions, ToastClose, ToastContent,  ToastDefaultsProvider, ToastDescription, ToastRegion, ToastTitle,  toast } from '@slotted/react';

Render one region near the root of the application and show messages from anywhere: toast.show in React, SlToastService.show in Angular. The examples below use a queue each, so that a message appears once on this page.

Tones

tone says what the message is about and draws the mark that goes with it: info, success, warning and danger. accent, the default, draws the information mark, and secondary is a note with no mark.

Variants

raised, the default, keeps the neutral surface and puts the tone in the mark, which suits a message over any content. soft tints the surface, and solid fills it.

Duration

A message stays for five seconds, counted from the moment it appears. duration sets another time in milliseconds, and 0 keeps the message until it is dismissed. progress draws the time that is left. The timer stops while the pointer is over the region, while focus is inside it and while the tab is hidden.

Actions

actions puts controls under the text, in React as a node and in Angular as a template that receives the message. A message with actions does not expire unless it states a duration, so there is time to reach Undo.

SO-10482 is open.

Following a promise

promise shows one message for the whole operation. It starts as loading, with a loader and no timer, and becomes the success or the failure in place. The result of the promise is returned, so a caller can still handle it.

Updating a message

Showing an id that is already in the queue updates that message in place and starts its timer again. Press Upload the price list, then Finish.

Announcement priority

Every message is announced when it appears. polite, the default, waits for the screen reader to finish its sentence, and assertive interrupts it. The priority does not follow the tone, so state assertive only for what cannot wait.

Queue

limit sets how many messages are on screen at once, three by default. The rest wait without a timer, and take a place as a message leaves. clear empties the queue.

Stacking

stacking="stack" draws the messages as a pile and opens it while the pointer is over it or focus is inside it. It suits a region that can hold several messages on a small screen.

Placement

placement names an edge in the direction of the text: bottom-end, the default, is the bottom right in English and the bottom left in Arabic.

Why a message left

The close button, Escape, a swipe and the timer each request the change with a reason of their own. onToastsChange in React and toastsChange in Angular receive it, and cancel() refuses the change. Show a message and let it go in different ways.

Last departure: none yet

A queue you own

A region that receives toasts draws that array and only asks for changes, for messages that live in the state of a page. attach="inline" positions the region in the nearest positioned ancestor instead of the viewport. defaultToasts gives a queue its first messages without owning it.

  1. Import 118 finished
Messages in the queue: 1

Keyboard

A message never takes focus when it appears. hotkey sets the key that moves focus to the newest message, F6 by default. Tab reaches its controls, and Escape dismisses it.

Long content

A title wraps, and a string without spaces breaks inside the box. hideIcon leaves the mark out, and the text then starts at the edge of the box.

Defaults for an application

ToastDefaultsProvider in React and provideToastDefaults in Angular set the axes once for every region and message under them. Here every message is soft, appears at the top and stays until it is dismissed.

Accessibility

KeyEffectFocus
F6moves focus into the region, when at least one message is on screenthe newest message's own element
Tabmoves through the controls of the messages, in the order they arrived, and then out of the regionthe next focusable element
Shift+Tabmoves back through the same controlsthe previous focusable element
Escapecloses the message focus is inside, with the reason `escape`the next message, or the element focus came from when there is none
Enteractivates the control focus is onunchanged, unless the control removed the message it was in
Spaceactivates the control focus is onunchanged, unless the control removed the message it was in
  • The region is a landmark with the name Notifications in English. The words of a message are announced by a live region that exists before any message does, so the buttons of a message are not read inside its sentence.
  • A message is announced as its title and its description. A message whose content is not a string announces the string in announcement, and nothing without one.
  • A message that arrives never moves focus. The hotkey, F6 by default, moves focus to the newest message, and Escape dismisses the message focus is in.
  • When the message that holds focus leaves, focus moves to the next message, and after the last one to the element it came from.
  • The timers stop while the pointer is over the region, while focus is inside it and while the tab is hidden, and go on with the time that was left.
  • A duration of 0 in the defaults turns every time limit off, which WCAG 2.2 SC 2.2.1 asks an application to offer. The setting that does so is yours.
  • A message with actions, and one that is loading, never expires unless it states a duration.
  • Every tone with a severity has a mark of its own shape, so the tone is not told by color alone. secondary has none.
  • The region lets pointer events through, and each message takes its own.

API

Toast

Props

placement'top-start' | 'top' | 'top-end' | 'bottom-start' | 'bottom' | 'bottom-end'Default bottom-end
On ToastRegionThe edge the region sits at. The names are logical, so a right-to-left document mirrors them.
attach'portal' | 'viewport' | 'inline'Default portal
On ToastRegionWhere the region is rendered. portal places it at the end of the document with the theme it came from, viewport fixes it where it is written, and inline positions it in the nearest positioned ancestor.
stacking'list' | 'stack'Default list
On ToastRegionHow the messages are arranged. list is a column at their own height; stack is a pile that opens while the pointer is over it or focus is inside it.
limitnumberDefault 3
On ToastRegionHow many messages are on screen at once. The rest wait, and a waiting message has no timer.
durationnumberDefault 5000
On ToastRegion, toast.showThe milliseconds a message stays on screen, counted from the moment it appears. 0 never expires.
swipebooleanDefault true
On ToastRegionWhether a message can be dragged toward the edge of the placement to dismiss it. A drag that starts on a control is not a swipe.
pauseOnHoverbooleanDefault true
On ToastRegionWhether the timers stop while the pointer is over the region. Focus inside the region always stops them.
pauseWhenHiddenbooleanDefault true
On ToastRegionWhether the timers stop while the tab is hidden.
hotkeystringDefault F6
On ToastRegionThe key that moves focus into the region, such as F6, Alt+T or Shift+F8.
messagesPartial<ToastMessages>
On ToastRegionThe words of the region over the ones of the language in force: label names the region and close names each close button.
tone'accent' | 'secondary' | 'info' | 'success' | 'warning' | 'danger'Default accent
On toast.showThe color and the mark of the message. secondary draws no mark.
variant'raised' | 'soft' | 'solid'Default raised
On toast.showHow the tone is painted. raised keeps the neutral surface and puts the tone in the mark, soft tints the surface, and solid fills it.
priority'polite' | 'assertive'Default polite
On toast.showHow the message is announced. assertive interrupts the screen reader. It does not follow the tone.
dismissiblebooleanDefault true
On toast.showWhether the message has a close button.
progressbooleanDefault false
On toast.showWhether the message draws how much of its duration is left.
toastsreadonly ToastMessage[]
On ToastRegionThe queue you own. A region that receives it is controlled for its whole life and only asks for changes.
defaultToastsreadonly ToastMessage[]
On ToastRegionThe messages the queue starts with, read once.
titleReactNode
On toast.showThe name of the message. With the description, it is what is announced.
descriptionReactNode
On toast.showThe text under the title.
announcementstring
On toast.showWhat is announced instead of the title and the description. State it for a message whose content is not a string.
loadingbooleanDefault false
On toast.showWhether the message waits for work to end. It draws a loader in place of the mark and never expires.
iconReactNode
On toast.showThe content that replaces the mark of the tone.
hideIconbooleanDefault false
On toast.showWhether the mark is left out.
actionsReactNode
On toast.showThe controls under the text. A message with actions never expires unless it states a duration.
idstring
On toast.showThe identifier of the message. Showing an id that is already in the queue updates that message in place.
containerElement | null
On ToastRegionThe element a portalled region is placed in.

Events

NamePayloadWhen
onToastsChange(toasts, request)Before the queue changes. The request carries the previous queue, the reason (programmatic, dismiss, escape, keyboard, pointer, timeout or clear), the originating event and cancel().
onStatusChange(status, message)When a message appears (visible), leaves the queue (closing) and leaves the document (removed). It is passed to the call that shows the message.

Composition

children(message) => ReactNode
On ToastRegionRenders each message. The timer, the exit, the swipe, the keyboard and the announcement stay.
render(props) => ReactNode
On ToastContent, ToastTitle, ToastDescription, ToastActions, ToastCloseRenders the part as another element with the attributes it receives.
toastToaster
On anywhereThe shared queue: show, update, dismiss, clear, pauseAll, resumeAll and promise. It is a plain object, so it works outside components.
createToaster() => Toaster
On an applicationCreates another queue, for an application with two regions. Pass it to a region as toaster, or to a subtree with ToasterProvider.
ToastDefaultsProvider{ placement, attach, stacking, tone, variant, limit, duration, hotkey, … }
On a subtreeSets the axes once for every region and message under it. A value on a region or a message still wins.
useToastQueue() => { visible, queued, paused }
On inside a regionReads how many messages are on screen, how many wait and whether the timers are stopped.

Parts

PartElementRequiredPurpose
rootsectionYesThe region. It is a landmark with a name, and it carries the placement, the attachment and the stacking. It is never a live region.
listolYesThe messages in order, newest first.
itemliNoOne message. It carries the tone, the variant and its states, and the hotkey moves focus to it.
iconspanNoThe mark of the tone, hidden from assistive technology. While the message is loading it holds a loader.
contentdivNoThe title and the description. A long string wraps inside it.
titlediv, or the element render returnsNoThe name of the message. It is not a heading, because a message is not a section of the page.
descriptionp, or the element render returnsNoThe text of the message. A string without spaces breaks instead of overflowing.
actionsdivNoThe controls that act on the message, under its text. They stack when the region is narrower than 20rem.
closebuttonNoThe button that dismisses the message. It is present while the message is dismissible, and its target holds the minimum pointer size at every density.
progressdivNoHow much of the duration is left. It stops with the timer, and under reduced motion it is a static rule.

Theming

Each token is a CSS custom property: set it in a theme, or on any element to retune every toast inside it.

Toast tokens

TokenDefault
--slotted-toast-actions-gapvar(--slotted-action-gap, 8px)
--slotted-toast-backdrop-filternone
--slotted-toast-border-colorvar(--slotted-border-subtle, #e2e8f0)
--slotted-toast-border-widthvar(--slotted-border-width-thin, 1px)
--slotted-toast-close-radiusvar(--slotted-control-radius, 4px)
--slotted-toast-close-sizemax( var(--slotted-target-min, 24px), min(calc(var(--_glyph-size) + 2 * var(--slotted-space-2, 0.25rem)), var(--_inner-size)) )
--slotted-toast-column-gapvar(--slotted-space-4, 0.5rem)
--slotted-toast-font-familyvar(--slotted-control-font-family, sans-serif)
--slotted-toast-gapvar(--slotted-space-5, 0.75rem)
--slotted-toast-measure24rem
--slotted-toast-offsetvar(--slotted-space-6, 1rem)
--slotted-toast-padding-blockvar(--slotted-space-5, 0.75rem)
--slotted-toast-padding-inlinevar(--slotted-space-6, 1rem)
--slotted-toast-progress-sizevar(--slotted-space-1, 0.125rem)
--slotted-toast-radiusvar(--slotted-radius-lg, 8px)
--slotted-toast-row-gapvar(--slotted-space-5, 0.75rem)
--slotted-toast-shadowvar(--slotted-shadow-md)
--slotted-toast-stack-fade0.15
--slotted-toast-stack-stepvar(--slotted-space-3, 0.375rem)
--slotted-toast-state-layer-active16%
--slotted-toast-state-layer-hover10%
--slotted-toast-surfacevar(--slotted-surface-raised, #ffffff)
--slotted-toast-textvar(--slotted-text, #0f172a)
--slotted-toast-text-gapvar(--slotted-space-2, 0.25rem)
--slotted-toast-text-mutedvar(--slotted-text-muted, #5b6b7f)
--slotted-toast-title-font-familyvar( --slotted-font-family-heading, var(--slotted-font-family-sans, ui-sans-serif, system-ui, sans-serif) )
--slotted-toast-title-font-weightvar(--slotted-font-weight-semibold, 600)

Theme tokens it reads

TokenDefault
--slotted-action-gapcomfortable 8px · compact 6px · dense 4px
--slotted-border-subtleSet per scheme by the theme
--slotted-border-width-thin1px
--slotted-control-font-familyvar(--slotted-font-family-sans)
--slotted-control-font-size-mdcomfortable 13px · compact 12px · dense 12px
--slotted-control-icon-size-mdcomfortable 16px · compact 14px · dense 13px
--slotted-control-inner-size-mdcomfortable 24px · compact 20px · dense 18px
--slotted-control-radiusvar(--slotted-radius-sm)
--slotted-control-transition-durationvar(--slotted-duration-short)
--slotted-control-transition-easingvar(--slotted-easing-standard)
--slotted-duration-long320ms
--slotted-duration-medium200ms
--slotted-duration-short120ms
--slotted-easing-entercubic-bezier(0.05, 0.7, 0.1, 1)
--slotted-easing-exitcubic-bezier(0.3, 0, 0.8, 0.15)
--slotted-easing-standardcubic-bezier(0.2, 0, 0, 1)
--slotted-focus-ring-colorSet per scheme by the theme
--slotted-focus-ring-offsetvar(--slotted-border-width-thick)
--slotted-focus-ring-widthvar(--slotted-border-width-thick)
--slotted-font-family-headingvar(--slotted-font-family-sans)
--slotted-font-family-sans'Geist Variable', ui-sans-serif, system-ui, sans-serif
--slotted-font-weight-semibold600
--slotted-line-height-md1.5
--slotted-radius-lg8px
--slotted-shadow-mdSet per scheme by the theme
--slotted-space-10.125rem
--slotted-space-20.25rem
--slotted-space-30.375rem
--slotted-space-40.5rem
--slotted-space-50.75rem
--slotted-space-61rem
--slotted-space-71.25rem
--slotted-surface-raisedSet per scheme by the theme
--slotted-target-comfortable44px
--slotted-target-min24px
--slotted-textSet per scheme by the theme
--slotted-text-mutedSet per scheme by the theme
--slotted-tone-accent-borderSet per scheme by the theme
--slotted-tone-accent-on-solidSet per scheme by the theme
--slotted-tone-accent-solidSet per scheme by the theme
--slotted-tone-accent-surfaceSet per scheme by the theme
--slotted-tone-accent-textSet per scheme by the theme
--slotted-tone-danger-borderSet per scheme by the theme
--slotted-tone-danger-on-solidSet per scheme by the theme
--slotted-tone-danger-solidSet per scheme by the theme
--slotted-tone-danger-surfaceSet per scheme by the theme
--slotted-tone-danger-textSet per scheme by the theme
--slotted-tone-info-borderSet per scheme by the theme
--slotted-tone-info-on-solidSet per scheme by the theme
--slotted-tone-info-solidSet per scheme by the theme
--slotted-tone-info-surfaceSet per scheme by the theme
--slotted-tone-info-textSet per scheme by the theme
--slotted-tone-secondary-borderSet per scheme by the theme
--slotted-tone-secondary-on-solidSet per scheme by the theme
--slotted-tone-secondary-solidSet per scheme by the theme
--slotted-tone-secondary-surfaceSet per scheme by the theme
--slotted-tone-secondary-textSet per scheme by the theme
--slotted-tone-success-borderSet per scheme by the theme
--slotted-tone-success-on-solidSet per scheme by the theme
--slotted-tone-success-solidSet per scheme by the theme
--slotted-tone-success-surfaceSet per scheme by the theme
--slotted-tone-success-textSet per scheme by the theme
--slotted-tone-warning-borderSet per scheme by the theme
--slotted-tone-warning-on-solidSet per scheme by the theme
--slotted-tone-warning-solidSet per scheme by the theme
--slotted-tone-warning-surfaceSet per scheme by the theme
--slotted-tone-warning-textSet per scheme by the theme
--slotted-z-index-toast1200