Skip to content

HyToggleGroup

Usage examples on this page are written for Lit / plain HTML (<hy-toggle-group>). The same component ships as HyToggleGroup in @whitespaceux/harmony-react (native React 19), with typed wrappers for Angular, Solid, Svelte, and Vue. The API reference below applies to all frameworks.

A form-associated container component that manages a group of toggle buttons. Provides centralized selection state management, keyboard navigation, accessibility features, and form integration via ElementInternals API.

Supports two selection modes:

  • Single mode (default): Radio-group pattern — one toggle selected at a time. Arrow keys move focus and select. Uses role="radiogroup".
  • Multiple mode: Toolbar pattern — multiple toggles can be selected. Arrow keys move focus only. Space/Enter toggles selection. Uses role="toolbar".

Form Integration

Implements Form-Associated Custom Elements API via ElementInternals. In single mode, submits a single string value. In multiple mode, submits pipe-delimited values (e.g. "bold|italic").

Keyboard Navigation

  • Single mode: Arrow keys move focus and auto-select. Space selects focused item.
  • Multiple mode: Arrow keys move focus only. Space/Enter toggles focused item.
  • Home/End: Move focus to first/last item.
  • Tab: Enters/exits the group (roving tabindex).

Examples

Basic single selection

html
<hy-toggle-group value="center" label="Text alignment">
  <hy-toggle value="left">Left</hy-toggle>
  <hy-toggle value="center">Center</hy-toggle>
  <hy-toggle value="right">Right</hy-toggle>
</hy-toggle-group>

Inline layout

html
<div style="--hy-toggle-group-label-column-width: 6rem;">
  <hy-toggle-group layout="inline" label="Alignment" value="left">
    <hy-toggle value="left">Left</hy-toggle>
    <hy-toggle value="center">Center</hy-toggle>
    <hy-toggle value="right">Right</hy-toggle>
  </hy-toggle-group>
</div>

API

Properties

PropertyAttributeTypeDefaultDescription
selectionModeselection-mode'single' | 'multiple''single'Selection mode: 'single' (radio-like) or 'multiple' (checkbox-like).
valuevaluestring''The currently selected value(s). In single mode: a single string value. In multiple mode: pipe-delimited string (e.g. "bold|italic").
orientationorientation'horizontal' | 'vertical''horizontal'Layout direction of the toggle items.
sizesize'small' | 'medium' | 'large''medium'Size of all toggle items in the group.
variantvariant'neutral' | 'brand''neutral'Visual style (role) of the toggle items.
emphasisemphasis'solid' | 'soft' | 'outlined''soft'Visual chrome weight propagated to every child toggle. Orthogonal to variant (role).
disableddisabledbooleanfalseDisables all toggle items in the group.
requiredrequiredbooleanfalseAt least one value must be selected for form validation.
disallowEmptydisallow-emptybooleanfalsePrevents deselecting the last selected item.
namenamestring''Form submission name.
labellabelstring''Visible group label (also sets the accessible name via aria-labelledby).
helpTexthelp-textstring''Helper text below the toggle list. Hidden when errorMessage is shown.
errorMessageerror-messagestring''Error message below the toggle list — shown when invalid.
invalidinvalidbooleanfalseVisually marks the group as invalid.
layoutlayout'block' | 'inline''block'Row layout: block (default) stacks label above the toggle list; inline puts label on the left and list on the right. Helper/error always sit under the list (col 2–end), never under the label. Set --hy-toggle-group-label-column-width on a wrapping container so stacked groups align.
fullWidthfull-widthbooleanfalseStretch the group to fill its container's inline size, distributing the toggle segments equally (flex: 1 each) — a true segmented control rather than a content-width strip. Off by design; opt in for full-bleed rows.
looploopbooleantrueWhether keyboard navigation wraps around at the edges.
formHTMLFormElement | nullReturns the form element this group is associated with.
validityValidityStateReturns the ValidityState for this group.
validationMessagestringReturns the validation message for this group.

Events

EventDetailDescription
changeFired when selection changes. Detail: { selectedValues: string[], reason: 'user' | 'programmatic', name: string }
inputStandard form input event

Slots

SlotDescription
defaulthy-toggle elements

CSS Parts

PartDescription
baseOuter wrapper containing label, toggle list, and helper/error text
labelThe visible group label element
toggle-listThe container that holds the toggle items (role=radiogroup|toolbar)
helper-textHelper text below the list
error-messageError message below the list

CSS Custom Properties

PropertyDescription
--hy-toggle-group-child-radiusBorder radius for child toggle items (defaults to --hy-radius-action-md, the same family hy-toggle reads)
--hy-toggle-group-gap-printGap between toggle items in print
--hy-toggle-group-label-column-widthWidth of the label column in layout="inline" mode. No default — set on a wrapping container so stacked groups align.

Methods

checkValidity()

Checks validity against constraints.

reportValidity()

Checks validity and shows browser validation UI if invalid.

setCustomValidity()

Sets a custom validation message.

focus()

Sets focus on the selected toggle, or the first toggle if none selected.

Built with Lit. Documented with VitePress.