Introducing PrimeReact v11-alpha 🎉Discover Now

ToggleButton

An unstyled toggle button component with group selection support for single and multiple modes.

Build fully custom toggle buttons and button groups with complete control over layout and styling.

basic-demo

Pre-styled Versions

Choose a pre-styled library to use ToggleButton with ready-made designs.
For hook-based usage without components, see the Headless API.

Features#

  • Compound component API with sub-components: Root, Indicator
  • Standalone toggle with boolean pressed state
  • Group selection with ToggleButtonGroup for single and multiple modes
  • Configurable allowEmpty to require at least one selection in a group

Usage#

import { ToggleButton } from 'primereact/togglebutton';
import { ToggleButtonGroup } from 'primereact/togglebuttongroup';
<ToggleButton.Root>
    <ToggleButton.Indicator></ToggleButton.Indicator>
</ToggleButton.Root>
 
<ToggleButtonGroup>
    <ToggleButton.Root value="a">
        <ToggleButton.Indicator></ToggleButton.Indicator>
    </ToggleButton.Root>
    <ToggleButton.Root value="b">
        <ToggleButton.Indicator></ToggleButton.Indicator>
    </ToggleButton.Root>
</ToggleButtonGroup>

Behavior#

Polymorphic Rendering#

Use as on any sub-component to change the rendered element.

<ToggleButton.Root as="div"></ToggleButton.Root>
<ToggleButton.Indicator as="span"></ToggleButton.Indicator>

Default elements: Root=button, Indicator=span, ToggleButtonGroup=div.

Render Function Children#

Indicator accepts a render function as children, providing access to the component instance. The instance exposes togglebutton (parent instance with state.pressed).

<ToggleButton.Indicator>{({ togglebutton }: ToggleButtonIndicatorInstance) => (togglebutton?.state.pressed ? 'On' : 'Off')}</ToggleButton.Indicator>

Pass Through#

Some parts may not be visible in the preview depending on the component's current state.

Loading...
/* Select a part to see its CSS selector for custom styling */

API#

ToggleButtonRoot#

NameTypeDefault
refRef<unknown>—
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: ToggleButtonRootInstance) => CSSProperties)—
The style to apply to the component.
classNamestring | ((instance?: ToggleButtonRootInstance) => string)—
The class name to apply to the component.
asstring | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode>—
The component type to render.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceToggleButtonRootInstance—
The instance to pass to the component.
ptSafeRecord<ToggleButtonRootPassThrough>—
The pass-through props to pass to the component.
ptOptionsPassThroughOptions—
The pass-through options to pass to the component.
unstyledboolean—
Whether the component should be rendered without classes.
dtunknown—
The design token to use for the component.
stylesStylesOptions<ComponentInstance>—
The styles to use for the component.
render(instance: ToggleButtonRootInstance) => ReactNode—
The render function to render the component with instance access.
childrenany—
The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates.
pressedboolean—
When present, it specifies that the ToggleButton should be pressed.
defaultPressedboolean—
The default pressed value when not controlled by `pressed` and `onPressedChange` .
valueunknown—
Value of the ToggleButton.
size"small" | "large" | "normal"—
Defines the size of the ToggleButton.
disabledbooleanfalse
When present, it specifies that the element should be disabled.
invalidbooleanfalse
When present, it specifies that the component should have invalid state style.
onPressedChange(event: ToggleButtonRootChangeEvent) => void—
Callback fired when the ToggleButton's pressed state changes.
[key: string]any—
pt-{optionName}-*-—
Pass through attributes for customizing component. For more info, see Pass Through tab.
AttributeValue
data-pressedPresent when pressed
data-disabledPresent when disabled
data-invalidPresent when invalid

Defines passthrough(pt) options of ToggleButton component.

labeltypedescription
rootToggleButtonRootPassThroughType<HTMLAttributes<HTMLDivElement>>Used to pass attributes to the root's DOM element.
indicatorToggleButtonRootPassThroughType<HTMLAttributes<HTMLSpanElement>>Used to pass attributes to the indicator's DOM element.

ToggleButtonIndicator#

NameTypeDefault
refRef<unknown>—
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: ToggleButtonIndicatorInstance) => CSSProperties)—
The style to apply to the component.
classNamestring | ((instance?: ToggleButtonIndicatorInstance) => string)—
The class name to apply to the component.
asstring | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode>—
The component type to render.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceToggleButtonIndicatorInstance—
The instance to pass to the component.
ptSafeRecord<ToggleButtonIndicatorPassThrough>—
The pass-through props to pass to the component.
ptOptionsPassThroughOptions—
The pass-through options to pass to the component.
unstyledboolean—
Whether the component should be rendered without classes.
dtunknown—
The design token to use for the component.
stylesStylesOptions<ComponentInstance>—
The styles to use for the component.
render(instance: ToggleButtonIndicatorInstance) => ReactNode—
The render function to render the component with instance access.
childrenany—
The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates.
[key: string]any—
pt-{optionName}-*-—
Pass through attributes for customizing component. For more info, see Pass Through tab.
AttributeValue
data-pressedPresent when pressed
data-disabledPresent when disabled
data-invalidPresent when invalid

Defines passthrough(pt) options of ToggleButtonIndicator component.

labeltypedescription
rootToggleButtonIndicatorPassThroughType<HTMLAttributes<HTMLDivElement>>Used to pass attributes to the root's DOM element.

ToggleButtonGroup#

NameTypeDefault
refRef<unknown>—
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: ToggleButtonGroupInstance) => CSSProperties)—
The style to apply to the component.
classNamestring | ((instance?: ToggleButtonGroupInstance) => string)—
The class name to apply to the component.
asstring | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode>—
The component type to render.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceToggleButtonGroupInstance—
The instance to pass to the component.
ptSafeRecord<ToggleButtonGroupPassThrough>—
The pass-through props to pass to the component.
ptOptionsPassThroughOptions—
The pass-through options to pass to the component.
unstyledboolean—
Whether the component should be rendered without classes.
dtunknown—
The design token to use for the component.
stylesStylesOptions<ComponentInstance>—
The styles to use for the component.
render(instance: ToggleButtonGroupInstance) => ReactNode—
The render function to render the component with instance access.
childrenany—
The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates.
valueunknown—
Value of the ToggleButtonGroup.
defaultValueunknown—
The default value of the ToggleButtonGroup.
multiplebooleanfalse
When present, it specifies that the ToggleButton group allows multiple selections.
allowEmptybooleantrue
When present, it specifies that the ToggleButton group allows empty selection.
size"small" | "large" | "normal"—
Defines the size of the ToggleButton components.
disabledbooleanfalse
When present, it specifies that the ToggleButton group should be disabled.
invalidbooleanfalse
When present, it specifies that the ToggleButton group is invalid.
onValueChange(event: ToggleButtonGroupValueChangeEvent) => void—
Callback function that is called when the ToggleButton group value changes.
[key: string]any—
pt-{optionName}-*-—
Pass through attributes for customizing component. For more info, see Pass Through tab.
AttributeValue
data-scope"togglebuttongroup"
data-part"root"
data-disabledPresent when disabled
data-invalidPresent when invalid
data-multiplePresent when multiple selection active
data-allow-emptyPresent when empty selection allowed

Accessibility#

Screen Reader#

ToggleButton uses aria-pressed to indicate its toggle state and aria-disabled when disabled. Provide aria-label or visible text content to give each button an accessible name. ToggleButtonGroup uses role="group" and should have aria-label to describe the group purpose.

<ToggleButton.Root aria-label="Favorite">
    <ToggleButton.Indicator></ToggleButton.Indicator>
</ToggleButton.Root>
 
<ToggleButtonGroup aria-label="Text alignment">
    <ToggleButton.Root value="left" aria-label="Align left">
        <ToggleButton.Indicator></ToggleButton.Indicator>
    </ToggleButton.Root>
</ToggleButtonGroup>

Keyboard Support#

KeyFunction
SpaceToggles the pressed state.
EnterToggles the pressed state.
TabMoves focus to the next element.