Browse the docs
ComponentsNo. 60 of 109
Toggle
A button that stays pressed, in default and outline variants and three sizes.
Installation
pnpm dlx shadcn@latest add @corsair-ui/toggleNo setup needed: the CLI finds @corsair-ui in the shadcn registry directory. The files land in your project as source: edit them like your own code. More in Installation.
- Also adds
- utils
- npm
@radix-ui/react-toggleclass-variance-authorityclsxtailwind-merge
Examples
Default
Icon-only toggles need an aria-label.
Outline with text
Sizes
API
| Prop | Type | Default |
|---|---|---|
variantPlain or with a border. | "default" | "outline" | "default" |
sizeHeight. | "sm" | "default" | "lg" | "default" |
Built on Radix Toggle; every prop it takes is passed through.
Accessibility
- Announced with aria-pressed; icon-only toggles need an aria-label.
Compatibility
- Tailwind CSS
- 3.4 and 4, both checked in CI
- React
- 19
- Rendering
- Client component ("use client")
Source
1 file, installed as source you own
toggle.tsxView on GitHub"use client";
import * as TogglePrimitive from "@radix-ui/react-toggle";
import { cva, type VariantProps } from "class-variance-authority";
import type { ComponentProps } from "react";
import { cn } from "@/registry/default/lib/utils";
const toggleVariants = cva(
[
"inline-flex cursor-pointer items-center justify-center gap-2 rounded-md text-sm font-medium whitespace-nowrap",
"transition-[color,background-color,border-color,box-shadow] outline-none motion-reduce:transition-none",
"hover:bg-muted hover:text-muted-foreground",
"data-[state=on]:bg-accent data-[state=on]:text-accent-foreground",
"focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]",
"aria-[invalid=true]:border-destructive aria-[invalid=true]:ring-destructive/20",
"disabled:pointer-events-none disabled:opacity-50",
"[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
],
{
variants: {
variant: {
default: "bg-transparent",
outline: "border-input hover:bg-accent hover:text-accent-foreground border bg-transparent",
},
size: {
sm: "h-8 min-w-8 px-1.5",
default: "h-9 min-w-9 px-2",
lg: "h-10 min-w-10 px-2.5",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
}
);
/**
* A button that stays pressed (`aria-pressed`), e.g. bold in a toolbar. An
* icon-only toggle needs an `aria-label`.
*/
function Toggle({
className,
variant,
size,
...props
}: ComponentProps<typeof TogglePrimitive.Root> & VariantProps<typeof toggleVariants>) {
return (
<TogglePrimitive.Root
data-slot="toggle"
className={cn(toggleVariants({ variant, size, className }))}
{...props}
/>
);
}
export { Toggle, toggleVariants };