Switch
A control that indicates whether a setting is on or off.
View as MarkdownUsage guidelines
- Form controls must have an accessible name: The switch must have a meaningful label. Prefer using
<Field>to provide a visible text label and description, or use thearia-labelattribute as an alternative. See the forms guide for more on building form controls.
Anatomy
Import the component and assemble its parts:
import { Switch } from '@base-ui-components/react/switch';
<Switch.Root>
<Switch.Thumb />
</Switch.Root>API reference
Root
Represents the switch itself.
Renders a <span> element and a hidden <input> beside.
namestring—
- Name
- Description
Identifies the field when a form is submitted.
- Type
string | undefined
defaultCheckedbooleanfalse
- Name
- Description
Whether the switch is initially active.
To render a controlled switch, use the
checkedprop instead.- Type
boolean | undefined- Default
false
checkedboolean—
- Name
- Description
Whether the switch is currently active.
To render an uncontrolled switch, use the
defaultCheckedprop instead.- Type
boolean | undefined
onCheckedChangefunction—
- Name
- Description
Event handler called when the switch is activated or deactivated.
- Type
| (( checked: boolean, eventDetails: Switch.Root.ChangeEventDetails, ) => void) | undefined
nativeButtonbooleanfalse
- Name
- Description
Whether the component renders a native
<button>element when replacing it via therenderprop. Set totrueif the rendered element is a native button.- Type
boolean | undefined- Default
false
disabledbooleanfalse
- Name
- Description
Whether the component should ignore user interaction.
- Type
boolean | undefined- Default
false
readOnlybooleanfalse
- Name
- Description
Whether the user should be unable to activate or deactivate the switch.
- Type
boolean | undefined- Default
false
requiredbooleanfalse
- Name
- Description
Whether the user must activate the switch before submitting a form.
- Type
boolean | undefined- Default
false
inputRefRef<HTMLInputElement>—
- Name
- Description
A ref to access the hidden
<input>element.- Type
React.Ref<HTMLInputElement> | undefined
idstring—
- Name
- Description
The id of the switch element.
- Type
string | undefined
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Switch.Root.State) => string | undefined)
styleReact.CSSProperties | function—
- Name
- Type
| React.CSSProperties | ((state: Switch.Root.State) => CSSProperties | undefined) | undefined
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Switch.Root.State, ) => ReactElement)
data-checked
data-unchecked
data-disabled
data-readonly
data-required
data-valid
data-invalid
data-dirty
data-touched
data-filled
data-focused
Thumb
The movable part of the switch that indicates whether the switch is on or off.
Renders a <span>.
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Switch.Thumb.State) => string | undefined)
styleReact.CSSProperties | function—
- Name
- Type
| React.CSSProperties | ((state: Switch.Thumb.State) => CSSProperties | undefined) | undefined
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Switch.Thumb.State, ) => ReactElement)