Inputs
ToggleGroup A compound component for managing single or multiple selection across a set of toggle buttons.
npm pnpm yarn bun
npm install @morphos/inputs
import { ToggleGroup, ToggleGroupItem } from '@morphos/inputs'
Component Description ToggleGroupRoot container managing selection state, rendered with role="group" ToggleGroupItemIndividual toggle button within the group
@ Component ()
class MyComponent extends StatefulComponent {
@ State () group = new ToggleGroup ({ type: 'single' , defaultValue: 'left' , orientation: 'horizontal' })
render () {
return (
< ToggleGroup class = "morphos-toggle-group" type = "single" orientation = "horizontal" aria-label = "Text alignment" >
< ToggleGroupItem class = "morphos-toggle-group-item" group = { this .group} value = "left" aria-label = "Align left" >
Left
</ ToggleGroupItem >
< ToggleGroupItem class = "morphos-toggle-group-item" group = { this .group} value = "center" aria-label = "Align center" >
Center
</ ToggleGroupItem >
< ToggleGroupItem class = "morphos-toggle-group-item" group = { this .group} value = "right" aria-label = "Align right" >
Right
</ ToggleGroupItem >
</ ToggleGroup >
)
}
}
The group prop on each ToggleGroupItem must be the same ToggleGroup instance backing the rendered <ToggleGroup> root — ToggleGroupItem reads group.isPressed(value) and calls group.toggle(value) directly on it.
@ Component ()
class MyComponent extends StatefulComponent {
@ State () group = new ToggleGroup ({ type: 'multiple' , defaultValue: [ 'bold' ] })
render () {
return (
< ToggleGroup class = "morphos-toggle-group" type = "multiple" aria-label = "Text formatting" >
< ToggleGroupItem class = "morphos-toggle-group-item" group = { this .group} value = "bold" aria-label = "Bold" >
< strong >B</ strong >
</ ToggleGroupItem >
< ToggleGroupItem class = "morphos-toggle-group-item" group = { this .group} value = "italic" aria-label = "Italic" >
< em >I</ em >
</ ToggleGroupItem >
< ToggleGroupItem class = "morphos-toggle-group-item" group = { this .group} value = "underline" aria-label = "Underline" >
< u >U</ u >
</ ToggleGroupItem >
</ ToggleGroup >
)
}
}
@ Component ()
class MyComponent extends StatefulComponent {
@ State () group = new ToggleGroup ({ type: 'single' })
@ State () layout = 'grid'
render () {
return (
< ToggleGroup
class = "morphos-toggle-group"
type = "single"
value = {() => this .layout}
onValueChange = {( value ) => ( this .layout = value as string )}
aria-label = "View layout"
>
< ToggleGroupItem class = "morphos-toggle-group-item" group = { this .group} value = "list" aria-label = "List view" >
List
</ ToggleGroupItem >
< ToggleGroupItem class = "morphos-toggle-group-item" group = { this .group} value = "grid" aria-label = "Grid view" >
Grid
</ ToggleGroupItem >
</ ToggleGroup >
)
}
}
Prop Type Default Description type"single" | "multiple"— Selection mode: one or many (required) valuestring | string[]— Controlled selected value(s) defaultValuestring | string[]— Uncontrolled initial value(s) — resolves to [] when type is "multiple" and no default is given disabledbooleanfalseDisables all items in the group orientation"horizontal" | "vertical""horizontal"Sets aria-orientation and data-orientation onValueChange(value: string | string[]) => void— Fires when selection changes classstring— Additional CSS classes idstring— HTML id attribute childrenChildren— The ToggleGroupItem items aria-labelstring— Accessible label for the group container aria-labelledbystring— ID of an element that labels the group
Method Description isPressed(value: string)Returns whether value is currently selected (in the array for "multiple", or the equal value for "single"). toggle(value: string)Selects/deselects value according to type. Emits onValueChange with the new value.
Prop Type Default Description groupToggleGroup— The parent ToggleGroup instance (required) valuestring— The value this item represents (required) disabledboolean— Overrides the group's disabled for this item only childrenChildren— Content rendered inside the button classstring— Additional CSS classes idstring— HTML id attribute aria-labelstring— Accessible label for this item aria-labelledbystring— ID of an element that labels this item
Attribute Element When present data-typeGroup root Always — value is "single" or "multiple" data-orientationGroup root Always — value is "horizontal" or "vertical" data-disabledGroup root disabled is truedata-pressedItem root Item is in the current selection data-disabledItem root Group's disabled or the item's own disabled is true
ToggleGroup renders a <div role="group"> with aria-orientation and aria-disabled. Each ToggleGroupItem renders a <button> with aria-pressed reflecting whether its value is in the current selection.
ToggleGroup and ToggleGroupItem do not implement roving-tabindex or arrow-key navigation — each item button is independently focusable via Tab, and pressing Space/Enter on a focused button activates it natively. If you need arrow-key movement between items, wire it up yourself.
.morphos-toggle-group [ data-orientation = "vertical" ] {
flex-direction : column ;
}
.morphos-toggle-group-item [ data-pressed ] {
background : var ( --morphos-color-accent );
color : var ( --morphos-color-accent-text );
}
.morphos-toggle-group-item [ data-disabled ] {
opacity : 0.5 ;
cursor : not-allowed ;
}