Toggle Group

Segmented toggle controls for toolbars, filters, and formatting actions.

Toggle Group

Segmented toggle controls for toolbars, filters, and formatting actions.

Press the outline Bold, Italic, and Underline chips — each one springs a muted fill in behind its icon, flashes a one-shot sheen, and snaps the glyph as the launch headline copy updates live. The group tracks multiple active formats at once and hands the full selection to onValueChange.

Installation

Install the component with the Craft-UI Blocks CLI — it drops r-togglegroup.tsx into your project and wires up its runtime dependencies:

npx mvpblocks add r-togglegroup
pnpm dlx mvpblocks add r-togglegroup
yarn dlx mvpblocks add r-togglegroup
bunx mvpblocks add r-togglegroup

File Structure

The CLI saves the component to your project's components/ui folder:

Import it from @/components/ui/r-togglegroup.

Usage

"use client";

import { Bold, Italic, Underline } from "lucide-react";
import { useState } from "react";
import {
  ToggleGroup,
  ToggleGroupItem,
} from "@/components/ui/r-togglegroup";

const options = [
  { value: "bold", label: "Bold", icon: Bold },
  { value: "italic", label: "Italic", icon: Italic },
  { value: "underline", label: "Underline", icon: Underline },
] as const;

function getHeadlineClassName(formats: string[]) {
  return [
    "transition-[color,font-style,font-weight,text-decoration-color] duration-200",
    formats.includes("bold") ? "font-bold" : "",
    formats.includes("italic") ? "italic" : "",
    formats.includes("underline") ? "underline underline-offset-4" : "",
    formats.length > 0
      ? "text-neutral-950 dark:text-neutral-50"
      : "text-neutral-800 dark:text-neutral-100",
  ]
    .filter(Boolean)
    .join(" ");
}

export function ToggleGroupPreview() {
  const [formats, setFormats] = useState<string[]>(["bold"]);

  return (
    <div className="flex min-h-[18rem] w-full items-center justify-center px-4 py-6">
      <div className="flex flex-col items-start gap-3">
        <ToggleGroup
          aria-label="Text formatting"
          onValueChange={setFormats}
          value={formats}
          variant="outline"
          type="multiple"
        >
          {options.map(({ value, label, icon: Icon }) => (
            <ToggleGroupItem
              aria-label={label}
              className="size-8 min-h-8 min-w-8 shrink-0 px-0"
              key={value}
              value={value}
            >
              <Icon className="size-4" />
            </ToggleGroupItem>
          ))}
        </ToggleGroup>
        <p className="text-balance text-left font-medium text-lg text-neutral-800 leading-snug tracking-tight sm:text-xl dark:text-neutral-100">
          <span>Edit the </span>
          <span className={getHeadlineClassName(formats)}>launch headline</span>
        </p>
      </div>
    </div>
  );
}

Use type="multiple" for formatting toolbars that allow several active chips, or type="single" for an exclusive choice. The variant, size, spacing, and orientation set on the group cascade to every item through context, and any item can override variant or size locally.

Theming

The group inherits the host shadcn theme tokens rather than shipping a bespoke palette, so it matches whatever theme your app already runs. Each pressed item layers a bg-muted fill behind its content, the resting label sits on text-muted-foreground and lifts to text-foreground once pressed (data-[state=on]), and the outline variant adds a hairline border-input that stays put in both states. Focus draws a ring-ring/50 ring with a border-ring, invalid states use border-destructive with a ring-destructive/20 (or /40 in dark mode) ring, and disabled items drop to opacity-50 while keeping their pressed appearance.

Because the fill and text read from the shared --muted, --muted-foreground, --foreground, --input, and --ring variables, both light and dark mode resolve automatically with no extra token block. The group shell lays items out with a --gap spacing variable and, at spacing={0}, collapses into one connected rounded shell with a single border-input outer border and internal border-l / border-t dividers. Corners upgrade to a true squircle via corner-shape where the browser supports it, falling back to plain radii elsewhere.

Animations

  • Liquid fill: each item's pressed fill is driven by a fillProgress motion value that springs between 0 and 1 on a heavy 210 / 28 / 1.08 spring, and its opacity is mapped straight from that progress — so the bg-muted surface blooms in and retracts with weight instead of a hard swap.
  • Sheen sweep: every state change fires a one-shot diagonal light sweep on that item. A skewed gradient bar (via-foreground/18, via-foreground/26 in dark mode) shoots from left: -40% to 140% over 0.58s ease-[0.22, 1, 0.36, 1] while its opacity fades out on [0.4, 0, 0.2, 1].
  • Icon snap: on each toggle the icon scale springs 0.93 ↔ 1 on a 340 / 32 / 0.92 spring, then squashes wide-and-short (scaleX 1.1, scaleY 0.91) on a 520 / 34 / 0.72 spring and settles back on a 0.45s bounce: 0.32 spring.
  • Press scale: pointer-down compresses the icon to scaleX 0.86, scaleY 1.08; releasing or leaving the surface springs it back to rest.
  • Group layout: item spacing comes from the --gap custom property (default 1 → 4px), and at spacing={0} the shell switches to connected mode with overflow-hidden, an outer border-input, internal dividers, and flattened item corners. Items also raise their z-index on focus so the shared ring is never clipped.

Props

ToggleGroup

Prop

Type

ToggleGroupItem

Prop

Type

toggleGroupItemVariants

Prop

Type

Registry Bundle

Install the exact registry entry shown on the right when you want the component file and its declared runtime dependencies together. Dependencies: radix-ui, class-variance-authority, motion.

Installs a Radix toggle group with inline spring fill, sheen sweep, and press-scale motion in the same file. Default spacing leaves a 4px gap between each bordered item; pass spacing={0} for a connected outline shell with one outer border and internal dividers.

Contact

If you find any bug or issue, feel free to raise an issue.

Last updated on