Switch
Switch toggles an immediate on/off setting.
@duskmoon-dev/components/switch Usage
When to use
- Use it inside forms, filters, settings, and any workflow that captures user input.
- Prefer controlled props when the value must stay synchronized with application state.
Implementation notes
Features
Documents default values, value props, and change callbacks for form integration.
Surfaces status, size, disabled, and option data patterns where the component supports them.
Switch is most often configured through `checked`, `defaultChecked`, `onChange`, `disabled`.
Renders with default classes
Feature Demos
Feature demos are authored for the component page, then supplemented with behavior scenarios from the component test coverage.
Colors
Switch supports the full semantic color palette: primary, secondary, tertiary, accent, neutral, base, info, success, warning, error.
import "@duskmoon-dev/components/styles.css";
import { Switch } from "@duskmoon-dev/components/switch";
const colors = [
"primary",
"secondary",
"tertiary",
"accent",
"neutral",
"base",
"info",
"success",
"warning",
"error"
] as const;
export function SwitchColorsDemo() {
return (<div style={{ display: "grid", gap: 10 }}>
{colors.map((color) => (
<Switch
key={color}
color={color}
defaultChecked
checkedChildren={color.slice(0, 2)}
unCheckedChildren={color.slice(0, 2)}
/>
))}
</div>);
} Basic usage
Import the component stylesheet and Switch from its package subpath, then render it with the core props.
import "@duskmoon-dev/components/styles.css";
import { Switch } from "@duskmoon-dev/components/switch";
export function Example() {
return (<Switch
defaultChecked
checkedChildren="On"
unCheckedChildren="Off"
/>);
} Renders with default classes
test-backedSwitch scenario from the component test coverage: renders with default classes.
import "@duskmoon-dev/components/styles.css";
import { Switch } from "@duskmoon-dev/components/switch";
export function SwitchRendersWithDefaultClassesDemo() {
return (<Switch
defaultChecked
checkedChildren="On"
unCheckedChildren="Off"
/>);
} Supports controlled checked state
test-backedSwitch scenario from the component test coverage: supports controlled checked state.
import "@duskmoon-dev/components/styles.css";
import { Switch } from "@duskmoon-dev/components/switch";
export function SwitchSupportsControlledCheckedStateDemo() {
return (<Switch
defaultChecked
checkedChildren="On"
unCheckedChildren="Off"
/>);
} Calls onChange with the next checked boolean
test-backedSwitch scenario from the component test coverage: calls onchange with the next checked boolean.
import "@duskmoon-dev/components/styles.css";
import { Switch } from "@duskmoon-dev/components/switch";
export function SwitchCallsOnChangeWithTheDemo() {
return (<Switch
defaultChecked
checkedChildren="On"
unCheckedChildren="Off"
/>);
} Loading disables input and applies loading class
test-backedSwitch scenario from the component test coverage: loading disables input and applies loading class.
import "@duskmoon-dev/components/styles.css";
import { Switch } from "@duskmoon-dev/components/switch";
export function SwitchLoadingDisablesInputAndDemo() {
return (<Switch
defaultChecked
checkedChildren="On"
unCheckedChildren="Off"
/>);
} Applies size and color classes
test-backedSwitch scenario from the component test coverage: applies size and color classes.
import "@duskmoon-dev/components/styles.css";
import { Switch } from "@duskmoon-dev/components/switch";
export function SwitchAppliesSizeAndColorDemo() {
return (<Switch
defaultChecked
checkedChildren="On"
unCheckedChildren="Off"
/>);
} Renders checked and unchecked children
test-backedSwitch scenario from the component test coverage: renders checked and unchecked children.
import "@duskmoon-dev/components/styles.css";
import { Switch } from "@duskmoon-dev/components/switch";
export function SwitchRendersCheckedAndUncheckedDemo() {
return (<Switch
defaultChecked
checkedChildren="On"
unCheckedChildren="Off"
/>);
} Theme aware
Docs previews inherit the DuskMoon data-theme value. Use the header switch to compare light and dark rendering.
<div data-theme="sunshine">
<Switch
defaultChecked
checkedChildren="On"
unCheckedChildren="Off"
/>
</div>
<div data-theme="moonlight">
<Switch
defaultChecked
checkedChildren="On"
unCheckedChildren="Off"
/>
</div> API
The API reference below lists every parsed exported type or interface for Switch. Start with `checked`, `defaultChecked`, `onChange`, `disabled` for common usage.
packages/components/src/components/switch/Switch.types.ts
Scenarios: packages/components/src/components/switch/Switch.test.tsx | Prop | Type | Required | Description |
|---|---|---|---|
checked | boolean | No | checked configures Switch. |
defaultChecked | boolean | No | defaultChecked configures Switch. |
onChange | (checked: boolean, event: SwitchChangeEvent) => void | No | onChange configures Switch. |
disabled | boolean | No | disabled configures Switch. |
loading | boolean | No | loading configures Switch. |
size | SwitchSize | No | size configures Switch. |
color | SwitchColor | No | color configures Switch. |
checkedChildren | ReactNode | No | checkedChildren configures Switch. |
unCheckedChildren | ReactNode | No | unCheckedChildren configures Switch. |
className | string | No | className configures Switch. |
"sm" | "md" | "lg" | "primary" | "secondary" | "tertiary" | "accent" | "neutral" | "base" | "info" | "success" | "warning" | "error" ChangeEvent<HTMLInputElement>