');}.css-3v0kdy-MuiSwitch-root .MuiSwitch-switchBase.Mui-checked+.MuiSwitch-track{opacity:1;background-color:#aab4be;}.css-3v0kdy-MuiSwitch-root .MuiSwitch-thumb{background-color:#001e3c;width:32px;height:32px;}.css-3v0kdy-MuiSwitch-root .MuiSwitch-thumb:before{content:'';position:absolute;width:100%;height:100%;left:0;top:0;background-repeat:no-repeat;-webkit-background-position:center;background-position:center;background-image:url('data:image/svg+xml;utf8, ');}.css-3v0kdy-MuiSwitch-root .MuiSwitch-track{opacity:1;background-color:#aab4be;border-radius:10px;}.css-zhvqw0-MuiSwitch-root{display:-webkit-inline-box;display:-webkit-inline-flex;display:-ms-inline-flexbox;display:inline-flex;width:58px;height:38px;overflow:hidden;padding:12px;box-sizing:border-box;position:relative;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;z-index:0;vertical-align:middle;padding:8px;}@media print{.css-zhvqw0-MuiSwitch-root{-webkit-print-color-adjust:exact;color-adjust:exact;}}.css-zhvqw0-MuiSwitch-root .MuiSwitch-track{border-radius:11px;}.css-zhvqw0-MuiSwitch-root .MuiSwitch-track:before,.css-zhvqw0-MuiSwitch-root .MuiSwitch-track:after{content:"";position:absolute;top:50%;-webkit-transform:translateY(-50%);-moz-transform:translateY(-50%);-ms-transform:translateY(-50%);transform:translateY(-50%);width:16px;height:16px;}.css-zhvqw0-MuiSwitch-root .MuiSwitch-track:before{background-image:url('data:image/svg+xml;utf8, ');left:12px;}.css-zhvqw0-MuiSwitch-root .MuiSwitch-track:after{background-image:url('data:image/svg+xml;utf8, ');right:12px;}.css-zhvqw0-MuiSwitch-root .MuiSwitch-thumb{box-shadow:none;width:16px;height:16px;margin:2px;}.css-chcv83-MuiSwitch-root{display:-webkit-inline-box;display:-webkit-inline-flex;display:-ms-inline-flexbox;display:inline-flex;width:58px;height:38px;overflow:hidden;padding:12px;box-sizing:border-box;position:relative;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;z-index:0;vertical-align:middle;width:42px;height:26px;padding:0;margin:8px;}@media print{.css-chcv83-MuiSwitch-root{-webkit-print-color-adjust:exact;color-adjust:exact;}}.css-chcv83-MuiSwitch-root .MuiSwitch-switchBase{padding:0;margin:2px;transition-duration:300ms;}.css-chcv83-MuiSwitch-root .MuiSwitch-switchBase.Mui-checked{-webkit-transform:translateX(16px);-moz-transform:translateX(16px);-ms-transform:translateX(16px);transform:translateX(16px);color:#fff;}.css-chcv83-MuiSwitch-root .MuiSwitch-switchBase.Mui-checked+.MuiSwitch-track{background-color:#65C466;opacity:1;border:0;}.css-chcv83-MuiSwitch-root .MuiSwitch-switchBase.Mui-checked.Mui-disabled+.MuiSwitch-track{opacity:0.5;}.css-chcv83-MuiSwitch-root .MuiSwitch-switchBase.Mui-focusVisible .MuiSwitch-thumb{color:#33cf4d;border:6px solid #fff;}.css-chcv83-MuiSwitch-root .MuiSwitch-switchBase.Mui-disabled .MuiSwitch-thumb{color:#f5f5f5;}.css-chcv83-MuiSwitch-root .MuiSwitch-switchBase.Mui-disabled+.MuiSwitch-track{opacity:0.7;}.css-chcv83-MuiSwitch-root .MuiSwitch-thumb{box-sizing:border-box;width:22px;height:22px;}.css-chcv83-MuiSwitch-root .MuiSwitch-track{border-radius:13px;background-color:#E9E9EA;opacity:1;-webkit-transition:background-color 500ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:background-color 500ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;}.css-1q36rmd-MuiStack-root{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-direction:row;-ms-flex-direction:row;flex-direction:row;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;}.css-1q36rmd-MuiStack-root>:not(style)+:not(style){margin:0;margin-left:8px;}.css-nlsk2e-MuiSwitch-root{display:-webkit-inline-box;display:-webkit-inline-flex;display:-ms-inline-flexbox;display:inline-flex;width:58px;height:38px;overflow:hidden;padding:12px;box-sizing:border-box;position:relative;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;z-index:0;vertical-align:middle;width:28px;height:16px;padding:0;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;}@media print{.css-nlsk2e-MuiSwitch-root{-webkit-print-color-adjust:exact;color-adjust:exact;}}.css-nlsk2e-MuiSwitch-root:active .MuiSwitch-thumb{width:15px;}.css-nlsk2e-MuiSwitch-root:active .MuiSwitch-switchBase.Mui-checked{-webkit-transform:translateX(9px);-moz-transform:translateX(9px);-ms-transform:translateX(9px);transform:translateX(9px);}.css-nlsk2e-MuiSwitch-root .MuiSwitch-switchBase{padding:2px;}.css-nlsk2e-MuiSwitch-root .MuiSwitch-switchBase.Mui-checked{-webkit-transform:translateX(12px);-moz-transform:translateX(12px);-ms-transform:translateX(12px);transform:translateX(12px);color:#fff;}.css-nlsk2e-MuiSwitch-root .MuiSwitch-switchBase.Mui-checked+.MuiSwitch-track{opacity:1;background-color:#1890ff;}.css-nlsk2e-MuiSwitch-root .MuiSwitch-thumb{box-shadow:0 2px 4px 0 rgb(0 35 11 / 20%);width:12px;height:12px;border-radius:6px;-webkit-transition:width 200ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:width 200ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;}.css-nlsk2e-MuiSwitch-root .MuiSwitch-track{border-radius:8px;opacity:1;background-color:rgba(0,0,0,.25);box-sizing:border-box;}.css-3js15y{font-size:0;position:relative;display:inline-block;width:32px;height:20px;margin:10px;cursor:pointer;}.css-3js15y.Mui-disabled{opacity:0.4;cursor:not-allowed;}.css-3js15y .MuiSwitch-track{background:#B3C3D3;border-radius:10px;display:block;height:100%;width:100%;position:absolute;}.css-3js15y .MuiSwitch-thumb{display:block;width:14px;height:14px;top:3px;left:3px;border-radius:16px;background-color:#FFF;position:relative;-webkit-transition:all 200ms ease;transition:all 200ms ease;}.css-3js15y.Mui-focusVisible .MuiSwitch-thumb{background-color:rgba(255,255,255,1);box-shadow:0 0 1px 8px rgba(0,0,0,0.25);}.css-3js15y.Mui-checked .MuiSwitch-thumb{left:14px;top:3px;background-color:#FFF;}.css-3js15y.Mui-checked .MuiSwitch-track{background:#007FFF;}.css-3js15y .MuiSwitch-input{cursor:inherit;position:absolute;width:100%;height:100%;top:0;left:0;opacity:0;z-index:1;margin:0;}.css-faibt5-MuiSwitch-switchBase{position:absolute;top:0;left:0;z-index:1;color:#fff;-webkit-transition:left 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,-webkit-transform 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:left 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,transform 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;padding:9px;border-radius:50%;display:-webkit-inline-box;display:-webkit-inline-flex;display:-ms-inline-flexbox;display:inline-flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-pack:center;-ms-flex-pack:center;-webkit-justify-content:center;justify-content:center;box-sizing:border-box;-webkit-tap-highlight-color:transparent;background-color:transparent;outline:0;border:0;margin:0;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;vertical-align:middle;-moz-appearance:none;-webkit-appearance:none;-webkit-text-decoration:none;text-decoration:none;}.css-faibt5-MuiSwitch-switchBase::-moz-focus-inner{border-style:none;}@media print{.css-faibt5-MuiSwitch-switchBase{-webkit-print-color-adjust:exact;color-adjust:exact;}}.css-faibt5-MuiSwitch-switchBase.Mui-checked{-webkit-transform:translateX(20px);-moz-transform:translateX(20px);-ms-transform:translateX(20px);transform:translateX(20px);}.css-faibt5-MuiSwitch-switchBase.Mui-disabled{color:#f5f5f5;pointer-events:none;cursor:default;}.css-faibt5-MuiSwitch-switchBase.Mui-checked+.MuiSwitch-track{opacity:0.5;}.css-faibt5-MuiSwitch-switchBase.Mui-disabled+.MuiSwitch-track{opacity:0.12;}.css-faibt5-MuiSwitch-switchBase:hover{background-color:rgba(0, 0, 0, 0.04);}@media (hover: none){.css-faibt5-MuiSwitch-switchBase:hover{background-color:transparent;}}.css-faibt5-MuiSwitch-switchBase.Mui-checked{color:#1976d2;}.css-faibt5-MuiSwitch-switchBase.Mui-checked:hover{background-color:rgba(25, 118, 210, 0.04);}@media (hover: none){.css-faibt5-MuiSwitch-switchBase.Mui-checked:hover{background-color:transparent;}}.css-faibt5-MuiSwitch-switchBase.Mui-checked.Mui-disabled{color:rgb(167, 202, 237);}.css-faibt5-MuiSwitch-switchBase.Mui-checked+.MuiSwitch-track{background-color:#1976d2;}.css-1puj9nj-MuiSwitch-input{cursor:inherit;position:absolute;opacity:0;width:300%;height:100%;top:0;left:-100%;margin:0;padding:0;z-index:1;}.css-1gkix75-MuiSwitch-switchBase{position:absolute;top:0;left:0;z-index:1;color:#fff;-webkit-transition:left 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,-webkit-transform 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:left 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,transform 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;padding:9px;border-radius:50%;display:-webkit-inline-box;display:-webkit-inline-flex;display:-ms-inline-flexbox;display:inline-flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-pack:center;-ms-flex-pack:center;-webkit-justify-content:center;justify-content:center;box-sizing:border-box;-webkit-tap-highlight-color:transparent;background-color:transparent;outline:0;border:0;margin:0;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;vertical-align:middle;-moz-appearance:none;-webkit-appearance:none;-webkit-text-decoration:none;text-decoration:none;}.css-1gkix75-MuiSwitch-switchBase::-moz-focus-inner{border-style:none;}@media print{.css-1gkix75-MuiSwitch-switchBase{-webkit-print-color-adjust:exact;color-adjust:exact;}}.css-1gkix75-MuiSwitch-switchBase.Mui-checked{-webkit-transform:translateX(20px);-moz-transform:translateX(20px);-ms-transform:translateX(20px);transform:translateX(20px);}.css-1gkix75-MuiSwitch-switchBase.Mui-disabled{color:#f5f5f5;pointer-events:none;cursor:default;}.css-1gkix75-MuiSwitch-switchBase.Mui-checked+.MuiSwitch-track{opacity:0.5;}.css-1gkix75-MuiSwitch-switchBase.Mui-disabled+.MuiSwitch-track{opacity:0.12;}.css-1gkix75-MuiSwitch-switchBase:hover{background-color:rgba(0, 0, 0, 0.04);}@media (hover: none){.css-1gkix75-MuiSwitch-switchBase:hover{background-color:transparent;}}.css-1gkix75-MuiSwitch-switchBase.Mui-checked{color:#9c27b0;}.css-1gkix75-MuiSwitch-switchBase.Mui-checked:hover{background-color:rgba(156, 39, 176, 0.04);}@media (hover: none){.css-1gkix75-MuiSwitch-switchBase.Mui-checked:hover{background-color:transparent;}}.css-1gkix75-MuiSwitch-switchBase.Mui-checked.Mui-disabled{color:rgb(217, 172, 224);}.css-1gkix75-MuiSwitch-switchBase.Mui-checked+.MuiSwitch-track{background-color:#9c27b0;}.css-vvmzys{font-size:0;position:relative;display:inline-block;width:32px;height:20px;background:#B3C3D3;border-radius:10px;margin:10px;cursor:pointer;}.css-vvmzys.Switch-disabled{opacity:0.4;cursor:not-allowed;}.css-vvmzys.Switch-checked{background:#007FFF;}.css-64f9fw{display:block;width:14px;height:14px;top:3px;left:3px;border-radius:16px;background-color:#FFF;position:relative;-webkit-transition:all 200ms ease;transition:all 200ms ease;}.css-64f9fw.Switch-focusVisible{background-color:rgba(255,255,255,1);box-shadow:0 0 1px 8px rgba(0,0,0,0.25);}.css-64f9fw.Switch-checked{left:14px;top:3px;background-color:#FFF;}.css-1ch62ki{cursor:inherit;position:absolute;width:100%;height:100%;top:0;left:0;opacity:0;z-index:1;margin:0;}.css-qtciu2{display:inline-block;position:relative;width:62px;height:34px;padding:7px;}.css-1gw2u5y{background-color:#aab4be;border-radius:10px;width:100%;height:100%;display:block;}.css-113f5xj{position:absolute;display:block;background-color:#001e3c;width:32px;height:32px;border-radius:16px;top:1px;left:7px;-webkit-transition:-webkit-transform 150ms cubic-bezier(0.4, 0, 0.2, 1);transition:transform 150ms cubic-bezier(0.4, 0, 0.2, 1);}.css-113f5xj:before{display:block;content:"";width:100%;height:100%;background:url('data:image/svg+xml;utf8, ') center center no-repeat;}.css-113f5xj.focusVisible{background-color:#79B;}.css-113f5xj.checked{-webkit-transform:translateX(16px);-moz-transform:translateX(16px);-ms-transform:translateX(16px);transform:translateX(16px);}.css-113f5xj.checked:before{background-image:url('data:image/svg+xml;utf8, ');}.css-1exj8kq{position:absolute;width:100%;height:100%;top:0;left:0;opacity:0;z-index:1;margin:0;cursor:pointer;}.css-qfz70r-MuiFormGroup-root{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-direction:row;-ms-flex-direction:row;flex-direction:row;-webkit-box-flex-wrap:wrap;-webkit-flex-wrap:wrap;-ms-flex-wrap:wrap;flex-wrap:wrap;}.css-1hpqosy-MuiFormControlLabel-root{display:-webkit-inline-box;display:-webkit-inline-flex;display:-ms-inline-flexbox;display:inline-flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;cursor:pointer;vertical-align:middle;-webkit-tap-highlight-color:transparent;margin-left:16px;margin-right:16px;-webkit-flex-direction:column-reverse;-ms-flex-direction:column-reverse;flex-direction:column-reverse;}.css-1hpqosy-MuiFormControlLabel-root.Mui-disabled{cursor:default;}.css-1hpqosy-MuiFormControlLabel-root .MuiFormControlLabel-label.Mui-disabled{color:rgba(0, 0, 0, 0.38);}.css-1f2kvjf-MuiFormControlLabel-root{display:-webkit-inline-box;display:-webkit-inline-flex;display:-ms-inline-flexbox;display:inline-flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;cursor:pointer;vertical-align:middle;-webkit-tap-highlight-color:transparent;margin-left:16px;margin-right:-11px;-webkit-flex-direction:row-reverse;-ms-flex-direction:row-reverse;flex-direction:row-reverse;}.css-1f2kvjf-MuiFormControlLabel-root.Mui-disabled{cursor:default;}.css-1f2kvjf-MuiFormControlLabel-root .MuiFormControlLabel-label.Mui-disabled{color:rgba(0, 0, 0, 0.38);}.css-1c1s55e-MuiFormControlLabel-root{display:-webkit-inline-box;display:-webkit-inline-flex;display:-ms-inline-flexbox;display:inline-flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;cursor:pointer;vertical-align:middle;-webkit-tap-highlight-color:transparent;margin-left:16px;margin-right:16px;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;}.css-1c1s55e-MuiFormControlLabel-root.Mui-disabled{cursor:default;}.css-1c1s55e-MuiFormControlLabel-root .MuiFormControlLabel-label.Mui-disabled{color:rgba(0, 0, 0, 0.38);}.css-1fcz3bk{top:70px;margin-top:70px;width:210px;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;position:-webkit-sticky;position:sticky;height:calc(100vh - 70px);overflow-y:auto;padding:16px 32px 16px 0px;display:none;}@media (min-width:600px){.css-1fcz3bk{display:block;}}.css-7xjbio-MuiTypography-root{margin:0;font-size:1rem;line-height:1.5;letter-spacing:0;font-family:"IBM Plex Sans",-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";font-weight:400;margin-bottom:0.35em;margin-top:16px;padding-left:12px;font-size:.75rem;font-weight:600;color:#AAB4BE;}.css-usbye9{padding:0;margin:0;list-style:none;}.css-1i0gep7-MuiTypography-root{margin:0;font-size:1rem;line-height:1.5;letter-spacing:0;font-family:"IBM Plex Sans",-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";font-weight:400;padding:0;margin:0;list-style:none;}.css-3gagg1-MuiTypography-root-MuiLink-root{cursor:pointer;margin:0;color:#007FFF;display:block;-webkit-text-decoration:none;text-decoration:none;color:#0072E5;font-weight:600;display:-webkit-inline-box;display:-webkit-inline-flex;display:-ms-inline-flexbox;display:inline-flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;font-size:.8125rem;padding:0px 8px 0px 10px;margin:4px 0px 8px 0px;border-left:2px solid transparent;box-sizing:border-box;font-weight:500;color:#20262D;}.css-3gagg1-MuiTypography-root-MuiLink-root.MuiTypography-body1>svg{margin-top:2px;}.css-3gagg1-MuiTypography-root-MuiLink-root svg:last-child{margin-left:2px;}.css-3gagg1-MuiTypography-root-MuiLink-root:hover{border-left-color:#A5D8FF;color:#007FFF;}.css-3gagg1-MuiTypography-root-MuiLink-root:active{border-left-color:#A5D8FF;color:#007FFF;font-weight:600;}.css-dtdl18-MuiTypography-root-MuiLink-root{cursor:pointer;margin:0;color:#007FFF;display:block;-webkit-text-decoration:none;text-decoration:none;color:#0072E5;font-weight:600;display:-webkit-inline-box;display:-webkit-inline-flex;display:-ms-inline-flexbox;display:inline-flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;font-size:.8125rem;padding:0px 8px 0px 24px;margin:4px 0px 8px 0px;border-left:2px solid transparent;box-sizing:border-box;font-weight:500;color:#20262D;}.css-dtdl18-MuiTypography-root-MuiLink-root.MuiTypography-body1>svg{margin-top:2px;}.css-dtdl18-MuiTypography-root-MuiLink-root svg:last-child{margin-left:2px;}.css-dtdl18-MuiTypography-root-MuiLink-root:hover{border-left-color:#A5D8FF;color:#007FFF;}.css-dtdl18-MuiTypography-root-MuiLink-root:active{border-left-color:#A5D8FF;color:#007FFF;font-weight:600;}
React Switch component - Material-UI Skip to content Getting Started Components Layout Inputs Navigation Surfaces Feedback Data Display Data Grid Utils Lab Component API System Customization How To Guides Styles (legacy) Premium themes Discover More Blog
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.
< Switch { ... label} defaultChecked />
< Switch { ... label} />
< Switch { ... label} disabled defaultChecked />
< Switch { ... label} disabled />
LabelYou can provide a label to the Switch
thanks to the FormControlLabel
component.
< FormGroup >
< FormControlLabel control = { < Switch defaultChecked /> } label = " Label" />
< FormControlLabel disabled control = { < Switch /> } label = " Disabled" />
</ FormGroup >
SizeUse the size
prop to change the size of the switch.
< Switch { ... label} defaultChecked size = " small" />
< Switch { ... label} defaultChecked />
< Switch { ... label} defaultChecked />
< Switch { ... label} defaultChecked color = " secondary" />
< Switch { ... label} defaultChecked color = " warning" />
< Switch { ... label} defaultChecked color = " default" />
< GreenSwitch { ... label} defaultChecked />
ControlledYou can control the switch with the checked
and onChange
props:
< Switch
checked = { checked}
onChange = { handleChange}
inputProps = { { 'aria-label' : 'controlled' } }
/>
FormGroup
is a helpful wrapper used to group selection controls components that provides an easier API.
However, you are encouraged to use Checkboxes instead if multiple related controls are required. (See: When to use ).
CustomizationHere are some examples of customizing the component.
You can learn more about this in the overrides documentation page .
🎨 If you are looking for inspiration, you can check MUI Treasury's customization examples .
Unstyled switchesThe switch also comes with an unstyled version. It's ideal for doing heavy customizations and minimizing bundle size.
import SwitchUnstyled from '@mui/core/SwitchUnstyled' ;
The SwitchUnstyled
component provides default components and assigns CSS classes you can style entirely on your own.
You are free to choose any styling solution - plain CSS classes, a CSS framework, Emotion, etc.
It is also possible to replace these default components by other HTML elements or custom components.
There are three components you can override by the components
prop: Root
, Thumb
and Input
. Each one's props can be set using the componentsProps
object.
< SwitchUnstyled component = { Root} { ... label} defaultChecked />
< SwitchUnstyled component = { Root} { ... label} />
< SwitchUnstyled component = { Root} { ... label} defaultChecked disabled />
< SwitchUnstyled component = { Root} { ... label} disabled />
Recreation of Material-UI's Switch
< ThemeProvider theme = { createTheme ( ) } >
< Switch { ... label} defaultChecked />
< Switch { ... label} color = " secondary" />
< Switch { ... label} disabled defaultChecked />
< Switch { ... label} disabled />
</ ThemeProvider >
useSwitch hookFor the ultimate customizability, a useSwitch
hook is available.
It accepts almost the same options as the SwitchUnstyled component minus the component
, components
, and componentsProps
props.
import { useSwitch } from '@mui/core/SwitchUnstyled' ;
Basic example
< BasicSwitch defaultChecked />
< BasicSwitch />
< BasicSwitch defaultChecked disabled />
< BasicSwitch disabled />
Customized look and feel
< MUISwitch defaultChecked />
Recreation of Material-UI's Switch
< ThemeProvider theme = { createTheme ( ) } >
< Switch { ... label} defaultChecked />
< Switch { ... label} color = " secondary" />
< Switch { ... label} disabled defaultChecked />
< Switch { ... label} disabled />
</ ThemeProvider >
Label placementYou can change the placement of the label:
When to use
Accessibility
It will render an element with the checkbox
role not switch
role since this
role isn't widely supported yet. Please test first if assistive technology of your
target audience supports this role properly. Then you can change the role with
<Switch inputProps={{ role: 'switch' }}>
All form controls should have labels, and this includes radio buttons, checkboxes, and switches. In most cases, this is done by using the <label>
element (FormControlLabel ).
When a label can't be used, it's necessary to add an attribute directly to the input component.
In this case, you can apply the additional attribute (e.g. aria-label
, aria-labelledby
, title
) via the inputProps
prop.
< Switch value = " checkedA" inputProps = { { 'aria-label' : 'Switch A' } } />
Contents