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 place its parts the following way:
Anatomy
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 | |
---|---|---|---|
checked | boolean | undefined | |
className | string | (state) => string | undefined | |
defaultChecked | boolean | false | |
disabled | boolean | false | |
inputRef | React.Ref | undefined | |
name | string | undefined | |
onCheckedChange | (checked, event) => void | undefined | |
readOnly | boolean | false | |
render | | React.ReactElement | undefined | |
required | boolean | false |
Thumb
The movable part of the switch that indicates whether the switch is on or off.
Renders a <span>
.
Prop | Type | Default | |
---|---|---|---|
className | string | (state) => string | undefined | |
render | | React.ReactElement | undefined |