Switch

A switch allows a user to turn a setting on or off.

Example

Features

There is no native HTML element with switch styling. <input type="checkbox"> is the closest semantically, but isn't styled or exposed to assistive technology as a switch. Switch helps achieve accessible switches that can be styled as needed.

  • Styleable – Hover, press, keyboard focus, and selection states are provided for easy styling. These states only apply when interacting with an appropriate input device, unlike CSS pseudo classes.
  • Accessible – Uses a visually hidden <input> element with role="switch" under the hood, which also enables HTML form integration and autofill. A label element is built-in to ensure the switch is usable with assistive technologies.
  • Cross-browser – Mouse, touch, keyboard, and focus interactions are normalized to ensure consistency across browsers and devices.

Installation

Install following dependencies:

npm install react-aria-components tw-animate-css tailwindcss-react-aria-components class-variance-authority lucide-react clsx tailwind-merge motion

Add cn helper using clsx and twMerge for conditional class merging in Tailwind components.

lib/utils.ts

Add composeTailwindRenderProps helper using cn and composeRenderProps from react-aria-components for conditional class rendering.

lib/render-props.ts

Copy and paste the following code into your project.

components/ui/core/switch.tsx
npx @libravelui@latest add switch

Anatomy

import { Switch } from "@/components/ui/core/switch";
<Switch
  label="A label"
  description="Some description"
/>

<Switch>
    {/* Some Content */}
</Switch>

Another Example

With Card

Props

Loading types…