# Toolbar

A row of related controls that is one stop in the tab order.

_Diagram: A toolbar is a row of related buttons at the top of the area it acts on, such as the formatting buttons above a text editor._

## Live example

### Text formatting

```html
<dui-toolbar role="toolbar" aria-label="Text formatting">
  <button type="button" aria-pressed="true">Bold</button>
  <button type="button" aria-pressed="false">Italic</button>
  <button type="button" aria-pressed="false">Underline</button>
  <hr>
  <button type="button">Insert link</button>
  <button type="button">Clear formatting</button>
</dui-toolbar>
```

### Vertical

```html
<dui-toolbar role="toolbar" aria-label="Drawing tools" aria-orientation="vertical">
  <button type="button" aria-pressed="true">Select</button>
  <button type="button" aria-pressed="false">Pen</button>
  <button type="button" aria-pressed="false">Eraser</button>
</dui-toolbar>
```

## Attributes

| Attribute or value | Where | Effect |
| --- | --- | --- |
| `role="toolbar" aria-label="…"` | the container (`<dui-toolbar>` or any element) |  |
| `aria-orientation="vertical"` |  | optional; default horizontal |
| `aria-pressed="true \| false"` | toggle buttons (native ARIA) |  |
| `<hr>` |  | a separator between groups of controls |

### Keyboard

| Key | Action |
| --- | --- |
| <kbd>Tab</kbd> | moves into the toolbar (to the last focused control), then out of it |
| <kbd>Right Arrow</kbd> / <kbd>Left Arrow</kbd> | next or previous control, wrapping (swapped in right-to-left) |
| <kbd>Down Arrow</kbd> / <kbd>Up Arrow</kbd> | the same in a vertical toolbar |
| <kbd>Home</kbd> / <kbd>End</kbd> | first or last control |
| <kbd>Enter</kbd> / <kbd>Space</kbd> | activates the focused button |

## Accessibility

- Use a toolbar for three or more controls. Without JavaScript, each control is a Tab stop.
- Toggle buttons use [`aria-pressed`](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-pressed), shown by more than color ([WCAG 1.4.1](https://www.w3.org/WAI/WCAG22/Understanding/use-of-color.html)).

## Specifications

- [APG Toolbar pattern](https://www.w3.org/WAI/ARIA/apg/patterns/toolbar/)
- [MDN `toolbar` role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/toolbar_role)
- [APG kbd roving tabindex](https://www.w3.org/WAI/ARIA/apg/practices/keyboard-interface/#kbd_roving_tabindex)

## See also

- [Molecules](https://domestic-ui.odonckers.dev/molecules/index.md)
- Source: [`css/components/molecules/toolbar.css`](https://github.com/odonckers/domestic-ui/blob/main/css/components/molecules/toolbar.css), [`examples/molecules/toolbar.html`](https://github.com/odonckers/domestic-ui/blob/main/examples/molecules/toolbar.html)
- Demos: Orders view ([Plain HTML](https://github.com/odonckers/domestic-ui/blob/main/demos/vanilla/orders.html), [React](https://github.com/odonckers/domestic-ui/blob/main/demos/react/src/OrdersPage.tsx), [Angular](https://github.com/odonckers/domestic-ui/tree/main/demos/angular/src/app/orders), [Astro](https://github.com/odonckers/domestic-ui/blob/main/demos/astro/src/pages/orders.astro), [Vue](https://github.com/odonckers/domestic-ui/blob/main/demos/vue/src/OrdersPage.vue), [Svelte](https://github.com/odonckers/domestic-ui/blob/main/demos/svelte/src/OrdersPage.svelte))

## How other systems do it

For comparison only; these are not the standard this system follows.

- [Apple Human Interface Guidelines: Toolbars](https://developer.apple.com/design/human-interface-guidelines/toolbars)
- [Material Design 3: Toolbars](https://m3.material.io/components/toolbars/overview)
- Vercel Geist: No equivalent
