Documentation

Switch

An on/off setting that applies at once.

WebBetaForms

Web only — React, from @krizaka/ui/switch. Beta: its API may still change in a minor version.

When to use

  • For a setting that takes effect immediately: autoplay, notifications, a weekly digest.

When not to use

  • For a choice submitted with a form, or an agreement.Use instead: Checkbox
  • For more than two states.Use instead: Radio group
  • To run an action.Use instead: Button

Installation

Install

npm install @krizaka/ui@beta @krizaka/tailwind@beta @krizaka/tokens@beta tailwindcss

Styles

@import "tailwindcss";
@import "@krizaka/tailwind";
@import "@krizaka/ui/tailwind.css";

Import

import { Switch } from "@krizaka/ui/switch";

Examples

Off

Named by label.

On

defaultChecked (or checked + onCheckedChange).

Disabled

Off and on.

Small

size="sm".

With a visible label

A label and a hint on the same line.

One email on Monday with what you missed.

Props

An on/off switch: checked/defaultChecked + onCheckedChange.

PropTypeDefaultDescription
invalidboolean—Marks it invalid: aria-invalid and data-invalid.
labelstring—The accessible name — required when no visible label names it, passed translated.
size"sm" | "md"mdsm · md.

Accessibility

  • Radix Switch: role="switch" with aria-checked.
  • label names it when no visible label does; otherwise use Field.Label htmlFor and aria-describedby for the hint.

Keyboard

KeysAction
SpaceToggles the switch.

Best practices

  • Name the setting, not the state: “Autoplay”, not “Turn autoplay on”.
  • Put the label first and the switch at the end of the line (Field.Label htmlFor), with a hint when it helps.

Generated from the code of @krizaka/ui 2.0.0-beta.6: its meta.ts, its examples and its types.Edit this documentation on GitHub

On this page