icon

Morainev0.5.0

Switch
formsswitch

Switch

Toggle switch control with icon slots and loading state.

Import#

import { Switch } from 'moraine'

Slot Structure#

Toggle track with thumb and optional label area.

root
├── container
│ ├── input
│ └── track
│ └── thumb
└── wrapper (optional)
├── label (optional)
└── description (optional)

Examples#

Basic + Controlled#

Uncontrolled and controlled switch with icon slots.

function BasicControlled() {
const [checked, setChecked] = createSignal(false)
return (
<div class="flex flex-col gap-3 max-w-xl">
<Switch
label="Email alerts"
description="Uncontrolled"
defaultChecked
checkedIcon="i-lucide-bell"
uncheckedIcon="i-lucide-bell-off"
/>
<Switch
label="Deploy protection"
description={`Current: ${checked() ? 'enabled' : 'disabled'}`}
checked={checked()}
onChange={setChecked}
checkedIcon="i-lucide-shield-check"
uncheckedIcon="i-lucide-shield"
/>
</div>
)
}

Variants#

Loading, disabled, and explicit icon combinations.

function Variants() {
return (
<div class="flex flex-col gap-3 max-w-xl">
<Switch
label="Sync in progress"
description="Loading state"
loading
checked
checkedIcon="i-lucide-check"
uncheckedIcon="i-lucide-x"
/>
<Switch
label="Dark mode"
description="Custom icons"
defaultChecked
checkedIcon="i-lucide-moon-star"
uncheckedIcon="i-lucide-sun"
/>
<Switch
label="Billing lock"
description="Disabled"
disabled
checked
checkedIcon="i-lucide-lock"
uncheckedIcon="i-lucide-unlock"
/>
</div>
)
}

Sizes#

Switch size scale from xs to xl.

function Sizes() {
const SIZES: SwitchSizeName[] = ['xs', 'sm', 'md', 'lg', 'xl']
type SwitchSizeName = Exclude<SwitchT.Variant['size'], undefined>
return (
<div class="gap-3 grid sm:grid-cols-2">
<For each={SIZES}>
{(size) => (
<Switch
size={size}
label={`Size ${size}`}
description="Size preview"
defaultChecked={size === 'lg' || size === 'xl'}
/>
)}
</For>
</div>
)
}

Custom True/False Values#

Map checked state to domain values instead of boolean.

function CustomTrueFalseValues() {
const [deploymentGuard, setDeploymentGuard] = createSignal<'enabled' | 'disabled'>('disabled')
return (
<div class="max-w-xl space-y-3">
<Switch<'enabled', 'disabled'>
label="Deployment gate"
description="Domain value binding"
trueValue="enabled"
falseValue="disabled"
checked={deploymentGuard()}
onChange={setDeploymentGuard}
checkedIcon="i-lucide-check-check"
uncheckedIcon="i-lucide-x"
/>
<p class="text-xs text-muted-foreground">Current value: {deploymentGuard()}</p>
</div>
)
}

API Reference#

Attributes#

Slotroot0 attributes
Switch wrapper that coordinates input, track, thumb, and text content.
No attribute metadata for this slot.

Props#

PropTypeDefaultDescription
checkedTTrue | TFalse | undefined
Whether the switch is checked.
checkedIconIconT.Name
Icon shown when the switch is checked.
classClassValue
Class applied to the component root or trigger element.
classesSwitchT.Classes | undefined
defaultCheckedboolean | undefined
Whether the switch is checked by default.
descriptionJSX.Element
Description for the switch.
disabledboolean | undefinedfalse
Whether the input is disabled.
falseValueTFalse | undefinedfalse
Value to use when the switch is unchecked.
idstring | undefined
The ID of the input element.
labelJSX.Element
Label for the switch.
loadingboolean | undefinedfalse
Whether the switch is in a loading state.
loadingIconIconT.Nameicon-loading
Icon shown during loading state.
namestring | undefined
The name of the input element, used for form submission.
onChange((value: TTrue | TFalse) => void) | undefined
Callback when the switch state changes.
onPointerDownJSX.EventHandlerUnion<HTMLButtonElement, PointerEvent> | undefined
Pointer down handler for the switch root container.
readOnlyboolean | undefinedfalse
Whether the input is read-only.
refJSX.HTMLElementTags["div"] extends { ref?: infer Ref; } ? Ref : never | undefined
requiredboolean | undefinedfalse
Whether the input is required.
size"xs" | "sm" | "md" | "lg" | "xl" | undefined
styleJSX.CSSProperties | undefined
stylesSwitchT.Styles | undefined
trueValueTTrue | undefinedtrue
Value to use when the switch is checked.
uncheckedIconIconT.Name
Icon shown when the switch is unchecked.
valuestring | undefinedon
Native value submitted when the switch is checked.