@akhatmirzo/ui

Switch

An on/off toggle (role="switch") for settings that take effect immediately.

Import

import { Switch } from '@akhatmirzo/ui';

When to use

  • When a setting applies right away — notifications, dark theme, opening or closing a store. No "Save" click needed.
  • In forms with a "Save" button, use a Checkbox — there the choice only takes effect on submit.
  • Name the setting, not the state: "Notifications", not "On/Off" — the switch itself shows the state.
  • If the change takes time (a server request), confirm it with a toast or roll the value back on error.

Sizes

sm is 32×18px and md (default) is 40×24px. The thumb slides with a spring animation.

With a description

description appears under the label and is linked with aria-describedby.

Controlled

With checked and onCheckedChange, the value lives in your state. Use defaultChecked for uncontrolled mode.

Buyurtmalar qabul qilinmoqda

States

disabled dims the switch and blocks clicks. invalid draws a red ring around the track and sets aria-invalid on the input.

With Field

The Field label is linked to the switch with htmlFor and the hint goes into aria-describedby — no need for the switch's own label.

Har kirishda SMS kod so'raladi

Props

Switch accepts every other <input> attribute (name, value, form, onChange ...), and ref points to the inner <input>. className goes to the root <label>.

PropTypeDefaultDescription
checkedboolean—Controlled value
defaultCheckedbooleanfalseInitial value (uncontrolled)
onCheckedChange(checked: boolean) => void—Called when the user toggles it
labelReactNode—Text next to the switch
descriptionReactNode—Hint under the label
size'sm' | 'md''md'Size: 32×18 / 40×24px
invalidboolean—Error state (automatic inside a Field with an error)
disabledboolean—Disabled state
classNamestring—Class for the root <label>

Data attributes

On the root <label>: data-state (checked · unchecked), data-size (sm · md), data-disabled and data-invalid.

Accessibility

  • Native <input type="checkbox" role="switch"> — screen readers announce "switch, on", it toggles with Space and submits with the form.
  • Clicking the label toggles it too — the whole component is a <label>.
  • The hit area is larger than the track, so it's easy to tap.
  • The focus ring shows only for keyboard focus.
  • For a switch without a label, pass aria-label.

On this page