All components
Toggle Group
checkboxesOrigin UI component.
responsive · 600px
Install
Same command in any shadcn project — React (Vite/CRA), Next.js, Remix, Astro, and more:
$
npx shadcn@latest add https://your-domain/r/toggle-group.jsonUsage
"use client";
import { ToggleGroup, ToggleGroupItem } from "@/registry/origin-ui/toggle-group";
import { BoldIcon, ItalicIcon, UnderlineIcon } from "lucide-react";
export default function Demo() {
return (
<div className="flex flex-col items-center justify-center gap-6 min-h-[200px] p-8">
<ToggleGroup type="multiple" variant="outline" aria-label="Text formatting">
<ToggleGroupItem value="bold" aria-label="Bold">
<BoldIcon className="size-4" />
</ToggleGroupItem>
<ToggleGroupItem value="italic" aria-label="Italic">
<ItalicIcon className="size-4" />
</ToggleGroupItem>
<ToggleGroupItem value="underline" aria-label="Underline">
<UnderlineIcon className="size-4" />
</ToggleGroupItem>
</ToggleGroup>
<ToggleGroup type="single" defaultValue="center" aria-label="Text alignment">
<ToggleGroupItem value="left" aria-label="Left align">
Left
</ToggleGroupItem>
<ToggleGroupItem value="center" aria-label="Center align">
Center
</ToggleGroupItem>
<ToggleGroupItem value="right" aria-label="Right align">
Right
</ToggleGroupItem>
</ToggleGroup>
</div>
);
}Component source
"use client";
import type { VariantProps } from "class-variance-authority";
import { ToggleGroup as ToggleGroupPrimitive } from "radix-ui";
import * as React from "react";
import { cn } from "@/lib/utils";
import { toggleVariants } from "@/components/ui/toggle";
const ToggleGroupContext = React.createContext<
VariantProps<typeof toggleVariants>
>({
size: "default",
variant: "default",
});
function ToggleGroup({
className,
variant,
size,
children,
...props
}: React.ComponentProps<typeof ToggleGroupPrimitive.Root> &
VariantProps<typeof toggleVariants>) {
return (
<ToggleGroupPrimitive.Root
className={cn(
"group/toggle-group flex items-center rounded-md data-[variant=outline]:shadow-xs",
className,
)}
data-size={size}
data-slot="toggle-group"
data-variant={variant}
{...props}
>
<ToggleGroupContext.Provider value={{ size, variant }}>
{children}
</ToggleGroupContext.Provider>
</ToggleGroupPrimitive.Root>
);
}
function ToggleGroupItem({
className,
children,
variant,
size,
...props
}: React.ComponentProps<typeof ToggleGroupPrimitive.Item> &
VariantProps<typeof toggleVariants>) {
const context = React.useContext(ToggleGroupContext);
return (
<ToggleGroupPrimitive.Item
className={cn(
toggleVariants({
size: context.size || size,
variant: context.variant || variant,
}),
"min-w-0 shrink-0 rounded-none shadow-none first:rounded-l-md last:rounded-r-md focus:z-10 focus-visible:z-10 data-[variant=outline]:border-l-0 data-[variant=outline]:first:border-l",
className,
)}
data-size={context.size || size}
data-slot="toggle-group-item"
data-variant={context.variant || variant}
{...props}
>
{children}
</ToggleGroupPrimitive.Item>
);
}
export { ToggleGroup, ToggleGroupItem };Dependencies
radix-ui
Source: Origin UI