Liking cljdoc? Tell your friends :D

x-switch

A toggle switch form control. Semantically a role="switch" button that reflects its state via the checked attribute. Form-associated (participates in <form> submission and reset).

Tag

<x-switch></x-switch>

Observed Attributes

AttributeTypeDefaultDescription
checkedbooleanabsentPresent → switch is on
disabledbooleanabsentPrevents interaction; control is inert
readonlybooleanabsentPrevents toggling without affecting form submission
requiredbooleanabsentMarks the field as required (must be checked for validity)
namestringForm field name
valuestring"on"Value submitted when checked
aria-labelstringForwarded to inner control
aria-describedbystringForwarded to inner control
aria-labelledbystringForwarded to inner control

Properties

PropertyTypeReflects attribute
checkedbooleanchecked
disabledbooleandisabled
readOnlybooleanreadonly
requiredbooleanrequired
namestringname
valuestringvalue

Events

x-switch-change-request

Fired before the state changes. Cancelable — call event.preventDefault() to block the toggle.

el.addEventListener('x-switch-change-request', (e) => {
  console.log(e.detail.previousChecked, e.detail.nextChecked, e.detail.value);
  e.preventDefault(); // optional: block the toggle
});
Detail fieldTypeDescription
valuestringCurrent form value
previousCheckedbooleanState before toggle
nextCheckedbooleanState after toggle (if not blocked)

x-switch-change

Fired after the state has changed. Not cancelable.

el.addEventListener('x-switch-change', (e) => {
  console.log(e.detail.checked, e.detail.value);
});
Detail fieldTypeDescription
valuestringForm value
checkedbooleanNew checked state

Shadow Parts

PartElementDescription
controlbuttonInteractive switch root
trackspanThe pill-shaped track
thumbspanThe sliding thumb

CSS Custom Properties

PropertyDefaultDescription
--x-switch-track-width44pxTrack width
--x-switch-track-height24pxTrack height
--x-switch-thumb-size18pxThumb diameter
--x-switch-thumb-offset3pxThumb inset from track edge
--x-switch-track-radius999pxTrack border radius
--x-switch-track-bg#d1d5dbTrack background (off)
--x-switch-track-bg-checked#2563ebTrack background (on)
--x-switch-thumb-bg#ffffffThumb background
--x-switch-focus-ring#60a5faFocus ring color
--x-switch-disabled-opacity0.45Opacity when disabled

Dark-mode overrides are applied automatically via @media (prefers-color-scheme: dark).

Accessibility

  • Inner button carries role="switch" and aria-checked.
  • aria-disabled, aria-required, aria-readonly are kept in sync.
  • aria-label, aria-labelledby, aria-describedby are forwarded to the inner button.
  • A <label for="..."> in the document is automatically wired via aria-labelledby.
  • Keyboard: Space and Enter toggle the switch.
  • Animations respect @media (prefers-reduced-motion: reduce).

Form participation

x-switch is a form-associated custom element. It submits name=value when checked, nothing when unchecked. It responds to formResetCallback and formDisabledCallback.

Usage examples

<!-- Basic -->
<x-switch name="notifications"></x-switch>

<!-- Pre-checked -->
<x-switch name="dark-mode" checked></x-switch>

<!-- With label -->
<label for="sw1">Enable alerts</label>
<x-switch id="sw1" name="alerts"></x-switch>

<!-- Disabled -->
<x-switch name="feature" disabled checked></x-switch>

<!-- Custom value -->
<x-switch name="theme" value="dark"></x-switch>

<!-- React to changes -->
<script>
  import '@vanelsas/baredom/x-switch';
  const sw = document.querySelector('x-switch');
  sw.addEventListener('x-switch-change', (e) => {
    console.log('now:', e.detail.checked);
  });
</script>

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close