Your next page is taking shape...

Opening Your Next Page…

Loading the guide and preparing its examples for you to explore. The page will appear here automatically when it’s ready, so you can keep this view open without refreshing.

Made for Preact. Built to be yours.

Forms & Input

Switch

A compact control for on/off settings. Make the expected input clear before someone interacts, and keep the current value, help text and validation feedback connected. Import it from @/components/kamod-ui/switch.

Source · Design reference · API reference · Theming · Styles


Interactive Live Preview
Try it. Make it yours.
Setting the scene...

Interactive preview

Live Preact example Local preview theme · Responsive styles follow the preview widthFull width

Installation

Import Switch from @/components/kamod-ui/switch.

Install PackagesTerminal
pnpm add @kamod-ch/ui

Copy an example with @kamod-ch/ui or the matching @kamod-ch/ui/… subpath. The code block switches between those two installs. Connect The Global Theme CSS and Tailwind Source Detection before your first render.

Usage

Use defaultChecked for local state, or checked with onCheckedChange for controlled usage. The size prop supports compact layouts, while aria-invalid and disabled help communicate state clearly.

Import SetupTypeScript
import { Switch } from "@kamod-ch/ui";

Choose a complete composition from the examples below; required child components and props vary by pattern.

Component Examples & Variants

Explore Switch in Different Contexts, from the starting pattern to the compositions below. Begin with Description, then compare the examples that match your content, state and layout. These are working patterns to adapt, not a list of interchangeable variant values: check the API Reference before using a prop or combining behaviors. Look at the surrounding labels, supporting text and spacing as well as the component itself.

Read the Result and Its Implementation Together. Use each showcase’s Code view to inspect imports and composition, or its Prompt view to prepare a setup or adaptation brief. Try the narrow preview and local theme controls before connecting real data, and use Reset to return an example to its initial state. Keep layout changes in class utilities where appropriate; follow the Component Styles Guide for hierarchy and the Theming Guide for shared surfaces and colors. The Accessibility Notes explain what to preserve when making those changes.

Description

Explain What Turning the Setting on Means. Arrange a setting name and short explanation opposite a Switch in one row. The text describes the effect of enabling the preference, while the switch provides a compact view and control of its boolean value.

Keep the switch associated with that label and clarify whether the change applies immediately or after saving; the user's expectation should match the application's actual behavior.

src/components/SwitchExample.tsx13 lines
import { Switch } from "@kamod-ch/ui";

export const Example = () => (
  <div class="flex w-full max-w-md items-start justify-between gap-4 rounded-xl border border-border bg-card p-4">
    <div class="space-y-1">
      <p class="text-sm font-medium leading-none">Share across devices</p>
      <p class="text-xs text-muted-foreground">
        Keep your settings synced when switching devices.
      </p>
    </div>
    <Switch defaultChecked aria-label="Share across devices" />
  </div>
);

Choice Card

Keep Each Preference Independently Understandable. Place each independent setting in a lightly bounded choice surface with its own switch. The card groups the label and explanation, but multiple options may still be enabled because these are separate booleans rather than radio choices.

Avoid nesting unrelated actions in its label and make the checked state clear; use Radio Group when only one card may be selected.

Interactive Live Preview
Try it. Make it yours.
Setting the scene...

Interactive preview

Live Preact example Local preview theme · Responsive styles follow the preview widthFull width

Disabled & Invalid

Use Different Explanations for Different Constraints. Use disabled styling for an unavailable switch and invalid feedback for a value that needs correction. Keep those situations distinct in the accompanying text so users know whether they can act now or need a prerequisite first.

Pair each state with useful text, keep the current value readable and avoid expecting a muted color alone to explain the distinction.

Interactive Live Preview
Try it. Make it yours.
Setting the scene...

Interactive preview

Live Preact example Local preview theme · Responsive styles follow the preview widthFull width

Size

Choose Density without Separating the Label. Compare compact and default switches within equivalent settings rows. The control's size should fit the label hierarchy and available interaction space while preserving a clear relationship between the description and its on/off state.

Keep enough interaction space around the row, align the control with its label and compare the result with longer descriptions before reducing the overall layout.

Interactive Live Preview
Try it. Make it yours.
Setting the scene...

Interactive preview

Live Preact example Local preview theme · Responsive styles follow the preview widthFull width
API reference

Props and Data

Configure Switch using the contracts below. Read the documented options alongside the Source Definitions to understand which values your application supplies and which details the component owns. Start with a prop or helper, then follow its type link to inspect the declaration without leaving the page.

Props, slots and variants for this component.

API reference
Your API, from source

Type cards are extracted from this checkout’s TypeScript source at build time. Props inherited through HTMLAttributes, Omit or variant helpers remain references in those declarations; their fields are not flattened here. Match the reference to your installed version, and check a package’s exports before writing an import type. A type exported by a source file is not necessarily re-exported by @kamod-ch/ui.

Component Props

Read each option with its owning component. The
marker identifies required fields verified in that declaration. A ? means a field can be omitted; it does not promise a fallback value. Existing documented defaults remain alongside the descriptions. For callbacks, check the argument and return types before connecting your own state or services.
@kamod-ch/ui/switch6.0 KB min · 1.8 KB gzipSignals: none

Switch

SwitchProps

Switch documented props
Prop / typeDescription
checked
booleanSwitchProps

Configure checked for Switch. Compare its accepted type with the Usage Guidance before supplying a value.

Default
—
children
ComponentChild[] | ComponentChildSwitchProps

Configure children for Switch. Compare its accepted type with the Usage Guidance before supplying a value.

Default
—
class
stringSwitchProps

Configure class for Switch. Compare its accepted type with the Usage Guidance before supplying a value.

Default
—
defaultChecked
booleanSwitchProps

Configure defaultChecked for Switch. Compare its accepted type with the Usage Guidance before supplying a value.

Default
false
disabled
boolean | SignalLike<boolean | undefined>SwitchProps

Configure disabled for Switch. Compare its accepted type with the Usage Guidance before supplying a value.

Default
—
id
string | SignalLike<string | undefined>SwitchProps

Configure id for Switch. Compare its accepted type with the Usage Guidance before supplying a value.

Default
—
name
string | SignalLike<string | undefined>SwitchProps

Configure name for Switch. Compare its accepted type with the Usage Guidance before supplying a value.

Default
—
onCheckedChange
((next: boolean) => void)SwitchProps

Configure onCheckedChange for Switch. Compare its accepted type with the Usage Guidance before supplying a value.

Default
—
onClick
MouseEventHandler<HTMLButtonElement>SwitchProps

Configure onClick for Switch. Compare its accepted type with the Usage Guidance before supplying a value.

Default
—
required
booleanSwitchProps

Configure required for Switch. Compare its accepted type with the Usage Guidance before supplying a value.

Default
—
size
"default" | "sm"SwitchProps

Configure size for Switch. Compare its accepted type with the Usage Guidance before supplying a value.

Default
"default"
value
string | number | SignalLike<string | number | undefined>SwitchProps

Configure value for Switch. Compare its accepted type with the Usage Guidance before supplying a value.

Default
—

Data Type Reference

Expand a card to inspect and copy the exact declaration. The code header identifies its repository file; follow imported or inherited types in the Implementation Source when you need the complete dependency chain. Required-field summaries cover fields declared directly in that type. Source-only helpers and inferred schema outputs describe the local implementation, not additional props you can pass to every component.

Switch Props

6 declared fields
SwitchProps
Exported Source Declaration from Switch.tsx. Referenced types retain their original names; inspect that file for imports and supporting definitions. Inspect the Complete Definition of SwitchProps before adapting it.

Accessibility

Build the Complete Interaction, Including the Parts Outside Switch. The notes below distinguish the current implementation from the labels, content and behavior your application supplies. Start with the Live Preview, check the Props and Data Reference, and test the finished composition with real content rather than assuming that an unchanged visual example covers every use case.

Built-in Behavior and Defaults

Switch is a button with role="switch" and aria-checked; its uncontrolled default is off. It is not a native checkbox input. Text passed as children is displayed beside the button and does not automatically label that button.

Labels and Relationships

Name the setting consistently, for example Email notifications, while the checked state conveys on or off. Connect a visible label using the button's ID or aria-labelledby. Do not rename the setting every time its state changes.

Keyboard and Focus

The button supports native Enter and Space activation. When changing a saved preference, communicate pending and failed requests and restore the previous value when appropriate. Decide whether the setting applies immediately or is saved with a larger form.

States and Integration Details

A switch does not add a checkbox value to native FormData. Integrate the documented onCheckedChange callback with your state or form adapter. An adjacent sentence or an on/off color alone is not a reliable accessible name.

Verify the Complete Interaction

Check Behavior as Well as Markup. Work through these scenarios in the application where the component will be used. Automated checks can help find structural problems; also review the reading experience with a screen reader, keyboard focus and the actual feedback from your application.

  1. Verify the control is announced as a named switch with the current checked state.
  2. Toggle through pointer and keyboard, including a simulated failed save and retry.
  3. Confirm form submission uses the actual state and that disabled settings include a readable explanation.

Recheck the result after changing Theme Tokens, translations or class overrides. Include text enlargement, narrow layouts and reduced-motion settings where animation is present. Inspect the Component Source when a behavior differs from your expectation; a role or state attribute does not implement keyboard interaction by itself.

For background, read the WAI-ARIA Authoring Guidance and Keyboard Interface Guidance. These explain the patterns; verify browser and assistive-technology behavior for the implementation and audience you actually support.

Take the Next Useful Step

You have the pieces. Put them into one real interaction, keep its state in one place, and check the result with the content your application will actually show.

Connect the Foundation

Return to Choose and Compose Components to connect this reference to a working screen. Keep global CSS in one entrypoint and import only the components that screen needs.

Build the Surrounding Screen

Find a neighboring interaction in the Component Library, or start with a complete Block. Replace sample content and destinations before refining the layout; a local class change can stay with the composition.

Make the Result Hold Up

Use shared theme roles for changes that belong everywhere. Then review the whole journey: keyboard focus, long labels, narrow screens and a clear way to recover when something fails.

Keep a link to the source and note your intentional changes. Your next edit should begin with a clear decision, not another search.