Skip to content

Switch

An on/off toggle for settings that apply at once. role=switch, controlled or uncontrolled.

On this page
Used for
  • Auto-approve tools
  • Enable streaming
  • Turn on alerts
  • Pause a schedule

Examples

Installation

npx knowns-ui@latest add switch
For Claude Code, Cursor or Codex: installs it and wires the props to your own data.

Props

checked / defaultChecked
boolean
Defaultfalse
Controlled or uncontrolled state.
onCheckedChange
(checked: boolean) => void
Called on every toggle.
label
ReactNode
Clickable label; without it, pass aria-label.
disabled
boolean
Defaultfalse

What it handles

  1. 01Draws a track and a thumb on a button with role switch and aria-checked, so a screen reader hears On or Off instead of checked.
  2. 02Is controlled with checked and onCheckedChange, or uncontrolled with defaultChecked.
  3. 03Wraps the control and a text label in one label element when you pass label, so clicking the text flips it.
  4. 04Takes an id for an outside label and an aria-label for a switch that has no visible text.
  5. 05Reads the on colour from --kn-accent, falling back to --primary, and exports switchVariants so you can restyle the track.
  6. 06Dims to half strength and takes no input when disabled.

States

First render
Nothing. The thumb sits at its saved side
none
Turn on
The thumb slides across the track
180ms, ease in-out
Track colour
The track changes from a neutral tint to the accent colour
120ms, ease out
Keyboard focus
A focus ring appears around the track
none, at once
Disabled
The switch dims and the cursor changes
none, at once

Works with

When to use

When to use

  • Settings that take effect immediately, on or off.

When not to use

  • Choices submitted with a form: use Checkbox.

Using with AI

Use Copy page for AI above to give your assistant this page, Copy prompt under the install command to have it installed and wired to your data, or connect the Knowns UI MCP server and just ask:

  • Add the Knowns UI Switch to my app and wire it to my own data.
  • I need settings that take effect immediately, on or off. Set that up with the Knowns UI Switch.
  • Which Switch props do I need for a minimal setup? Show me a short example.

With the server connected, your assistant calls get_component({ name: "switch" }) and search_docs({ query: "switch" }).

Need help?

Stuck on Switch, or found something off? We read every message.