Switch

Switches toggle the state of a single setting on or off.

<Switch />

Props

PropTypeDefaultDescription
checkedboolIf true, the component is checked.
checkedIconnodeThe icon to display when the component is checked.
classesobjectOverride or extend the styles applied to the component. See CSS API below for more details.
color'default' | 'primary' | 'secondary''secondary'The color of the component. It supports those theme colors that make sense for this component.
disabledboolIf true, the switch will be disabled.
disableRippleboolIf true, the ripple effect will be disabled.
edge'end' | 'start' | falsefalseIf given, uses a negative margin to counteract the padding on one side (this is often helpful for aligning the left or right side of the icon with content above or below, without ruining the border size and shape).
iconnodeThe icon to display when the component is unchecked.
idstringThe id of the input element.
inputPropsobjectAttributes applied to the input element.
inputRefrefPass a ref to the input element.
onChangefuncCallback fired when the state is changed. Signature: function(event: object) => 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).
requiredboolIf true, the input element will be required.
size'medium' | 'small''medium'The size of the switch. small is equivalent to the dense switch styling.
valueanyThe value of the component. The DOM API casts this to a string. The browser uses "on" as the default value.

Examples

Basic

() => {
const [state, setState] = React.useState({
checkedA: true,
checkedB: true,
});
const handleChange = (event) => {
setState({ ...state, [event.target.name]: event.target.checked });
};
return (
<div>
<Switch
checked={state.checkedA}
onChange={handleChange}
name="checkedA"
inputProps={{ 'aria-label': 'secondary checkbox' }}
/>
<Switch
checked={state.checkedB}
onChange={handleChange}
color="primary"
name="checkedB"
inputProps={{ 'aria-label': 'primary checkbox' }}
/>
<Switch inputProps={{ 'aria-label': 'primary checkbox' }} />
<Switch disabled inputProps={{ 'aria-label': 'disabled checkbox' }} />
<Switch
disabled
checked
inputProps={{ 'aria-label': 'primary checkbox' }}
/>
</div>
);
};

With FormControlLabel

() => {
const [state, setState] = React.useState({
checkedA: true,
checkedB: true,
});
const handleChange = (event) => {
setState({ ...state, [event.target.name]: event.target.checked });
};
return (
<FormGroup row>
<FormControlLabel
control={
<Switch
checked={state.checkedB}
onChange={handleChange}
name="checkedB"
color="primary"
/>
}
label="Primary"
/>
<FormControlLabel control={<Switch />} label="Uncontrolled" />
<FormControlLabel disabled control={<Switch />} label="Disabled" />
<FormControlLabel
disabled
control={<Switch checked />}
label="Disabled"
/>
</FormGroup>
);
};

With FormGroup

() => {
const [state, setState] = React.useState({
gilad: true,
jason: false,
antoine: true,
});
const handleChange = (event) => {
setState({ ...state, [event.target.name]: event.target.checked });
};
return (
<FormControl component="fieldset">
<FormLabel component="legend">Assign responsibility</FormLabel>
<FormGroup>
<FormControlLabel
control={
<Switch
checked={state.gilad}
onChange={handleChange}
name="gilad"
/>
}
label="Gilad Gray"
/>
<FormControlLabel
control={
<Switch
checked={state.jason}
onChange={handleChange}
name="jason"
/>
}
label="Jason Killian"
/>
<FormControlLabel
control={
<Switch
checked={state.antoine}
onChange={handleChange}
name="antoine"
/>
}
label="Antoine Llorca"
/>
</FormGroup>
<FormHelperText>Be careful</FormHelperText>
</FormControl>
);
};