Skip to Content

Switch

A control that toggles between checked and unchecked.

import { Switch } from "@dapplelight/signet-ui/components/switch"
import { Label } from "@dapplelight/signet-ui/components/label"

export function Demo() {
  return (
    <div className="flex items-center gap-2">
      <Switch id="email-alerts" />
      <Label htmlFor="email-alerts">Email alerts</Label>
    </div>
  )
}

Installation

npm i @dapplelight/signet-ui

Usage

import { Switch } from "@dapplelight/signet-ui/components/switch"

<Switch aria-label="Email alerts" />

Examples

Size

import { Switch } from "@dapplelight/signet-ui/components/switch"
import { Label } from "@dapplelight/signet-ui/components/label"

export function Demo() {
  return (
    <div className="flex items-center gap-4">
      <Switch size="sm" aria-label="Compact view" />
      <Switch aria-label="Compact view" />
    </div>
  )
}

Controlled

import { useState } from "react"
import { Switch } from "@dapplelight/signet-ui/components/switch"
import { Label } from "@dapplelight/signet-ui/components/label"

export function Demo() {
  const [autoRenew, setAutoRenew] = useState(true)
  return (
    <div className="flex items-center gap-2">
      <Switch id="auto-renew" checked={autoRenew} onCheckedChange={setAutoRenew} />
      <Label htmlFor="auto-renew">Auto-renew subscription: {autoRenew ? "on" : "off"}</Label>
    </div>
  )
}

Disabled

import { Switch } from "@dapplelight/signet-ui/components/switch"
import { Label } from "@dapplelight/signet-ui/components/label"

export function Demo() {
  return (
    <div className="flex items-center gap-2">
      <Switch id="sso" disabled defaultChecked />
      <Label htmlFor="sso">Single sign-on (managed by your admin)</Label>
    </div>
  )
}

Invalid

import { Switch } from "@dapplelight/signet-ui/components/switch"
import { Label } from "@dapplelight/signet-ui/components/label"

export function Demo() {
  return (
    <div className="flex items-center gap-2">
      <Switch id="terms" aria-invalid />
      <Label htmlFor="terms">Accept the terms</Label>
    </div>
  )
}

API Reference

PropTypeDefaultDescription
size"default" | "sm""default"Track and thumb size.
checkedboolean-Controlled state.
defaultCheckedbooleanfalseInitial state.
onCheckedChange(checked: boolean, details) => void-Called when toggled.
disabledbooleanfalseBlocks interaction.
readOnlybooleanfalseShows the state but blocks changes.
requiredbooleanfalseRequire it to be on in a form.
namestring-Form field name.
value / uncheckedValuestring"on" / noneSubmitted when on / off.
aria-invalidboolean-Shows the destructive ring.

The remaining props come from the Base UI switch root.