Toggle Group
Two-state button groups for editor toolbars, view switchers, and filter bars — with the neobrutalist borders-and-shadows treatment.
import { Bold, Italic, Underline } from "lucide-react"
import {The toggle group is a row of two-state buttons where one (the default) or several (multiple) can be pressed at a time. Built on the Base UI Toggle Group component and styled to the neobrutalist recipe: thick borders, hard shadows, and bold type.
Reach for it when:
- Editor toolbars — bold/italic/underline, text alignment, list styles with
multiple. - View switchers — grid vs. list, chart vs. table, day/week/month; the default keeps at most one active.
- Filter bars — tags, categories, or facets the user can stack on and off.
Installation#
pnpm dlx shadcn@latest add https://neobrutalism.com/r/base/toggle-group.jsonnpx shadcn@latest add https://neobrutalism.com/r/base/toggle-group.jsonyarn dlx shadcn@latest add https://neobrutalism.com/r/base/toggle-group.jsonbunx --bun shadcn@latest add https://neobrutalism.com/r/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 { type VariantProps } from "class-variance-authority"
import { ToggleGroup as ToggleGroupPrimitive } from "radix-ui"
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",
})
function ToggleGroup({
className,
variant,
size,
spacing = 2,
orientation = "horizontal",
children,
...props
}: React.ComponentProps<typeof ToggleGroupPrimitive.Root> &
VariantProps<typeof toggleVariants> & {
spacing?: number
orientation?: "horizontal" | "vertical"
}) {
return (
<ToggleGroupPrimitive.Root
data-slot="toggle-group"
data-variant={variant}
data-size={size}
data-spacing={spacing}
data-orientation={orientation}
style={{ "--gap": spacing } as React.CSSProperties}
className={cn(
"group/toggle-group flex w-fit flex-row items-center gap-[--spacing(var(--gap))] rounded-lg data-[size=sm]:rounded-[min(var(--radius-md),10px)] data-vertical:flex-col data-vertical:items-stretch",
className
)}
{...props}
>
<ToggleGroupContext.Provider
value={{ variant, size, spacing, orientation }}
>
{children}
</ToggleGroupContext.Provider>
</ToggleGroupPrimitive.Root>
)
}
function ToggleGroupItem({
className,
children,
variant = "default",
size = "default",
...props
}: React.ComponentProps<typeof ToggleGroupPrimitive.Item> &
VariantProps<typeof toggleVariants>) {
const context = React.useContext(ToggleGroupContext)
return (
<ToggleGroupPrimitive.Item
data-slot="toggle-group-item"
data-variant={context.variant || variant}
data-size={context.size || size}
data-spacing={context.spacing}
className={cn(
"shrink-0 group-data-[spacing=0]/toggle-group:rounded-none group-data-[spacing=0]/toggle-group:px-2 focus:z-10 focus-visible:z-10 group-data-[spacing=0]/toggle-group:has-data-[icon=inline-end]:pr-1.5 group-data-[spacing=0]/toggle-group:has-data-[icon=inline-start]:pl-1.5 group-data-horizontal/toggle-group:data-[spacing=0]:first:rounded-l-lg group-data-vertical/toggle-group:data-[spacing=0]:first:rounded-t-lg group-data-horizontal/toggle-group:data-[spacing=0]:last:rounded-r-lg group-data-vertical/toggle-group:data-[spacing=0]:last:rounded-b-lg 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}
</ToggleGroupPrimitive.Item>
)
}
export { ToggleGroup, ToggleGroupItem }
Update the import paths to match your project setup.
Usage#
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"<ToggleGroup>
<ToggleGroupItem value="a">A</ToggleGroupItem>
<ToggleGroupItem value="b">B</ToggleGroupItem>
<ToggleGroupItem value="c">C</ToggleGroupItem>
</ToggleGroup>Only one item can be pressed at a time by default. Add the multiple prop to allow several.
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 client"
import * as React from "react"RTL#
To enable RTL support in Neobrutalism.com, see the RTL configuration guide.
"use client"
import * as React from "react"Accessibility#
Each item is a toggle button per the WAI-ARIA Button pattern: the group renders role="group" and every item is a real <button> with aria-pressed, in both single and multiple mode. Focus uses a roving tabindex, so the whole group is a single Tab stop. Icon-only items need an aria-label.
Keyboard interactions:
| Key | Action |
|---|---|
Tab / Shift + Tab | Move focus into the group and out |
Space / Enter | Toggle the focused item |
ArrowRight / ArrowDown | Move focus to the next item |
ArrowLeft / ArrowUp | Move focus to the previous item |
Home / End | Jump to the first / last item |
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.