') center center no-repeat;}.css-1bzsqls.focusVisible{background-color:#79b;}.css-1bzsqls.checked{-webkit-transform:translateX(24px);-moz-transform:translateX(24px);-ms-transform:translateX(24px);transform:translateX(24px);}.css-1bzsqls.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-xwev9j{top:var(--MuiDocs-header-height);margin-top:var(--MuiDocs-header-height);padding-left:2px;position:-webkit-sticky;position:sticky;height:calc(100vh - var(--MuiDocs-header-height));overflow-y:auto;padding-top:32px;padding-bottom:32px;padding-right:32px;display:none;}@media (min-width:900px){.css-xwev9j{display:block;}}.css-1ltvbib{margin:0;font:inherit;color:#007FFF;-webkit-text-decoration:none;text-decoration:none;font-weight:700;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;color:#0072E5;margin-bottom:16px;margin-left:4px;margin-right:4px;padding:8px;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;gap:12px;background-color:rgba(243, 246, 249, 0.4);border:1px solid;border-color:#E5EAF2;border-radius:12px;transition-property:all;transition-timing:cubic-bezier(0.4, 0, 0.2, 1);transition-duration:150ms;}.css-1ltvbib.MuiTypography-body1>svg{margin-top:2px;}.css-1ltvbib svg:last-child{margin-left:2px;}.css-1ltvbib:hover{color:#0059B2;}.css-1ltvbib:hover,.css-1ltvbib:focus-visible{background-color:#F0F7FF;border-color:#99CCF3;}.css-1yuqj86{border-radius:3px;overflow:auto;width:-webkit-fit-content;width:-moz-fit-content;width:fit-content;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;}.css-spfw6s{height:6px;width:16px;background-color:#0057B7;}.css-1u18uw9{height:6px;width:16px;background-color:#FFD700;}.css-193blm8{margin:0;display:inline-block;font-size:0.75rem;line-height:1.5;letter-spacing:0;font-weight:700;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";scroll-snap-margin-top:calc(var(--MuiDocs-header-height) + 32px);scroll-margin-top:calc(var(--MuiDocs-header-height) + 32px);font-weight:500;color:#434D5B;}.css-eiadz7{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;scroll-snap-margin-top:calc(var(--MuiDocs-header-height) + 32px);scroll-margin-top:calc(var(--MuiDocs-header-height) + 32px);margin-bottom:0.35em;margin-top:16px;margin-bottom:8px;padding-left:11.2px;font-size:0.6875rem;font-weight:700;text-transform:uppercase;letter-spacing:.08rem;color:#6B7A90;}.css-17mrx6g{padding:0;margin:0;list-style:none;}.css-utqcip{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;scroll-snap-margin-top:calc(var(--MuiDocs-header-height) + 32px);scroll-margin-top:calc(var(--MuiDocs-header-height) + 32px);padding:0;margin:0;list-style:none;}.css-6k4vws{margin:0;font:inherit;color:#007FFF;display:block;-webkit-text-decoration:none;text-decoration:none;font-weight:700;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;color:#0072E5;font-size:0.8125rem;padding:0px 8px 0px 10px;margin:4px 0px 8px 0px;border-left:1px solid transparent;box-sizing:border-box;font-weight:500;color:#1C2025;}.css-6k4vws.MuiTypography-body1>svg{margin-top:2px;}.css-6k4vws svg:last-child{margin-left:2px;}.css-6k4vws:hover{color:#0059B2;}.css-6k4vws:hover{border-left-color:#B0B8C4;color:#6B7A90;}.css-6k4vws:active{border-left-color:#99CCF3;color:#0072E5;}.css-6k4vws:active:hover{border-left-color:#0072E5;color:#0072E5;}.css-wnca9u{margin:0;font:inherit;color:#007FFF;display:block;-webkit-text-decoration:none;text-decoration:none;font-weight:700;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;color:#0072E5;font-size:0.8125rem;padding:0px 8px 0px 20px;margin:4px 0px 8px 0px;border-left:1px solid transparent;box-sizing:border-box;font-weight:500;color:#1C2025;}.css-wnca9u.MuiTypography-body1>svg{margin-top:2px;}.css-wnca9u svg:last-child{margin-left:2px;}.css-wnca9u:hover{color:#0059B2;}.css-wnca9u:hover{border-left-color:#B0B8C4;color:#6B7A90;}.css-wnca9u:active{border-left-color:#99CCF3;color:#0072E5;}.css-wnca9u:active:hover{border-left-color:#0072E5;color:#0072E5;}.css-1f23llj{position:fixed;bottom:24px;right:24px;z-index:10;}.css-1gnaizj{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:#C7D0DD;background-color:#C2E0FF;box-shadow:0px 4px 20px rgba(170, 180, 190, 0.3);}.css-1gnaizj::-moz-focus-inner{border-style:none;}.css-1gnaizj.Mui-disabled{pointer-events:none;cursor:default;}@media print{.css-1gnaizj{-webkit-print-color-adjust:exact;color-adjust:exact;}}.css-1gnaizj: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-1gnaizj:hover{background-color:#f5f5f5;-webkit-text-decoration:none;text-decoration:none;}@media (hover: none){.css-1gnaizj:hover{background-color:#C7D0DD;}}.css-1gnaizj.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-1gnaizj.Mui-disabled{color:rgba(0, 0, 0, 0.26);box-shadow:none;background-color:rgba(0, 0, 0, 0.12);}.css-1gnaizj:hover{background-color:#99CCF3;}.css-1gnaizj: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 and hook - Base UI Skip to content
Switch Switches are UI elements that let users choose between two states—most commonly on/off.
Introduction The Switch 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 { Switch } from '@mui/base/Switch' ;
export default function MyApp ( ) {
return < Switch /> ;
}
(or Ctrl + C)
Basics The following demo shows how to assign styles and props to the Switch component:
Anatomy The Switch 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>
(or Ctrl + C)
Custom structure Use the slots
prop to override the root or any other interior slot:
< Switch slots = { { root : 'div' , track : 'div' } } />
(or Ctrl + C)
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:
< Switch slotProps = { { thumb : { className : 'my-thumb' } } } />
(or Ctrl + C)
Usage with TypeScript In TypeScript, you can specify the custom component type used in the slots.root
as a generic parameter of the unstyled component. This way, you can safely provide the custom root's props directly on the component:
< Switch< typeof CustomComponent> slots= { { root: CustomComponent } } customProp / >
(or Ctrl + C)
The same applies for props specific to custom primitive elements:
< Switch< 'input' > slots= { { root: 'input' } } autoFocus= { true } / >
(or Ctrl + C)
Hook import { useSwitch } from '@mui/base/useSwitch' ;
(or Ctrl + C)
The useSwitch
hook lets you apply the functionality of a switch 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 HTML structure .
Basic example
Press Enter to start editing
(or Ctrl + C)
Press Enter to start editing
(or Ctrl + C) Accessibility To make the switch component accessible, you should ensure that the corresponding labels reflect the current state of the switch.
MUI stands in solidarity with Ukraine. Contents