eo-n/ui
UIComponentsToggle

Toggle

Switches between two states when clicked.

Installation

CLI

npx shadcn@latest add "https://eo-n.vercel.app/r/toggle"

Manual

Copy and paste the following code into your project.

"use client";
 
import * as React from "react";
import { Toggle as ToggleRoot } from "@base-ui-components/react/toggle";
import { cva, type VariantProps } from "class-variance-authority";
 
import { cn } from "@/lib/utils";
 
const toggleVariants = cva(
  "inline-flex items-center justify-center gap-2 rounded-md text-sm font-medium hover:bg-muted hover:text-muted-foreground disabled:pointer-events-none disabled:opacity-50 data-[pressed]:bg-accent data-[pressed]:text-accent-foreground [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4 [&_svg]:shrink-0 focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] outline-none transition-[color,box-shadow] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive whitespace-nowrap",
  {
    variants: {
      variant: {
        default: "bg-transparent",
        outline:
          "border bg-background shadow-xs hover:bg-accent hover:text-accent-foreground dark:bg-input/30 border-input dark:hover:bg-input/50",
      },
      size: {
        default: "h-9 px-2 min-w-9",
        sm: "h-8 px-1.5 min-w-8",
        lg: "h-10 px-2.5 min-w-10",
      },
    },
    defaultVariants: {
      variant: "default",
      size: "default",
    },
  }
);
 
export type ToggleVariants = VariantProps<typeof toggleVariants>;
 
interface ToggleProps
  extends React.ComponentProps<typeof ToggleRoot>,
    ToggleVariants {}
 
function Toggle({ className, variant, size, ...props }: ToggleProps) {
  return (
    <ToggleRoot
      data-slot="toggle"
      className={cn(toggleVariants({ variant, size, className }))}
      {...props}
    />
  );
}
 
export { Toggle, toggleVariants };
Update the import paths to match your project setup.

Usage

Import all parts and piece them together.

import { Toggle } from "@/components/ui/toggle";
<Toggle>Press</Toggle>

Examples

Default

Outline

Large

Small

Disabled

On this page