Get Started
Input
Feedback
Display
Installation
i18n
import { Bold, Italic, Underline } from "lucide-react"
import {Installation#
pnpm dlx shadcn@latest add https://ui.tyap.me/r/styles/base/toggle-group.jsonnpx shadcn@latest add https://ui.tyap.me/r/styles/base/toggle-group.jsonyarn dlx shadcn@latest add https://ui.tyap.me/r/styles/base/toggle-group.jsonbunx --bun shadcn@latest add https://ui.tyap.me/r/styles/base/toggle-group.json
Install the following dependencies:
pnpm add @base-ui/reactnpm install @base-ui/reactyarn add @base-ui/reactbun add @base-ui/react
Copy and paste the following code into your project.
"use client"
import * as React from "react"
import { Toggle as TogglePrimitive } from "@base-ui/react/toggle"
import { ToggleGroup as ToggleGroupPrimitive } from "@base-ui/react/toggle-group"
import { type VariantProps } from "class-variance-authority"
import { cn } from "@/lib/utils"
import { toggleVariants } from "@/components/ui/toggle"
const ToggleGroupContext = React.createContext<
VariantProps<typeof toggleVariants> & {
spacing?: number
orientation?: "horizontal" | "vertical"
}
>({
size: "default",
variant: "default",
spacing: 2,
orientation: "horizontal",
})
type ToggleGroupChangeDetails = Parameters<
NonNullable<ToggleGroupPrimitive.Props["onValueChange"]>
>[1]
type ToggleGroupProps = Omit<
ToggleGroupPrimitive.Props,
"defaultValue" | "onValueChange" | "value"
> &
VariantProps<typeof toggleVariants> & {
defaultValue?: string | readonly string[]
onValueChange?: (
value: string | readonly string[],
eventDetails: ToggleGroupChangeDetails
) => void
spacing?: number
orientation?: "horizontal" | "vertical"
type?: "single" | "multiple"
value?: string | readonly string[]
}
function ToggleGroup({
className,
variant,
size,
spacing = 2,
orientation = "horizontal",
defaultValue,
children,
onValueChange,
type = "multiple",
value,
...props
}: ToggleGroupProps) {
const isSingle = type === "single"
const primitiveDefaultValue =
typeof defaultValue === "string" ? [defaultValue] : defaultValue
const primitiveValue = typeof value === "string" ? [value] : value
return (
<ToggleGroupPrimitive
data-slot="toggle-group"
data-variant={variant}
data-size={size}
data-spacing={spacing}
data-orientation={orientation}
defaultValue={primitiveDefaultValue}
value={primitiveValue}
onValueChange={(nextValue, eventDetails) => {
onValueChange?.(
isSingle ? (nextValue[0] ?? "") : nextValue,
eventDetails
)
}}
style={{ "--gap": spacing } as React.CSSProperties}
className={cn(
"group/toggle-group flex w-fit flex-row items-center gap-[--spacing(var(--gap))] data-vertical:flex-col data-vertical:items-stretch",
className
)}
{...props}
>
<ToggleGroupContext.Provider
value={{ variant, size, spacing, orientation }}
>
{children}
</ToggleGroupContext.Provider>
</ToggleGroupPrimitive>
)
}
function ToggleGroupItem({
className,
children,
variant = "default",
size = "default",
...props
}: TogglePrimitive.Props & VariantProps<typeof toggleVariants>) {
const context = React.useContext(ToggleGroupContext)
return (
<TogglePrimitive
data-slot="toggle-group-item"
data-variant={context.variant || variant}
data-size={context.size || size}
data-spacing={context.spacing}
className={cn(
"shrink-0 focus:z-10 focus-visible:z-10 group-data-horizontal/toggle-group:data-[spacing=0]:data-[variant=outline]:border-l-0 group-data-vertical/toggle-group:data-[spacing=0]:data-[variant=outline]:border-t-0 group-data-horizontal/toggle-group:data-[spacing=0]:data-[variant=outline]:first:border-l group-data-vertical/toggle-group:data-[spacing=0]:data-[variant=outline]:first:border-t",
toggleVariants({
variant: context.variant || variant,
size: context.size || size,
}),
className
)}
{...props}
>
{children}
</TogglePrimitive>
)
}
export { ToggleGroup, ToggleGroupItem }
Update the import paths to match your project setup.
Usage#
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>Composition#
Use the following composition to build a ToggleGroup:
ToggleGroup
├── ToggleGroupItem
└── ToggleGroupItemExamples#
Outline#
Use variant="outline" for an outline style.
import {
ToggleGroup,
ToggleGroupItem,Size#
Use the size prop to change the size of the toggle group.
import {
ToggleGroup,
ToggleGroupItem,Spacing#
Use spacing to add spacing between toggle group items.
import {
ToggleGroup,
ToggleGroupItem,Vertical#
Use orientation="vertical" for vertical toggle groups.
import { BoldIcon, ItalicIcon, UnderlineIcon } from "lucide-react"
import {Disabled#
import { Bold, Italic, Underline } from "lucide-react"
import {Custom#
A custom toggle group example.
Use font-normal to set the font weight.
"use client"
import * as React from "react"RTL#
To enable RTL support in shadcn/ui, see the RTL configuration guide.
"use client"
import * as React from "react"API Reference#
See the Base UI Toggle Group documentation.
Changelog#
2026-05-17 Default Spacing#
Changed the default spacing from 0 to 2 so toggle groups render with space between items by default. Use spacing={0} for connected items.