--- productId: material-ui title: React Switch component components: Switch, FormControl, FormGroup, FormLabel, FormControlLabel githubLabel: 'scope: switch' materialDesign: https://m2.material.io/components/selection-controls#switches githubSource: packages/mui-material/src/Switch --- # Switch Switches toggle the state of a single setting on or off. Switches are the preferred way to adjust settings on mobile. The option that the switch controls, as well as the state it's in, should be made clear from the corresponding inline label. ## Basic switches ```tsx import Switch from '@mui/material/Switch'; const label = { slotProps: { input: { 'aria-label': 'Switch demo' } } }; export default function BasicSwitches() { return (
); } ``` ## Label You can provide a label to the `Switch` thanks to the `FormControlLabel` component. ```tsx import FormGroup from '@mui/material/FormGroup'; import FormControlLabel from '@mui/material/FormControlLabel'; import Switch from '@mui/material/Switch'; export default function SwitchLabels() { return ( } label="Label" /> } label="Required" /> } label="Disabled" /> ); } ``` ## Size Use the `size` prop to change the size of the switch. ```tsx import Switch from '@mui/material/Switch'; const label = { slotProps: { input: { 'aria-label': 'Size switch demo' } } }; export default function SwitchesSize() { return (
); } ``` ## Color ```tsx import { alpha, styled } from '@mui/material/styles'; import { pink } from '@mui/material/colors'; import Switch from '@mui/material/Switch'; const PinkSwitch = styled(Switch)(({ theme }) => ({ '& .MuiSwitch-switchBase.Mui-checked': { color: pink[600], '&:hover': { backgroundColor: alpha(pink[600], theme.palette.action.hoverOpacity), }, }, '& .MuiSwitch-switchBase.Mui-checked + .MuiSwitch-track': { backgroundColor: pink[600], }, })); const label = { slotProps: { input: { 'aria-label': 'Color switch demo' } } }; export default function ColorSwitches() { return (
); } ``` ## Controlled You can control the switch with the `checked` and `onChange` props: ```tsx import * as React from 'react'; import Switch from '@mui/material/Switch'; export default function ControlledSwitches() { const [checked, setChecked] = React.useState(true); const handleChange = (event: React.ChangeEvent) => { setChecked(event.target.checked); }; return ( ); } ``` ## Switches with FormGroup `FormGroup` is a helpful wrapper used to group selection controls components that provides an easier API. However, you are encouraged to use [Checkboxes](/material-ui/react-checkbox/) instead if multiple related controls are required. (See: [When to use](#when-to-use)). ```tsx import * as React from 'react'; import FormLabel from '@mui/material/FormLabel'; import FormControl from '@mui/material/FormControl'; import FormGroup from '@mui/material/FormGroup'; import FormControlLabel from '@mui/material/FormControlLabel'; import FormHelperText from '@mui/material/FormHelperText'; import Switch from '@mui/material/Switch'; export default function SwitchesGroup() { const [state, setState] = React.useState({ gilad: true, jason: false, antoine: true, }); const handleChange = (event: React.ChangeEvent) => { setState({ ...state, [event.target.name]: event.target.checked, }); }; return ( Assign responsibility } label="Gilad Gray" /> } label="Jason Killian" /> } label="Antoine Llorca" /> Be careful ); } ``` ## Customization Here are some examples of customizing the component. You can learn more about this in the [overrides documentation page](/material-ui/customization/how-to-customize/). ```tsx import { styled } from '@mui/material/styles'; import FormGroup from '@mui/material/FormGroup'; import FormControlLabel from '@mui/material/FormControlLabel'; import Switch, { SwitchProps } from '@mui/material/Switch'; import Stack from '@mui/material/Stack'; import Typography from '@mui/material/Typography'; const MaterialUISwitch = styled(Switch)(({ theme }) => ({ width: 62, height: 34, padding: 7, '& .MuiSwitch-switchBase': { margin: 1, padding: 0, transform: 'translateX(6px)', '&.Mui-checked': { color: '#fff', transform: 'translateX(22px)', '& .MuiSwitch-thumb:before': { backgroundImage: `url('data:image/svg+xml;utf8,')`, }, '& + .MuiSwitch-track': { opacity: 1, backgroundColor: '#aab4be', ...theme.applyStyles('dark', { backgroundColor: '#8796A5', }), }, }, }, '& .MuiSwitch-thumb': { backgroundColor: '#001e3c', width: 32, height: 32, '&::before': { content: "''", position: 'absolute', width: '100%', height: '100%', left: 0, top: 0, backgroundRepeat: 'no-repeat', backgroundPosition: 'center', backgroundImage: `url('data:image/svg+xml;utf8,')`, }, ...theme.applyStyles('dark', { backgroundColor: '#003892', }), }, '& .MuiSwitch-track': { opacity: 1, backgroundColor: '#aab4be', borderRadius: 20 / 2, ...theme.applyStyles('dark', { backgroundColor: '#8796A5', }), }, })); const Android12Switch = styled(Switch)(({ theme }) => ({ padding: 8, '& .MuiSwitch-track': { borderRadius: 22 / 2, '&::before, &::after': { content: '""', position: 'absolute', top: '50%', transform: 'translateY(-50%)', width: 16, height: 16, }, '&::before': { backgroundImage: `url('data:image/svg+xml;utf8,')`, left: 12, }, '&::after': { backgroundImage: `url('data:image/svg+xml;utf8,')`, right: 12, }, }, '& .MuiSwitch-thumb': { boxShadow: 'none', width: 16, height: 16, margin: 2, }, })); const IOSSwitch = styled((props: SwitchProps) => ( ))(({ theme }) => ({ width: 42, height: 26, padding: 0, '& .MuiSwitch-switchBase': { padding: 0, margin: 2, transitionDuration: '300ms', '&.Mui-checked': { transform: 'translateX(16px)', color: '#fff', '& + .MuiSwitch-track': { backgroundColor: '#65C466', opacity: 1, border: 0, ...theme.applyStyles('dark', { backgroundColor: '#2ECA45', }), }, '&.Mui-disabled + .MuiSwitch-track': { opacity: 0.5, }, }, '&.Mui-focusVisible .MuiSwitch-thumb': { color: '#33cf4d', border: '6px solid #fff', }, '&.Mui-disabled .MuiSwitch-thumb': { color: theme.palette.grey[100], ...theme.applyStyles('dark', { color: theme.palette.grey[600], }), }, '&.Mui-disabled + .MuiSwitch-track': { opacity: 0.7, ...theme.applyStyles('dark', { opacity: 0.3, }), }, }, '& .MuiSwitch-thumb': { boxSizing: 'border-box', width: 22, height: 22, }, '& .MuiSwitch-track': { borderRadius: 26 / 2, backgroundColor: '#E9E9EA', opacity: 1, transition: theme.transitions.create(['background-color'], { duration: 500, }), ...theme.applyStyles('dark', { backgroundColor: '#39393D', }), }, })); const AntSwitch = styled(Switch)(({ theme }) => ({ width: 28, height: 16, padding: 0, display: 'flex', '&:active': { '& .MuiSwitch-thumb': { width: 15, }, '& .MuiSwitch-switchBase.Mui-checked': { transform: 'translateX(9px)', }, }, '& .MuiSwitch-switchBase': { padding: 2, '&.Mui-checked': { transform: 'translateX(12px)', color: '#fff', '& + .MuiSwitch-track': { opacity: 1, backgroundColor: '#1890ff', ...theme.applyStyles('dark', { backgroundColor: '#177ddc', }), }, }, }, '& .MuiSwitch-thumb': { boxShadow: '0 2px 4px 0 rgb(0 35 11 / 20%)', width: 12, height: 12, borderRadius: 6, transition: theme.transitions.create(['width'], { duration: 200, }), }, '& .MuiSwitch-track': { borderRadius: 16 / 2, opacity: 1, backgroundColor: 'rgba(0,0,0,.25)', boxSizing: 'border-box', ...theme.applyStyles('dark', { backgroundColor: 'rgba(255,255,255,.35)', }), }, })); export default function CustomizedSwitches() { return ( } label="MUI switch" /> } label="Android 12" /> } label="iOS style" /> Off On ); } ``` 🎨 If you are looking for inspiration, you can check [MUI Treasury's customization examples](https://mui-treasury.com/primitive/switch). ## Label placement You can change the placement of the label: ```tsx import Switch from '@mui/material/Switch'; import FormGroup from '@mui/material/FormGroup'; import FormControlLabel from '@mui/material/FormControlLabel'; import FormControl from '@mui/material/FormControl'; import FormLabel from '@mui/material/FormLabel'; export default function FormControlLabelPosition() { return ( Label placement } label="Bottom" labelPlacement="bottom" /> } label="End" labelPlacement="end" /> ); } ``` ## When to use - [Checkboxes vs. Switches](https://uxplanet.org/checkbox-vs-toggle-switch-7fc6e83f10b8) ## Accessibility - All form controls should have labels, and this includes radio buttons, checkboxes, and switches. In most cases, this is done by using the `