Skip to content

Toggle Group

View as Markdown

Coordinate single or multiple pressed controls. Custom element

Toggle Group
Copy this markup
<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

Styles and registration
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:

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

Anatomy

Parts are authored in your own markup and identified by the selector below, so your CSS can target the same anatomy without loading Timeless styles. 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>

PartRequiredSelectorPurpose
itemYesbutton[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>

AttributeValuesDefaultDescription
attachedpresenceabsentPresent to join the buttons into one segmented control. Styling only, resolved by themes/atmosphere/toggle.css.
orientationhorizontal · verticalhorizontalLayout and arrow-key axis.
selectionsingle · multiplesingleWhether pressing one button releases the others (single) or toggles independently (multiple).

ui-toggle

AttributeValuesDefaultDescription
data-ui-variantprimary · secondary · outline · ghost · danger · danger-outline · linkprimaryVisual intent, resolved by themes/atmosphere/button.css. Author class="ui-button ui-toggle" so the shared button styling applies.
data-ui-sizesm · md · lgmdControl 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>

PropertyTypeNotes
orientation'horizontal' | 'vertical'Reflects the orientation attribute, documented above.
selection'single' | 'multiple'Reflects the selection attribute, documented above.
EventDetailDescription
ui-before-changeCustomEvent<ToggleGroupChangeDetail>Cancelable proposal dispatched before the pressed set changes. Call preventDefault() to reject the transition and keep the current value.
ui-changeCustomEvent<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.

StateSourceHow to express it
pressedariaAuthor aria-pressed="true" or "false"; never omit it.
disablednativeNative 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 or your own CSS.

Design tokens this component reads (24)

These are global Atmosphere tokens. Change them once at the theme level rather than per component.

--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 example. 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 from the ARIA Authoring Practices Guide.

KeyAction
Arrow keysMove focus to the previous or next button, following the orientation and skipping disabled ones.
Home / EndMove 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.

More examples

Open Toggle Group in StoryLite for variants, controls, and manual testing.