<!-- index: /slotted/llms.txt -->
# toast

`data-ui="toast"` · Temporary notification with optional action.

## Import

```tsx
import {
  Toast,
  ToastActions,
  ToastClose,
  ToastContent,
  ToastDefaultsProvider,
  ToastDescription,
  ToastRegion,
  ToastTitle,
  toast,
} from '@slotted/react';
```

## Minimal example

Example

```tsx
import { Button } from '@slotted/react/button';
import { toast, ToastRegion } from '@slotted/react/toast';

export default function ToastBasic() {
  return (
    <>
      {/* Once, near the root of the application. */}
      <ToastRegion />
      <Button
        onClick={() =>
          toast.show({ title: 'Draft saved', description: 'Everything since 2:02 PM is saved.' })
        }
      >
        Save draft
      </Button>
    </>
  );
}
```

## Anatomy

| Part | Element | Required |
| --- | --- | --- |
| `root` | section | yes |
| `list` | ol | yes |
| `item` | li | no |
| `icon` | span | no |
| `content` | div | no |
| `title` | div, or the element render returns | no |
| `description` | p, or the element render returns | no |
| `actions` | div | no |
| `close` | button | no |
| `progress` | div | no |

## Props

| Name | Type | Default | Applies to | Description |
| --- | --- | --- | --- | --- |
| `placement` | `'top-start' \| 'top' \| 'top-end' \| 'bottom-start' \| 'bottom' \| 'bottom-end'` | bottom-end | ToastRegion | The edge the region sits at. The names are logical, so a right-to-left document mirrors them. |
| `attach` | `'portal' \| 'viewport' \| 'inline'` | portal | ToastRegion | Where 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'` | list | ToastRegion | How 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. |
| `limit` | `number` | 3 | ToastRegion | How many messages are on screen at once. The rest wait, and a waiting message has no timer. |
| `duration` | `number` | 5000 | ToastRegion, toast.show | The milliseconds a message stays on screen, counted from the moment it appears. 0 never expires. |
| `swipe` | `boolean` | true | ToastRegion | Whether 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. |
| `pauseOnHover` | `boolean` | true | ToastRegion | Whether the timers stop while the pointer is over the region. Focus inside the region always stops them. |
| `pauseWhenHidden` | `boolean` | true | ToastRegion | Whether the timers stop while the tab is hidden. |
| `hotkey` | `string` | F6 | ToastRegion | The key that moves focus into the region, such as F6, Alt+T or Shift+F8. |
| `messages` | `Partial<ToastMessages>` | — | ToastRegion | The 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'` | accent | toast.show | The color and the mark of the message. secondary draws no mark. |
| `variant` | `'raised' \| 'soft' \| 'solid'` | raised | toast.show | How 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'` | polite | toast.show | How the message is announced. assertive interrupts the screen reader. It does not follow the tone. |
| `dismissible` | `boolean` | true | toast.show | Whether the message has a close button. |
| `progress` | `boolean` | false | toast.show | Whether the message draws how much of its duration is left. |
| `toasts` | `readonly ToastMessage[]` | — | ToastRegion | The queue you own. A region that receives it is controlled for its whole life and only asks for changes. |
| `defaultToasts` | `readonly ToastMessage[]` | — | ToastRegion | The messages the queue starts with, read once. |
| `title` | `ReactNode` | — | toast.show | The name of the message. With the description, it is what is announced. |
| `description` | `ReactNode` | — | toast.show | The text under the title. |
| `announcement` | `string` | — | toast.show | What is announced instead of the title and the description. State it for a message whose content is not a string. |
| `loading` | `boolean` | false | toast.show | Whether the message waits for work to end. It draws a loader in place of the mark and never expires. |
| `icon` | `ReactNode` | — | toast.show | The content that replaces the mark of the tone. |
| `hideIcon` | `boolean` | false | toast.show | Whether the mark is left out. |
| `actions` | `ReactNode` | — | toast.show | The controls under the text. A message with actions never expires unless it states a duration. |
| `id` | `string` | — | toast.show | The identifier of the message. Showing an id that is already in the queue updates that message in place. |
| `container` | `Element \| null` | — | ToastRegion | The element a portalled region is placed in. |
| `children` | `(message) => ReactNode` | — | ToastRegion | Renders each message. The timer, the exit, the swipe, the keyboard and the announcement stay. |
| `render` | `(props) => ReactNode` | — | ToastContent, ToastTitle, ToastDescription, ToastActions, ToastClose | Renders the part as another element with the attributes it receives. |
| `toast` | `Toaster` | — | anywhere | The shared queue: show, update, dismiss, clear, pauseAll, resumeAll and promise. It is a plain object, so it works outside components. |
| `createToaster` | `() => Toaster` | — | an application | Creates 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, … }` | — | a subtree | Sets the axes once for every region and message under it. A value on a region or a message still wins. |
| `useToastQueue` | `() => { visible, queued, paused }` | — | inside a region | Reads how many messages are on screen, how many wait and whether the timers are stopped. |

## Events

| Name | Payload | When |
| --- | --- | --- |
| `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. |

## Axes and defaults

| Axis | Values | Default |
| --- | --- | --- |
| `placement` | `top-start`, `top`, `top-end`, `bottom-start`, `bottom`, `bottom-end` | `bottom-end` |
| `attach` | `portal`, `viewport`, `inline` | `portal` |
| `stacking` | `list`, `stack` | `list` |
| `tone` | `accent`, `secondary`, `info`, `success`, `warning`, `danger` | `accent` |
| `variant` | `raised`, `soft`, `solid` | `raised` |
| `priority` | `polite`, `assertive` | `polite` |
| `dismissible` | `true`, `false` | `true` |
| `swipe` | `true`, `false` | `true` |
| `progress` | `true`, `false` | `false` |
| `pauseOnHover` | `true`, `false` | `true` |
| `pauseWhenHidden` | `true`, `false` | `true` |

## Rules

- 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.

## Tokens

`--slotted-action-gap`, `--slotted-border-subtle`, `--slotted-border-width-thin`, `--slotted-control-font-family`, `--slotted-control-font-size-md`, `--slotted-control-icon-size-md`, `--slotted-control-inner-size-md`, `--slotted-control-radius`, `--slotted-control-transition-duration`, `--slotted-control-transition-easing`, `--slotted-duration-long`, `--slotted-duration-medium`, `--slotted-duration-short`, `--slotted-easing-enter`, `--slotted-easing-exit`, `--slotted-easing-standard`, `--slotted-focus-ring-color`, `--slotted-focus-ring-offset`, `--slotted-focus-ring-width`, `--slotted-font-family-heading`, `--slotted-font-family-sans`, `--slotted-font-weight-semibold`, `--slotted-line-height-md`, `--slotted-radius-lg`, `--slotted-shadow-md`, `--slotted-space-1`, `--slotted-space-2`, `--slotted-space-3`, `--slotted-space-4`, `--slotted-space-5`, `--slotted-space-6`, `--slotted-space-7`, `--slotted-surface-raised`, `--slotted-target-comfortable`, `--slotted-target-min`, `--slotted-text`, `--slotted-text-muted`, `--slotted-toast-actions-gap`, `--slotted-toast-backdrop-filter`, `--slotted-toast-border-color`, `--slotted-toast-border-width`, `--slotted-toast-close-radius`, `--slotted-toast-close-size`, `--slotted-toast-column-gap`, `--slotted-toast-font-family`, `--slotted-toast-gap`, `--slotted-toast-measure`, `--slotted-toast-offset`, `--slotted-toast-padding-block`, `--slotted-toast-padding-inline`, `--slotted-toast-progress-size`, `--slotted-toast-radius`, `--slotted-toast-row-gap`, `--slotted-toast-shadow`, `--slotted-toast-stack-fade`, `--slotted-toast-stack-step`, `--slotted-toast-state-layer-active`, `--slotted-toast-state-layer-hover`, `--slotted-toast-surface`, `--slotted-toast-text`, `--slotted-toast-text-gap`, `--slotted-toast-text-muted`, `--slotted-toast-title-font-family`, `--slotted-toast-title-font-weight`, `--slotted-tone-accent-border`, `--slotted-tone-accent-on-solid`, `--slotted-tone-accent-solid`, `--slotted-tone-accent-surface`, `--slotted-tone-accent-text`, `--slotted-tone-danger-border`, `--slotted-tone-danger-on-solid`, `--slotted-tone-danger-solid`, `--slotted-tone-danger-surface`, `--slotted-tone-danger-text`, `--slotted-tone-info-border`, `--slotted-tone-info-on-solid`, `--slotted-tone-info-solid`, `--slotted-tone-info-surface`, `--slotted-tone-info-text`, `--slotted-tone-secondary-border`, `--slotted-tone-secondary-on-solid`, `--slotted-tone-secondary-solid`, `--slotted-tone-secondary-surface`, `--slotted-tone-secondary-text`, `--slotted-tone-success-border`, `--slotted-tone-success-on-solid`, `--slotted-tone-success-solid`, `--slotted-tone-success-surface`, `--slotted-tone-success-text`, `--slotted-tone-warning-border`, `--slotted-tone-warning-on-solid`, `--slotted-tone-warning-solid`, `--slotted-tone-warning-surface`, `--slotted-tone-warning-text`, `--slotted-z-index-toast`

## Examples

See [toast.examples.md](./toast.examples.md).
