# Web components

Each element's attributes, keyboard and accessibility are on the page of the pattern it enhances.

A few small [custom elements](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements),
each added only because native HTML cannot do the job. Before reaching for one, check whether an
element already covers it: `<dialog>`, `<details>`, the
[`popover`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/popover)
attribute and [invoker commands](https://developer.mozilla.org/en-US/docs/Web/API/Invoker_Commands_API)
handle many cases that used to need script, and most molecules need none.

## The rule

Every element here:

- exists because HTML or CSS cannot do something, and its header comment says what;
- enhances native markup that already works without JavaScript
  ([progressive enhancement](https://developer.mozilla.org/en-US/docs/Glossary/Progressive_Enhancement)):
  content stays readable and links, buttons and sections stay usable with the script off;
- uses the **light DOM**: no shadow root, so the [molecule CSS](https://github.com/odonckers/domestic-ui/blob/main/css/components/molecules/README.md)
  and your own CSS style the markup directly, and there is no second styling API;
- follows the matching [ARIA Authoring Practices Guide](https://www.w3.org/WAI/ARIA/apg/) pattern,
  and adds ARIA roles only when it also adds the keyboard behavior those roles promise;
- is one ES module with no dependencies and no build step, named with the `dui-` prefix, and
  documents its API, events and keyboard in its header comment.

Load one from a page with a module script; it upgrades matching elements already in the page and
any added later:

```html
<script type="module" src="web-components/dui-tabs.js"></script>
```

## Index

### `<dui-tabs>`

No tabs element: one panel at a time, roving tabindex and arrow keys ([APG Tabs](https://www.w3.org/WAI/ARIA/apg/patterns/tabs/)). Upgrades a `<nav>` of in-page links and `<section>` panels; `aria-current="true"` on a link selects it on load.

Styles: [tabs.css](https://domestic-ui.odonckers.dev/patterns/tabs.md). Example: [Tabs](https://domestic-ui.odonckers.dev/patterns/tabs.md). Source: [`dui-tabs.js`](https://github.com/odonckers/domestic-ui/blob/main/web-components/dui-tabs.js).

### `<dui-menu>`

`popovertarget` opens the list, but there is no menu widget: arrow keys, Home/End, typeahead, opening on a chosen item ([APG Menu Button](https://www.w3.org/WAI/ARIA/apg/patterns/menu-button/)).

Styles: [menu-button.css](https://domestic-ui.odonckers.dev/patterns/menu-button.md). Example: [Menu button](https://domestic-ui.odonckers.dev/patterns/menu-button.md). Source: [`dui-menu.js`](https://github.com/odonckers/domestic-ui/blob/main/web-components/dui-menu.js).

### `<dui-combobox>`

`<input list>` suggests values, but the browser draws the popup: it cannot be styled, filters differently per engine and announces no result count. Builds an APG list autocomplete listbox from the `<datalist>` and keeps the same input ([APG Combobox](https://www.w3.org/WAI/ARIA/apg/patterns/combobox/)). See [Combobox (typeahead) explainer](https://domestic-ui.odonckers.dev/explainers/combobox.md).

Styles: [combobox.css](https://domestic-ui.odonckers.dev/patterns/typeahead.md). Example: [Typeahead](https://domestic-ui.odonckers.dev/patterns/typeahead.md). Source: [`dui-combobox.js`](https://github.com/odonckers/domestic-ui/blob/main/web-components/dui-combobox.js).

### `<dui-toolbar>`

No roving focus in HTML: one Tab stop with arrow keys between controls ([APG Toolbar](https://www.w3.org/WAI/ARIA/apg/patterns/toolbar/)).

Styles: [toolbar.css](https://domestic-ui.odonckers.dev/patterns/toolbar.md). Example: [Toolbar](https://domestic-ui.odonckers.dev/patterns/toolbar.md). Source: [`dui-toolbar.js`](https://github.com/odonckers/domestic-ui/blob/main/web-components/dui-toolbar.js).

### `<dui-tooltip>`

A popover cannot open on hover or focus yet. [`interestfor`](https://open-ui.org/components/interest-invokers.explainer/) is the coming standard (Chromium only today); this element stands in until it is interoperable.

Styles: [popover.css](https://domestic-ui.odonckers.dev/patterns/popover.md). Example: [Popover](https://domestic-ui.odonckers.dev/patterns/popover.md). Source: [`dui-tooltip.js`](https://github.com/odonckers/domestic-ui/blob/main/web-components/dui-tooltip.js).

### `<dui-toaster>`

A live region announces messages, but nothing builds a toast, adds its Dismiss button and moves focus sensibly when one goes away. See [Toast explainer](https://domestic-ui.odonckers.dev/explainers/toast.md).

Styles: [alerts.css](https://domestic-ui.odonckers.dev/patterns/alerts.md). Example: [Alerts](https://domestic-ui.odonckers.dev/patterns/alerts.md). Source: [`dui-toaster.js`](https://github.com/odonckers/domestic-ui/blob/main/web-components/dui-toaster.js).

### `<dui-app-shell>`

A button cannot hide another element and keep `aria-expanded` in step, and a sidebar that is part of the page when wide must become a modal drawer only when the shell is narrow. Toggles `hidden` on a sidebar beside main, moves the one copy of a sidebar into a `<dialog data-dialog="drawer">` (`showModal()`) while it is open on a narrow shell, and switches mode on the shell's own width ([`ResizeObserver`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver)); toggles are `hidden` until it upgrades. See [App shell explainer](https://domestic-ui.odonckers.dev/explainers/app-shell.md).

Styles: [app-shell.css](https://domestic-ui.odonckers.dev/patterns/app-shell.md) (layout). Example: [App shell](https://domestic-ui.odonckers.dev/patterns/app-shell.md). Source: [`dui-app-shell.js`](https://github.com/odonckers/domestic-ui/blob/main/web-components/dui-app-shell.js).

### `<dui-table>`

A table cannot reorder its rows, and a checkbox cannot check its siblings, count them or be indeterminate from markup. Adds sortable column buttons with `aria-sort` ([APG Sortable Table](https://www.w3.org/WAI/ARIA/apg/patterns/table/examples/sortable-table/)), a "select all" checkbox, the selection count and bulk-action enabling to a native `<table>`; without it the table is a plain table with ordinary checkboxes.

Styles: [data-table.css](https://domestic-ui.odonckers.dev/patterns/data-table.md). Example: [Data table](https://domestic-ui.odonckers.dev/patterns/data-table.md). Source: [`dui-table.js`](https://github.com/odonckers/domestic-ui/blob/main/web-components/dui-table.js).

## Successors

Each element is a bridge until the platform can do its job. Its header comment names the feature that would
replace it and the condition that retires it; retiring one is a breaking change
([Versioning](https://domestic-ui.odonckers.dev/contributing.md#versioning), [architecture](https://github.com/odonckers/domestic-ui/blob/main/docs/ARCHITECTURE.md#7-web-components)).

| Element | Platform successor | Retire when |
| --- | --- | --- |
| `<dui-tooltip>` | [`interestfor`](https://open-ui.org/components/interest-invokers.explainer/) | `interestfor` is Baseline newly available |
| `<dui-toolbar>` | [`focusgroup`](https://open-ui.org/components/focusgroup.explainer/) | `focusgroup` is Baseline newly available |
| `<dui-tabs>` | `focusgroup` for the arrow keys; no tabs element is proposed | The keys move to `focusgroup`; the element stays for selection |
| `<dui-menu>` | The Open UI [menu elements](https://open-ui.org/components/menu.explainer/) proposal | A native menu element is Baseline newly available |
| `<dui-combobox>` | None for free text; [customizable select](https://developer.mozilla.org/en-US/docs/Learn_web_development/Extensions/Forms/Customizable_select) for pick-one lists | Kept; pick-one uses move to `<select>` |
| `<dui-app-shell>` | Container queries cover the layout; nothing moves one node into a modal | Kept |
| `<dui-table>` | None proposed | Kept |
| `<dui-toaster>` | None active | Kept |

## Events

| Element | Event | When |
| --- | --- | --- |
| `<dui-tabs>` | `dui-tabs-change` | After the selected tab changes; `event.detail` is `{ tab, panel }`. Bubbles. |
| `<dui-combobox>` | `dui-combobox-filter` | On every input, before filtering; `event.detail` is `{ query }`. Bubbles; cancelable: cancel it to load options yourself, then call `setOptions()`. |
| `<dui-combobox>` | `change` (on the input) | When an option is accepted with Enter or a click, as a native control would. |
| `<dui-toaster>` | `dui-toaster-dismiss` | Before a toast is removed. Bubbles; cancelable. |
| `<dui-app-shell>` | `dui-app-shell-toggle` | After a sidebar is expanded, collapsed, opened or closed; `event.detail` is `{ sidebar, edge, expanded, drawer }`. Bubbles. |
| `<dui-table>` | `dui-table-sort` | Before the rows move; `event.detail` is `{ column, index, direction }` (the `<th>`, its column index, `"ascending"` or `"descending"`). Bubbles; cancelable: cancel it to sort on the server, then set `aria-sort` and replace the rows yourself. |

`<dui-menu>`, `<dui-toolbar>` and `<dui-tooltip>` fire no events of their own (nor does `<dui-table>` for selection: listen for `change` on its checkboxes): listen for the native
`click` on items and controls, and the native `toggle` on popovers.
