{
  "schemaVersion": 1,
  "target": "react",
  "name": "switch",
  "version": "0.1.1",
  "category": "forms",
  "title": "Switch",
  "description": "An on/off switch with a gliding thumb, two sizes, and an optional label and description for settings rows.",
  "tags": [
    "form",
    "switch",
    "toggle",
    "settings",
    "base-ui"
  ],
  "requires": {
    "projectTypes": [
      "next",
      "vite-react"
    ],
    "styling": "tailwind-v4"
  },
  "npmDependencies": {
    "@base-ui/react": "^1.9.0"
  },
  "registryDependencies": [],
  "docsPath": "/components/switch",
  "props": [
    {
      "name": "checked / defaultChecked",
      "type": "boolean",
      "description": "Controlled or initial state."
    },
    {
      "name": "onCheckedChange",
      "type": "(checked: boolean) => void",
      "description": "Called when the switch flips."
    },
    {
      "name": "size",
      "type": "\"sm\" | \"md\"",
      "default": "\"md\"",
      "description": "16px or 20px tall."
    },
    {
      "name": "label",
      "type": "ReactNode",
      "description": "Text beside the switch. Clicking it flips the switch."
    },
    {
      "name": "description",
      "type": "ReactNode",
      "description": "A secondary line, linked with aria-describedby."
    },
    {
      "name": "controlPosition",
      "type": "\"start\" | \"end\"",
      "default": "\"start\"",
      "description": "Put the switch before the text, or after it at the far edge for a settings row."
    },
    {
      "name": "containerClassName",
      "type": "string",
      "description": "Classes for the label wrapper. className styles the track."
    }
  ],
  "accessibility": [
    "Base UI renders role=\"switch\" with aria-checked and a hidden native checkbox for forms.",
    "The label names the switch through aria-labelledby and the description is linked with aria-describedby.",
    "Space toggles the focused switch; the focus ring shows on keyboard focus only.",
    "On and off differ in thumb position and track fill, not only color.",
    "The thumb glides in 200ms; under reduced motion it moves without a transition."
  ],
  "states": [
    "off",
    "on",
    "focus-visible",
    "disabled"
  ],
  "files": [
    {
      "path": "ui/switch.tsx",
      "content": "\"use client\";\n\nimport { Switch as BaseSwitch } from \"@base-ui/react/switch\";\nimport { type ReactNode, useId } from \"react\";\n\ntype ClassName<State> = string | ((state: State) => string | undefined) | undefined;\n\nfunction cx(...classes: (string | false | null | undefined)[]) {\n  return classes.filter(Boolean).join(\" \");\n}\n\n/** Prepends base classes while keeping Base UI's state-function form of className working. */\nfunction withBase<State>(base: string, className: ClassName<State>) {\n  return typeof className === \"function\" ? (state: State) => cx(base, className(state)) : cx(base, className);\n}\n\nconst sizes = {\n  sm: { track: \"h-4 w-7\", thumb: \"size-3 data-[checked]:translate-x-3\" },\n  md: { track: \"h-5 w-9\", thumb: \"size-4 data-[checked]:translate-x-4\" },\n} as const;\n\nexport type SwitchSize = keyof typeof sizes;\n\nexport interface SwitchProps extends BaseSwitch.Root.Props {\n  /** `sm` (16px tall) for dense lists, `md` (20px) everywhere else. */\n  size?: SwitchSize;\n  /** Text beside the switch. Clicking it toggles the switch. */\n  label?: ReactNode;\n  /** A secondary line under the label, linked with `aria-describedby`. */\n  description?: ReactNode;\n  /**\n   * `start` puts the switch before the text. `end` puts it after, pushed to the far edge,\n   * the usual layout for a settings row.\n   */\n  controlPosition?: \"start\" | \"end\";\n  /** Classes for the wrapper that holds the switch, label, and description. `className` styles the track. */\n  containerClassName?: string;\n}\n\n/**\n * An on/off switch for settings that apply right away. The thumb glides across in 200ms with a\n * quick ease-out and the track fills; under reduced motion both change in place.\n */\nexport function Switch({\n  size = \"md\",\n  label,\n  description,\n  controlPosition = \"start\",\n  containerClassName,\n  className,\n  \"aria-describedby\": describedBy,\n  ...props\n}: SwitchProps) {\n  const id = useId();\n  const labelId = `${id}-label`;\n  const descriptionId = `${id}-description`;\n  const hasText = label != null || description != null;\n\n  const control = (\n    <BaseSwitch.Root\n      aria-labelledby={label != null ? labelId : undefined}\n      aria-describedby={cx(description != null && descriptionId, describedBy) || undefined}\n      className={withBase(\n        cx(\n          \"group/switch relative inline-flex shrink-0 cursor-pointer items-center rounded-full p-0.5 outline-none\",\n          \"after:absolute after:-inset-1.5 after:content-['']\",\n          sizes[size].track,\n          \"bg-zinc-950/[0.12] shadow-[inset_0_0_0_1px_rgb(0_0_0/0.04)] hover:bg-zinc-950/[0.18]\",\n          \"dark:bg-white/[0.16] dark:shadow-[inset_0_0_0_1px_rgb(255_255_255/0.04)] dark:hover:bg-white/[0.22]\",\n          \"data-[checked]:bg-zinc-950 data-[checked]:hover:bg-zinc-800 dark:data-[checked]:bg-white dark:data-[checked]:hover:bg-zinc-200\",\n          \"data-[invalid]:shadow-[inset_0_0_0_1px_rgb(239_68_68)]\",\n          \"transition-[background-color,box-shadow] duration-200 ease-out motion-reduce:transition-none\",\n          \"focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-sky-500\",\n          \"data-[disabled]:cursor-not-allowed data-[disabled]:opacity-45\",\n        ),\n        className,\n      )}\n      {...props}\n    >\n      <BaseSwitch.Thumb\n        className={cx(\n          \"block rounded-full bg-white shadow-[0_1px_2px_rgb(0_0_0/0.2),0_0_0_0.5px_rgb(0_0_0/0.04)]\",\n          \"dark:data-[checked]:bg-black dark:data-[checked]:shadow-none\",\n          sizes[size].thumb,\n          \"transition-[translate,background-color] duration-200 ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-none\",\n        )}\n      />\n    </BaseSwitch.Root>\n  );\n\n  if (!hasText) return control;\n\n  const end = controlPosition === \"end\";\n  return (\n    <label\n      className={cx(\n        \"group/item flex cursor-pointer items-start gap-3 has-[[data-disabled]]:cursor-not-allowed\",\n        end && \"justify-between\",\n        containerClassName,\n      )}\n    >\n      {end ? null : <span className=\"flex h-5 shrink-0 items-center\">{control}</span>}\n      <span className=\"grid min-w-0 gap-0.5 group-has-[[data-disabled]]/item:opacity-50\">\n        {label != null ? (\n          <span id={labelId} className=\"text-sm leading-5 text-zinc-950 select-none dark:text-white\">\n            {label}\n          </span>\n        ) : null}\n        {description != null ? (\n          <span id={descriptionId} className=\"text-sm leading-5 text-zinc-500 dark:text-zinc-400\">\n            {description}\n          </span>\n        ) : null}\n      </span>\n      {end ? <span className=\"flex h-5 shrink-0 items-center\">{control}</span> : null}\n    </label>\n  );\n}\n"
    }
  ],
  "example": {
    "content": "import { Switch } from \"@/components/shaken/ui/switch\";\n\nexport default function SwitchExample() {\n  return (\n    <div className=\"grid w-full max-w-md gap-8\">\n      <div className=\"divide-y divide-zinc-950/[0.07] rounded-xl border border-zinc-950/10 bg-white px-4 dark:divide-white/[0.08] dark:border-white/10 dark:bg-black\">\n        <Switch\n          controlPosition=\"end\"\n          containerClassName=\"py-3.5\"\n          defaultChecked\n          label=\"Stream tokens\"\n          description=\"Show the reply as it is written instead of all at once.\"\n        />\n        <Switch\n          controlPosition=\"end\"\n          containerClassName=\"py-3.5\"\n          label=\"Pause before destructive tools\"\n          description=\"Deleting files or rows waits for an approval.\"\n          defaultChecked\n        />\n        <Switch controlPosition=\"end\" containerClassName=\"py-3.5\" label=\"Share traces with support\" disabled />\n      </div>\n      <div className=\"flex flex-wrap items-center gap-6\">\n        <Switch size=\"sm\" label=\"Compact\" />\n        <Switch size=\"sm\" defaultChecked label=\"Wrap lines\" />\n        <Switch aria-label=\"Auto-save\" defaultChecked />\n      </div>\n    </div>\n  );\n}\n"
  }
}
