Skip to content
+

Switch

Switch is a UI element that let users choose between two states.

SwitchRoot API

Import

import * as Switch from '@base_ui/react/Switch';
const SwitchRoot = Switch.Root;

Props

Props of the native component are also available.

NameTypeDefaultDescription
checkedbool-

If true, the switch is checked.

classNamefunc
| string
-

Class names applied to the element or a function that returns them based on the component's state.

defaultCheckedbool-

The default checked state. Use when the component is not controlled.

disabledboolfalse

If true, the component is disabled and can't be interacted with.

idstring-

The id of the switch element.

inputRefref-

Ref to the underlying input element.

namestring-

Name of the underlying input element.

onCheckedChangefunc-

Callback fired when the checked state is changed.

Signature:function(checked: boolean, event: React.ChangeEvent) => void
  • checked The new checked state.
  • event The event source of the callback.
readOnlyboolfalse

If true, the component is read-only. Functionally, this is equivalent to being disabled, but the assistive technologies will announce this differently.

renderelement
| func
-

A function to customize rendering of the component.

requiredboolfalse

If true, the switch must be checked for the browser validation to pass.


The ref is forwarded to the root element.

Theme default props

You can use SwitchRoot to change the default props of this component with the theme.

SwitchThumb API

Import

import * as Switch from '@base_ui/react/Switch';
const SwitchThumb = Switch.Thumb;

Props

Props of the native component are also available.

NameTypeDescription
classNamefunc
| string

Class names applied to the element or a function that returns them based on the component's state.

renderelement
| func

A function to customize rendering of the component.


The ref is forwarded to the root element.

Theme default props

You can use SwitchThumb to change the default props of this component with the theme.