Skip to content

ToggleButton

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

State: not pressed
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

Bold (div)
export const AsDiv: Story = {
  render: (args) => (
    <ToggleButton
      as="div"
      appearance={args.appearance}
      size={args.size}
      onPressedChange={args.onPressedChange}
    >
      Bold (div)
    </ToggleButton>
  ),
};

Props

NameTypeRequiredDefaultDescription
onChangeneverNo

The native change event does not exist on a button: onChange stays a type error, as it was before ToggleButton became polymorphic. Use onPressedChange.

pressedbooleanNo

Controlled pressed state.

defaultPressedbooleanNofalse

Initial pressed state for uncontrolled usage.

onPressedChange(pressed: boolean) => voidNo

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 pressed until the parent passes the new value.

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.

iconSlot<'span'>No

Icon slot rendered before the label (after it with iconPosition="after"). Decorative: it renders with aria-hidden="true" (a slot object can override it). An icon-only toggle needs aria-label, aria-labelledby or title.

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.

disabledFocusablebooleanNofalse

Marks the toggle unavailable but keeps it focusable and in the tab order (see Button.disabledFocusable): aria-disabled="true", data-disabled and data-disabled-focusable instead of the native disabled attribute. Clicks, Enter and Space neither toggle it nor call onClick or onPressedChange; a pressed toggle shows the pressed and disabled look. Wins over disabled when both are set. In a Toolbar it stays in the arrow-key order.

isAccessiblebooleanNofalse

Draws the pressed state as a brand fill with on-brand text (on primary, the pressed fill with an inset on-brand stroke), so the state never depends on a light tint (Fluent's isAccessible). Recommended for icon-only toggles in toolbars. Forced colors are unchanged.

asReact.ElementTypeNo'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

PartClass
rootwave-toggle-button
iconwave-toggle-button__icon