Switch
sl-switch
Switches allow the user to toggle an option on or off.
Examples
Basic Switch
<sl-switch>Shareholder view</sl-switch>
sl-switch Shareholder view
This component works with standard <form>
elements. Please refer to the section on
form controls to learn more about form submission and
client-side validation.
Label Position
Use the label-position
attribute to change the position of the switch’s label. The default
position is right
.
<sl-switch>Shareholder view</sl-switch> <sl-divider style="--spacing: 2rem;"></sl-divider> <sl-switch label-position="left">President view</sl-switch> <sl-divider style="--spacing: 2rem;"></sl-divider> <sl-switch label-position="left-justified">Admin view</sl-switch>
sl-switch Shareholder view sl-divider style="--spacing: 2rem;" sl-switch label-position="left" President view sl-divider style="--spacing: 2rem;" sl-switch label-position="left-justified" Shareholder view
Checked
Use the checked
attribute to activate the switch.
<sl-switch checked>Checked</sl-switch>
sl-switch checked=true Checked
Disabled
Use the disabled
attribute to disable the switch.
<sl-switch disabled>Disabled</sl-switch>
sl-switch disabled=true Disabled
Sizes
Use the size
attribute to change a switch’s size. Size medium
is the switch’s
default.
Size large
is currently not part of the Teamshares Design System, and there is no Figma
component for this option. Please check with the design team before using this option.
<sl-switch size="small">Small switch</sl-switch> <br /> <sl-switch size="medium">Medium switch (default)</sl-switch>
sl-switch size="small" Small switch br sl-switch size="medium" Medium switch (default)
Help Text
Add descriptive help text to a switch with the help-text
attribute. For help texts that contain
HTML, use the help-text
slot instead.
Note: To avoid awkward alignment of the switch, label, and help text, don’t display help
text when using label-position="left"
.
<sl-switch help-text="Displays company financials">Shareholder view</sl-switch> <sl-divider style="--spacing: 2rem;"></sl-divider> <sl-switch label-position="left-justified" help-text="Displays network financials and reporting data">Admin view</sl-switch> <sl-divider style="--spacing: 2rem;"></sl-divider> <sl-switch label-position="left" help-text="Don't display help text when using label-position: 'left'" checked>Please avoid doing this</sl-switch>
sl-switch help-text="Displays company financials" | Shareholder view sl-divider style="--spacing: 2rem;" sl-switch label-position="left-justified" help-text="Displays network financials and reporting data" | Shareholder view
Custom Styles
Use the available custom properties to change how the switch is styled.
Note: In general, you shouldn’t need to do this. If you are working on a design that requires custom styling, please ensure that there’s not a standard switch in the design system that would work instead. If you really do need a non-standard switch, please consult the design team before implementing a custom version, so that the team can determine whether the existing pattern should be updated.
<sl-switch style="--width: 80px; --height: 40px; --thumb-size: 36px;"><span style="font-size:1.25rem; padding-left: .5rem;">Large custom switch</span></sl-switch>
sl-switch style="--width: 80px; --height: 40px; --thumb-size: 36px;" Really big
Usage
Switch or Checkbox?
- Use a switch to let people toggle a setting that takes effect immediately, like a light switch
- Use a checkbox to let people toggle a selection that must be saved before taking effect
Switch Labels
Label Dos
- Always have a label for the switch
- Use sentence case
- Keep labels short and easy to scan
- Use a leading verb only when it clarifies the switch’s purpose
Label Don’ts
- Don’t end labels with punctuation
- Avoid labels that describe the switch’s “on” state (e.g. “Turn on” or “Enable”)
-
Don’t update the label dynamically based on a switch’s
checked
state
Do
- Do use sentence case and keep the label short
- Do use a leading verb if it clarifies what the switch does
Don’t
- Don’t use title case or end with punctuation
- Don’t use negative labels (like “hide” and “disable”) that conflict with the “on” state of the switch
- Don’t repeat the switch’s “on” state in the label
Testing
With Cypress
Adding data-test-id
to a component
To test sl-switch
, add the data-test-id
attribute directly to the component:
sl-switch[ data-test-id="switch-test" ] | Switch test
Cypress commands for sl-switch
Because sl-switch
uses <input type="checkbox">
, we can test the switch the
same way we would test the checkbox.
To toggle the switch ON:
cy.slCheckboxCheck(`[data-test-id="switch-test"]`);
To toggle the switch OFF::
cy.slCheckboxUncheck(`[data-test-id="switch-test"]`);
To verify the switch is ON:
cy.get(`[data-test-id="switch-test"]`).should("have.prop", "checked", true);
To verify the switch is OFF:
cy.get(`[data-test-id="switch-test"]`).should("have.prop", "checked", false);
Component Props
Note: The following appear as options in the Properties table but are currently not part of the Teamshares Design System. Please check with the design team before using these options:
- Size
large
Property | Default | Details |
---|---|---|
name
|
''
|
The name of the switch, submitted as a name/value pair with form data. |
value
|
— |
The current value of the switch, submitted as a name/value pair with form data. |
labelPosition
label-position
|
'right'
|
The switch’s label position. |
size
|
'medium'
|
The switch’s size. |
disabled
|
false
|
Disables the switch. |
checked
|
false
|
Draws the switch in a checked state. |
defaultChecked
|
false
|
The default value of the form control. Primarily used for resetting the form control. |
form
|
''
|
By default, form controls are associated with the nearest containing
|
required
|
false
|
Makes the switch a required field. |
helpText
help-text
|
''
|
The switch’s help text. If you need to display HTML, use the |
validity
|
— |
— Gets the validity state object |
validationMessage
|
— |
— Gets the validation message |
updateComplete
|
A read-only promise that resolves when the component has finished updating. |
Learn more about attributes and properties.
Slots
Name | Details |
---|---|
(default) | The switch’s label. |
help-text
|
Text that describes how to use the switch. Alternatively, you can use the
help-text attribute.
|
Learn more about using slots.
Events
Name | Name | Name | React Event | Details | |
---|---|---|---|---|---|
sl-blur
|
sl-blur
|
sl-blur
|
onSlBlur
|
Emitted when the control loses focus. |
|
sl-change
|
sl-change
|
sl-change
|
onSlChange
|
Emitted when the control’s checked state changes. |
|
sl-input
|
sl-input
|
sl-input
|
onSlInput
|
Emitted when the control receives input. |
|
sl-focus
|
sl-focus
|
sl-focus
|
onSlFocus
|
Emitted when the control gains focus. |
|
sl-invalid
|
sl-invalid
|
sl-invalid
|
onSlInvalid
|
Emitted when the form control has been checked for validity and its constraints aren’t satisfied. |
Learn more about events.
Methods
Name | Details |
---|---|
click()
|
Simulates a click on the switch. |
focus()
|
Sets focus on the switch. |
blur()
|
Removes focus from the switch. |
checkValidity()
|
Checks for validity but does not show a validation message. Returns |
getForm()
|
Gets the associated form, if one exists. |
reportValidity()
|
Checks for validity and shows the browser’s validation message if the control is invalid. |
setCustomValidity()
|
Sets a custom validation message. Pass an empty string to restore validity. |
Learn more about methods.
Custom Properties
Name | Details |
---|---|
--width
|
The width of the switch. |
--height
|
The height of the switch. |
--thumb-size
|
The size of the thumb. |
Learn more about customizing CSS custom properties.
CSS Parts
Name | Description |
---|---|
base
|
The component’s base wrapper. |
control
|
The control that houses the switch’s thumb. |
thumb
|
The switch’s thumb. |
label
|
The switch’s label. |
form-control-help-text
|
The help text’s wrapper. |
Learn more about customizing CSS parts.