# Toolbar

> A group of controls that Tab reaches once, with the arrow keys to move between them.

`<slab-toolbar>` · Navigation · built on `[role=toolbar]`

## Install

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

## Usage

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

<slab-toolbar label="Formatting">
  <button aria-pressed="false">Bold</button>
  <button aria-pressed="false">Italic</button>
  <button>Link</button>
</slab-toolbar>
```

## Example

Here's a styled version, `<slab-styled-toolbar>`. 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
<slab-styled-toolbar label="Finish">
  <button aria-pressed="true" type="button">Broom</button>
  <button aria-pressed="false" type="button">Trowel</button>
  <button aria-pressed="false" type="button">Stamp</button>
  <button aria-pressed="false" type="button" disabled>Polish</button>
</slab-styled-toolbar>
```

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                                                                                                                      |
| ----------- | --------------------------------------------------------------------------------------------------------------------------------- |
| Left, Right | Moves to the previous or next control, wrapping and skipping disabled ones. Up and Down instead when `orientation` is "vertical". |
| Home, End   | Moves to the first or last control.                                                                                               |
| Tab         | Leaves the toolbar. Coming back, focus returns to the control focused last.                                                       |

## Behavior

A toolbar with no visual styles: a `[role=toolbar]` around the controls slotted into it, usually `<button>`s, so Tab reaches the group once and the arrow keys move between them.

Exactly one control is tabbable: the one most recently focused, or the first that isn't disabled. Left and Right, or Up and Down when `orientation` is `"vertical"`, move focus between them, wrapping at each end and skipping disabled ones, and Home and End go to the first and last. A control that edits text, such as a text `<input>`, keeps its arrow keys, and so does one that handles a key itself and cancels it.

Controls are focused directly, so put ones that take focus themselves in it, such as `<button>`s, `<input>`s and links. A custom element whose focusable part is in its shadow root can't be made untabbable from outside it.

Style it through its part, and the controls directly.

## API

### `<slab-toolbar>`

#### Attributes

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `label` (required) | `string` |  | Names the toolbar. |
| `orientation` | `'horizontal' \| 'vertical'` | `'horizontal'` | Which arrow keys move between the controls. Where they sit is still up to your styles. |
| `version` | `string` |  |  |

#### Properties

| Name | Type | Description |
| --- | --- | --- |
| `items` (read-only) | `HTMLElement[]` |  |

#### Methods

| Signature | Description |
| --- | --- |
| `focus(options?: FocusOptions): void` |  |

#### Slots

| Name | Type | Description |
| --- | --- | --- |
| `(default)` | `Element` |  |

#### CSS parts

| Name | Description |
| --- | --- |
| `toolbar` | The controls |

## Copy and own it

`<slab-styled-toolbar>` 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.

### toolbar.ts

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

declare global {
  interface HTMLElementTagNameMap {
    'slab-styled-toolbar': StyledToolbar;
  }
}

/**
 * Toolbar, styled: a raised bar of controls, with a pressed one in ink.
 */
@customElement('slab-styled-toolbar')
export default class StyledToolbar extends Toolbar {
  static override styles = [tokens, styles];
}
```

### toolbar.styles.ts

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

/*
  The controls are the consumer's own buttons, slotted in, so they're styled
  through "::slotted()". A toggle's "aria-pressed" is what shows it's on.
*/
export default css`
  [part~='toolbar'] {
    background-color: var(--paper);
    border: var(--border);
    box-shadow: var(--shadow);
    display: flex;
    gap: 0.25rem;
    inline-size: fit-content;
    padding: 0.375rem;
  }

  :host([orientation='vertical']) [part~='toolbar'] {
    flex-direction: column;
  }

  ::slotted(button) {
    background-color: transparent;
    border: 2px solid transparent;
    color: var(--ink);
    cursor: pointer;
    font-family: var(--font-display);
    font-size: 0.9375rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    padding: 0.375rem 0.75rem;
    text-transform: uppercase;
    transition:
      background-color 100ms,
      color 100ms;
  }

  ::slotted(button:hover) {
    background-color: var(--chalk);
  }

  ::slotted(button:focus-visible) {
    outline: 3px solid var(--ember);
    outline-offset: 2px;
  }

  /*
    As specific as the hover above, and after it, so a pressed control keeps
    its ink while it's hovered. Were the hover to win, its chalk would sit
    under the pressed control's paper text.
  */
  ::slotted(button[aria-pressed='true']) {
    background-color: var(--ink);
    color: var(--paper);
  }

  ::slotted(button:disabled) {
    background-color: transparent;
    color: var(--ash);
    cursor: not-allowed;
    text-decoration: line-through;
  }

  @media (prefers-reduced-motion: reduce) {
    ::slotted(button) {
      transition: 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);
  }
`;
```
