{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "switch",
  "title": "Switch",
  "description": "iOS, Material 3, square, neo-brutalist, icon thumb and ON/OFF label variants with none, smooth, spring and bounce motion, six tones, three sizes, track shadows, sun/moon or check/x glyphs, label and description on either side, and a loading state.",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json",
    "https://goodui.dev/r/sounds.json"
  ],
  "files": [
    {
      "path": "registry/components/core/switch.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { motion, useReducedMotion, type Transition } from \"motion/react\"\nimport { Check, Moon, Sun, X } from \"lucide-react\"\nimport { cn } from \"@/lib/utils\"\nimport type { SoundName } from \"@/lib/goodui-sounds\"\nimport { GooduiTokens, spring, ease as easing } from \"@/lib/goodui-motion\"\n\n/*\n * One switch, independent layers:\n *   variant    ios, material, square, neo, icon-thumb, label-inside\n *   tone       which color when on         animation  none, smooth, spring, bounce\n *   shadow     how the track sits          size       sm, md, lg\n *   icons      glyphs inside the thumb     label/description on the left or right, loading state\n */\n\nexport type SwitchVariant = \"ios\" | \"material\" | \"square\" | \"neo\" | \"icon-thumb\" | \"label-inside\"\nexport type SwitchTone = \"primary\" | \"neutral\" | \"success\" | \"warning\" | \"danger\" | \"info\"\nexport type SwitchAnimation = \"none\" | \"smooth\" | \"spring\" | \"bounce\"\nexport type SwitchShadow = \"none\" | \"sm\" | \"inner\" | \"glow\"\nexport type SwitchSize = \"sm\" | \"md\" | \"lg\"\nexport type SwitchIcons = \"auto\" | \"none\" | \"check\" | \"theme\"\n\nexport type SwitchProps = Omit<React.ComponentProps<\"button\">, \"value\" | \"onChange\" | \"children\"> & {\n  /** On state (controlled). */\n  checked?: boolean\n  /** Initial state when uncontrolled. */\n  defaultChecked?: boolean\n  /** Called with the new state on click, Space or Enter (not while loading). */\n  onCheckedChange?: (checked: boolean) => void\n  /** Track and thumb style: ios (default), material, square, neo, icon-thumb (check/x) or label-inside (ON/OFF). */\n  variant?: SwitchVariant\n  /** Track color when on. */\n  tone?: SwitchTone\n  /** How the thumb travels: spring (default), smooth ease, bounce with squash, or none. */\n  animation?: SwitchAnimation\n  /** Track shadow; glow only shows when on. */\n  shadow?: SwitchShadow\n  size?: SwitchSize\n  /** Glyphs inside the thumb. \"auto\" shows check/x on icon-thumb and nothing elsewhere. */\n  icons?: SwitchIcons\n  /** Custom thumb glyphs, override `icons`. */\n  checkedIcon?: React.ReactNode\n  /** Custom thumb glyph for the off state. */\n  uncheckedIcon?: React.ReactNode\n  /** Text inside the track for the label-inside variant. */\n  onText?: string\n  /** Off text for the label-inside variant. */\n  offText?: string\n  /** Visible label; the row becomes a <label> linked with aria-labelledby. */\n  label?: React.ReactNode\n  /** Muted helper text under the label, linked with aria-describedby. */\n  description?: React.ReactNode\n  /** Where the text sits. \"left\" makes a full-width settings row with the switch at the end. */\n  labelPosition?: \"left\" | \"right\"\n  /** Shows a spinner in the thumb and blocks toggling, e.g. while saving. */\n  loading?: boolean\n  /** Submits \"on\" (or `value`) under this name in a form when checked. */\n  name?: string\n  /** Value submitted with name when checked; defaults to \"on\". */\n  value?: string\n  /** Plays \"toggle-on\" / \"toggle-off\" when the visitor has sounds on (lib/goodui-sounds); pass a sound name to use one sound for both. Off by default. */\n  sound?: boolean | SoundName\n}\n\ntype Geo = { w: number; h: number; t: number; p: number; b?: number }\nconst GEO: Record<\"base\" | \"material\" | \"neo\" | \"label\", Record<SwitchSize, Geo>> = {\n  base: { sm: { w: 32, h: 18, t: 14, p: 2 }, md: { w: 44, h: 24, t: 20, p: 2 }, lg: { w: 56, h: 30, t: 26, p: 2 } },\n  material: { sm: { w: 40, h: 24, t: 18, p: 3, b: 2 }, md: { w: 52, h: 32, t: 24, p: 4, b: 2 }, lg: { w: 60, h: 36, t: 28, p: 4, b: 2 } },\n  neo: { sm: { w: 36, h: 20, t: 12, p: 4, b: 2 }, md: { w: 48, h: 26, t: 16, p: 5, b: 2 }, lg: { w: 58, h: 32, t: 20, p: 6, b: 2 } },\n  label: { sm: { w: 48, h: 20, t: 16, p: 2 }, md: { w: 62, h: 26, t: 22, p: 2 }, lg: { w: 76, h: 32, t: 28, p: 2 } },\n}\nconst geoFor = (v: SwitchVariant, s: SwitchSize) => (v === \"material\" ? GEO.material : v === \"neo\" ? GEO.neo : v === \"label-inside\" ? GEO.label : GEO.base)[s]\n\nconst tones = (c: string) => `\n.${c}{--pc-c:var(--primary);--pc-fg:var(--primary-foreground)}\n.${c}[data-tone=neutral]{--pc-c:var(--foreground);--pc-fg:var(--background)}\n.${c}[data-tone=success]{--pc-c:oklch(0.53 0.13 155);--pc-fg:#fff}\n.${c}[data-tone=warning]{--pc-c:oklch(0.75 0.16 70);--pc-fg:oklch(0.25 0.05 70)}\n.${c}[data-tone=danger]{--pc-c:oklch(0.56 0.21 27);--pc-fg:#fff}\n.${c}[data-tone=info]{--pc-c:oklch(0.55 0.17 250);--pc-fg:#fff}\n.dark .${c}[data-tone=success]{--pc-c:oklch(0.7 0.16 155);--pc-fg:oklch(0.2 0.05 155)}\n.dark .${c}[data-tone=danger]{--pc-c:oklch(0.68 0.2 25);--pc-fg:oklch(0.2 0.05 25)}\n.dark .${c}[data-tone=info]{--pc-c:oklch(0.7 0.15 245);--pc-fg:oklch(0.2 0.05 245)}\n.${c}{--pc-tint:color-mix(in oklab,var(--pc-c) 14%,transparent);--pc-ink:color-mix(in oklab,var(--pc-c) 82%,var(--foreground))}\n.${c}[data-tone=warning]{--pc-ink:color-mix(in oklab,var(--pc-c) 62%,var(--foreground))}\n`\n\nconst K = \".pc-switch-track\"\nconst CSS = `${tones(\"pc-switch\")}\n.pc-switch{--pc-off:color-mix(in oklab,var(--foreground) 15%,transparent);--pc-thumb:#fff;--pc-thumb-on:#fff}\n.dark .pc-switch{--pc-off:color-mix(in oklab,var(--foreground) 22%,transparent);--pc-thumb:oklch(0.96 0 0)}\n.pc-switch[data-tone=primary],.pc-switch[data-tone=neutral]{--pc-thumb-on:var(--pc-fg)}\n${K}{background:var(--pc-off);box-shadow:inset 0 1px 2px hsl(var(--pc-sh)/.16),inset 0 0 0 .5px hsl(var(--pc-sh)/.06);-webkit-tap-highlight-color:transparent}\n${K}[data-state=checked]{background:var(--pc-sheen),var(--pc-c);box-shadow:inset 0 1px 2px rgb(0 0 0/.14),inset 0 0 0 .5px rgb(0 0 0/.08)}\n.pc-switch-thumb{background:var(--pc-thumb);color:color-mix(in oklab,var(--foreground) 55%,transparent);box-shadow:0 0 0 .5px rgb(0 0 0/.06),0 1px 2px hsl(var(--pc-sh)/.2),0 3px 6px -1px hsl(var(--pc-sh)/.22)}\n[data-state=checked]>.pc-switch-thumb{background:var(--pc-thumb-on);color:var(--pc-c)}\n.dark .pc-switch-thumb{color:oklch(0.4 0 0)}\n.dark [data-state=checked]>.pc-switch-thumb{color:var(--pc-c)}\n.dark .pc-switch[data-tone=primary] [data-state=checked]>.pc-switch-thumb,.dark .pc-switch[data-tone=neutral] [data-state=checked]>.pc-switch-thumb{color:var(--pc-c)}\n\n${K}[data-variant=material]{box-shadow:none;background:color-mix(in oklab,var(--foreground) 6%,transparent);border:2px solid color-mix(in oklab,var(--foreground) 45%,transparent)}\n${K}[data-variant=material][data-state=checked]{background:var(--pc-c);border-color:var(--pc-c)}\n${K}[data-variant=material]>.pc-switch-thumb{background:color-mix(in oklab,var(--foreground) 55%,transparent);color:var(--background);box-shadow:none}\n${K}[data-variant=material][data-state=checked]>.pc-switch-thumb{background:var(--pc-fg);color:var(--pc-c)}\n\n${K}[data-variant=neo]{background:var(--background);border:2px solid var(--foreground);box-shadow:2px 2px 0 var(--foreground)}\n${K}[data-variant=neo][data-state=checked]{background:var(--pc-c)}\n${K}[data-variant=neo]>.pc-switch-thumb{background:var(--foreground);color:var(--background);box-shadow:none}\n${K}[data-variant=neo][data-state=checked]>.pc-switch-thumb{background:var(--background);color:var(--foreground);outline:2px solid var(--foreground)}\n${K}[data-variant=neo]:active:not(:disabled){box-shadow:0 0 0 var(--foreground);transform:translate(2px,2px)}\n\n.pc-switch-text{color:color-mix(in oklab,var(--muted-foreground) 60%,var(--foreground))}\n[data-state=checked]>.pc-switch-text{color:var(--pc-fg)}\n\n${K}[data-shadow=sm]{box-shadow:inset 0 1px 2px hsl(var(--pc-sh)/.14),var(--pc-e1)}\n${K}[data-shadow=inner]{box-shadow:inset 0 1px 3px hsl(var(--pc-sh)/.34)}\n${K}[data-shadow=glow][data-state=checked]{box-shadow:0 0 0 3px var(--pc-tint),0 4px 16px -2px color-mix(in oklab,var(--pc-c) 60%,transparent)}\n\n${K}[data-anim]:not([data-anim=none]){transition:scale 120ms var(--pc-ease-out),background-color 200ms ease,border-color 200ms ease,box-shadow 200ms ease,transform 100ms var(--pc-ease-out)}\n${K}[data-anim]:not([data-anim=none])>.pc-switch-thumb{transition:background-color .22s,color .22s}\n${K}[data-anim]:not([data-anim=none]) .pc-switch-glyph{transition:opacity .2s,transform .3s var(--pc-ease-spring)}\n${K}[data-anim]:not([data-anim=none])>.pc-switch-text{transition:opacity .2s,color .2s}\n.pc-switch-glyph{position:absolute;inset:0;display:grid;place-items:center}\n.pc-switch-glyph[data-show=false]{opacity:0;transform:rotate(-90deg) scale(.4)}\n.pc-switch-spin{animation:pc-switch-spin .7s linear infinite}\n@keyframes pc-switch-spin{to{transform:rotate(360deg)}}\n@media (prefers-reduced-motion:reduce){\n  .pc-switch *{transition:none!important}\n  .pc-switch-spin{animation-duration:1.6s}\n}\n`\n\nconst TEXT_SIZE: Record<SwitchSize, string> = { sm: \"text-[9px]\", md: \"text-[10px]\", lg: \"text-[11px]\" }\nconst LABEL_SIZE: Record<SwitchSize, string> = { sm: \"text-[13px]\", md: \"text-sm\", lg: \"text-[15px]\" }\n\n// Optional UI sound: the sound module is loaded only when `sound` is on, so silent apps pay nothing.\nfunction useCue(sound: boolean | SoundName | undefined) {\n  React.useEffect(() => {\n    if (sound) void import(\"@/lib/goodui-sounds\")\n  }, [sound])\n  return React.useCallback(\n    (fallback: SoundName, o?: { rate?: number; exact?: boolean }) => {\n      if (!sound) return\n      void import(\"@/lib/goodui-sounds\").then((m) => m.play(sound === true || o?.exact ? fallback : sound, { rate: o?.rate }))\n    },\n    [sound],\n  )\n}\n\nexport function Switch({\n  checked: checkedProp,\n  defaultChecked = false,\n  onCheckedChange,\n  variant = \"ios\",\n  tone = \"primary\",\n  animation = \"spring\",\n  shadow = \"none\",\n  size = \"md\",\n  icons = \"auto\",\n  checkedIcon,\n  uncheckedIcon,\n  onText = \"ON\",\n  offText = \"OFF\",\n  label,\n  description,\n  labelPosition = \"right\",\n  loading,\n  name,\n  value,\n  sound,\n  disabled,\n  className,\n  onClick,\n  onPointerDown,\n  onKeyDown,\n  onKeyUp,\n  onBlur,\n  id: idProp,\n  ...props\n}: SwitchProps) {\n  const prefersReduced = useReducedMotion()\n  const cue = useCue(sound)\n  // Reduced motion feeds data-anim, so wait for mount to keep the first client render equal to the server's.\n  const [mounted, setMounted] = React.useState(false)\n  React.useEffect(() => setMounted(true), [])\n  const reduce = mounted && !!prefersReduced\n  const uid = React.useId()\n  const id = idProp ?? uid\n  const [inner, setInner] = React.useState(defaultChecked)\n  const checked = checkedProp ?? inner\n  const g = geoFor(variant, size)\n  const travel = g.w - g.t - 2 * g.p\n  const anim: SwitchAnimation = reduce ? \"none\" : animation\n  const [pressed, setPressed] = React.useState(false)\n  const btn = React.useRef<HTMLButtonElement>(null)\n  // Right-to-left: the thumb starts on the right and travels the other way. Read after mount so SSR output is unchanged.\n  const [rtl, setRtl] = React.useState(false)\n  React.useLayoutEffect(() => {\n    if (btn.current) setRtl(getComputedStyle(btn.current).direction === \"rtl\")\n  }, [])\n  React.useEffect(() => {\n    if (!pressed) return\n    const up = () => setPressed(false)\n    window.addEventListener(\"pointerup\", up)\n    window.addEventListener(\"pointercancel\", up)\n    return () => {\n      window.removeEventListener(\"pointerup\", up)\n      window.removeEventListener(\"pointercancel\", up)\n    }\n  }, [pressed])\n\n  const toggle = (e: React.MouseEvent<HTMLButtonElement>) => {\n    onClick?.(e)\n    if (e.defaultPrevented || loading) return\n    if (checkedProp === undefined) setInner(!checked)\n    onCheckedChange?.(!checked)\n    cue(checked ? \"toggle-off\" : \"toggle-on\")\n  }\n\n  const set = icons === \"auto\" ? (variant === \"icon-thumb\" ? \"check\" : \"none\") : icons\n  const onGlyph = checkedIcon ?? (set === \"check\" ? <Check strokeWidth={3} /> : set === \"theme\" ? <Moon strokeWidth={2.5} /> : null)\n  const offGlyph = uncheckedIcon ?? (set === \"check\" ? <X strokeWidth={3} /> : set === \"theme\" ? <Sun strokeWidth={2.5} /> : null)\n\n  const material = variant === \"material\"\n  const offScale = material && !offGlyph && !loading ? (g.t - 8) / g.t : 1\n  const transition: Transition =\n    anim === \"none\"\n      ? { duration: 0 }\n      : anim === \"smooth\"\n        ? { duration: 0.2, ease: easing.out }\n        : anim === \"spring\"\n          ? spring.snap\n          : { x: spring.play, scale: spring.play, scaleX: { duration: 0.34 }, scaleY: { duration: 0.34 } }\n  const squash = anim === \"bounce\" ? { scaleX: [1, 1.28, 1], scaleY: [1, 0.86, 1] } : {}\n  const rest = { x: checked ? (rtl ? -travel : travel) : 0, scale: checked ? 1 : offScale }\n  // Held down, the thumb stretches toward its travel and flattens a little, as if it had weight.\n  const held = !pressed || anim === \"none\" || variant === \"neo\" || loading ? squash : material ? { scale: 1.12 } : { scaleX: 1.18, scaleY: 0.93 }\n\n  const radius = variant === \"square\" ? \"rounded-[5px]\" : variant === \"neo\" ? \"rounded-[6px]\" : \"rounded-full\"\n  const thumbRadius = variant === \"square\" ? \"rounded-[3px]\" : variant === \"neo\" ? \"rounded-[3px]\" : \"rounded-full\"\n  const glyphSize = Math.round(g.t * 0.6)\n\n  const sw = (\n    <button\n      type=\"button\"\n      role=\"switch\"\n      id={id}\n      aria-checked={checked}\n      aria-busy={loading || undefined}\n      aria-labelledby={label ? `${id}-label` : undefined}\n      aria-describedby={description ? `${id}-desc` : undefined}\n      ref={btn}\n      disabled={disabled}\n      data-slot=\"switch\"\n      data-state={checked ? \"checked\" : \"unchecked\"}\n      data-variant={variant}\n      data-shadow={shadow}\n      data-anim={anim}\n      onClick={toggle}\n      onPointerDown={(e) => {\n        if (e.button === 0) setPressed(true)\n        onPointerDown?.(e)\n      }}\n      onKeyDown={(e) => {\n        if (e.key === \" \" || e.key === \"Enter\") setPressed(true)\n        onKeyDown?.(e)\n      }}\n      onKeyUp={(e) => {\n        setPressed(false)\n        onKeyUp?.(e)\n      }}\n      onBlur={(e) => {\n        setPressed(false)\n        onBlur?.(e)\n      }}\n      className={cn(\n        \"pc-press pc-switch-track relative inline-flex shrink-0 items-center outline-none\",\n        \"focus-visible:ring-3 focus-visible:ring-[color-mix(in_oklab,var(--pc-c)_40%,transparent)] focus-visible:ring-offset-1 focus-visible:ring-offset-background\",\n        \"disabled:cursor-not-allowed disabled:opacity-50\",\n        loading && \"cursor-progress\",\n        radius,\n        !label && !description && className,\n      )}\n      style={{ width: g.w, height: g.h }}\n      {...props}\n    >\n      {variant === \"label-inside\" && (\n        <span\n          aria-hidden=\"true\"\n          className={cn(\"pc-switch-text pointer-events-none absolute inset-y-0 flex items-center font-bold tracking-wide select-none\", TEXT_SIZE[size])}\n          style={checked ? { insetInlineStart: g.p + 5, insetInlineEnd: g.t + g.p + 2 } : { insetInlineStart: g.t + g.p + 2, insetInlineEnd: g.p + 5, justifyContent: \"flex-end\" }}\n        >\n          {checked ? onText : offText}\n        </span>\n      )}\n      <motion.span\n        aria-hidden=\"true\"\n        data-slot=\"switch-thumb\"\n        className={cn(\"pc-switch-thumb pointer-events-none relative block\", thumbRadius)}\n        style={{ width: g.t, height: g.t, marginInlineStart: g.p - (g.b ?? 0), originX: checked !== rtl ? 1 : 0 }}\n        initial={false}\n        animate={{ ...rest, ...held }}\n        transition={transition}\n      >\n        {loading ? (\n          <span className=\"pc-switch-glyph\">\n            <svg className=\"pc-switch-spin\" width={glyphSize} height={glyphSize} viewBox=\"0 0 24 24\" fill=\"none\">\n              <circle cx=\"12\" cy=\"12\" r=\"9\" stroke=\"currentColor\" strokeOpacity=\".25\" strokeWidth=\"3.5\" />\n              <path d=\"M21 12a9 9 0 0 0-9-9\" stroke=\"currentColor\" strokeWidth=\"3.5\" strokeLinecap=\"round\" />\n            </svg>\n          </span>\n        ) : (\n          (onGlyph || offGlyph) && (\n            <>\n              <span className=\"pc-switch-glyph [&_svg]:size-[var(--g)]\" data-show={checked} style={{ [\"--g\" as string]: `${glyphSize}px` }}>\n                {onGlyph}\n              </span>\n              <span className=\"pc-switch-glyph [&_svg]:size-[var(--g)]\" data-show={!checked} style={{ [\"--g\" as string]: `${glyphSize}px` }}>\n                {offGlyph}\n              </span>\n            </>\n          )\n        )}\n      </motion.span>\n    </button>\n  )\n\n  const hidden = name ? <input type=\"checkbox\" aria-hidden=\"true\" tabIndex={-1} hidden name={name} value={value ?? \"on\"} checked={checked} readOnly /> : null\n\n  if (!label && !description) {\n    return (\n      <>\n        <GooduiTokens />\n        <style href=\"pc-switch\" precedence=\"default\">\n          {CSS}\n        </style>\n        <span className=\"pc-switch contents\" data-tone={tone}>\n          {sw}\n          {hidden}\n        </span>\n      </>\n    )\n  }\n\n  const text = (\n    <span className=\"flex min-w-0 flex-col gap-0.5\">\n      {label && (\n        <span id={`${id}-label`} className={cn(\"leading-snug font-medium\", LABEL_SIZE[size])}>\n          {label}\n        </span>\n      )}\n      {description && (\n        <span id={`${id}-desc`} className=\"text-[13px] leading-snug text-muted-foreground\">\n          {description}\n        </span>\n      )}\n    </span>\n  )\n\n  return (\n    <>\n      <GooduiTokens />\n      <style href=\"pc-switch\" precedence=\"default\">\n        {CSS}\n      </style>\n      <label\n        htmlFor={id}\n        data-tone={tone}\n        data-disabled={disabled || undefined}\n        className={cn(\n          \"pc-switch gap-3 select-none\",\n          description ? \"items-start\" : \"items-center\",\n          labelPosition === \"left\" ? \"flex w-full justify-between gap-4\" : \"inline-flex\",\n          disabled ? \"cursor-not-allowed opacity-70\" : \"cursor-pointer\",\n          className,\n        )}\n      >\n        {labelPosition === \"left\" && text}\n        {description ? (\n          <span className=\"flex shrink-0 items-center\" style={{ height: size === \"lg\" ? 22 : 20 }}>\n            {sw}\n          </span>\n        ) : (\n          sw\n        )}\n        {labelPosition === \"right\" && text}\n        {hidden}\n      </label>\n    </>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/switch.tsx"
    }
  ],
  "categories": [
    "components",
    "core"
  ],
  "type": "registry:component"
}