') center center no-repeat;}.css-1ydgzvd.focusVisible{background-color:#79b;}.css-1ydgzvd.checked{-webkit-transform:translateX(24px);-moz-transform:translateX(24px);-ms-transform:translateX(24px);transform:translateX(24px);}.css-1ydgzvd.checked::before{background-image:url('data:image/svg+xml;utf8, ');}.css-u9fjb1{position:absolute;width:100%;height:100%;top:0;left:0;opacity:0;z-index:1;margin:0;cursor:pointer;}.css-1xc2jgn{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;scroll-snap-margin-top:calc(var(--MuiDocs-header-height) + 32px);scroll-margin-top:calc(var(--MuiDocs-header-height) + 32px);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-1xc2jgn::-moz-focus-inner{border-style:none;}.css-1xc2jgn.Mui-disabled{pointer-events:none;cursor:default;}@media print{.css-1xc2jgn{-webkit-print-color-adjust:exact;color-adjust:exact;}}.css-1xc2jgn: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-1xc2jgn:hover{background-color:#f5f5f5;-webkit-text-decoration:none;text-decoration:none;}@media (hover: none){.css-1xc2jgn:hover{background-color:#CDD2D7;}}.css-1xc2jgn.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-1xc2jgn.Mui-disabled{color:rgba(0, 0, 0, 0.26);box-shadow:none;background-color:rgba(0, 0, 0, 0.12);}.css-1xc2jgn:hover{background-color:#99CCF3;}.css-1xc2jgn: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;}
Unstyled React Switch component and hook - MUI Base 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
Unstyled Switch Switches are UI elements that let users choose between two states—most commonly on/off.
Introduction The SwitchUnstyled
component provides users with a switch for toggling between two mutually exclusive states.
Component Usage After installation , you can start building with this component using the following basic elements:
import SwitchUnstyled from '@mui/base/SwitchUnstyled' ;
export default function MyApp ( ) {
return < SwitchUnstyled /> ;
}
Copy (or Ctrl + C)
Basics The following demo shows how to assign styles and props to the SwitchUnstyled
component:
Press Enter to start editing
Copy (or Ctrl + C) Anatomy The SwitchUnstyled
component is composed of a root <span>
that houses three interior slots—a track, a thumb, and an input:
< span class = " MuiSwitch-root" >
< span class = " MuiSwitch-track" > </ span>
< span class = " MuiSwitch-thumb" > </ span>
< input type = " checkbox" class = " MuiSwitch-input" />
</ span>
Copy (or Ctrl + C)
Slot props Use the component
prop to override the root slot with a custom element:
< SwitchUnstyled component = " div" />
Copy (or Ctrl + C)
Use the slots
prop to override any interior slots in addition to the root:
< SwitchUnstyled slots = { { root : 'div' , track : 'div' } } />
Copy (or Ctrl + C)
If the root element is customized with both the component
and slots
props, then component
will take precedence.
Use the slotProps
prop to pass custom props to internal slots.
The following code snippet applies a CSS class called my-thumb
to the thumb slot:
< SwitchUnstyled slotProps = { { thumb : { className : 'my-thumb' } } } />
Copy (or Ctrl + C)
Hook import { useSwitch } from '@mui/base/SwitchUnstyled' ;
Copy (or Ctrl + C)
The useSwitch
hook lets you apply the functionality of SwitchUnstyled
to a fully custom component.
It returns props to be placed on the custom component, along with fields representing the component's internal state.
Hooks do not support slot props , but they do support customization props .
Hooks give you the most room for customization, but require more work to implement.
With hooks, you can take full control over how your component is rendered, and define all the custom props and CSS classes you need.
You may not need to use hooks unless you find that you're limited by the customization options of their component counterparts—for instance, if your component requires significantly different internal slot structure .
Basic example
Press Enter to start editing
Copy (or Ctrl + C)
Press Enter to start editing
Copy (or Ctrl + C) Accessibility To make the switch component accessible, you should ensure that the corresponding labels reflect the current state of the switch.