Switch
@codeui/solid v0.3.0
A toggle that turns a setting on or off immediately.
When to use
Turn a setting on or off immediately, such as line numbers in the editor preview.
When to choose another component
Use Checkbox for agreement or independent options submitted together. Switch has no indeterminate state.
Live demo
import { Switch } from "@codeui/solid";
export default function Demo() {
return (
<Switch
label="Show line numbers"
description="Include line numbers in exported snippets."
defaultChecked
/>
);
}Anatomy
Tap a part to outline it in the demo.
- 01Root
switchclasses.root - 02Input
switch-inputclasses.input - 03Control
switch-controlclasses.control - 04Thumb
switch-thumbclasses.thumb - 05Label
switch-labelclasses.label - 06Description
switch-descriptionclasses.description - 07ErrorMessage
switch-error-messageclasses.errorMessage
| Export | Renders | classes key | data-cui | Role |
|---|---|---|---|---|
Root | div | root | switch | none |
Input | input | input | switch-input | switch |
Control | div | control | switch-control | none |
Thumb | div | thumb | switch-thumb | none |
Label | label | label | switch-label | none |
Description | div | description | switch-description | none |
ErrorMessage | div | errorMessage | switch-error-message | none |
Variants
import { Switch } from "@codeui/solid";
export default function Demo() {
return (
<div style={{ display: "grid", gap: "var(--cui-space-4)" }}>
<Switch size="sm" label="Line numbers (sm)" defaultChecked />
<Switch size="md" label="Line numbers (md)" defaultChecked />
</div>
);
}md is a 36×22 track with a 16px thumb, as in CodeImage. sm is a 28×16 track with a 12px thumb. On a touch screen both grow to 44×26 with a 20px thumb, so the target is at least 40px. The thumb carries --cui-shadow-thumb, so it stays visible on a light track.
Motion
A pointer toggle slides the thumb in 120ms with the ease-out curve and fades the track colour in 120ms. A toggle from the keyboard is instant, and the 2px focus ring shows only after keyboard input: Kobalte focuses the input from script after a click, which the browser marks :focus-visible, so the kit reads the last input from <html data-cui-input> instead. Reduced motion keeps the colour fade and drops the slide.
States
import { Switch } from "@codeui/solid";
import { createSignal } from "solid-js";
export default function Demo() {
const [checked, setChecked] = createSignal(true);
return (
<>
<Switch label="Show line numbers" checked={checked()} onChange={setChecked} />
<output>{checked() ? "Enabled" : "Disabled"}</output>
</>
);
}import { Switch } from "@codeui/solid";
export default function Demo() {
return (
<>
<Switch
label="Show line numbers"
description="Include line numbers in the export."
disabled
defaultChecked
/>
<Switch label="Show window header" disabled />
</>
);
}import { Switch } from "@codeui/solid";
export default function Demo() {
return (
<Switch
label="Show line numbers"
description="Include line numbers in the export."
readOnly
defaultChecked
/>
);
}import { Switch } from "@codeui/solid";
export default function Demo() {
return (
<Switch
label="Show line numbers"
description="Include line numbers in the export."
required
defaultChecked
/>
);
}import { Switch } from "@codeui/solid";
export default function Demo() {
return (
<Switch
label="Show line numbers"
description="Include line numbers in the export."
validationState="invalid"
errorMessage="Enable line numbers before you export."
defaultChecked
/>
);
}Ready API
Switch
Renders the complete field. SwitchProps is the public props type.
Prop
Type
Prop
Type
Inherited behavior follows the Kobalte switch contract.
Styled parts API
Import the parts from @codeui/solid/switch/parts.
Control
Wraps Kobalte Switch.Control with kit styles.
Prop
Type
Description
Wraps Kobalte Switch.Description with kit styles.
Prop
Type
ErrorMessage
Wraps Kobalte Switch.ErrorMessage with kit styles.
Prop
Type
Input
Wraps Kobalte Switch.Input with kit styles.
Prop
Type
Label
Wraps Kobalte Switch.Label with kit styles.
Prop
Type
Root
Wraps Kobalte Switch.Root with kit styles.
Prop
Type
Prop
Type
Thumb
Wraps Kobalte Switch.Thumb with kit styles.
Prop
Type
useSwitchContext
Reads the Kobalte context inside its Root. Call it from a descendant component. It exposes reactive field state and throws outside the provider.
Styles export
switchStyles
switchStyles maps each part name to a function that returns its class. Set data-size="sm" on a custom control to select the compact track. SwitchStyles types the map and SwitchParts exposes the Solid components.
Import switchStyleProps from @codeui/styles/switch to bind the base class and attributes in one step. For example, spread switchStyleProps.control({ size: "sm", class: "preview-track" }) on a custom control. A standalone part leaves data-size absent when the caller leaves size unspecified. The behavior adapter supplies checked and disabled states.
Accessibility
Keyboard
| Key | Action |
|---|---|
| Tab | Focus the control. |
| Space | Toggle the checked state. |
ARIA
The input exposes role="switch" and aria-checked. Description and error components associate hint text with the field. validationState="invalid" exposes invalid state. Supply a visible label or an accessible name.
Focus
These controls do not open an overlay or move focus on mount. Disabled controls cannot receive keyboard focus. Read-only controls remain focusable and do not change value.
SSR notes
The root, labels, controls, and form inputs render on the server. No part uses a portal. Keep initial values and component order stable between server and client so generated IDs hydrate consistently. Create signals per component instance.
Theming hooks
The component reads these foundation variables. Shared field labels and messages also use the field typography and validation tokens.
--cui-accent-focus (on), --cui-action-neutral-active (off), --cui-action-brand-text, --cui-shadow-thumb, --cui-focus-color, --cui-focus-offset, --cui-focus-width, --cui-font-size-caption, --cui-font-size-xs, --cui-font-line-height-caption, --cui-font-line-height-xs, --cui-motion-duration-fast, --cui-motion-ease-out, --cui-radius-full, --cui-space-0-5, --cui-space-1, --cui-space-2, --cui-space-3, --cui-space-4, --cui-space-5, --cui-space-6, --cui-space-10, --cui-text-danger, --cui-text-default, --cui-text-hint, --cui-text-label.
Composition examples
import { Switch, Button, Stack, Text } from "@codeui/solid";
import { createSignal } from "solid-js";
export default function Demo() {
const [result, setResult] = createSignal("Submit to inspect");
return (
<form
onSubmit={event => {
event.preventDefault();
setResult(
new FormData(event.currentTarget).get("lineNumbers")?.toString() ?? "omitted",
);
}}
>
<Stack gap="3" align="start">
<Switch
label="Show line numbers"
name="lineNumbers"
value="include"
defaultChecked
/>
<Stack direction="row" gap="3" align="center" wrap>
<Button type="submit" size="sm">
Inspect form value
</Button>
<Text as="output" size="caption" tone="muted">
{result()}
</Text>
</Stack>
</Stack>
</form>
);
}The form demo reads the submitted value with FormData. Only checked, enabled inputs submit their value. Unchecked and disabled inputs are omitted. An omitted checkbox or switch value defaults to "on".
Related
Use Checkbox for agreement or independent options submitted together. Switch has no indeterminate state. See Control helpers for custom compositions.