');}.css-vnvfgk .MuiSwitch-switchBase.Mui-checked+.MuiSwitch-track{opacity:1;background-color:#aab4be;}.css-vnvfgk .MuiSwitch-thumb{background-color:#001e3c;width:32px;height:32px;}.css-vnvfgk .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-vnvfgk .MuiSwitch-track{opacity:1;background-color:#aab4be;border-radius:10px;}.css-12ncwjy{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-12ncwjy{-webkit-print-color-adjust:exact;color-adjust:exact;}}.css-12ncwjy .MuiSwitch-track{border-radius:11px;}.css-12ncwjy .MuiSwitch-track:before,.css-12ncwjy .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-12ncwjy .MuiSwitch-track:before{background-image:url('data:image/svg+xml;utf8, ');left:12px;}.css-12ncwjy .MuiSwitch-track:after{background-image:url('data:image/svg+xml;utf8, ');right:12px;}.css-12ncwjy .MuiSwitch-thumb{box-shadow:none;width:16px;height:16px;margin:2px;}.css-1tcbki6{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-1tcbki6{-webkit-print-color-adjust:exact;color-adjust:exact;}}.css-1tcbki6 .MuiSwitch-switchBase{padding:0;margin:2px;transition-duration:300ms;}.css-1tcbki6 .MuiSwitch-switchBase.Mui-checked{-webkit-transform:translateX(16px);-moz-transform:translateX(16px);-ms-transform:translateX(16px);transform:translateX(16px);color:#fff;}.css-1tcbki6 .MuiSwitch-switchBase.Mui-checked+.MuiSwitch-track{background-color:#65C466;opacity:1;border:0;}.css-1tcbki6 .MuiSwitch-switchBase.Mui-checked.Mui-disabled+.MuiSwitch-track{opacity:0.5;}.css-1tcbki6 .MuiSwitch-switchBase.Mui-focusVisible .MuiSwitch-thumb{color:#33cf4d;border:6px solid #fff;}.css-1tcbki6 .MuiSwitch-switchBase.Mui-disabled .MuiSwitch-thumb{color:#f5f5f5;}.css-1tcbki6 .MuiSwitch-switchBase.Mui-disabled+.MuiSwitch-track{opacity:0.7;}.css-1tcbki6 .MuiSwitch-thumb{box-sizing:border-box;width:22px;height:22px;}.css-1tcbki6 .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-vb6e92{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-vb6e92>:not(style)+:not(style){margin:0;margin-left:8px;}.css-1s6m0eo{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-1s6m0eo{-webkit-print-color-adjust:exact;color-adjust:exact;}}.css-1s6m0eo:active .MuiSwitch-thumb{width:15px;}.css-1s6m0eo:active .MuiSwitch-switchBase.Mui-checked{-webkit-transform:translateX(9px);-moz-transform:translateX(9px);-ms-transform:translateX(9px);transform:translateX(9px);}.css-1s6m0eo .MuiSwitch-switchBase{padding:2px;}.css-1s6m0eo .MuiSwitch-switchBase.Mui-checked{-webkit-transform:translateX(12px);-moz-transform:translateX(12px);-ms-transform:translateX(12px);transform:translateX(12px);color:#fff;}.css-1s6m0eo .MuiSwitch-switchBase.Mui-checked+.MuiSwitch-track{opacity:1;background-color:#1890ff;}.css-1s6m0eo .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-1s6m0eo .MuiSwitch-track{border-radius:8px;opacity:1;background-color:rgba(0,0,0,.25);box-sizing:border-box;}.css-p58oka{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-fma74j{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-fma74j.Mui-disabled{cursor:default;}.css-fma74j .MuiFormControlLabel-label.Mui-disabled{color:rgba(0, 0, 0, 0.38);}.css-8l6tpu{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-8l6tpu.Mui-disabled{cursor:default;}.css-8l6tpu .MuiFormControlLabel-label.Mui-disabled{color:rgba(0, 0, 0, 0.38);}.css-12bf8up{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-12bf8up.Mui-disabled{cursor:default;}.css-12bf8up .MuiFormControlLabel-label.Mui-disabled{color:rgba(0, 0, 0, 0.38);}.css-121gpia{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;position:relative;box-sizing:border-box;-webkit-tap-highlight-color:transparent;background-color:transparent;outline:0;border:0;margin:0;border-radius:0;padding: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;color:inherit;text-transform:initial;font-weight:700;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-size:0.875rem;line-height:1.75;min-height:36px;-webkit-transition:background-color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,box-shadow 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,border-color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:background-color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,box-shadow 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,border-color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;border-radius:50%;padding:0;min-width:0;width:40px;height:40px;z-index:1050;box-shadow:0px 3px 5px -1px rgba(0,0,0,0.2),0px 6px 10px 0px rgba(0,0,0,0.14),0px 1px 18px 0px rgba(0,0,0,0.12);color:rgba(0, 0, 0, 0.87);background-color:#CDD2D7;position:fixed;bottom:24px;right:24px;z-index:10;background-color:#C2E0FF;box-shadow:0px 4px 20px rgba(170, 180, 190, 0.3);}.css-121gpia::-moz-focus-inner{border-style:none;}.css-121gpia.Mui-disabled{pointer-events:none;cursor:default;}@media print{.css-121gpia{-webkit-print-color-adjust:exact;color-adjust:exact;}}.css-121gpia:active{box-shadow:0px 7px 8px -4px rgba(0,0,0,0.2),0px 12px 17px 2px rgba(0,0,0,0.14),0px 5px 22px 4px rgba(0,0,0,0.12);}.css-121gpia:hover{background-color:#f5f5f5;-webkit-text-decoration:none;text-decoration:none;}@media (hover: none){.css-121gpia:hover{background-color:#CDD2D7;}}.css-121gpia.Mui-focusVisible{box-shadow:0px 3px 5px -1px rgba(0,0,0,0.2),0px 6px 10px 0px rgba(0,0,0,0.14),0px 1px 18px 0px rgba(0,0,0,0.12);}.css-121gpia.Mui-disabled{color:rgba(0, 0, 0, 0.26);box-shadow:none;background-color:rgba(0, 0, 0, 0.12);}.css-121gpia:hover{background-color:#99CCF3;}.css-121gpia:active{box-shadow:0px 4px 20px rgba(170, 180, 190, 0.6);}.css-14rkw53{-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;width:1em;height:1em;display:inline-block;fill:currentColor;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;-webkit-transition:fill 200ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;transition:fill 200ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;font-size:1.5rem;color:#004C99;}
React Switch component - Material UI Skip to content MUI stands in solidarity with the Ukrainian people against the Russian invasion. Find out how you can help.
🚀 Join the MUI team! We're looking for React Engineers and other amazing roles-come find out more!
Contents
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 />
Copy (Or Ctrl + C) Label You 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 >
Copy (Or Ctrl + C) Size Use the size
prop to change the size of the switch.
< Switch { ... label} defaultChecked size = " small" />
< Switch { ... label} defaultChecked />
Copy (Or Ctrl + C)
< Switch { ... label} defaultChecked />
< Switch { ... label} defaultChecked color = " secondary" />
< Switch { ... label} defaultChecked color = " warning" />
< Switch { ... label} defaultChecked color = " default" />
< GreenSwitch { ... label} defaultChecked />
Copy (Or Ctrl + C) Controlled You can control the switch with the checked
and onChange
props:
< Switch
checked = { checked}
onChange = { handleChange}
inputProps = { { 'aria-label' : 'controlled' } }
/>
Copy (Or Ctrl + C) 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 ).
Customization Here 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 .
Label placement You 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' } } />
Copy (Or Ctrl + C)
Unstyled The component also comes with an unstyled version . It's ideal for doing heavy customizations and minimizing bundle size.