Switch

Switch

Toggle switch for binary on/off states.

Based on APG Switch Pattern.

Demo

Usage

// Create a signal for state
let enabled = @resource.signal(true)

// Reactive switch
@styled_switch.switch(
  enabled,
  aria_label="Enable notifications",
  [@luna.text("Notifications")],
)

Props

PropTypeDefaultDescription
onSignal[Bool]-Reactive state signal
disabledBoolfalseDisable the switch
aria_labelString?NoneAccessible label
aria_labelledbyString?NoneID of labelling element
classString?NoneAdditional CSS class
childrenArray[Node]-Label content

BEM Classes

ClassDescription
.switchRoot button element
.switch__trackBackground track
.switch__thumbSliding thumb
.switch__labelLabel text
.switch-groupContainer for multiple switches

Data Attributes

AttributeValuesDescription
data-stateon, offCurrent state
data-disabled-Disabled state

Accessibility

  • role="switch" announces as toggle

  • aria-checked reflects current state

  • Space/Enter toggles state

  • Focus ring on keyboard navigation