Components
- Accordion
- Alert
- Alert Dialog
- Aspect Ratio
- Attachment
- Avatar
- Badge
- Breadcrumb
- Bubble
- Button
- Button Group
- Calendar
- Card
- Carousel
- Chart
- Checkbox
- Code Block
- Collapsible
- Combobox
- Command
- Context Menu
- Data Table
- Date Picker
- Dialog
- Direction
- Drawer
- Dropdown Menu
- Empty
- Field
- Hover Card
- Input
- Input Group
- Input OTP
- Item
- Kbd
- Label
- Marker
- Message
- Message Scroller
- Native Select
- Pagination
- Popover
- Progress
- Questionnaire
- Radio Group
- Resizable
- Scroll Area
- Select
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Sonner
- Spinner
- Switch
- Table
- Tabs
- Textarea
- Toast
- Toggle
- Toggle Group
- Tooltip
- Trace
- Typography
AI Elements
Get Started
@shadcn/react
@shadcn/helpers
Utilities
import { BookmarkIcon } from "lucide-react"
import { Toggle } from "@/components/ui/toggle"import { BookmarkIcon } from "lucide-react"
import { Toggle } from "@/components/ui/toggle"
export function ToggleDemo() {
return (
<Toggle aria-label="Toggle bookmark" size="sm" variant="outline">
<BookmarkIcon className="group-aria-pressed/toggle:fill-foreground" />
Bookmark
</Toggle>
)
}
Installation#
pnpm dlx shadcn@latest add toggle
Install the following dependencies:
pnpm add react-aria-components
Copy and paste the following code into your project.
"use client"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
import {
ToggleButton as TogglePrimitive,
type ToggleButtonProps,
} from "react-aria-components"
const toggleVariants = cva(
"group/toggle inline-flex items-center justify-center gap-1 rounded-lg text-sm font-medium whitespace-nowrap transition-all outline-none hover:bg-muted hover:text-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 aria-pressed:bg-muted data-[state=on]:bg-muted dark:aria-invalid:ring-destructive/40 data-selected:bg-muted [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
{
variants: {
variant: {
default: "bg-transparent",
outline: "border border-input bg-transparent hover:bg-muted",
},
size: {
default:
"h-8 min-w-8 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
sm: "h-7 min-w-7 rounded-[min(var(--radius-md),12px)] px-2.5 text-[0.8rem] has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3.5",
lg: "h-9 min-w-9 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
}
)
function Toggle({
className,
variant = "default",
size = "default",
...props
}: ToggleButtonProps & VariantProps<typeof toggleVariants>) {
return (
<TogglePrimitive
data-slot="toggle"
className={cn(toggleVariants({ variant, size, className }))}
{...props}
/>
)
}
export { Toggle, toggleVariants }
Update the import paths to match your project setup.
Usage#
import { Toggle } from "@/components/ui/toggle"<Toggle>Toggle</Toggle>Outline#
Use variant="outline" for an outline style.
import { BoldIcon, ItalicIcon } from "lucide-react"
import { Toggle } from "@/components/ui/toggle"import { BoldIcon, ItalicIcon } from "lucide-react"
import { Toggle } from "@/components/ui/toggle"
export function ToggleOutline() {
return (
<div className="flex flex-wrap items-center gap-2">
<Toggle variant="outline" aria-label="Toggle italic">
<ItalicIcon />
Italic
</Toggle>
<Toggle variant="outline" aria-label="Toggle bold">
<BoldIcon />
Bold
</Toggle>
</div>
)
}
With Text#
import { ItalicIcon } from "lucide-react"
import { Toggle } from "@/components/ui/toggle"import { ItalicIcon } from "lucide-react"
import { Toggle } from "@/components/ui/toggle"
export function ToggleText() {
return (
<Toggle aria-label="Toggle italic">
<ItalicIcon />
Italic
</Toggle>
)
}
Size#
Use the size prop to change the size of the toggle.
import { Toggle } from "@/components/ui/toggle"
export function ToggleSizes() {import { Toggle } from "@/components/ui/toggle"
export function ToggleSizes() {
return (
<div className="flex flex-wrap items-center gap-2">
<Toggle variant="outline" aria-label="Toggle small" size="sm">
Small
</Toggle>
<Toggle variant="outline" aria-label="Toggle default" size="default">
Default
</Toggle>
<Toggle variant="outline" aria-label="Toggle large" size="lg">
Large
</Toggle>
</div>
)
}
Disabled#
import { Toggle } from "@/components/ui/toggle"
export function ToggleDisabled() {import { Toggle } from "@/components/ui/toggle"
export function ToggleDisabled() {
return (
<div className="flex flex-wrap items-center gap-2">
<Toggle aria-label="Toggle disabled" isDisabled>
Disabled
</Toggle>
<Toggle variant="outline" aria-label="Toggle disabled outline" isDisabled>
Disabled
</Toggle>
</div>
)
}
RTL#
To enable RTL support in shadcn/ui, see the RTL configuration guide.
"use client"
import * as React from "react"// You will notice this example uses dir and data-lang attributes. // This is because this site is not RTL by default. // In your application, you won't need these.// See the RTL guide for more information.
"use client"
import * as React from "react"
import { BookmarkIcon } from "lucide-react"
import {
useTranslation,
type Translations,
} from "@/components/language-selector"
import { Toggle } from "@/components/ui/toggle"
const translations: Translations = {
en: {
dir: "ltr",
values: {
label: "Bookmark",
},
},
ar: {
dir: "rtl",
values: {
label: "إشارة مرجعية",
},
},
he: {
dir: "rtl",
values: {
label: "סימנייה",
},
},
}
export function ToggleRtl() {
const { dir, t } = useTranslation(translations, "ar")
return (
<Toggle aria-label="Toggle bookmark" size="sm" variant="outline" dir={dir}>
<BookmarkIcon className="group-aria-pressed/toggle:fill-foreground" />
{t.label}
</Toggle>
)
}
API Reference#
See the React Aria ToggleButton documentation.
Deploy your shadcn/ui app on Vercel
Trusted by OpenAI, Sonos, Adobe, and more.
Vercel provides tools and infrastructure to deploy apps and features at scale.
Deploy to Vercel