Toggle Group

A set of two-state buttons that can be toggled on or off.

tsx
import { Bold, Italic, Underline } from "lucide-react"
import {
ToggleGroup,
ToggleGroupItem,
} from "@/components/ui/toggle-group"
export function ToggleGroupDemo() {
return (
<ToggleGroup type="multiple">
<ToggleGroupItem value="bold" aria-label="Toggle bold">
<Bold className="h-4 w-4" />
</ToggleGroupItem>
<ToggleGroupItem value="italic" aria-label="Toggle italic">
<Italic className="h-4 w-4" />
</ToggleGroupItem>
<ToggleGroupItem value="underline" aria-label="Toggle underline">
<Underline className="h-4 w-4" />
</ToggleGroupItem>
</ToggleGroup>
)
}

Installation

CLI

bash
npx fivui add toggle-group

Manual

Copy and paste the following code into your project.

bash
npm install @radix-ui/react-toggle-group
components/ui/toggle-group.tsx
"use client"
import * as React from "react"
import * as ToggleGroupPrimitive from "@radix-ui/react-toggle-group"
import { VariantProps } from "class-variance-authority"
import { cn } from "@/lib/utils"
import { toggleVariants } from "@/components/ui/toggle"
const ToggleGroupContext = React.createContext<
VariantProps<typeof toggleVariants>
>({})
const ToggleGroup = React.forwardRef<
React.ElementRef<typeof ToggleGroupPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof ToggleGroupPrimitive.Root> &
VariantProps<typeof toggleVariants>
>(({ className, variant, size, children, ...props }, ref) => (
<ToggleGroupPrimitive.Root
ref={ref}
className={cn("flex items-center justify-center gap-1", className)}
{...props}
>
<ToggleGroupContext.Provider value={{ variant, size }}>
{children}
</ToggleGroupContext.Provider>
</ToggleGroupPrimitive.Root>
))
ToggleGroup.displayName = ToggleGroupPrimitive.Root.displayName
const ToggleGroupItem = React.forwardRef<
React.ElementRef<typeof ToggleGroupPrimitive.Item>,
React.ComponentPropsWithoutRef<typeof ToggleGroupPrimitive.Item> &
VariantProps<typeof toggleVariants>
>(({ className, children, variant, size, ...props }, ref) => {
const context = React.useContext(ToggleGroupContext)
return (
<ToggleGroupPrimitive.Item
ref={ref}
className={cn(
toggleVariants({
variant: context.variant || variant,
size: context.size || size,
}),
className
)}
{...props}
>
{children}
</ToggleGroupPrimitive.Item>
)
})
ToggleGroupItem.displayName = ToggleGroupPrimitive.Item.displayName
export { ToggleGroup, ToggleGroupItem }

Usage

tsx
import {
ToggleGroup,
ToggleGroupItem,
} from "@/components/ui/toggle-group"
<ToggleGroup type="single">
<ToggleGroupItem value="a">A</ToggleGroupItem>
<ToggleGroupItem value="b">B</ToggleGroupItem>
<ToggleGroupItem value="c">C</ToggleGroupItem>
</ToggleGroup>

Examples

Single

tsx
<ToggleGroup type="single">
<ToggleGroupItem value="left">Left</ToggleGroupItem>
<ToggleGroupItem value="center">Center</ToggleGroupItem>
<ToggleGroupItem value="right">Right</ToggleGroupItem>
</ToggleGroup>

API Reference

ToggleGroup

PropTypeDefaultDescription
type"single" | "multiple"-Determines whether one or multiple items can be pressed at a time
valuestring | string[]-The controlled value of the pressed item when type is "single" or the controlled value of the pressed items when type is "multiple"
defaultValuestring | string[]-The value of the item to press when initially rendered and type is "single" or the value of the items to press when initially rendered and type is "multiple"
onValueChangefunction-Event handler called when the pressed state of an item changes
disabledbooleanfalseWhen true, prevents the user from interacting with the toggle group and all its items

ToggleGroupItem

PropTypeDefaultDescription
valuestring-A unique value for the item
disabledbooleanfalseWhen true, prevents the user from interacting with the item

For more detailed information about all available props and behaviors, refer to the Radix UI documentation.

View Radix Docs