← Components

Actions · Material 3: Button groups

Button group

Related actions joined in one container. One hover pill slides across them.

Examples and variants

Basic

Hover across the actions.

Example
function ButtonGroupBasic() {
  return (
    <ButtonGroup aria-label="Edit">
      <Button variant="ghost">Cut</Button>
      <Button variant="ghost">Copy</Button>
      <Button variant="ghost">Paste</Button>
    </ButtonGroup>
  )
}

Spec drawing

Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.

Install

With the CLI

The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/button-group.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install cn
  2. Copy this file to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/button-group.tsxbutton-group
      components/ui/button-group.tsx
      "use client"
      
      import * as React from "react"
      import { cn } from "cn"
      import { useLiquidIndicator } from "@/hooks/use-liquid-indicator"
      import { useMotionLevel } from "@/lib/motion/use-motion-level"
      
      function ButtonGroup({
        className,
        children,
        ...props
      }: React.ComponentProps<"div">) {
        const rootRef = React.useRef<HTMLDivElement>(null)
        const { level, ref: motionRef } = useMotionLevel<HTMLDivElement>()
        // Stable merged ref: an inline one re-attaches every render and loops through useMotionLevel's state.
        const setRefs = React.useCallback(
          (node: HTMLDivElement | null) => {
            rootRef.current = node
            motionRef(node)
          },
          [motionRef]
        )
        const [index, setIndex] = React.useState(0)
        const [hovering, setHovering] = React.useState(false)
        const indicator = useLiquidIndicator(rootRef, index, { stretch: level === "liquid" })
      
        return (
          <div
            ref={setRefs}
            role="group"
            data-slot="button-group"
            className={cn(
              "relative inline-flex items-center rounded-(--segmented-corner) bg-(--segmented-container) p-(--segmented-padding)",
              className
            )}
            onMouseLeave={() => setHovering(false)}
            {...props}
          >
            {level === "still" ? null : (
              <span
                aria-hidden
                className={cn(
                  "group-pill pointer-events-none absolute top-inline-xs bottom-inline-xs rounded-(--segmented-corner) bg-(--segmented-indicator) shadow-(--segmented-indicator-elevation) transition-transform duration-(--duration-micro) ease-(--ease-out)",
                  hovering ? "opacity-100" : "opacity-0"
                )}
                style={indicator.style}
              />
            )}
            {React.Children.map(children, (child, childIndex) => (
              <span
                data-liquid-item
                className="relative z-[1]"
                onMouseEnter={() => {
                  setIndex(childIndex)
                  setHovering(true)
                }}
              >
                {child}
              </span>
            ))}
          </div>
        )
      }
      
      export { ButtonGroup }
      

Imports

What the examples on this page import:

Imports
import { Button } from "@/components/ui/button"
import { ButtonGroup } from "@/components/ui/button-group"

Anatomy

  1. 1Container · Same tokens as the segmented control.
  2. 2Hover pill · Follows the pointer; squashes with the pressed button.
  3. 3Buttons · Ghost Buttons.

Props

PropTypeDefaultDescription
childrenButton[]—Use variant="ghost" Buttons.

Tokens

Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.

TokenPoints toLightDark
--segmented-heightvar(--size-control-md)40pxsame
--segmented-containervar(--surface-container)oklch(0.965 0.005 95)oklch(0.12 0 0)
--segmented-indicatorvar(--surface-container-lowest)oklch(1 0 0)oklch(0.14 0 0)
--segmented-indicator-elevationvar(--elevation-1)0 1px 1px -0.5px oklch(0.22 0.01 260 / 7%), 0 2px 3px -1px oklch(0.22 0.01 260 / 10%), 0 4px 8px -4px oklch(0.22 0.01 260 / 6%)0 1px 2px oklch(0 0 0 / 35%), 0 2px 10px oklch(0 0 0 / 25%)
--segmented-cornervar(--shape-full)999pxsame
--segmented-paddingvar(--space-gap-inline-xs)4pxsame

Motion

TriggerWhat happensTokens
HoverThe pill slides and stretches to the hovered button.--duration-fast, --ease-liquid

Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.

Accessibility

  • Give the group an aria-label.
  • Each Button keeps its own focus and label.

Usage

Do

  • Group actions on the same object.

Don’t

  • Don't mix primary and ghost buttons in one group.

Code

components/ui/button-group.tsx
"use client"

import * as React from "react"
import { cn } from "cn"
import { useLiquidIndicator } from "@/hooks/use-liquid-indicator"
import { useMotionLevel } from "@/lib/motion/use-motion-level"

function ButtonGroup({
  className,
  children,
  ...props
}: React.ComponentProps<"div">) {
  const rootRef = React.useRef<HTMLDivElement>(null)
  const { level, ref: motionRef } = useMotionLevel<HTMLDivElement>()
  // Stable merged ref: an inline one re-attaches every render and loops through useMotionLevel's state.
  const setRefs = React.useCallback(
    (node: HTMLDivElement | null) => {
      rootRef.current = node
      motionRef(node)
    },
    [motionRef]
  )
  const [index, setIndex] = React.useState(0)
  const [hovering, setHovering] = React.useState(false)
  const indicator = useLiquidIndicator(rootRef, index, { stretch: level === "liquid" })

  return (
    <div
      ref={setRefs}
      role="group"
      data-slot="button-group"
      className={cn(
        "relative inline-flex items-center rounded-(--segmented-corner) bg-(--segmented-container) p-(--segmented-padding)",
        className
      )}
      onMouseLeave={() => setHovering(false)}
      {...props}
    >
      {level === "still" ? null : (
        <span
          aria-hidden
          className={cn(
            "group-pill pointer-events-none absolute top-inline-xs bottom-inline-xs rounded-(--segmented-corner) bg-(--segmented-indicator) shadow-(--segmented-indicator-elevation) transition-transform duration-(--duration-micro) ease-(--ease-out)",
            hovering ? "opacity-100" : "opacity-0"
          )}
          style={indicator.style}
        />
      )}
      {React.Children.map(children, (child, childIndex) => (
        <span
          data-liquid-item
          className="relative z-[1]"
          onMouseEnter={() => {
            setIndex(childIndex)
            setHovering(true)
          }}
        >
          {child}
        </span>
      ))}
    </div>
  )
}

export { ButtonGroup }