Switch
Switches are UI elements that let users choose between two states—most commonly on/off.
Switch API
Import
import Switch from '@mui/base/Switch';
// or
import { Switch } from '@mui/base';
Props
Props of the native component are also available.
Name | Type | Default | Description |
---|---|---|---|
checked | bool | If true , the component is checked. | |
defaultChecked | bool | The default checked state. Use when the component is not controlled. | |
disabled | bool | If true , the component is disabled. | |
onChange | func | Callback fired when the state is changed. Signature: function(event: React.ChangeEvent<HTMLInputElement>) => void event: The event source of the callback. You can pull out the new value by accessing event.target.value (string). You can pull out the new checked state by accessing event.target.checked (boolean). | |
readOnly | bool | If true , the component is read only. | |
required | bool | If true , the input element is required. | |
slotProps | { input?: func | object, root?: func | object, thumb?: func | object, track?: func | object } | {} | The props used for each slot inside the Switch. |
slots | { input?: elementType, root?: elementType, thumb?: elementType, track?: elementType | null } | {} | The components used for each slot inside the Switch. Either a string to use a HTML element or a component. See Slots API below for more details. |
The
ref
is forwarded to the root element.Slots
To learn how to customize the slot, check out the Overriding component structure guide.
Name | Default class | Default HTML tag | Description |
---|---|---|---|
root | .MuiSwitch-root | 'span' | The component that renders the root. |
input | .MuiSwitch-input | 'input' | The component that renders the input. |
thumb | .MuiSwitch-thumb | 'span' | The component that renders the thumb. |
track | .MuiSwitch-track | 'span' | The component that renders the track. |
You can override the style of the component using one of these customization options:
- With a global class name.
- With a rule name as part of the component's
styleOverrides
property in a custom theme.
CSS classes
These class names are useful for styling with CSS. They are applied to the root slot when specific states are triggered.
Global class | Description |
---|---|
.Mui-checked STATE | State class applied to the root element if the switch is checked |
.Mui-disabled STATE | State class applied to the root element if the switch is disabled |
.Mui-focusVisible STATE | State class applied to the root element if the switch has visible focus |
.MuiSwitch-readOnly | Class applied to the root element if the switch is read-only |