# Toast

> A polite heads-up that something just happened. It lives on the page up front, so screen readers actually hear it.

`<slab-toast>` · Overlays · built on `[role=status]`

## Install

```sh
pnpm add slab-elements lit
```

## Usage

```html
<script type="module">
  import 'slab-elements/toast.js';
</script>

<slab-toast duration="5000">Changes saved</slab-toast>

<script type="module">
  document.querySelector('slab-toast').show();
</script>
```

## Example

Here's a styled version, `<slab-styled-toast>`. It's not in the package — it's an example of what you can build with the headless element. Copy it and make it yours at the bottom of this page.

```html
<button class="styled-button styled-button--primary" type="button">Save pour</button>

<slab-styled-toast id="toast-example">Pour saved.</slab-styled-toast>
```

Swap `slab-styled-` for `slab-` in that markup and you've got the same example as it ships: the headless elements with no CSS at all. That's your starting point.

## Keyboard

| Key    | What it does                              |
| ------ | ----------------------------------------- |
| Escape | Dismisses the toast while focus is in it. |

## Behavior

A toast with no visual styles: a polite `[role=status]` live region that lives on the page for good and is shown and dismissed rather than added and removed.

Put it on the page up front, not when there's something to say. A live region only announces what's inserted into it after screenreaders have found it, so a region added along with its content is usually read by nobody. The region inside this one is rendered, empty, as soon as the host is connected. Its content goes in the default slot, and the slot is only rendered by `show()`, which is the insertion screenreaders announce. `show()` again, with new content or the same, announces it again.

The region is a `popover="manual"` in the top layer, shown when the host is connected and raised again by each `show()`, so it sits above a dialog opened since. The top layer doesn't get it out of a modal dialog's inertness, though: everything outside the dialog is inert, and inert content isn't announced or clickable. So a toast for something that happens in a modal dialog belongs inside the dialog. While dismissed, the region stays in the accessibility tree but is hidden visually.

`duration` dismisses the toast that many milliseconds after it's shown. 0 keeps it until it's dismissed. The countdown pauses while the pointer is over the toast or focus is in it, and starts over when both have left, so no one loses a toast they're reading or about to act on.

Escape dismisses it while focus is in it.

Style it through its part and through `:state(open)`. Where it sits on the page, and whether several stack, is left to the styles around it.

## API

### `<slab-toast>`

#### Attributes

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `duration` | `number` | `5000` | Milliseconds until it dismisses itself. 0 keeps it until it's dismissed. |
| `version` | `string` |  |  |

#### Properties

| Name | Type | Description |
| --- | --- | --- |
| `open` (read-only) | `boolean` |  |

#### Methods

| Signature | Description |
| --- | --- |
| `dismiss(): void` |  |
| `show(): Promise<void>` |  |

#### Events

| Name | Type | Description |
| --- | --- | --- |
| `dismiss` | `Event` |  |

#### Slots

| Name | Type | Description |
| --- | --- | --- |
| `(default)` | `Element \| string` | What's announced |

#### CSS parts

| Name | Description |
| --- | --- |
| `region` |  |

## Copy and own it

`<slab-styled-toast>` isn't in the package. It's an example — copy it into your project and change whatever you want. Under the hood, it's just the headless element, subclassed, with `styles` set. That's it. Keyboard, focus, ARIA, forms, and events all come from the headless element, and the styles only reach in through `::part()` and `:state()`, same as yours would.

To use it, copy these files, swap the tag in `@customElement()` for one of your own, and import it. `tokens.styles.ts` holds the colors and fonts every styled example shares, so you only need it once.

### TypeScript

These files are TypeScript, and `@customElement()` is one of Lit's decorators (TypeScript's experimental flavor). You'll need two options in your tsconfig, per [Lit's docs](https://lit.dev/docs/components/decorators/#decorators-typescript): `experimentalDecorators` to compile them, and `useDefineForClassFields` turned off so class fields don't stomp on the accessors Lit sets up.

```json
{
  "compilerOptions": {
    "experimentalDecorators": true,
    "useDefineForClassFields": false
  }
}
```

Using plain JavaScript? Drop the types and register the class with `customElements.define()` instead of the decorator.

### toast.ts

```ts
import { customElement } from 'lit/decorators.js';
import Toast from 'slab-elements/toast.js';
import styles from './toast.styles.js';
import tokens from './tokens.styles.js';

declare global {
  interface HTMLElementTagNameMap {
    'slab-styled-toast': StyledToast;
  }
}

/**
 * Toast, styled: paper on ink in the page's bottom corner, with an ember edge
 * and a hard shadow, rising into place when it's shown.
 */
@customElement('slab-styled-toast')
export default class StyledToast extends Toast {
  static override styles = [tokens, styles];
}
```

### toast.styles.ts

```ts
import { css } from 'lit';

/*
  Only while it's open. A dismissed toast's region is hidden visually, and
  padding or a border here would give it size again.
*/
export default css`
  :host(:state(open)) [part~='region'] {
    animation: rise 200ms ease-out;
    background-color: var(--ink);
    border: 0;
    border-inline-start: 8px solid var(--ember);
    box-shadow: 4px 4px 0 var(--ember);
    color: var(--paper);
    font-family: var(--font-body);
    inset: auto 1.5rem 1.5rem auto;
    margin: 0;
    max-inline-size: min(24rem, 100% - 3rem);
    padding: 0.875rem 1.25rem;
    position: fixed;
  }

  @keyframes rise {
    from {
      opacity: 0;
      translate: 0 1rem;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    :host(:state(open)) [part~='region'] {
      animation: none;
    }
  }
`;
```

### tokens.styles.ts

```ts
import { css } from 'lit';

/*
  The styled components' palette and type, taken from the logo: ink for the
  courses, ember for the base, paper behind them.

  Each is a short alias for a "--slab-styled-*" property, with the default as
  its fallback. Retheming is "--slab-styled-ember" and the rest on ":root",
  which every shadow root inherits. Were the defaults declared under those
  names here instead, each host's own declaration would shadow the page's.

  Each default is "light-dark()", so the components follow the page's
  "color-scheme": "dark" for dark, "light" for light, and "light dark" to
  follow the operating system. Ink and paper trade places in the dark, so
  ink is always the text and paper always what's behind it. Contrast is given
  as light, then dark.
*/
export default css`
  :host {
    /* Secondary text. 6.8:1 and 6.9:1 on paper. */
    --ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
    --border: 2px solid var(--ink);

    /* Behind disabled and read-only controls, and hovered rows. */
    --chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));

    /*
      The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
      enough for borders, fills and large text, but not always for small text,
      which takes "--ember-deep".
    */
    --ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));

    /*
      Orange for small text on paper, and for paper text on it. 5.7:1 and
      7.4:1.
    */
    --ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
    --font-body: var(
      --slab-styled-font-body,
      'IBM Plex Sans',
      system-ui,
      sans-serif
    );
    --font-display: var(
      --slab-styled-font-display,
      'Zilla Slab',
      rockwell,
      'Roboto Slab',
      serif
    );
    --font-mono: var(
      --slab-styled-font-mono,
      'IBM Plex Mono',
      ui-monospace,
      monospace
    );
    --ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
    --paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));

    /* The hard, offset shadow a raised surface casts. */
    --shadow: 4px 4px 0 var(--ink);
  }
`;
```
