Skip to content

ToggleButton

Button that toggles between selected and unselected states.

function Default() {
const [isSelected, setIsSelected] = useState(false);
return (
<ToggleButton
isSelected={isSelected}
label="Toggle"
onChange={setIsSelected}
/>
);
}
function WithIcon() {
const [isSelected, setIsSelected] = useState(false);
return (
<ToggleButton
icon={Star}
isSelected={isSelected}
label="Favorite"
onChange={setIsSelected}
selectedIcon={Heart}
/>
);
}
function IconOnly() {
const [isSelected, setIsSelected] = useState(false);
return (
<ToggleButton
icon={Star}
isIconOnly
isSelected={isSelected}
label="Favorite"
onChange={setIsSelected}
/>
);
}
<div style={{display: 'flex', gap: '1rem', alignItems: 'center'}}>
<ToggleButton icon={Star} label="Small" size="sm" />
<ToggleButton icon={Star} label="Medium" size="md" />
<ToggleButton icon={Star} label="Large" size="lg" />
</div>
<ToggleButton label="Syncing" isLoading />
function WithTooltip() {
const [isSelected, setIsSelected] = useState(false);
return (
<ToggleButton
icon={Star}
isIconOnly
isSelected={isSelected}
label="Favorite"
onChange={setIsSelected}
tooltip="Add to favorites"
/>
);
}
<ToggleButton label="Disabled" isDisabled />
function SingleGroup() {
const [value, setValue] = useState<string | null>('left');
return (
<ToggleButtonGroup
label="Alignment"
onChange={setValue}
type="single"
value={value}>
<ToggleButton
icon={AlignLeft}
isIconOnly
label="Align left"
value="left"
/>
<ToggleButton
icon={AlignCenter}
isIconOnly
label="Align center"
value="center"
/>
<ToggleButton
icon={AlignRight}
isIconOnly
label="Align right"
value="right"
/>
</ToggleButtonGroup>
);
}
function MultipleGroup() {
const [value, setValue] = useState<string[]>(['bold']);
return (
<ToggleButtonGroup
label="Formatting"
onChange={setValue}
type="multiple"
value={value}>
<ToggleButton icon={Bold} isIconOnly label="Bold" value="bold" />
<ToggleButton icon={Italic} isIconOnly label="Italic" value="italic" />
<ToggleButton
icon={Underline}
isIconOnly
label="Underline"
value="underline"
/>
</ToggleButtonGroup>
);
}
function VerticalGroup() {
const [value, setValue] = useState<string | null>('left');
return (
<ToggleButtonGroup
label="Alignment"
onChange={setValue}
orientation="vertical"
type="single"
value={value}>
<ToggleButton
icon={AlignLeft}
isIconOnly
label="Align left"
value="left"
/>
<ToggleButton
icon={AlignCenter}
isIconOnly
label="Align center"
value="center"
/>
<ToggleButton
icon={AlignRight}
isIconOnly
label="Align right"
value="right"
/>
</ToggleButtonGroup>
);
}
function DisabledGroup() {
const [value, setValue] = useState<string | null>('bold');
return (
<ToggleButtonGroup
isDisabled
label="Formatting"
onChange={setValue}
type="single"
value={value}>
<ToggleButton icon={Bold} isIconOnly label="Bold" value="bold" />
<ToggleButton icon={Italic} isIconOnly label="Italic" value="italic" />
<ToggleButton
icon={Underline}
isIconOnly
label="Underline"
value="underline"
/>
</ToggleButtonGroup>
);
}
<div style={{display: 'flex', flexDirection: 'column', gap: '1rem'}}>
{(['sm', 'md', 'lg'] as const).map(size => (
<ToggleButtonGroup
key={size}
label={`Alignment (${size})`}
onChange={() => {}}
size={size}
value="left">
<ToggleButton
icon={AlignLeft}
isIconOnly
label="Align left"
value="left"
/>
<ToggleButton
icon={AlignCenter}
isIconOnly
label="Align center"
value="center"
/>
<ToggleButton
icon={AlignRight}
isIconOnly
label="Align right"
value="right"
/>
</ToggleButtonGroup>
))}
</div>

Button that toggles between selected and unselected states.

PropTypeDefaultDescription
classNamestringAdditional CSS class names applied to the button root.
data-testidstringTest ID applied to the button root.
iconIconComponentIcon element rendered before the label.
isDisabledbooleanfalseWhether the button is disabled.
isIconOnlybooleanfalseWhether to visually hide the label and render a square icon button.
isLoadingbooleanfalseWhether the button is loading.
isSelectedbooleanfalseWhether the button is currently selected.
label*stringAccessible label for the button.
onChange( isSelected: boolean, event: MouseEvent<HTMLButtonElement>, ) => voidCalled when the selected state should change. Receives the next selected state and the originating click event.
refRef<HTMLButtonElement>Ref forwarded to the button root.
selectedIconIconComponentIcon element rendered when the button is selected.
size"sm" | "md" | "lg"Visual size of the button.
styleCSSPropertiesInline styles applied to the button root.
tooltipstringTooltip text shown on hover.
valuestringValue identifier when used inside ToggleButtonGroup.
formstringHTML form attribute associating the button with a <form> by ID.
idstringHTML id attribute applied to the root element.
aria-controlsstringIdentifies the element(s) whose contents are controlled by the button.
aria-describedbystringIdentifies the element(s) that describe the button.
aria-detailsstringIdentifies the element that provides a detailed description.
aria-expandedbooleanIndicates whether a controlled element is expanded or collapsed.
aria-haspopupboolean | 'menu' | 'listbox' | 'tree' | 'grid' | 'dialog'Indicates the button opens an interactive popup element.
aria-keyshortcutsstringKeyboard shortcuts that activate or focus the button.
aria-labelledbystringIdentifies the element(s) that label the button.
onFocusFocusEventHandler<HTMLElement>Focus event handler for the root element.
onKeyDown(event: KeyboardEvent<HTMLElement>) => voidKeyboard event handler for the root element.

Groups related ToggleButtons and manages shared selection state.

When type: "single"

PropTypeDefaultDescription
onChange*( value: string | null, event: MouseEvent<HTMLButtonElement>, ) => voidCalled with the selected value when selection changes, or null when the active button is deselected. Also receives the originating click event.
type'single''single'Single-selection mode. Clicking the active button clears selection.
value*string | nullCurrently selected value, or null for no selection.
children*ReactNodeToggle button children.
classNamestringAdditional CSS class names applied to the group root.
data-testidstringTest ID applied to the group root.
isDisabledbooleanfalseWhether all buttons in the group are disabled.
label*stringAccessible label for the group.
orientation"horizontal" | "vertical"'horizontal'Group orientation.
refRef<HTMLDivElement>Ref forwarded to the group root.
size"sm" | "md" | "lg"Default size for buttons in the group.
styleCSSPropertiesInline styles applied to the group root.

When type: "multiple"

PropTypeDefaultDescription
onChange*(value: string[], event: MouseEvent<HTMLButtonElement>) => voidCalled with the array of selected values when selection changes. Also receives the originating click event.
type*'multiple'Multiple-selection mode.
value*string[]Currently selected values.
children*ReactNodeToggle button children.
classNamestringAdditional CSS class names applied to the group root.
data-testidstringTest ID applied to the group root.
isDisabledbooleanfalseWhether all buttons in the group are disabled.
label*stringAccessible label for the group.
orientation"horizontal" | "vertical"'horizontal'Group orientation.
refRef<HTMLDivElement>Ref forwarded to the group root.
size"sm" | "md" | "lg"Default size for buttons in the group.
styleCSSPropertiesInline styles applied to the group root.