# Toggle Group

A row of pixel toggles for one or many choices.

Source: https://www.ui95.dev/ui/toggle-group

## Examples

```tsx
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"

import type { ShowcaseDemo } from "../types"

function renderGroup(
  variant: "default" | "outline",
  size: "default" | "sm" | "lg",
  spacing: number,
  orientation: "horizontal" | "vertical" = "horizontal",
  isDisabled = false
) {
  return (
    <ToggleGroup
      type="single"
      defaultValue="b"
      variant={variant}
      size={size}
      spacing={spacing}
      orientation={orientation}
      disabled={isDisabled}
    >
      <ToggleGroupItem value="a">A</ToggleGroupItem>
      <ToggleGroupItem value="b">B</ToggleGroupItem>
      <ToggleGroupItem value="c">C</ToggleGroupItem>
    </ToggleGroup>
  )
}

export const demo: ShowcaseDemo = {
  span: "single",
  variants: [
    { label: "default", render: () => renderGroup("default", "default", 2) },
    { label: "outline joined", render: () => renderGroup("outline", "default", 0) },
    { label: "sm", render: () => renderGroup("outline", "sm", 0) },
    { label: "lg", render: () => renderGroup("outline", "lg", 0) },
    { label: "vertical", render: () => renderGroup("outline", "default", 0, "vertical") },
    { label: "disabled", render: () => renderGroup("outline", "default", 0, "horizontal", true) },
  ],
}
```

## Installation

```bash
npx shadcn@latest add https://www.ui95.dev/r/toggle-group.json
```

Run it in a Radix shadcn project that already has the UI95 theme.

Dependencies:

```bash
npm install class-variance-authority cn radix-ui
```

Add these items first:

- [toggle](https://www.ui95.dev/ui/toggle)

`components/ui/toggle-group.tsx`

```tsx
"use client"

import { type VariantProps } from "class-variance-authority"
import { cn } from "cn"
import { ToggleGroup as ToggleGroupPrimitive } from "radix-ui"
import * as React from "react"

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))] 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 focus:z-10 focus-visible:z-10 group-data-[spacing=0]/toggle-group:px-2 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]:not-first:-ml-(--pixel) group-data-vertical/toggle-group:data-[spacing=0]:not-first:-mt-(--pixel) data-[state=on]:z-10",
        toggleVariants({
          variant: context.variant || variant,
          size: context.size || size,
        }),
        className
      )}
      {...props}
    >
      {children}
    </ToggleGroupPrimitive.Item>
  )
}

export { ToggleGroup, ToggleGroupItem }
```

## API reference

### ToggleGroup

| Prop | Type | Default |
| --- | --- | --- |
| `orientation` | `"vertical" \| "horizontal"` | `"horizontal"` |
| `variant` | `"default" \| "outline"` | - |
| `size` | `"sm" \| "lg" \| "default"` | - |
| `spacing` | `number` | `2` |

### ToggleGroupItem

| Prop | Type | Default |
| --- | --- | --- |
| `variant` | `"default" \| "outline"` | `"default"` |
| `size` | `"sm" \| "lg" \| "default"` | `"default"` |

## Sounds

- `toggle.on`
- `toggle.off`
- `toggle.select`
