Skip to main content

rc-switch

Switch wrapper that enhances a native <input type="checkbox">.

Package
@rcarls/rc-switch
Element
<rc-switch>
Native dependency
Direct-child native checkbox
State model
Controlled or uncontrolled checked state
Main events
rc-switch-change

Installation

npm install @rcarls/rc-switch
import '@rcarls/rc-switch/define';

Markup

<label for="dark-mode">Dark mode</label>
<rc-switch>
<input id="dark-mode" name="darkMode" type="checkbox" />
</rc-switch>

The native checkbox stays in light DOM for labels, forms, validation, reset, and no-JavaScript fallback. rc-switch adds role="switch" only when the input does not already have a role.

Without a theme, the native checkbox remains visible. Packaged themes hide its visual box and enable the component's track, thumb, icons, and motion while leaving the checkbox connected and interactive.

Live demo

API

Properties

PropertyMarkupTypeDefaultDescription
checkedcheckedbooleanNot specifiedCurrent checked state. Host writes are silent.
defaultCheckeddefault-checkedbooleanNot specifiedInitial checked state for uncontrolled usage.
disableddisabledbooleanfalseMirror disabled state to the native checkbox.
iconsiconsbooleanfalseShow selected and deselected icon affordance containers.
showOnlySelectedIconshow-only-selected-iconbooleanfalseShow only the selected icon affordance container.

Methods

No public methods are documented in the custom elements manifest.

Events

EventDetail typeDescription
rc-switch-changeNo detail type documentedFired after user interaction changes the checked state.

Slots

NameDescription
(default)A direct native `<input type="checkbox">` child.

CSS Custom Properties

PropertyDefaultDescription
--rc-switch-track-inline-sizeNot specifiedTrack inline size. No built-in default; a theme must set this for the switch to render with a visible size.
--rc-switch-track-block-sizeNot specifiedTrack block size. No built-in default; a theme must set this for the switch to render with a visible size.
--rc-switch-native-appearancerevertSlotted native checkbox `appearance`.
--rc-switch-native-inline-sizerevertSlotted native checkbox inline size.
--rc-switch-native-block-sizerevertSlotted native checkbox block size.
--rc-switch-native-marginrevertSlotted native checkbox margin.
--rc-switch-native-opacityrevertSlotted native checkbox opacity.
--rc-switch-decoration-displaynoneDisplay of the track/thumb/icon decoration. Themes set this to `block` (or similar) to opt into the decorated switch look.
--rc-switch-track-border2px solid ButtonBorderTrack border.
--rc-switch-track-radius9999pxTrack and thumb border radius.
--rc-switch-track-bgButtonFaceUnselected track background.
--rc-switch-duration150msTrack/thumb/icon transition duration.
--rc-switch-easingeaseTrack/thumb/icon transition easing.
--rc-switch-thumb-size1remUnselected thumb inline and block size.
--rc-switch-thumb-offset0.375remThumb and icon inline start offset within the track.
--rc-switch-thumb-radius9999pxThumb border radius.
--rc-switch-thumb-bgButtonTextUnselected thumb background.
--rc-switch-icon-size1remSelected/deselected icon container size.
--rc-switch-icon-colorCanvasIcon color.
--rc-switch-icon-displaynoneDisplay of the active icon container when `icons` or `show-only-selected-icon` is set.
--rc-switch-selected-track-border-colorHighlightSelected track border color.
--rc-switch-selected-track-bgHighlightSelected track background.
--rc-switch-selected-thumb-size1.5remSelected thumb inline and block size.
--rc-switch-selected-thumb-bgHighlightTextSelected thumb background.
--rc-switch-thumb-translate1.125remInline-axis translation applied to the thumb and icons when selected.
--rc-switch-focus-ring2px solid HighlightTrack focus ring, shown on focus-within.
--rc-switch-focus-ring-offset2pxTrack focus ring offset.
--rc-switch-disabled-opacity0.5Host opacity while `disabled`.

CSS Parts

PartDescription
trackVisual switch track.
thumbVisual switch thumb.
selected-iconOptional selected-state icon container.
deselected-iconOptional deselected-state icon container.