# Toggle (`toggle`)

Single pressable mode control (Bold / Italic toolbar style). State is aria-pressed on the button — pair with toggle-group for exclusive segments.

> **Layer:** L1 surface · **Recipe:** _(unset — see docs/agent/pick-a-surface.md)_
> Curriculum: `AGENTS.md` · pick matrix: `docs/agent/pick-a-surface.md` · blast radius: `CONSUMER_MAP.md`

> **Dialect:** Partial below is **unprefixed** (gallery / standalone HM). DOM contract Python often uses the **source token** `data-dz-*` / `dz-*` (Dazzle dual-lock). Match the CSS/JS bundle you load.

> **Demo vs contract:** Live gallery behaviour may use `/mock/*` or flash toasts. Those are **offline demos only** — implement **Server exchange** + **DOM contract**, not the mock. See AGENTS.md › Gallery demos.

## Copy this

```html
<div class="hm-demo-row" role="group" aria-label="Text style">
  <button type="button" class="toggle" data-toggle aria-pressed="true"><strong>B</strong> Bold</button>
  <button type="button" class="toggle" data-toggle aria-pressed="false"><em>I</em> Italic</button>
  <button type="button" class="toggle" data-toggle aria-pressed="false" data-size="sm"><span style="text-decoration:underline">U</span></button>
</div>
```

## Server exchange

This Hyperpart has **no server exchange** — presentation or client chrome only. If you put `hx-*` on a control that uses this markup, that action's exchange belongs to the action, not this part.

## Swap contract

Agent-visible HTMX topology (ADR-0054 / decision 0012). **Exchange envelope** = what the response may re-emit relative to the persistent slot (`body_only` | `outer` | `none` | `host_owned` | `document`). Dual-lock validates part markup only — not this envelope. Stem: `stems/morph-safe-hypermedia.md`.

**No host HTMX exchange** on this part — presentation or client chrome only. **Exchange envelope:** `n/a`.

If a **host** wraps this markup in `hx-*`, **that host owns the swap contract** (sole identity + envelope). Prefer `innerMorph` / `outerMorph` for stable slots; replacement for flash; body-only responses under inner swaps.

### Envelope response examples

What the **server returns** for each exchange. Match the **exchange envelope**; dual-lock still applies to interior markup.

This part has no owned exchange (envelope `n/a`). If a host adds `hx-*`, that host’s envelope applies — typically `body_only`:

```html
<!-- Prefer hx-swap=innerMorph into a stable body slot -->
<div class="dz-stack">content…</div>
```

Do **not** re-own the slot:

```html
<div id="toggle-root" data-dz-region>…</div>
```

## How to use it

### Seams

- button root carries data-dz-toggle (gallery: data-toggle)
- aria-pressed is the live state; SSR may set the initial value
- dz-toggle.js flips aria-pressed on click (no round-trip)
- named hidden under [data-dz-field-widget=toggle] syncs to aria-pressed

### Do / Don't

| Do | Don't |
|---|---|
| flip aria-pressed client-side for toolbar mode controls | require a server exchange to press Bold/Italic |
| pair multiple independent toggles in a toolbar row | make one toggle uncheck siblings (that is toggle-group) |

### Pitfalls

- do not use toggle for form booleans — use switch/checkbox
- do not invent exclusive group logic here — use toggle-group radios
- skip disabled / aria-disabled buttons and data-dz-widget bridges

### Keyboard / AT

- aria-pressed announces the mode to AT
- Space/Enter activate the button (native button semantics)

### Related parts

- `toggle-group` — agents/toggle-group.md
- `button` — agents/button.md
- `toolbar` — agents/toolbar.md

## DOM contract

What emitted markup must satisfy (CI: `tests/test_contracts.py`). Do not invent attrs outside the tables. Python modules under `contracts/` are **package-internal dual-locks** (`from contracts._kit import …`) — not FastAPI business handlers. App servers implement **Server exchange** endpoints; this section constrains the HTML those endpoints return.

### `contracts/toggle.py`

- **Required root:** `[data-dz-toggle]` (part `toggle`)

| Node | Attr | Constraint |
|---|---|---|
| `[data-dz-toggle]` | `data-dz-toggle` | present (any value) |

#### Module source

Monorepo dual-lock only — import `contracts._kit` from the HM package. Do not paste into app route modules.

```python
"""HYPERPART: toggle — pressable mode control (toolbar style).

Dual-lock unit is the toggle button root. Label content and size modifiers
are host-owned. Selector ``[data-dz-toggle]`` is the stable substrate root
(gallery CSS; state is ``aria-pressed``).

Emitter: ``widget=toggle`` → ToggleField / free Toggle fragment
(``button.dz-toggle[data-dz-toggle]`` + aria-pressed; ``dz-toggle.js``).
Form hosts may wrap a named hidden carrier in ``[data-dz-field-widget=toggle]``;
the controller syncs that carrier to ``aria-pressed`` on click.
"""

from contracts._kit import DomContract, Node, Present

DOM_CONTRACT = DomContract(
    part="toggle",
    root="[data-dz-toggle]",
    nodes=(
        Node(
            "[data-dz-toggle]",
            attrs={"data-dz-toggle": Present()},
        ),
    ),
)

__all__ = ["DOM_CONTRACT"]
```

## Notes

Distinct from switch (form boolean) and toggle-group (exclusive radios). Initial aria-pressed may come from SSR; dz-toggle.js flips it on click and syncs a named hidden carrier when the host is [data-dz-field-widget=toggle]. Dual-lock root [data-dz-toggle] (HMC-130).

## Source files

- `site/registry.py` (partial + exchanges + guidance)
- `contracts/toggle.py`
- `controllers/dz-toggle.js`
