Switch
Importing
Add rh-switch to your page with this import statement:
<script type="module">
import '@rhds/elements/rh-switch/rh-switch.js';
</script>
Copy to Clipboard
Copied!
Wrap lines
Overflow lines
To learn more about installing RHDS elements on your site using an import map read our getting started docs.
Usage
<rh-switch id="switch-a" accessible-label="Switch A" message-on="Message when on" message-off="Message when off" checked=""></rh-switch>
<script type="module">
import '@rhds/elements/rh-switch/rh-switch.js';
</script>
<style>
rh-switch {
margin: var(--rh-space-lg);
}
</style>
Copy to Clipboard
Copied!
Wrap lines
Overflow lines
rh-switch
A switch provides a visible toggle for a setting. Authors must supply
an accessible label via accessible-label or a <label for>. The
element uses ARIA role="switch" with aria-checked for screen
readers. Users should toggle with Space or Enter keys. Avoid using
a switch when multiple selections are needed; use checkboxes instead.
Slots
2
| Slot Name | Summary | Description |
|---|---|---|
message-on
|
Message content when checked. Overrides the |
Accepts inline content such as |
message-off
|
Message content when unchecked. Overrides the |
Accepts inline content such as |
Attributes
7
| Attribute | DOM Property | Description | Type | Default |
|---|---|---|---|---|
accessible-label
|
accessibleLabel |
invisible, accessible label for screen readers |
|
|
message-on
|
messageOn |
Message to display when the switch is on (i.e. checked) |
|
|
message-off
|
messageOff |
Message to display when the switch is off (i.e. unchecked) |
|
|
show-check-icon
|
showCheckIcon |
If the checkmark icon should be displayed when the switch is on |
|
|
checked
|
checked |
Whether the switch is on (checked) |
|
|
disabled
|
disabled |
If the switch is disabled |
|
|
reversed
|
reversed |
If the switch is reversed: message first, then control |
|
|
Methods
0
None
Events
1
| Event Name | Description |
|---|---|
change
|
Fires when the user toggles the switch on or
off via click, Space, or Enter. Does not fire when disabled. The
event bubbles and carries no detail; read the |
CSS Shadow Parts
2
| Part Name | Summary | Description |
|---|---|---|
container
|
The outer flex container for the switch and message slots |
|
switch
|
The toggle track and handle |
CSS Custom Properties
3
| CSS Property | Description | Default |
|---|---|---|
--rh-switch-unchecked |
Unchecked track color in light mode Unchecked track color in dark mode |
var(--rh-color-gray-40, #a3a3a3)
|
--rh-switch-checked |
light-dark(
/** Checked track color in light mode */
var(--rh-color-accent-base-on-light, #0066cc),
/** Checked track color in dark mode */
var(--rh-color-accent-base-on-dark, #92c5f9)
)
|
|
--rh-switch-disabled |
Disabled track color in light mode Disabled track color in dark mode |
var(--rh-color-gray-60, #4d4d4d)
|
Design Tokens
16
| Token | Description | Copy |
|---|---|---|
--rh-space-lg
|
Gap between track and message 16px spacer |
|
--rh-color-surface-lightest
|
Handle color in light mode Primary surface (light theme) |
|
--rh-color-surface-dark
|
Tertiary surface (dark theme) |
|
--rh-box-shadow-sm
|
Handle shadow in dark mode Small box shadow |
|
--rh-border-radius-pill
|
Track border radius Handle border radius Pill border radius; Example: Label |
|
--rh-color-text-secondary
|
Unchecked message text color |
|
--rh-color-accent-base-on-light
|
Checked track color in light mode Inline link (light theme) |
|
--rh-color-accent-base-on-dark
|
Checked track color in dark mode Inline link (dark theme) |
|
--rh-color-text-primary
|
Checked message text color |
|
--rh-border-width-md
|
Focus ring outline width 2px border width: Example: Alert |
|
--rh-color-border-interactive
|
Focus ring outline color |
|
--rh-space-sm
|
6px spacer |
|
--rh-color-gray-50
|
Disabled message text color Subtle icon |
|
--rh-color-gray-40
|
Disabled text color in dark mode Subtle icon (hover state) |
|
--rh-color-gray-30
|
Subtle borders (light theme) |
|
--rh-color-gray-60
|
Disabled text color in light mode Secondary text (light theme) |
|
Other libraries
To learn more about our other libraries, visit this page.
Feedback
To give feedback about anything on this page, contact us.