CwSwitch

Checkbox-based on/off control with native form attributes and bindable checked state.

Basic

State: On

CwSwitch example Copy code
<CwSwitch
	label="Show only online devices"
	description="Hide offline sensors from the dashboard"
	name="onlineOnly"
	value="yes"
	bind:checked={onlineOnly}
/>

Variants

Label positions

CwSwitch label positions Copy code
<CwSwitch label="Top label" labelPosition="top" />
<CwSwitch label="Bottom label" labelPosition="bottom" />
<CwSwitch label="Left label" labelPosition="left" />
<CwSwitch label="Right label" labelPosition="right" />
Documentation Upgrade

Start here

CwSwitch is a checkbox-based on/off control. Treat it as a boolean field with optional helper text, then use `oninput` or `onchange` only when the parent needs side effects.

How to think about it

  1. Bind `checked` for state ownership If the page needs to read or update the value, `bind:checked` is the simplest pattern.
  2. Use label and description to explain the effect Good switch labels describe the setting itself; the optional description explains the consequence.
  3. Pick the callback based on timing `oninput` fires as the control changes, while `onchange` matches the more familiar committed change event.

Props and callbacks

This component wraps a real checkbox input, so standard form attributes are still available.

APITypeDetails
checked

Default: false

booleanCurrent on or off state.
label
stringPrimary switch text.
description
stringSecondary explanatory text.
labelPosition

Default: 'right'

'top' | 'bottom' | 'left' | 'right'Places the label block on a specific side of the switch control.
id
stringOptional explicit input id.
name
stringForm field name forwarded to the real checkbox input.
oninput
(checked: boolean, event: Event) => voidRuns whenever the checkbox input event fires.
onchange
(checked: boolean, event: Event) => voidRuns when the checkbox change event fires.
...other native checkbox attrs
HTMLInputAttributesPass `value`, `disabled`, `aria-*`, and standard checkbox attributes through to the real input.

Copy-paste examples

These snippets intentionally show the full public API surface the live demo relies on.

Bound settings switch

This is the normal settings-page usage.

Bound settings switch Copy code
<script lang="ts">
	let onlineOnly = $state(true);
</script>

<CwSwitch
	label="Show only online devices"
	description="Hide offline sensors from the dashboard"
	name="onlineOnly"
	bind:checked={onlineOnly}
/>
Input and change callbacks

Use callbacks when turning the switch should trigger side effects immediately.

Input and change callbacks Copy code
<CwSwitch
	id="sms-alerts"
	label="SMS alerts"
	oninput={(checked) => console.log('input', checked)}
	onchange={(checked) => savePreference('smsAlerts', checked)}
/>
Move the label around the control

Use `labelPosition` when the surrounding layout needs the text above, below, or to the left of the switch.

Move the label around the control Copy code
<CwSwitch label="Top label" labelPosition="top" />
<CwSwitch label="Bottom label" labelPosition="bottom" />
<CwSwitch label="Left label" labelPosition="left" />
<CwSwitch label="Right label" labelPosition="right" />