Skip to content
2k
On This Page

Toggle Group

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

// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.

import { ToggleGroup, ToggleGroupItem } from "~/components/ui/toggle-group"

export default function ToggleGroupDemo() {
  return (
    <ToggleGroup variant="outline" multiple>
      <ToggleGroupItem value="bold" aria-label="Toggle bold">
        <svg
          width="24"
          height="24"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
        >
          <path d="M6 12h9a4 4 0 0 1 0 8H7a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h7a4 4 0 0 1 0 8" />
        </svg>
      </ToggleGroupItem>
      <ToggleGroupItem value="italic" aria-label="Toggle italic">
        <svg
          width="24"
          height="24"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
        >
          <line x1="19" x2="10" y1="4" y2="4" />
          <line x1="14" x2="5" y1="20" y2="20" />
          <line x1="15" x2="9" y1="4" y2="20" />
        </svg>
      </ToggleGroupItem>
      <ToggleGroupItem value="strikethrough" aria-label="Toggle strikethrough">
        <svg
          width="24"
          height="24"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
        >
          <path d="M6 4v6a6 6 0 0 0 12 0V4" />
          <line x1="4" x2="20" y1="20" y2="20" />
        </svg>
      </ToggleGroupItem>
    </ToggleGroup>
  )
}

Installation

pnpm dlx solidui-cli@latest add toggle-group

Install the required Solid UI dependencies:

pnpm dlx solidui-cli@latest add toggle

Copy and paste the following code into your project.

components/ui/toggle-group.tsx
import { type ComponentProps, type JSX } from "@solidjs/web"
import {
  Toggle as SdkToggle,
  ToggleGroup as SdkToggleGroup
} from "@ui-sdk/solid/toggle"
import type { VariantProps } from "class-variance-authority"
import { createContext, merge, omit, useContext } from "solid-js"

import { cn } from "~/lib/utils"
import { toggleVariants } from "~/components/ui/toggle"
type ToggleGroupContextValue = VariantProps<typeof toggleVariants> & {
  spacing?: number | undefined
  orientation?: "horizontal" | "vertical" | undefined
}
const ToggleGroupContext = createContext<ToggleGroupContextValue>({
  size: "default",
  variant: "default",
  spacing: 0,
  orientation: "horizontal"
})
type ToggleGroupProps = (Omit<
  ComponentProps<typeof SdkToggleGroup>,
  "class" | "children"
> & {
  children?: JSX.Element
  class?: string
}) &
  VariantProps<typeof toggleVariants> & {
    spacing?: number
    orientation?: "horizontal" | "vertical"
  }
const ToggleGroup = (rawProps: ToggleGroupProps) => {
  const local = merge(
    {
      spacing: 0,
      orientation: "horizontal"
    } as const,
    rawProps
  ) as ToggleGroupProps
  const others = omit(
    rawProps,
    "class",
    "children",
    "variant",
    "size",
    "spacing",
    "orientation"
  )
  return (
    <SdkToggleGroup
      class={cn(
        "group/toggle-group rounded-lg data-[size=sm]:rounded-[min(var(--radius-md),10px)] flex w-fit flex-row items-center gap-[--spacing(var(--gap))] data-[orientation=vertical]:flex-col data-[orientation=vertical]:items-stretch",
        local.class
      )}
      data-orientation={local.orientation}
      data-size={local.size}

      data-spacing={local.spacing}
      data-variant={local.variant}
      orientation={local.orientation ?? "horizontal"}
      style={{ "--gap": local.spacing } as JSX.CSSProperties}
      {...others}
    >
      <ToggleGroupContext value={local}>{local.children}</ToggleGroupContext>
    </SdkToggleGroup>
  )
}
type ToggleGroupItemComponentProps = (Omit<
  ComponentProps<typeof SdkToggle> & {
    value: string
  },
  "class"
> & {
  class?: string
}) &
  VariantProps<typeof toggleVariants>
const ToggleGroupItem = (rawProps: ToggleGroupItemComponentProps) => {
  const local = merge(
    { variant: "default" as const, size: "default" as const },
    rawProps
  ) as ToggleGroupItemComponentProps
  const others = omit(rawProps, "class", "children", "variant", "size")
  const context = useContext(ToggleGroupContext)
  return (
    <SdkToggle
      class={cn(
        "group-data-[spacing=0]/toggle-group:rounded-none 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]: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 shrink-0 focus:z-10 focus-visible:z-10 group-data-[orientation=horizontal]/toggle-group:data-[spacing=0]:data-[variant=outline]:border-l-0 group-data-[orientation=vertical]/toggle-group:data-[spacing=0]:data-[variant=outline]:border-t-0 group-data-[orientation=horizontal]/toggle-group:data-[spacing=0]:data-[variant=outline]:first:border-l group-data-[orientation=vertical]/toggle-group:data-[spacing=0]:data-[variant=outline]:first:border-t",
        toggleVariants({
          variant: context.variant || local.variant,
          size: context.size || local.size
        }),
        local.class
      )}
      data-size={context.size || local.size}

      data-spacing={context.spacing}
      data-variant={context.variant || local.variant}
      {...others}
    >
      {local.children}
    </SdkToggle>
  )
}
export { ToggleGroup, ToggleGroupItem, type ToggleGroupProps }

Update the import paths to match your project setup.

Usage

import { ToggleGroup, ToggleGroupItem } from "~/components/ui/toggle-group"
<ToggleGroup defaultValue={["a"]}>
  <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
โ””โ”€โ”€ ToggleGroupItem

Outline

Use variant="outline" for an outline style.

import { ToggleGroup, ToggleGroupItem } from "~/components/ui/toggle-group"

export default function ToggleGroupOutline() {
  return (
    <ToggleGroup defaultValue={["all"]} variant="outline">
      <ToggleGroupItem aria-label="Toggle all" value="all">
        All
      </ToggleGroupItem>
      <ToggleGroupItem aria-label="Toggle missed" value="missed">
        Missed
      </ToggleGroupItem>
    </ToggleGroup>
  )
}

Size

Use the size prop to change the size of the toggle group.

import { ToggleGroup, ToggleGroupItem } from "~/components/ui/toggle-group"

export default function ToggleGroupSizes() {
  return (
    <div class="flex flex-col gap-4">
      <ToggleGroup defaultValue={["top"]} size="sm" variant="outline">
        <ToggleGroupItem aria-label="Toggle top" value="top">
          Top
        </ToggleGroupItem>
        <ToggleGroupItem aria-label="Toggle bottom" value="bottom">
          Bottom
        </ToggleGroupItem>
        <ToggleGroupItem aria-label="Toggle left" value="left">
          Left
        </ToggleGroupItem>
        <ToggleGroupItem aria-label="Toggle right" value="right">
          Right
        </ToggleGroupItem>
      </ToggleGroup>
      <ToggleGroup defaultValue={["top"]} variant="outline">
        <ToggleGroupItem aria-label="Toggle top" value="top">
          Top
        </ToggleGroupItem>
        <ToggleGroupItem aria-label="Toggle bottom" value="bottom">
          Bottom
        </ToggleGroupItem>
        <ToggleGroupItem aria-label="Toggle left" value="left">
          Left
        </ToggleGroupItem>
        <ToggleGroupItem aria-label="Toggle right" value="right">
          Right
        </ToggleGroupItem>
      </ToggleGroup>
    </div>
  )
}

Spacing

Use spacing to add spacing between toggle group items.

import { ToggleGroup, ToggleGroupItem } from "~/components/ui/toggle-group"

export default function ToggleGroupSpacing() {
  return (
    <ToggleGroup defaultValue={["top"]} size="sm" spacing={2} variant="outline">
      <ToggleGroupItem aria-label="Toggle top" value="top">
        Top
      </ToggleGroupItem>
      <ToggleGroupItem aria-label="Toggle bottom" value="bottom">
        Bottom
      </ToggleGroupItem>
      <ToggleGroupItem aria-label="Toggle left" value="left">
        Left
      </ToggleGroupItem>
      <ToggleGroupItem aria-label="Toggle right" value="right">
        Right
      </ToggleGroupItem>
    </ToggleGroup>
  )
}

Vertical

Use orientation="vertical" for vertical toggle groups.

// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.

import { ToggleGroup, ToggleGroupItem } from "~/components/ui/toggle-group"

export default function ToggleGroupVertical() {
  return (
    <ToggleGroup
      multiple
      orientation="vertical"
      spacing={1}
      defaultValue={["bold", "italic"]}
    >
      <ToggleGroupItem value="bold" aria-label="Toggle bold">
        <svg
          width="24"
          height="24"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
        >
          <path d="M6 12h9a4 4 0 0 1 0 8H7a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h7a4 4 0 0 1 0 8" />
        </svg>
      </ToggleGroupItem>
      <ToggleGroupItem value="italic" aria-label="Toggle italic">
        <svg
          width="24"
          height="24"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
        >
          <line x1="19" x2="10" y1="4" y2="4" />
          <line x1="14" x2="5" y1="20" y2="20" />
          <line x1="15" x2="9" y1="4" y2="20" />
        </svg>
      </ToggleGroupItem>
      <ToggleGroupItem value="underline" aria-label="Toggle underline">
        <svg
          width="24"
          height="24"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
        >
          <path d="M6 4v6a6 6 0 0 0 12 0V4" />
          <line x1="4" x2="20" y1="20" y2="20" />
        </svg>
      </ToggleGroupItem>
    </ToggleGroup>
  )
}

Disabled

// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.

import { ToggleGroup, ToggleGroupItem } from "~/components/ui/toggle-group"

export default function ToggleGroupDisabled() {
  return (
    <ToggleGroup disabled multiple>
      <ToggleGroupItem value="bold" aria-label="Toggle bold">
        <svg
          width="24"
          height="24"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
        >
          <path d="M6 12h9a4 4 0 0 1 0 8H7a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h7a4 4 0 0 1 0 8" />
        </svg>
      </ToggleGroupItem>
      <ToggleGroupItem value="italic" aria-label="Toggle italic">
        <svg
          width="24"
          height="24"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
        >
          <line x1="19" x2="10" y1="4" y2="4" />
          <line x1="14" x2="5" y1="20" y2="20" />
          <line x1="15" x2="9" y1="4" y2="20" />
        </svg>
      </ToggleGroupItem>
      <ToggleGroupItem value="strikethrough" aria-label="Toggle strikethrough">
        <svg
          width="24"
          height="24"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
        >
          <path d="M6 4v6a6 6 0 0 0 12 0V4" />
          <line x1="4" x2="20" y1="20" y2="20" />
        </svg>
      </ToggleGroupItem>
    </ToggleGroup>
  )
}

Custom

A custom toggle group example.

Use font-normal to set the font weight.

import { createSignal } from "solid-js"

import { FieldDescription, FieldGroup, FieldLabel } from "~/components/ui/field"
import { ToggleGroup, ToggleGroupItem } from "~/components/ui/toggle-group"

export default function ToggleGroupFontWeightSelector() {
  const [fontWeight, setFontWeight] = createSignal("normal")

  return (
    <FieldGroup class="w-full max-w-xs">
      <FieldLabel>Font Weight</FieldLabel>
      <ToggleGroup
        onValueChange={(value) => setFontWeight(value[0] ?? "normal")}
        size="lg"
        spacing={2}
        value={[fontWeight()]}
        variant="outline"
      >
        <ToggleGroupItem
          aria-label="Light"
          class="flex size-16 flex-col items-center justify-center rounded-xl"
          value="light"
        >
          <span class="text-2xl leading-none font-light">Aa</span>
          <span class="text-xs text-muted-foreground">Light</span>
        </ToggleGroupItem>
        <ToggleGroupItem
          aria-label="Normal"
          class="flex size-16 flex-col items-center justify-center rounded-xl"
          value="normal"
        >
          <span class="text-2xl leading-none font-normal">Aa</span>
          <span class="text-xs text-muted-foreground">Normal</span>
        </ToggleGroupItem>
        <ToggleGroupItem
          aria-label="Medium"
          class="flex size-16 flex-col items-center justify-center rounded-xl"
          value="medium"
        >
          <span class="text-2xl leading-none font-medium">Aa</span>
          <span class="text-xs text-muted-foreground">Medium</span>
        </ToggleGroupItem>
        <ToggleGroupItem
          aria-label="Bold"
          class="flex size-16 flex-col items-center justify-center rounded-xl"
          value="bold"
        >
          <span class="text-2xl leading-none font-bold">Aa</span>
          <span class="text-xs text-muted-foreground">Bold</span>
        </ToggleGroupItem>
      </ToggleGroup>
      <FieldDescription>
        Use{" "}
        <code class="rounded-md bg-muted px-1 py-0.5 font-mono">
          font-{fontWeight()}
        </code>{" "}
        to set the font weight.
      </FieldDescription>
    </FieldGroup>
  )
}

API Reference

PartDescription
ToggleGroupRoot container that manages single/multiple select.
ToggleGroupItemToggle item that inherits group variant and size.

See the UI SDK documentation for more information.

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.