# Toggle Group

Coordinate single or multiple pressed controls. (Custom element.)

Reference: https://timeless.build/docs/components/toggle-group/

## Markup

```html
<ui-toggle-group selection="single" orientation="horizontal" attached aria-label="Text alignment">
  <button class="ui-button ui-toggle" type="button" value="left" aria-pressed="true">Left</button>
  <button class="ui-button ui-toggle" type="button" value="center" aria-pressed="false">Center</button>
  <button class="ui-button ui-toggle" type="button" value="right" aria-pressed="false">Right</button>
</ui-toggle-group>
```

## Install

```js
import '@timelessui/components/css/tokens.css'
import '@timelessui/components/css/core/toggle.css'
import '@timelessui/components/css/core/button.css'
import '@timelessui/components/css/themes/atmosphere/tokens.css'
import '@timelessui/components/css/themes/atmosphere/button.css'
import '@timelessui/components/css/themes/atmosphere/toggle.css'
import '@timelessui/components/register/ui-toggle-group'
```

A `register/` import is a side effect: it defines the element as the module evaluates, so it
has to run in code the browser loads. To register explicitly instead — to control the timing,
or to define into another window — call the function from the matching `define/` entry point,
which registers nothing on its own:

```js
import { defineToggleGroupElement } from '@timelessui/components/define/ui-toggle-group'
defineToggleGroupElement()
```

## Anatomy

Parts are authored in your own markup and identified by the selector below. Required parts
must be present for the component to work. Private `data-ui-internal-*` hooks are written by
the runtime and must never be authored.

**<ui-toggle-group>**

| Part | Required | Selector | Purpose |
| --- | --- | --- | --- |
| `item` | Yes | `button[aria-pressed]` | One toggle button. Author `value` and `aria-pressed`; Timeless keeps the pressed set in sync. |

## Attributes

Every value below is implemented by the stylesheets this component ships. Boolean attributes
are presence-based: author the attribute with no value, or omit it.

**<ui-toggle-group>**

| Attribute | Values | Default | Description |
| --- | --- | --- | --- |
| `attached` | presence | absent | Present to join the buttons into one segmented control. Styling only, resolved by `themes/atmosphere/toggle.css`. |
| `orientation` | `horizontal` · `vertical` | `horizontal` | Layout and arrow-key axis. |
| `selection` | `single` · `multiple` | `single` | Whether pressing one button releases the others (`single`) or toggles independently (`multiple`). |

**ui-toggle**

| Attribute | Values | Default | Description |
| --- | --- | --- | --- |
| `data-ui-variant` | `primary` · `secondary` · `outline` · `ghost` · `danger` · `danger-outline` · `link` | `primary` | Visual intent, resolved by `themes/atmosphere/button.css`. Author `class="ui-button ui-toggle"` so the shared button styling applies. |
| `data-ui-size` | `sm` · `md` · `lg` | `md` | Control height, padding, and font size. Resolved by `themes/atmosphere/button.css`. |

## Element API

Attributes above are the authoring surface. These are the DOM properties and events the
registered element adds once it upgrades.

### `<ui-toggle-group>`

| Property | Type | Notes |
| --- | --- | --- |
| `orientation` | `'horizontal' | 'vertical'` | Reflects the `orientation` attribute |
| `selection` | `'single' | 'multiple'` | Reflects the `selection` attribute |

| Event | Detail | Description |
| --- | --- | --- |
| `ui-before-change` | `CustomEvent<ToggleGroupChangeDetail>` | Cancelable proposal dispatched before the pressed set changes. Call `preventDefault()` to reject the transition and keep the current value. |
| `ui-change` | `CustomEvent<ToggleGroupChangeDetail>` | Dispatched after the pressed set has changed. Bubbles and is composed. |

## State

Native attributes, ARIA, and platform pseudo-classes are authoritative. Style state through
the selectors below rather than adding your own state classes.

| State | Source | How to express it |
| --- | --- | --- |
| `pressed` | aria | Author `aria-pressed="true"` or `"false"`; never omit it. |
| `disabled` | native | Native `disabled`. |

## Styling

Root identity: `ui-toggle-group`, `ui-toggle`.
Required stylesheets: `tokens.css`, `core/toggle.css`, `core/button.css`, `themes/atmosphere/tokens.css`, `themes/atmosphere/button.css`, `themes/atmosphere/toggle.css`.

This component adds no custom properties of its own. Restyle it through the design tokens at https://timeless.build/docs/styling/theming/ or your own CSS.

Design tokens this component reads (24), global and set at the theme level: `--ui-accent`, `--ui-accent-soft`, `--ui-bg-accent`, `--ui-bg-accent-active`, `--ui-bg-accent-hover`, `--ui-bg-control`, `--ui-bg-control-active`, `--ui-bg-control-hover`, `--ui-bg-control-muted`, `--ui-bg-danger`, `--ui-bg-danger-active`, `--ui-bg-danger-hover`, `--ui-duration-fast`, `--ui-ease-standard`, `--ui-fg`, `--ui-focus`, `--ui-line`, `--ui-radius-control`, `--ui-shadow-control-accent`, `--ui-shadow-control-active`, `--ui-shadow-control-danger`, `--ui-shadow-control-hover`, `--ui-shadow-outline-control`, `--ui-space-2`.

## Accessibility

Keep the native elements, roles, and relationships shown in the markup. Timeless adds only the
state and keyboard coordination the platform does not already provide, and it never supplies
your accessible names — those depend on your content.

Follows the [Toolbar pattern](https://www.w3.org/WAI/ARIA/apg/patterns/toolbar/) from the ARIA Authoring Practices Guide.

| Key | Action |
| --- | --- |
| `Arrow keys` | Move focus to the previous or next button, following the orientation and skipping disabled ones. |
| `Home / End` | Move focus to the first or last enabled button. |

The Toolbar pattern rather than the Button pattern, which is what the host renders: `role="toolbar"`, roving `tabindex`, and the whole group as one tab stop. Each button keeps native activation, and `aria-pressed` carries the state — so an individual control is still a button, which is why the pattern link used to point there. With `selection="single"` pressing one button releases the others. Give the host an accessible name; a toolbar without one is indistinguishable from any other.

## Before JavaScript runs

The markup above is complete and usable on its own: native controls submit, labels associate, and authored ARIA is already correct. Registration adds state synchronisation, focus management, and keyboard coordination on top of it.
