Skip to content

Toggle button API

Cimpress UI exports three toggle-button-related components:

  • ToggleButton: displays a toggle button that has a visible label
  • ToggleIconButton: displays a toggle button that has no visible label (icon-only)
  • ToggleButtonGroup: container component that groups related toggle buttons together, providing layout and shared state management.
import { UNSTABLE_ToggleButton, UNSTABLE_ToggleButtonGroup } from '@cimpress-ui/react';

This component can be used in a controlled or uncontrolled way.

In the controlled way, this component doesn’t maintain its own internal state, and its value is provided by the parent component. Use the controlled way when you need to be able to change the state of this component in other parts of your application.

In the uncontrolled way, this component maintains its own internal state, and can optionally notify the parent component when its internal state changes. Use the uncontrolled way when you don’t need to change the state of this component in other parts of your application.

The toggle button state can be changed by pressing Enter or Space, similar to a native HTML checkbox.

ToggleButtonGroup is a single tab stop within the page’s tab order. Use the Tab key to navigate to the group, and the arrow keys to navigate between toggle buttons within the group.

ToggleButton requires a textual label to remain accessible to assistive technologies. ToggleIconButton and ToggleButtonGroup should have an aria-label prop defined to identify the function of the button to assistive technologies. See our accessibility guide for more details.

The toggle button label must remain the same regardless of the selection state.

Selection state of toggle buttons within ToggleButtonGroup cannot be set individually. Please use ToggleButtonGroup’s selectedKeys/defaultSelectedKeys props instead.

// This won't work! :(
<ToggleButtonGroup aria-label="Text alignment">
<ToggleButton id="left">Left</ToggleButton>
<ToggleButton id="center">Center</ToggleButton>
<ToggleButton id="right" defaultSelected>Right</ToggleButton>
</ToggleButtonGroup>
// But this will :)
<ToggleButtonGroup aria-label="Text alignment" defaultSelectedKeys={['right']}>
<ToggleButton id="left">Left</ToggleButton>
<ToggleButton id="center">Center</ToggleButton>
<ToggleButton id="right">Right</ToggleButton>
</ToggleButtonGroup>

Displays a labelled button that allows users to toggle between two states. Can be used standalone, or as part of ToggleButtonGroup.

See toggle button usage guidelines.

ref Ref<HTMLButtonElement>

The ref type for this component.

UNSTABLE_ToggleButtonProps
children * StringLikeChildren

The text displayed on the button.

size 'small' | 'medium' | 'large'

The size of the button.

Defaults to 'medium' .
fullWidth boolean

Whether the button should take up the whole available width.

Defaults to false .
iconStart ReactNode

An icon displayed before the button text.

iconEnd ReactNode

An icon displayed after the button text.

id Key

An identifier for this button when used in a ToggleButtonGroup. Has to be unique across all buttons in the group.

data-cim-style-root boolean

Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.

UNSAFE_className string

Sets the CSS className for the element. Only use as a last resort. Use style props instead.

See styling guide.

UNSAFE_style CSSProperties

Sets the CSS style for the element. Only use as a last resort. Use style props instead.

See styling guide.

aria-label string

Defines a string value that labels the current element.

aria-labelledby string

Identifies the element (or elements) that labels the current element.

aria-describedby string

Identifies the element (or elements) that describes the object.

aria-details string

Identifies the element (or elements) that provide a detailed, extended description for the object.

isDisabled boolean

Whether the button is disabled.

isSelected boolean

Whether the element should be selected (controlled).

defaultSelected boolean

Whether the element should be selected (uncontrolled).

onChange (isSelected: boolean) => void

Handler that is called when the element's selection state changes.

type 'button' | 'reset' | 'submit'

The behavior of the button when used in an HTML form.

Defaults to 'button' .
onHoverStart (e: HoverEvent) => void

Handler that is called when a hover interaction starts.

onHoverEnd (e: HoverEvent) => void

Handler that is called when a hover interaction ends.

onPress (e: PressEvent) => void

Handler that is called when the press is released over the target.

StyleProps
margin Responsive<Spacing | "auto">

The amount of margin applied to all edges of this component.

marginX Responsive<Spacing | "auto">

The amount of margin applied to the left and right edges of this component. Takes priority over margin.

marginY Responsive<Spacing | "auto">

The amount of margin applied to the top and bottom edges of this component. Takes priority over margin.

marginTop Responsive<Spacing | "auto">

The amount of margin applied to the top edge of this component. Takes priority over marginY and margin.

marginRight Responsive<Spacing | "auto">

The amount of margin applied to the right edge of this component. Takes priority over marginX and margin.

marginBottom Responsive<Spacing | "auto">

The amount of margin applied to the bottom edge of this component. Takes priority over marginY and margin.

marginLeft Responsive<Spacing | "auto">

The amount of margin applied to the left edge of this component. Takes priority over marginX and margin.

Allows users to toggle multiple options, with single or multiple selection.

See toggle button usage guidelines.

ref Ref<HTMLDivElement>

The ref type for this component.

UNSTABLE_ToggleButtonGroupProps
children * ReactNode

Toggle buttons belonging to the group. Each button must have an id prop defined.

data-cim-style-root boolean

Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.

UNSAFE_className string

Sets the CSS className for the element. Only use as a last resort. Use style props instead.

See styling guide.

UNSAFE_style CSSProperties

Sets the CSS style for the element. Only use as a last resort. Use style props instead.

See styling guide.

aria-label string

Defines a string value that labels the current element.

aria-labelledby string

Identifies the element (or elements) that labels the current element.

aria-describedby string

Identifies the element (or elements) that describes the object.

aria-details string

Identifies the element (or elements) that provide a detailed, extended description for the object.

orientation Orientation

The orientation of the the toggle button group.

Defaults to 'horizontal' .
selectionMode 'multiple' | 'single'

Whether single or multiple selection is enabled.

selectedKeys Iterable<Key, any, any>

The currently selected keys in the collection (controlled).

defaultSelectedKeys Iterable<Key, any, any>

The initial selected keys in the collection (uncontrolled).

onSelectionChange (keys: Set<Key>) => void

Handler that is called when the selection changes.

isDisabled boolean

Whether all items are disabled.

StyleProps
margin Responsive<Spacing | "auto">

The amount of margin applied to all edges of this component.

marginX Responsive<Spacing | "auto">

The amount of margin applied to the left and right edges of this component. Takes priority over margin.

marginY Responsive<Spacing | "auto">

The amount of margin applied to the top and bottom edges of this component. Takes priority over margin.

marginTop Responsive<Spacing | "auto">

The amount of margin applied to the top edge of this component. Takes priority over marginY and margin.

marginRight Responsive<Spacing | "auto">

The amount of margin applied to the right edge of this component. Takes priority over marginX and margin.

marginBottom Responsive<Spacing | "auto">

The amount of margin applied to the bottom edge of this component. Takes priority over marginY and margin.

marginLeft Responsive<Spacing | "auto">

The amount of margin applied to the left edge of this component. Takes priority over marginX and margin.