Switch
A control that indicates whether a setting is on or off.
View as MarkdownAnatomy
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 <button>
element and a hidden <input>
beside.
name
string
string
- Name
- Description
Identifies the field when a form is submitted.
- Type
string
defaultChecked
boolean
(default: false
)
boolean
false
- Name
- Description
Whether the switch is initially active.
To render a controlled switch, use the
checked
prop instead.- Type
boolean
- Default
false
checked
boolean
boolean
- Name
- Description
Whether the switch is currently active.
To render an uncontrolled switch, use the
defaultChecked
prop instead.- Type
boolean
onCheckedChange
function
function
- Name
- Description
Event handler called when the switch is activated or deactivated.
- Type
((checked: boolean, event: Event) => void)
nativeButton
boolean
(default: true
)
boolean
true
- Name
- Description
Whether the component renders a native
<button>
element when replacing it via therender
prop. Set tofalse
if the rendered element is not a button (e.g.<div>
).- Type
boolean
- Default
true
disabled
boolean
(default: false
)
boolean
false
- Name
- Description
Whether the component should ignore user interaction.
- Type
boolean
- Default
false
readOnly
boolean
(default: false
)
boolean
false
- Name
- Description
Whether the user should be unable to activate or deactivate the switch.
- Type
boolean
- Default
false
required
boolean
(default: false
)
boolean
false
- Name
- Description
Whether the user must activate the switch before submitting a form.
- Type
boolean
- Default
false
inputRef
Ref<HTMLInputElement>
Ref<HTMLInputElement>
- Name
- Description
A ref to access the hidden
<input>
element.- Type
Ref<HTMLInputElement>
id
string
string
- Name
- Description
The id of the switch element.
- Type
string
className
string | function
string | 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)
render
ReactElement | function
ReactElement | function
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElement
or a function that returns the element to render.- Type
| ReactElement
| ((props: HTMLProps, state: Switch.Root.State) => ReactElement)
data-checked
Present when the switch is checked.
data-unchecked
Present when the switch is not checked.
data-disabled
Present when the switch is disabled.
data-readonly
Present when the switch is readonly.
data-required
Present when the switch is required.
data-valid
Present when the switch is in valid state (when wrapped in Field.Root).
data-invalid
Present when the switch is in invalid state (when wrapped in Field.Root).
data-dirty
Present when the switch's value has changed (when wrapped in Field.Root).
data-touched
Present when the switch has been touched (when wrapped in Field.Root).
data-filled
Present when the switch is active (when wrapped in Field.Root).
data-focused
Present when the switch is focused (when wrapped in Field.Root).
Thumb
The movable part of the switch that indicates whether the switch is on or off.
Renders a <span>
.
className
string | function
string | 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)
render
ReactElement | function
ReactElement | function
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElement
or a function that returns the element to render.- Type
| ReactElement
| ((props: HTMLProps, state: Switch.Thumb.State) => ReactElement)
data-checked
Present when the switch is checked.
data-unchecked
Present when the switch is not checked.
data-disabled
Present when the switch is disabled.
data-readonly
Present when the switch is readonly.
data-required
Present when the switch is required.
data-valid
Present when the switch is in valid state (when wrapped in Field.Root).
data-invalid
Present when the switch is in invalid state (when wrapped in Field.Root).
data-dirty
Present when the switch's value has changed (when wrapped in Field.Root).
data-touched
Present when the switch has been touched (when wrapped in Field.Root).
data-filled
Present when the switch is active (when wrapped in Field.Root).
data-focused
Present when the switch is focused (when wrapped in Field.Root).