ToggleButton
A button that switches between pressed and unpressed, e.g. Bold in a text toolbar. Built on Button: same appearances, sizes, type="button" default, decorative icon slot and icon-only warning. The pressed look uses the shared pressed colors (selected tokens; a Highlight outline in forced colors), or a brand fill with isAccessible.
- as renders another element or component with correctly typed props, as on Button (<ToggleButton as="a" href="/x">); the pressed attributes follow the element's role.
- Uncontrolled: defaultPressed; controlled: pressed + onPressedChange.
- A consumer onClick runs first; calling event.preventDefault() in it cancels the toggle.
- It reports its state with aria-pressed without a role or with role="button". With role checkbox, radio, switch, menuitemcheckbox, menuitemradio, option or treeitem it reports it with aria-checked (and data-checked while pressed) instead; with any other role it renders neither (development warning), because aria-pressed is allowed only on buttons. data-pressed is present while pressed in every case.
Examples
Default
export const Default: Story = {};Pressed
export const Pressed: Story = {
args: {
defaultPressed: true,
},
};With Icon
export const WithIcon: Story = {
args: {
icon: <BoldIcon />,
iconPosition: 'after',
},
};Icon Only
export const IconOnly: Story = {
args: {
children: undefined,
icon: <BoldIcon />,
'aria-label': 'Bold',
},
};Disabled
export const Disabled: Story = {
args: {
disabled: true,
},
};Disabled Pressed
export const DisabledPressed: Story = {
args: {
disabled: true,
defaultPressed: true,
},
};Accessible
export const Accessible: Story = {
args: {
isAccessible: true,
},
argTypes: {
appearance: { control: false },
},
render: (args) => (
<div className="grid grid-cols-2 items-center justify-items-start gap-2">
{appearances.map((appearance) => (
<div key={appearance} className="contents">
<ToggleButton {...args} appearance={appearance}>
{`${appearance} (off)`}
</ToggleButton>
<ToggleButton {...args} appearance={appearance} defaultPressed>
{`${appearance} (on)`}
</ToggleButton>
</div>
))}
</div>
),
};As Checkbox
export const AsCheckbox: Story = {
args: {
role: 'checkbox',
children: 'Show grid',
isAccessible: true,
},
};Controlled
export const Controlled: Story = {
render: function ControlledStory(args) {
const [pressed, setPressed] = useState(false);
return (
<div className="flex items-center gap-2">
<ToggleButton
{...args}
pressed={pressed}
onPressedChange={(next) => {
setPressed(next);
args.onPressedChange?.(next);
}}
>
{pressed ? 'On' : 'Off'}
</ToggleButton>
<Text>State: {pressed ? 'pressed' : 'not pressed'}</Text>
</div>
);
},
};Shapes
export const Shapes: Story = {
argTypes: {
shape: { control: false },
},
render: (args) => (
<div className="flex items-center gap-2">
<ToggleButton {...args} shape="rounded">
rounded
</ToggleButton>
<ToggleButton {...args} shape="circular" defaultPressed>
circular
</ToggleButton>
<ToggleButton {...args} shape="square">
square
</ToggleButton>
</div>
),
};As Div
export const AsDiv: Story = {
render: (args) => (
<ToggleButton
as="div"
appearance={args.appearance}
size={args.size}
onPressedChange={args.onPressedChange}
>
Bold (div)
</ToggleButton>
),
};Props
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
onChange | never | No | The native change event does not exist on a button: | |
pressed | boolean | No | Controlled pressed state. | |
defaultPressed | boolean | No | false | Initial pressed state for uncontrolled usage. |
onPressedChange | (pressed: boolean) => void | No | Called with the new pressed state when the user toggles the button. Fires only when the state changes (once per click, also in StrictMode). In controlled mode the button keeps showing | |
appearance | 'primary' | 'outline' | 'subtle' | 'transparent' | No | 'outline' | Visual style variant. |
size | 'extra-small' | 'small' | 'medium' | 'large' | 'extra-large' | No | 'medium' | Size affecting height, padding and font size. |
shape | 'circular' | 'square' | 'rounded' | No | 'rounded' | Corner shape, as on Button. |
icon | Slot<'span'> | No | Icon slot rendered before the label (after it with | |
iconPosition | 'before' | 'after' | No | 'before' | Where the icon renders: before the label (the inline start) or after it (the inline end). Follows the writing direction through DOM order. Has no effect on an icon-only toggle. |
disabledFocusable | boolean | No | false | Marks the toggle unavailable but keeps it focusable and in the tab order (see |
isAccessible | boolean | No | false | Draws the pressed state as a brand fill with on-brand text (on |
as | React.ElementType | No | 'button' | Render as a different element or component (its props are type-checked). |
Any other prop is passed to the element it renders (HTMLButtonElement).
Stable class names
| Part | Class |
|---|---|
root | wave-toggle-button |
icon | wave-toggle-button__icon |