Introducing PrimeReact v11-alpha 🎉Discover Now

Switch

An unstyled, accessible switch toggle component with compound composition.

Build fully custom switch toggle controls with complete control over layout and styling.

basic-demo

Pre-styled Versions

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

Features#

  • Compound component API with three sub-components: Root, Control, Thumb
  • Hidden native checkbox with role="switch" for full accessibility
  • Controlled and uncontrolled checked state
  • Invalid and disabled state support with data attributes

Usage#

import { Switch } from 'primereact/switch';
<Switch.Root>
    <Switch.Control>
        <Switch.Thumb />
    </Switch.Control>
</Switch.Root>

Behavior#

Polymorphic Rendering#

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

<Switch.Root as="label">
    <Switch.Control as="span">
        <Switch.Thumb as="span" />
    </Switch.Control>
</Switch.Root>

Default elements: Root=div, Control=div, Thumb=span.

Render Function Children#

Switch.Thumb accepts a render function as children, providing access to the component instance.

<Switch.Thumb>{(instance) => <span>{instance.switch?.state.checked ? 'ON' : 'OFF'}</span>}</Switch.Thumb>

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#

SwitchRoot#

NameTypeDefault
refRef<unknown>—
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: SwitchRootInstance) => CSSProperties)—
The style to apply to the component.
classNamestring | ((instance?: SwitchRootInstance) => 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.
instanceSwitchRootInstance—
The instance to pass to the component.
ptSafeRecord<SwitchRootPassThrough>—
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: SwitchRootInstance) => 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.
checkedboolean—
When present, it specifies the input's checked state.
defaultCheckedboolean—
The default value for the input when not controlled by `checked` and `onCheckedChange` .
tabIndexnumber—
Index of the element in tabbing order.
valueunknown—
Value of the switch.
disabledbooleanfalse
When present, it specifies that the element should be disabled.
requiredbooleanfalse
When present, it specifies that the element is required.
invalidbooleanfalse
When present, it specifies that the component should have invalid state style.
inputIdstring—
Identifier of the underlying input element.
inputStyleCSSProperties—
Inline style of the input field.
inputClassNamestring—
Style class of the input field.
ariaLabelstring—
Establishes a string value that labels the component.
ariaLabelledbystring—
Establishes relationships between the component and label(s) where its value should be one or more element IDs.
onFocus(event: FocusEvent<HTMLInputElement>) => void—
Callback function that is called when the switch is focused.
onBlur(event: FocusEvent<HTMLInputElement>) => void—
Callback function that is called when the switch loses focus.
onCheckedChange(event: SwitchRootChangeEvent) => void—
Callback fired when the switch's checked state changes.
[key: string]any—
pt-{optionName}-*-—
Pass through attributes for customizing component. For more info, see Pass Through tab.
AttributeValue
data-scope"switch"
data-part"root"
data-checkedPresent when checked
data-uncheckedPresent when unchecked
data-disabledPresent when disabled
data-invalidPresent when invalid

Defines passthrough(pt) options of Switch component.

labeltypedescription
rootSwitchRootPassThroughType<HTMLAttributes<HTMLSpanElement>>Used to pass attributes to the root's DOM element.
inputSwitchRootPassThroughType<HTMLAttributes<HTMLElement>>Used to pass attributes to the input's DOM element.
controlSwitchRootPassThroughType<HTMLAttributes<HTMLSpanElement>>Used to pass attributes to the control's DOM element.
thumbSwitchRootPassThroughType<HTMLAttributes<HTMLSpanElement>>Used to pass attributes to the thumb's DOM element.

SwitchControl#

NameTypeDefault
refRef<unknown>—
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: SwitchControlInstance) => CSSProperties)—
The style to apply to the component.
classNamestring | ((instance?: SwitchControlInstance) => 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.
instanceSwitchControlInstance—
The instance to pass to the component.
ptSafeRecord<SwitchControlPassThrough>—
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: SwitchControlInstance) => 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-scope"switch"
data-part"control"
data-checkedPresent when checked
data-uncheckedPresent when unchecked
data-disabledPresent when disabled
data-invalidPresent when invalid

Defines passthrough(pt) options of SwitchControl component.

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

SwitchThumb#

NameTypeDefault
refRef<unknown>—
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: SwitchThumbInstance) => CSSProperties)—
The style to apply to the component.
classNamestring | ((instance?: SwitchThumbInstance) => 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.
instanceSwitchThumbInstance—
The instance to pass to the component.
ptSafeRecord<SwitchThumbPassThrough>—
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: SwitchThumbInstance) => 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-scope"switch"
data-part"thumb"
data-checkedPresent when checked
data-uncheckedPresent when unchecked
data-disabledPresent when disabled
data-invalidPresent when invalid

Defines passthrough(pt) options of SwitchThumb component.

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

Accessibility#

Screen Reader#

Switch.Root renders a hidden native <input type="checkbox" role="switch"> that is announced as a switch by assistive technologies. The input reflects checked state via aria-checked. Provide an accessible name with aria-label, aria-labelledby, or an associated <label> element using inputId.

<label htmlFor="switch1">Notifications</label>
<Switch.Root inputId="switch1" />
 
<span id="switch2">Dark Mode</span>
<Switch.Root ariaLabelledby="switch2" />
 
<Switch.Root ariaLabel="Enable notifications" />

Keyboard Support#

KeyFunction
tabMoves focus to the switch.
spaceToggles the checked state.