Skip to Content
CodeUI

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

Include line numbers in exported snippets.
switch/basic.tsx
import { Switch } from "@codeui/solid";
export default function Demo() {
	return (
		<Switch
			label="Show line numbers"
			description="Include line numbers in exported snippets."
			defaultChecked
		/>
	);
}

Anatomy

Include line numbers in the export.

Tap a part to outline it in the demo.

  1. 01Rootswitchclasses.root
  2. 02Inputswitch-inputclasses.input
  3. 03Controlswitch-controlclasses.control
  4. 04Thumbswitch-thumbclasses.thumb
  5. 05Labelswitch-labelclasses.label
  6. 06Descriptionswitch-descriptionclasses.description
  7. 07ErrorMessageswitch-error-messageclasses.errorMessage
ExportRendersclasses keydata-cuiRole
Rootdivrootswitchnone
Inputinputinputswitch-inputswitch
Controldivcontrolswitch-controlnone
Thumbdivthumbswitch-thumbnone
Labellabellabelswitch-labelnone
Descriptiondivdescriptionswitch-descriptionnone
ErrorMessagediverrorMessageswitch-error-messagenone

Variants

switch/sizes.tsx
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

Enabled
switch/controlled.tsx
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>
		</>
	);
}
Include line numbers in the export.
switch/disabled.tsx
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 />
		</>
	);
}
Include line numbers in the export.
switch/read-only.tsx
import { Switch } from "@codeui/solid";
export default function Demo() {
	return (
		<Switch
			label="Show line numbers"
			description="Include line numbers in the export."
			readOnly
			defaultChecked
		/>
	);
}
Include line numbers in the export.
switch/required.tsx
import { Switch } from "@codeui/solid";
export default function Demo() {
	return (
		<Switch
			label="Show line numbers"
			description="Include line numbers in the export."
			required
			defaultChecked
		/>
	);
}
Include line numbers in the export.
switch/invalid.tsx
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

KeyAction
TabFocus the control.
SpaceToggle 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

Submit to inspect
switch/form.tsx
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".

Use Checkbox for agreement or independent options submitted together. Switch has no indeterminate state. See Control helpers for custom compositions.

On this page