Skip to Content
Neshel-UI
Components

Switch

An on/off control that takes effect immediately and works in native forms.

Client component ("use client")

Not in 0.1.0 yet

This API is on the main branch but not in the 0.1.0 packages on npm. To use it today, install locally packed tarballs.

Purpose

Switch turns a setting on or off. Use it for immediate, binary settings; use a checkbox for choices confirmed with a submit button in long forms.

Import

import.tsx
import { Label, Switch } from "@neshel-ui/components";

Usage

Preview: switch-form
Preview theme
switch-form.tsx
"use client";
import { Button, Label, Switch } from "@neshel-ui/components";
import { useState } from "react";

export default function SwitchForm() {
  const [data, setData] = useState("");
  return (
    <form
      onSubmit={(event) => {
        event.preventDefault();
        setData(JSON.stringify([...new FormData(event.currentTarget).entries()]));
      }}
      style={{ display: "grid", gap: 12, justifyItems: "start" }}
    >
      <div style={{ display: "flex", gap: 8, alignItems: "center" }}>
        <Switch id="demo-news" name="news" />
        <Label htmlFor="demo-news">Newsletter</Label>
      </div>
      <div style={{ display: "flex", gap: 8, alignItems: "center" }}>
        <Switch id="demo-terms" name="terms" required />
        <Label htmlFor="demo-terms" required>
          Accept terms
        </Label>
      </div>
      <Button type="submit" size="sm">
        Save
      </Button>
      <output>{data}</output>
    </form>
  );
}

API

SwitchProps

SwitchProps
PropTypeDefaultDescription
checkedboolean—Controlled state.
defaultCheckedbooleanfalseInitial state when uncontrolled (default false).
disabledboolean——
formstring——
idstring—Id of the switch button; associate a <Label htmlFor> with it.
invalidboolean—Marks the switch aria-invalid. Describe the error with aria-describedby.
namestring—Form field name; submitted only while the switch is on.
onCheckedChange(checked: boolean) => void——
readOnlyboolean—Keeps focus and announcements but ignores toggling.
refRef<HTMLButtonElement>——
requiredboolean—The form cannot be submitted while the switch is off.
uiSizeFieldSize"md"Size (default "md"), consistent with Input and Select.
valuestring"on"Submitted value while on (default "on", like a checkbox).

Also accepts the props of: React DOM attributes.

Controlled and uncontrolled

checked with onCheckedChange, or defaultChecked. In a form the switch submits name=value (value defaults to "on") only while it is on; required blocks submission while it is off; a form reset restores defaultChecked and, like native inputs, does not call onCheckedChange.

Preview: switch-controlled
Preview theme

Files are synchronised.

switch-controlled.tsx
"use client";
import { Label, Switch } from "@neshel-ui/components";
import { useState } from "react";

export default function SwitchControlled() {
  const [enabled, setEnabled] = useState(true);
  return (
    <div style={{ display: "grid", gap: 12, justifyItems: "start" }}>
      <div style={{ display: "flex", gap: 8, alignItems: "center" }}>
        <Switch
          id="demo-sync"
          uiSize="lg"
          checked={enabled}
          onCheckedChange={setEnabled}
          aria-describedby="demo-sync-hint"
        />
        <Label htmlFor="demo-sync">Synchronise files</Label>
      </div>
      <p id="demo-sync-hint">
        {enabled ? "Files are synchronised." : "Synchronisation is paused."}
      </p>
      <div style={{ display: "flex", gap: 8, alignItems: "center" }}>
        <Switch id="demo-locked" checked readOnly uiSize="sm" />
        <Label htmlFor="demo-locked">Managed by your organisation</Label>
      </div>
    </div>
  );
}

Sizes and states

uiSize is sm, md (default) or lg, like the other fields. readOnly keeps the value and stays focusable; disabled removes it from the tab order and from the form data.

Accessibility

  • A native <button role="switch"> with aria-checked; associate a <Label htmlFor> with its id (clicking the label toggles it).
  • invalid sets aria-invalid; describe the error with aria-describedby.
  • Visible focus ring on keyboard focus, system colours in forced-colours mode, no thumb transition under reduced motion.

Keyboard

KeyAction
Space / EnterToggle
TabMove focus

Common mistakes

  • A switch without a label, or a label that changes with the state: keep the name constant and let aria-checked tell the state.
  • A switch that only takes effect after pressing Save: use a checkbox.
  • Reading false from the form data: an off switch is absent, like an unchecked checkbox.
Edit on GitHub

On this page