');}.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;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:inline-flex;width:58px;height:38px;overflow:hidden;padding:12px;box-sizing:border-box;position:relative;flex-shrink:0;z-index:0;vertical-align:middle;padding:8px;}@media print{.css-12ncwjy{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%;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:inline-flex;width:58px;height:38px;overflow:hidden;padding:12px;box-sizing:border-box;position:relative;flex-shrink:0;z-index:0;vertical-align:middle;width:42px;height:26px;padding:0;margin:8px;}@media print{.css-1tcbki6{color-adjust:exact;}}.css-1tcbki6 .MuiSwitch-switchBase{padding:0;margin:2px;transition-duration:300ms;}.css-1tcbki6 .MuiSwitch-switchBase.Mui-checked{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;transition:background-color 500ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;}.css-vb6e92{display:flex;flex-direction:row;align-items:center;}.css-vb6e92>:not(style)+:not(style){margin:0;margin-left:8px;}.css-1s6m0eo{display:inline-flex;width:58px;height:38px;overflow:hidden;padding:12px;box-sizing:border-box;position:relative;flex-shrink:0;z-index:0;vertical-align:middle;width:28px;height:16px;padding:0;display:flex;}@media print{.css-1s6m0eo{color-adjust:exact;}}.css-1s6m0eo:active .MuiSwitch-thumb{width:15px;}.css-1s6m0eo:active .MuiSwitch-switchBase.Mui-checked{transform:translateX(9px);}.css-1s6m0eo .MuiSwitch-switchBase{padding:2px;}.css-1s6m0eo .MuiSwitch-switchBase.Mui-checked{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;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:flex;flex-direction:row;flex-wrap:wrap;}.css-fma74j{display:inline-flex;align-items:center;cursor:pointer;vertical-align:middle;-webkit-tap-highlight-color:transparent;margin-left:16px;margin-right:16px;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:inline-flex;align-items:center;cursor:pointer;vertical-align:middle;-webkit-tap-highlight-color:transparent;margin-left:16px;margin-right:-11px;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:inline-flex;align-items:center;cursor:pointer;vertical-align:middle;-webkit-tap-highlight-color:transparent;margin-left:16px;margin-right:16px;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:inline-flex;align-items: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;user-select:none;vertical-align:middle;-moz-appearance:none;-webkit-appearance: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-margin-top:calc(var(--MuiDocs-header-height) + 32px);min-height:36px;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{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;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{user-select:none;width:1em;height:1em;display:inline-block;fill:currentColor;flex-shrink:0;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.
< Switch { ... label} defaultChecked />
< Switch { ... label} />
< Switch { ... label} disabled defaultChecked />
< Switch { ... label} disabled />
Copy (Or Ctrl + C) 标签 得益于 FormControlLabel
组件,您可以为 Switch
提供标签。
< FormGroup >
< FormControlLabel control = { < Switch defaultChecked /> } label = " Label" />
< FormControlLabel disabled control = { < Switch /> } label = " Disabled" />
</ FormGroup >
Copy (Or Ctrl + C) Size 大小 使用 size
属性来改变开关的大小。
< 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. (参见: 何时使用 )。
自定义样式开关 你可以参考以下一些例子来自定义组件。 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 The component also comes with an unstyled version . It's ideal for doing heavy customizations and minimizing bundle size.