Switch
>
Since v2.0.0
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.
Theming
This element uses Red Hat design system theming and can be used in themable contexts.
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 Applies a 64px border radius to at least 1 corner of a container. It should be used for elements needing a circle or pill-shaped background like Avatar, Badge, or Switch. It may also be used for Hybrid style branding. Applying to all 4 corners at once is optional. It may replace the default 3px radius when not all corners need the same value. |
|
--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 Applies a 2px border width to at least 1 side of a container. It should be used to add emphasis or visual separation beyond a 1px border. Recommended for elements needing extra thickness like Alert or Progress stepper. It must be used to ensure hover, focus, and active states are discernible by more than color alone per WCAG. Applying to all 4 sides at once is optional. |
|
--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 scheme) |
|
--rh-color-gray-60
|
Disabled text color in light mode Secondary text (light scheme) |
|
Other libraries
To learn more about our other libraries, visit this page.
Feedback
To give feedback about anything on this page, contact us.