');}.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-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;}
React Switch(开关)组件 - Material UI 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!
目录
Switch 开关组件 开关控制能切换单个设置的开/关两个状态。
开关组件是在移动设备上调整设置的首选方式。 The option that the switch controls, as well as the state it's in, should be made clear from the corresponding inline label.
Press Enter to start editing
Copy (or Ctrl + C) 标签 得益于 FormControlLabel
组件,您可以为 Switch
提供标签。
Press Enter to start editing
Copy (or Ctrl + C) Size 大小 使用 size
属性来改变开关的大小。
Press Enter to start editing
Copy (or Ctrl + C)
Press Enter to start editing
Copy (or Ctrl + C) Controlled You can control the switch with the checked
and onChange
props:
Press Enter to start editing
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. (参见: 何时使用 )。
自定义样式开关 你可以参考以下一些例子来自定义组件。 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
它将渲染一个带有 checkbox
而不是 switch
角色的元素,鉴于该属性尚未得到广泛支持。 请首先测试目标受众的辅助技术 (assistive technology) 是否正确支持此 role 属性。 或者您可以使用 <Switch inputProps={{ role: 'switch' }}>
来更改 role 属性。
所有表单控件都应该带有标签,而这包括了单选按钮,复选框和开关。 In most cases, this is done by using the <label>
element (FormControlLabel ).
如果无法使用标签,您则必须在输入组件中直接添加属性。 在这种情况下,您可以通过 inputProps
属性来应用附加的属性(例如 aria-label
, aria-labelledby
, title
)。
< Switch value = " checkedA" inputProps = { { 'aria-label' : 'Switch A' } } />
Copy (or Ctrl + C)
Unstyled MUI Base provides a headless ("unstyled") version of this React Switch(开关)组件 . Try it if you need more flexibility in customization and a smaller bundle size.
API See the documentation below for a complete reference to all of the props and classes available to the components mentioned here.