Saltar al contenido
VEXA
Formularios

Switch

Hoja cliente

An instant on/off toggle for a single setting.

Vista previa en vivo

Renderizado desde el código fuente instalado de VEXA — los mismos archivos que recibes.

Escritorio

Playground

Ajusta las props y copia el JSX equivalente.

Playground

switch.tsx
<Switch />

Instalación

Añade el código del componente a tu proyecto. Las dependencias del registry se resuelven automáticamente.

npx shadcn@latest add https://vexa.valfiguer.com/r/switch.json

Se instala en components/vexa/switch.tsx

Dependencias npm

  • @radix-ui/react-switch

Dependencias del registry

  • @vexa/utils

Otros items de VEXA que necesita este componente. Instálalos primero — la CLI los resuelve automáticamente.

Código

Cópialo a su ruta destino, o instálalo con el comando de arriba.

components/vexa/switch.tsx
"use client";

import type * as React from "react";
import * as SwitchPrimitive from "@radix-ui/react-switch";

import { cn } from "@/lib/utils";

/**
 * Switch — an instant on/off toggle for a single setting.
 *
 * Client leaf. Radix supplies `role="switch"`, `aria-checked`, and the keyboard
 * model (Space/Enter toggles). Use a Switch for settings that apply immediately;
 * prefer a Checkbox inside forms that are submitted. State is conveyed by the
 * thumb position, not color alone. Provide an accessible name via `<Label>` or
 * `aria-label`. The thumb transition respects reduced-motion.
 */
export function Switch({
  className,
  ...props
}: React.ComponentProps<typeof SwitchPrimitive.Root>) {
  return (
    <SwitchPrimitive.Root
      data-slot="switch"
      className={cn(
        "peer inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-full border border-transparent",
        "transition-colors duration-[var(--vexa-duration-fast)] ease-[var(--vexa-ease-standard)]",
        "outline-none focus-visible:ring-2 focus-visible:ring-vexa-ring focus-visible:ring-offset-2 focus-visible:ring-offset-vexa-background",
        "disabled:cursor-not-allowed disabled:opacity-50",
        "data-[state=unchecked]:bg-vexa-input data-[state=checked]:bg-vexa-primary",
        className,
      )}
      {...props}
    >
      <SwitchPrimitive.Thumb
        data-slot="switch-thumb"
        className={cn(
          "pointer-events-none block size-5 rounded-full bg-vexa-surface-raised shadow-vexa-sm ring-0",
          "transition-transform duration-[var(--vexa-duration-fast)] ease-[var(--vexa-ease-standard)] motion-reduce:transition-none",
          "translate-x-0.5 data-[state=checked]:translate-x-[1.375rem]",
        )}
      />
    </SwitchPrimitive.Root>
  );
}
JSON del registryr/switch.json
r/switch.json
{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "switch",
  "type": "registry:ui",
  "title": "Switch",
  "description": "An instant on/off toggle for a single setting.",
  "dependencies": [
    "@radix-ui/react-switch"
  ],
  "registryDependencies": [
    "@vexa/utils"
  ],
  "files": [
    {
      "path": "registry/default/ui/switch.tsx",
      "type": "registry:ui",
      "target": "components/vexa/switch.tsx",
      "content": "\"use client\";\n\nimport type * as React from \"react\";\nimport * as SwitchPrimitive from \"@radix-ui/react-switch\";\n\nimport { cn } from \"@/lib/utils\";\n\n/**\n * Switch — an instant on/off toggle for a single setting.\n *\n * Client leaf. Radix supplies `role=\"switch\"`, `aria-checked`, and the keyboard\n * model (Space/Enter toggles). Use a Switch for settings that apply immediately;\n * prefer a Checkbox inside forms that are submitted. State is conveyed by the\n * thumb position, not color alone. Provide an accessible name via `<Label>` or\n * `aria-label`. The thumb transition respects reduced-motion.\n */\nexport function Switch({\n  className,\n  ...props\n}: React.ComponentProps<typeof SwitchPrimitive.Root>) {\n  return (\n    <SwitchPrimitive.Root\n      data-slot=\"switch\"\n      className={cn(\n        \"peer inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-full border border-transparent\",\n        \"transition-colors duration-[var(--vexa-duration-fast)] ease-[var(--vexa-ease-standard)]\",\n        \"outline-none focus-visible:ring-2 focus-visible:ring-vexa-ring focus-visible:ring-offset-2 focus-visible:ring-offset-vexa-background\",\n        \"disabled:cursor-not-allowed disabled:opacity-50\",\n        \"data-[state=unchecked]:bg-vexa-input data-[state=checked]:bg-vexa-primary\",\n        className,\n      )}\n      {...props}\n    >\n      <SwitchPrimitive.Thumb\n        data-slot=\"switch-thumb\"\n        className={cn(\n          \"pointer-events-none block size-5 rounded-full bg-vexa-surface-raised shadow-vexa-sm ring-0\",\n          \"transition-transform duration-[var(--vexa-duration-fast)] ease-[var(--vexa-ease-standard)] motion-reduce:transition-none\",\n          \"translate-x-0.5 data-[state=checked]:translate-x-[1.375rem]\",\n        )}\n      />\n    </SwitchPrimitive.Root>\n  );\n}\n"
    }
  ],
  "meta": {
    "frameworks": [
      "react",
      "next"
    ],
    "rsc": "client",
    "cssVars": [
      "--vexa-input",
      "--vexa-primary",
      "--vexa-surface-raised",
      "--vexa-ring",
      "--vexa-background"
    ],
    "a11y": "role=switch with aria-checked; Space/Enter toggles. State is shown by the thumb position, not color alone. The thumb transition respects reduced motion.",
    "example": "<div className=\"flex items-center gap-2\"><Switch id=\"wifi\" /><Label htmlFor=\"wifi\">Wi-Fi</Label></div>",
    "tests": "registry/tests/switch.test.tsx"
  }
}

Detalles

Accesibilidad

role=switch with aria-checked; Space/Enter toggles. State is shown by the thumb position, not color alone. The thumb transition respects reduced motion.