Toggle group

A joined set of toggle buttons in the segmented control’s geometry, single- or multi-select — Keynote’s bold, italic and underline.

controls

Apple: Segmented controls (select any)

· Base UI: ToggleGroup

Alignment and font style

"use client"

import {
  TextAlignCenterIcon,
  TextAlignLeftIcon,
  TextAlignRightIcon,
  TextBoldIcon,
  TextItalicIcon,
  TextUnderlineIcon,
} from "@hugeicons/core-free-icons"

import { Icon } from "@applecn/ui/components/icon"
import {
  ToggleGroup,
  ToggleGroupItem,
} from "@applecn/ui/components/toggle-group"

export default function ToggleGroupBasic() {
  return (
    <div className="flex flex-wrap items-center gap-4">
      <ToggleGroup aria-label="Alignment" defaultValue={["left"]}>
        <ToggleGroupItem value="left" aria-label="Align left">
          <Icon icon={TextAlignLeftIcon} weight="semibold" />
        </ToggleGroupItem>
        <ToggleGroupItem value="center" aria-label="Align centre">
          <Icon icon={TextAlignCenterIcon} weight="semibold" />
        </ToggleGroupItem>
        <ToggleGroupItem value="right" aria-label="Align right">
          <Icon icon={TextAlignRightIcon} weight="semibold" />
        </ToggleGroupItem>
      </ToggleGroup>
      <ToggleGroup aria-label="Font style" multiple defaultValue={["bold"]}>
        <ToggleGroupItem value="bold" aria-label="Bold">
          <Icon icon={TextBoldIcon} weight="bold" />
        </ToggleGroupItem>
        <ToggleGroupItem value="italic" aria-label="Italic">
          <Icon icon={TextItalicIcon} weight="bold" />
        </ToggleGroupItem>
        <ToggleGroupItem value="underline" aria-label="Underline">
          <Icon icon={TextUnderlineIcon} weight="bold" />
        </ToggleGroupItem>
      </ToggleGroup>
    </div>
  )
}

Install

From the registry this site publishes, with dependencies resolved by the CLI.

npx shadcn@latest add https://applecn.vercel.app/r/toggle-group.json

Source

packages/ui/src/components/toggle-group.tsx

"use client"

import { Toggle as TogglePrimitive } from "@base-ui/react/toggle"
import { ToggleGroup as ToggleGroupPrimitive } from "@base-ui/react/toggle-group"
import { cn } from "cn"

/**
 * A toggle group (HIG › Segmented controls, the select-any style): a joined set of toggle
 * buttons on the fill — Keynote's bold/italic/underline — single-select by default, or
 * `multiple`. It shares the segmented control's geometry: the platform's height and corner, a
 * white pressed segment with the segment shadow on iOS and the web, the accent fill on macOS.
 */
type ToggleGroupProps = ToggleGroupPrimitive.Props & {
  "aria-label"?: string
}

function ToggleGroup({ className, ...props }: ToggleGroupProps) {
  return (
    <ToggleGroupPrimitive
      data-slot="toggle-group"
      className={cn(
        "inline-flex h-(--segmented-height) items-stretch rounded-segmented bg-fill-3 p-(--segmented-inset)",
        className
      )}
      {...props}
    />
  )
}

function ToggleGroupItem({ className, ...props }: TogglePrimitive.Props) {
  return (
    <TogglePrimitive
      data-slot="toggle-group-item"
      className={cn(
        "inline-flex min-w-(--segmented-height) flex-1 items-center justify-center gap-1.5 rounded-[calc(var(--radius-segmented)-var(--segmented-inset))] px-3 text-[length:var(--segmented-font)] leading-none font-medium whitespace-nowrap text-label transition-[background-color,box-shadow,color] duration-(--duration-press) ease-(--ease-standard) outline-none select-none focus-visible:ring-4 focus-visible:ring-ring/60 disabled:opacity-40 data-pressed:bg-background data-pressed:font-semibold data-pressed:shadow-segment macos:data-pressed:bg-primary macos:data-pressed:text-white macos:data-pressed:shadow-none [&_svg]:pointer-events-none [&_svg]:shrink-0",
        className
      )}
      {...props}
    />
  )
}

export { ToggleGroup, ToggleGroupItem }
export type { ToggleGroupProps }