Toggle button API
Import
Section titled “Import”Cimpress UI exports three toggle-button-related components:
ToggleButton: displays a toggle button that has a visible labelToggleIconButton: displays a toggle button that has no visible label (icon-only)ToggleButtonGroup: container component that groups related toggle buttons together, providing a shared label, optional description, error handling, and state management.
import { ToggleButton, ToggleIconButton, ToggleButtonGroup } from '@cimpress-ui/react';Controlled/uncontrolled usage
Section titled “Controlled/uncontrolled usage”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.
Single toggle button
Section titled “Single toggle button”import { Stack, ToggleButton } from '@cimpress-ui/react';import { useState } from 'react';
export default function Demo() { const [isSelected, setIsSelected] = useState(false);
return ( <Stack gap={32}> <ToggleButton isSelected={isSelected} onChange={setIsSelected}> I am controlled by React state </ToggleButton>
<ToggleButton>I am uncontrolled and unselected by default</ToggleButton>
<ToggleButton defaultSelected>I am uncontrolled and selected by default</ToggleButton> </Stack> );}Toggle button group
Section titled “Toggle button group”import { type Key, Stack, ToggleButton, ToggleButtonGroup } from '@cimpress-ui/react';import { useState } from 'react';
export default function Demo() { const [selectedKeys, setSelectedKeys] = useState<Set<Key>>(() => new Set(['left']));
return ( <Stack gap={32}> <ToggleButtonGroup label="Text alignment (controlled)" selectedKeys={selectedKeys} onSelectionChange={setSelectedKeys} > <ToggleButton value="left">Left</ToggleButton> <ToggleButton value="center">Center</ToggleButton> <ToggleButton value="right">Right</ToggleButton> </ToggleButtonGroup>
<ToggleButtonGroup label="Text alignment (uncontrolled)" defaultSelectedKeys={['left']}> <ToggleButton value="left">Left</ToggleButton> <ToggleButton value="center">Center</ToggleButton> <ToggleButton value="right">Right</ToggleButton> </ToggleButtonGroup> </Stack> );}Accessibility notes
Section titled “Accessibility notes”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 and ToggleButtonGroup require a textual label to remain accessible to assistive technologies. ToggleIconButton 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.
Group state management
Section titled “Group state management”Selection state of toggle buttons within ToggleButtonGroup cannot be set individually.
Please use ToggleButtonGroup’s selectedKeys/defaultSelectedKeys props instead.
// This won't work! :(<ToggleButtonGroup label="Text alignment"> <ToggleButton value="left">Left</ToggleButton> <ToggleButton value="center">Center</ToggleButton> <ToggleButton value="right" defaultSelected>Right</ToggleButton></ToggleButtonGroup>
// But this will :)<ToggleButtonGroup label="Text alignment" defaultSelectedKeys={['right']}> <ToggleButton value="left">Left</ToggleButton> <ToggleButton value="center">Center</ToggleButton> <ToggleButton value="right">Right</ToggleButton></ToggleButtonGroup>Toggle button, toggle icon button, and toggle button group can integrate with native HTML forms. See our forms guide to learn how to work with forms.
API reference
Section titled “API reference”ToggleButton
Section titled “ToggleButton”Displays a labelled button that allows users to toggle between two states.
Can be used standalone, or as part of ToggleButtonGroup.
- Ref<HTMLButtonElement>
-
The
reftype for this component.
ToggleButtonProps
- StringLikeChildren
children *
Section titled “ children * ” -
The text displayed on the button. Must remain the same regardless of selection state.
- 'small' | 'medium' | 'large'
- Defaults to 'medium' .
The size of the button.
- boolean
fullWidth
Section titled “ fullWidth ” - Defaults to false .
Whether the button should take up the whole available width.
- ReactNode
iconStart
Section titled “ iconStart ” -
An icon displayed before the button text.
- ReactNode
iconEnd
Section titled “ iconEnd ” -
An icon displayed after the button text.
- string
-
The
<form>element to associate the input with. The value of this attribute must be the id of a<form>in the same document. See MDN. - string
-
The name of the input element, used when submitting an HTML form. See MDN.
- string
value
Section titled “ value ” -
The value of the input element, used when submitting an HTML form. See MDN.
When used in a
ToggleButtonGroup,valueserves as an identifier for this button, and has to be unique across all buttons in the group. - boolean
data-cim-style-root
Section titled “ data-cim-style-root ” -
Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.
- string
UNSAFE_className
Section titled “ UNSAFE_className ” -
Sets the CSS className for the element. Only use as a last resort. Use style props instead.
See styling guide.
- CSSProperties
UNSAFE_style
Section titled “ UNSAFE_style ” -
Sets the CSS style for the element. Only use as a last resort. Use style props instead.
See styling guide.
- string
aria-label
Section titled “ aria-label ” -
Defines a string value that labels the current element.
- string
aria-labelledby
Section titled “ aria-labelledby ” -
Identifies the element (or elements) that labels the current element.
- string
aria-describedby
Section titled “ aria-describedby ” -
Identifies the element (or elements) that describes the object.
- string
aria-details
Section titled “ aria-details ” -
Identifies the element (or elements) that provide a detailed, extended description for the object.
- boolean
isDisabled
Section titled “ isDisabled ” -
Whether the button is disabled.
- boolean
isSelected
Section titled “ isSelected ” -
Whether the element should be selected (controlled).
- boolean
defaultSelected
Section titled “ defaultSelected ” -
Whether the element should be selected (uncontrolled).
- (isSelected: boolean) => void
onChange
Section titled “ onChange ” -
Handler that is called when the element's selection state changes.
- (e: HoverEvent) => void
onHoverStart
Section titled “ onHoverStart ” -
Handler that is called when a hover interaction starts.
- (e: HoverEvent) => void
onHoverEnd
Section titled “ onHoverEnd ” -
Handler that is called when a hover interaction ends.
- (e: PressEvent) => void
onPress
Section titled “ onPress ” -
Handler that is called when the press is released over the target.
- boolean
autoFocus
Section titled “ autoFocus ” -
Whether the element should receive focus on render.
StyleProps
- Responsive<Spacing | "auto">
margin
Section titled “ margin ” -
The amount of margin applied to all edges of this component.
- Responsive<Spacing | "auto">
marginX
Section titled “ marginX ” -
The amount of margin applied to the left and right edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginY
Section titled “ marginY ” -
The amount of margin applied to the top and bottom edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginTop
Section titled “ marginTop ” -
The amount of margin applied to the top edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginRight
Section titled “ marginRight ” -
The amount of margin applied to the right edge of this component. Takes priority over
marginXandmargin. - Responsive<Spacing | "auto">
marginBottom
Section titled “ marginBottom ” -
The amount of margin applied to the bottom edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginLeft
Section titled “ marginLeft ” -
The amount of margin applied to the left edge of this component. Takes priority over
marginXandmargin.
ToggleIconButton
Section titled “ToggleIconButton”Displays an icon-only button that allows users to toggle between two states.
Can be used standalone, or as part of ToggleButtonGroup.
- Ref<HTMLButtonElement>
-
The
reftype for this component.
ToggleIconButtonProps
- ReactNode
icon *
Section titled “ icon * ” -
The icon displayed on the button.
- string
aria-label *
Section titled “ aria-label * ” -
The label describing the function of this button for assistive technologies. Must remain the same regardless of selection state.
- 'small' | 'medium' | 'large'
- Defaults to 'medium' .
The size of the button.
- boolean
fullWidth
Section titled “ fullWidth ” - Defaults to false .
Whether the button should take up the whole available width.
- string
-
The
<form>element to associate the input with. The value of this attribute must be the id of a<form>in the same document. See MDN. - string
-
The name of the input element, used when submitting an HTML form. See MDN.
- string
value
Section titled “ value ” -
The value of the input element, used when submitting an HTML form. See MDN.
When used in a
ToggleButtonGroup,valueserves as an identifier for this button, and has to be unique across all buttons in the group. - boolean
data-cim-style-root
Section titled “ data-cim-style-root ” -
Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.
- string
UNSAFE_className
Section titled “ UNSAFE_className ” -
Sets the CSS className for the element. Only use as a last resort. Use style props instead.
See styling guide.
- CSSProperties
UNSAFE_style
Section titled “ UNSAFE_style ” -
Sets the CSS style for the element. Only use as a last resort. Use style props instead.
See styling guide.
- string
aria-labelledby
Section titled “ aria-labelledby ” -
Identifies the element (or elements) that labels the current element.
- string
aria-describedby
Section titled “ aria-describedby ” -
Identifies the element (or elements) that describes the object.
- string
aria-details
Section titled “ aria-details ” -
Identifies the element (or elements) that provide a detailed, extended description for the object.
- boolean
isDisabled
Section titled “ isDisabled ” -
Whether the button is disabled.
- boolean
isSelected
Section titled “ isSelected ” -
Whether the element should be selected (controlled).
- boolean
defaultSelected
Section titled “ defaultSelected ” -
Whether the element should be selected (uncontrolled).
- (isSelected: boolean) => void
onChange
Section titled “ onChange ” -
Handler that is called when the element's selection state changes.
- (e: HoverEvent) => void
onHoverStart
Section titled “ onHoverStart ” -
Handler that is called when a hover interaction starts.
- (e: HoverEvent) => void
onHoverEnd
Section titled “ onHoverEnd ” -
Handler that is called when a hover interaction ends.
- (e: PressEvent) => void
onPress
Section titled “ onPress ” -
Handler that is called when the press is released over the target.
- boolean
autoFocus
Section titled “ autoFocus ” -
Whether the element should receive focus on render.
StyleProps
- Responsive<Spacing | "auto">
margin
Section titled “ margin ” -
The amount of margin applied to all edges of this component.
- Responsive<Spacing | "auto">
marginX
Section titled “ marginX ” -
The amount of margin applied to the left and right edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginY
Section titled “ marginY ” -
The amount of margin applied to the top and bottom edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginTop
Section titled “ marginTop ” -
The amount of margin applied to the top edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginRight
Section titled “ marginRight ” -
The amount of margin applied to the right edge of this component. Takes priority over
marginXandmargin. - Responsive<Spacing | "auto">
marginBottom
Section titled “ marginBottom ” -
The amount of margin applied to the bottom edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginLeft
Section titled “ marginLeft ” -
The amount of margin applied to the left edge of this component. Takes priority over
marginXandmargin.
ToggleButtonGroup
Section titled “ToggleButtonGroup”Allows users to toggle multiple options, with single or multiple selection.
- Ref<HTMLDivElement>
-
The
reftype for this component.
ToggleButtonGroupProps
- ReactNode
children *
Section titled “ children * ” -
Toggle buttons belonging to the group. Each button must have a
valueprop defined. - string
label *
Section titled “ label * ” -
The content to display as the label.
- boolean
isInvalid
Section titled “ isInvalid ” -
Whether the current selection is invalid.
- boolean
isRequired
Section titled “ isRequired ” -
Whether a selection is required before form submission.
- boolean
data-cim-style-root
Section titled “ data-cim-style-root ” -
Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.
- string
UNSAFE_className
Section titled “ UNSAFE_className ” -
Sets the CSS className for the element. Only use as a last resort. Use style props instead.
See styling guide.
- CSSProperties
UNSAFE_style
Section titled “ UNSAFE_style ” -
Sets the CSS style for the element. Only use as a last resort. Use style props instead.
See styling guide.
- string
aria-label
Section titled “ aria-label ” -
Defines a string value that labels the current element.
- string
aria-labelledby
Section titled “ aria-labelledby ” -
Identifies the element (or elements) that labels the current element.
- string
aria-describedby
Section titled “ aria-describedby ” -
Identifies the element (or elements) that describes the object.
- string
aria-details
Section titled “ aria-details ” -
Identifies the element (or elements) that provide a detailed, extended description for the object.
- string
-
The
<form>element to associate the input with. The value of this attribute must be the id of a<form>in the same document. See MDN. - string
-
The name of the input element, used when submitting an HTML form. See MDN.
- string
description
Section titled “ description ” -
A description for the field. Provides a hint such as specific requirements for what to choose.
- FieldError
error
Section titled “ error ” -
An error message for the field.
- (value: Set) => string | true | string[] | undefined
validate
Section titled “ validate ” -
A function that returns an error message (or
true) if a given value is invalid. Validation errors are displayed to the user when the form is submitted. For real-time validation, use theerrorprop instead. - 'multiple' | 'single'
selectionMode
Section titled “ selectionMode ” - Defaults to 'single' .
Whether single or multiple selection is enabled.
- Iterable<Key, any, any>
selectedKeys
Section titled “ selectedKeys ” -
The currently selected keys in the collection (controlled).
- Iterable<Key, any, any>
defaultSelectedKeys
Section titled “ defaultSelectedKeys ” -
The initial selected keys in the collection (uncontrolled).
- (keys: Set<Key>) => void
onSelectionChange
Section titled “ onSelectionChange ” -
Handler that is called when the selection changes.
- boolean
isDisabled
Section titled “ isDisabled ” -
Whether all items are disabled.
StyleProps
- Responsive<Spacing | "auto">
margin
Section titled “ margin ” -
The amount of margin applied to all edges of this component.
- Responsive<Spacing | "auto">
marginX
Section titled “ marginX ” -
The amount of margin applied to the left and right edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginY
Section titled “ marginY ” -
The amount of margin applied to the top and bottom edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginTop
Section titled “ marginTop ” -
The amount of margin applied to the top edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginRight
Section titled “ marginRight ” -
The amount of margin applied to the right edge of this component. Takes priority over
marginXandmargin. - Responsive<Spacing | "auto">
marginBottom
Section titled “ marginBottom ” -
The amount of margin applied to the bottom edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginLeft
Section titled “ marginLeft ” -
The amount of margin applied to the left edge of this component. Takes priority over
marginXandmargin.