An on/off toggle for settings that apply at once. role=switch, controlled or uncontrolled.
Used for
- Auto-approve tools
- Enable streaming
- Turn on alerts
- Pause a schedule
Examples
Installation
npx knowns-ui@latest add switchFor Claude Code, Cursor or Codex: installs it and wires the props to your own data.
Paste this into your coding agent (Claude Code, Cursor, Codex). It checks your project, sets up the registry, runs the install and verifies it.
Install prompt
Install Switch from Knowns UI into this project. Work step by step, show me each change, and ask before you edit files outside this project.
Context: Knowns UI (https://ui.knowns.dev) is a shadcn registry. Items land in the project as source files. Docs: https://ui.knowns.dev/docs/switch
1. Check the project first. Stop and tell me if any of these is missing:
- React 19 and Tailwind CSS v4.
- A components.json at the project root. If there is none, ask me before running `npx knowns-ui@latest init --no-key` (it runs shadcn init when needed, then adds the registries).
2. Run:
```bash
npx knowns-ui@latest add switch
```
It writes these files:
- lib/knowns/run.ts
- lib/knowns/json.tsx
- lib/knowns/status.ts
- lib/knowns/tone.ts
- lib/knowns/types.ts
- lib/knowns/use-abortable.ts
- lib/knowns/utils.ts
- components/ui/knowns/switch.tsx
and installs these npm packages: clsx@^2.1.1, tailwind-merge@^3.7.0, class-variance-authority@^0.7.1.
3. While it runs:
- It also adds a few @keyframes (all named kn-*) to the global CSS file (globals.css or the file components.json points at). Let it: they only add animations.
- If it asks to overwrite files under lib/knowns/: these are shared by every Knowns UI item. List them, say whether I changed them (check git), and ask me before you answer.
- If it asks to overwrite any other file, show me the diff and ask me.
4. Verify:
- Every file in the list above exists.
- The project type-checks (for example `npx tsc --noEmit`) and the dev build has no new errors.
5. Use it: follow the examples and props on https://ui.knowns.dev/docs/switch, and ask me if anything about my data is unclear.
If the Knowns UI MCP server is connected (https://ui.knowns.dev/docs/ai-and-mcp), use get_component({ name: "switch" }) or search_docs for details.
Finish with a short summary: the files you added or changed, the packages you installed, and anything I still need to do.Props
- checked / defaultChecked
boolean- Default
false - Controlled or uncontrolled state.
- onCheckedChange
(checked: boolean) => void- Called on every toggle.
- label
ReactNode- Clickable label; without it, pass aria-label.
- disabled
boolean- Default
false
| Prop | Type | Default | Description |
|---|---|---|---|
| checked / defaultChecked | boolean | false | Controlled or uncontrolled state. |
| onCheckedChange | (checked: boolean) => void | - | Called on every toggle. |
| label | ReactNode | - | Clickable label; without it, pass aria-label. |
| disabled | boolean | false |
What it handles
- 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.
- 02Is controlled with checked and onCheckedChange, or uncontrolled with defaultChecked.
- 03Wraps the control and a text label in one label element when you pass label, so clicking the text flips it.
- 04Takes an id for an outside label and an aria-label for a switch that has no visible text.
- 05Reads the on colour from --kn-accent, falling back to --primary, and exports switchVariants so you can restyle the track.
- 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
| State | What moves | Timing |
|---|---|---|
| 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" }).