Switch
Switches toggle the state of a single setting on or off.
Introduction
Switches are very commonly used for adjusting 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 />
Playground
Component
After installation, you can start building with this component using the following basic elements:
<span class="token keyword">import</span> Switch <span class="token keyword">from</span> <span class="token string">'@mui/joy/Switch'</span><span class="token punctuation">;</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">function</span> <span class="token function">MyApp</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Switch</span></span> <span class="token punctuation">/></span></span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
Controlled
To create a controlled switch, use the checked
and onChange
props.
Label
When a Switch
is used together with FormControl
and FormLabel
, the switch is labelled automatically. You can also use FormHelperText
to include a description to the switch as well.
An alternative way is to place the Switch
component inside a label element. The Typography
is used in this case to leverage the typography scale from the theme.
CSS Variables
Play around with all the CSS variables available in the switch component to see how the design changes.
You can use those to customize the component on both the sx
prop and the theme.
<Switch />
CSS Variables
px
px
px
px
Accessibility
Here are a few tips to make sure you have an accessible switch component:
The
Switch
will render with thecheckbox
role as opposed toswitch
. This is mainly because the latter isn't widely supported yet. However, if you believe your audience will support it, make sure to test with assistive technology. Use theslotProps
prop to change the role:<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Switch</span></span> <span class="token attr-name">slotProps</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token literal-property property">input</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">role</span><span class="token operator">:</span> <span class="token string">'switch'</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>
Every form control component should have proper labels. This includes radio buttons, checkboxes, and switches. In most cases, this is done using the
<label>
element.- If a label can't be applied, make sure to add an attribute (e.g.
aria-label
,aria-labelledby
,title
) to the input slot inside theslotProps
prop.
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Switch</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>checkedA<span class="token punctuation">"</span></span> <span class="token attr-name">slotProps</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token string-property property">'aria-label'</span><span class="token operator">:</span> <span class="token string">'Switch A'</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span>
- If a label can't be applied, make sure to add an attribute (e.g.
Common examples
Material Design
You can also find the Material Design switch design, out of the box, in Material UI's documentation.
Fluent UI
Here's how you'd customize Joy UI's switch to make it look like Microsoft's Fluent UI:
- Unchecked state:
outlined
variant andneutral
color. - Checked state:
solid
variant andprimary
color.
iOS
Note how we've used the :active
pseudo-class to replicate the small thumb size increase, which happens when you press and hold the switch.