ToggleGroup
ToggleGroup controls a set of Toggle.Root buttons. The group permits one selection or more than one selection. It has roving focus, it obeys the disabled state, and its value is an array.
Anatomy
ToggleGroup.Root contains ToggleGroup.Item buttons. That part is Toggle.Root under the namespace of the group, and the two names are the same component. Each button must have a unique value. The group uses the value to record the selection.
<script>
import { ToggleGroup } from '@human-kit/ui';
</script>
<ToggleGroup.Root defaultValue={['bold']} selectionMode="multiple" aria-label="Text style">
<ToggleGroup.Item value="bold">Bold</ToggleGroup.Item>
<ToggleGroup.Item value="italic">Italic</ToggleGroup.Item>
</ToggleGroup.Root>Single selection
selectionMode="single" is the default. It keeps one value selected, or none. The value stays an array. Add disallowEmptySelection when one enabled toggle must stay selected.
Vertical orientation
orientation="vertical" moves the roving focus to the Up arrow key and the Down arrow key. The keyboard focus does not stop on a disabled toggle.
Usage guidelines
- Use
valueanddefaultValuearrays for a single selection and for more than one selection. - Use
selectionMode="single"when only one toggle can be selected. - Use
selectionMode="multiple"when more than one toggle can be selected. - Use
disallowEmptySelectionwhen one enabled toggle must stay selected. - Each button in the group must have a unique
value. - A toggle in a group ignores its own
selectedanddefaultSelectedprops.
Accessibility
ToggleGroup.Roothasrole="group".- Give the group an accessible name with
aria-labeloraria-labelledby. - Each
Toggle.Rootstays a native toggle button witharia-pressed. - The arrow keys move the focus through the enabled toggles. The
Homekey and theEndkey move the focus to the first toggle and to the last toggle.
API reference
Root
The group container. It makes a div with role="group". It controls the selection across the Toggle.Root buttons in it, and it controls the roving focus.
* required. Native HTML attributes of the underlying element are also accepted.
Item
One button of the group. It is Toggle.Root under the namespace of the group, and the two names are the same component.
* required. Native HTML attributes of the underlying element are also accepted.