Toggle pattern
Toggle
Section titled “Toggle”The toggle pattern wires ARIA and visibility state for show/hide interactions. It powers every other context, so getting comfortable with toggle unlocks the entire toolkit. The Contexts guide shows how dialog, tooltip, and upcoming composites layer additional semantics on top of this foundation.
Behavior highlights
Section titled “Behavior highlights”- Adds
aria-expandedandaria-controlsto triggers. - Generates IDs when missing and links targets with
aria-labelledby. - Hides targets by default and removes
hiddenwhen opened. - Emits lifecycle events (
automagica11y:ready,automagica11y:toggle,automagica11y:toggle:opened,automagica11y:toggle:closed). - Normalizes truthiness so
active,open,true, andonall map to the open state.
Attribute reference
Section titled “Attribute reference”| Attribute | Type | Allowed values | Default | Description |
|---|---|---|---|---|
data-automagica11y-toggle | string | Selector | CSS selector, typically an #id | None | Identifies the target surface to show or hide. |
data-automagica11y-trigger-class-[state] | string | string[] | Any truthiness token (open, expanded, active, shown, true, on...) | automagic-toggle-open / automagic-toggle-closed | Maps classes onto the trigger for truthy/falsy states. |
data-automagica11y-target-class-[state] | string | string[] | Any truthiness token | None | Applies classes to the target element during each state. |
data-automagica11y-animate | `target` | `trigger` | Selector | `target`, `trigger`, or any CSS selector | `target` | Opt into the built-in animate lifecycle so close transitions wait for CSS to finish. |
Keyboard support
Section titled “Keyboard support”- Space/Enter toggle the target for non-button triggers (buttons work out of the box).
- Focus stays on the trigger when toggling so keyboard users keep context.
- Non-button triggers receive
role="button"andtabindex="0"automatically.
Events
Section titled “Events”| Event | Detail payload |
|---|---|
automagica11y:ready | { trigger, target, targets } once initialization completes. |
automagica11y:toggle | { trigger, target, targets, expanded } on every state change. |
automagica11y:toggle:opened | { trigger, target, targets } after an open transition completes. |
automagica11y:toggle:closed | { trigger, target, targets } after a close transition completes. |
Toggle playground
Truthiness-aware classes and lifecycle events.
View source
The toggle pattern sets aria-expanded and keeps attributes in sync.
<button data-automagica11y-toggle="#panel" data-automagica11y-trigger-class-open="toggle--active">Toggle content</button> Inspect events
Open your devtools console and listen for automagica11y:toggle to react to each state change.
Coverage plan
Section titled “Coverage plan”- Add a live demo combining toggle with
data-automagica11y-context="menu"once menu semantics ship. - Document how grouped toggles (
data-automagica11y-group) coordinate state across triggers. - Provide recipes showing how the animate lifecycle and announce plugin extend toggle events.
Track plan progress in Core modules and the Examples roadmap.