CxJS

Switch

import { Switch } from 'cx/widgets'; Copied

The Switch widget is a two-state toggle control. It offers the same functionality as a Checkbox but with a modern sliding appearance that’s commonly used for on/off settings.

<LabelsTopLayout vertical>
  <Switch
    label="Standard"
    on={bind(m.enabled, true)}
    text={expr(m.enabled, (v) => (v ? "ON" : "OFF"))}
  />
  <Switch label="Disabled" on={m.enabled} disabled />
  <Switch label="Read-only" on={m.enabled} readOnly />
  <Switch
    label="Styled"
    on={m.enabled}
    handleStyle="background: white"
    rangeStyle="background: lightsteelblue"
  >
    <span class="text-red-500">Custom text</span>
  </Switch>
</LabelsTopLayout>
 
ON
 
 
 
Custom text

Configuration

Core Properties

PropertyTypeDefaultDescription
onbooleanfalseState of the switch (true = on)
offbooleantrueInverse state (true = off). Alternative to on
valuebooleanAlias for on
textstringText displayed next to the switch
labelstringLabel displayed before the switch
disabledbooleanfalseDisables the switch
readOnlybooleanfalseMakes the switch read-only
requiredbooleanfalseMarks the field as required
viewModebooleanfalseDisplay as plain text instead of interactive switch
autoFocusbooleanfalseAutomatically focus the switch on mount

Appearance

PropertyTypeDefaultDescription
handleStyleobjectCSS styles for the switch handle (knob)
rangeStyleobjectCSS styles for the track when switch is on
emptyTextstringText shown in view mode when value is empty
tooltipstringTooltip text or configuration

Behavior

PropertyTypeDefaultDescription
focusOnMouseDownbooleanfalseWhether to focus the switch on mouse down

Callbacks

PropertyTypeDescription
onValidatefunctionCustom validation function returning error or undefined