Switch
A control that indicates whether a setting is on or off.
import * as React from 'react';
import { Switch } from '@base-ui-components/react/switch';
import styles from './index.module.css';
export default function ExampleSwitch() {
return (
<Switch.Root defaultChecked className={styles.Switch}>
<Switch.Thumb className={styles.Thumb} />
</Switch.Root>
);
}
API reference
Import the component and assemble its parts:
import { Switch } from '@base-ui-components/react/switch';
<Switch.Root> <Switch.Thumb /></Switch.Root>
Root
Represents the switch itself.
Renders a <button>
element and a hidden <input>
beside.
Prop | Type | Default | |
---|---|---|---|
name |
|
| |
defaultChecked |
|
| |
checked |
|
| |
onCheckedChange |
|
| |
disabled |
|
| |
readOnly |
|
| |
required |
|
| |
inputRef |
|
| |
className |
|
| |
render |
|
|
Attribute | Description | |
---|---|---|
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). |
Thumb
The movable part of the switch that indicates whether the switch is on or off.
Renders a <span>
.
Prop | Type | Default | |
---|---|---|---|
className |
|
| |
render |
|
|
Attribute | Description | |
---|---|---|
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). |