Skip to content
caliper ui docs

Components

Toggle and toggle group

A button that stays pressed, alone or in a raised group that holds one choice or many and moves with the arrow keys as a single tab stop.

Free
pnpm dlx shadcn@latest add @caliper/toggle
Note for #launch

Kiara Murayama · visible to everyone in the channel

Launch moves to Tuesday 28 October. The release note goes out at 09:00 CET.

Installs components/ui/toggle.tsxDepends on @radix-ui/react-slot, lucide-reactMotion none in component source

When to use

Use it for

  • Bold, italic, pin, mute: a setting that is on or off right now.
  • A small set of related options, such as alignment or view.

Skip it when

  • Choosing a page or a mode with a visible result; use tabs or a segmented control.
  • Yes or no in a form; use a switch.

Decisions

Why it is built this way, so you know what to keep when you change your copy.

Behaviour

One tab stop, arrows move
A group of twelve tools costs one Tab press. Arrow keys follow the reading direction, so it works right to left; in single mode the arrow also selects.

Styling

On is recessed, never raised
A pressed toggle sinks into the shell, the way a toggled tool does in a button group, so it never reads like the selected plate of a segmented control.

Usage

Single always keeps one selected
Single behaves like a radio group. If people need to turn everything off, use multiple.

Installation

terminal
pnpm dlx shadcn@latest add @caliper/toggle

Adds its 5 files to components/ui/, each with its own stylesheet, and installs their packages. No account needed. The first part also adds Caliper UI’s tokens; import them once as in Installation.

Usage

This is the code behind the preview above. It is built from Toggle, ToggleGroup and ToggleGroupItem.

example.tsx
import { AlignCenter, AlignLeft, AlignRight, Bold, Italic, Pin, Underline } from "lucide-react"
import { useState, type ReactNode } from "react"
import { cn } from "@/components/ui/lib/cn"
import { Toggle, ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle"

function Panel({ title, context, children, className }: { title: ReactNode; context: ReactNode; children: ReactNode; className?: string }) {
  return (
    <section className={cn("grid w-full max-w-md gap-5 rounded-overlay border border-border bg-background p-6 shadow-resting", className)}>
      <div className="grid gap-1">
        {/* div, not p: titles here can hold a badge or a direction-aware block. */}
        <div className="font-medium">{title}</div>
        <p className="text-sm text-muted-foreground">{context}</p>
      </div>
      {children}
    </section>
  );
}

export function ToggleDemo() {
  const [style, setStyle] = useState(["bold"]);
  const [align, setAlign] = useState(["left"]);
  return (
    <Panel
      title={<span className="flex items-center justify-between gap-3">Note for #launch<Toggle size="sm" defaultPressed><Pin aria-hidden="true" />Pinned</Toggle></span>}
      context="Kiara Murayama · visible to everyone in the channel"
    >
      <div className="flex flex-wrap items-center gap-2">
        <ToggleGroup label="Text style" value={style} onValueChange={setStyle}>
          <ToggleGroupItem value="bold" size="icon" aria-label="Bold"><Bold aria-hidden="true" /></ToggleGroupItem>
          <ToggleGroupItem value="italic" size="icon" aria-label="Italic"><Italic aria-hidden="true" /></ToggleGroupItem>
          <ToggleGroupItem value="underline" size="icon" aria-label="Underline"><Underline aria-hidden="true" /></ToggleGroupItem>
        </ToggleGroup>
        <ToggleGroup type="single" label="Alignment" value={align} onValueChange={setAlign}>
          <ToggleGroupItem value="left" size="icon" aria-label="Left"><AlignLeft aria-hidden="true" /></ToggleGroupItem>
          <ToggleGroupItem value="center" size="icon" aria-label="Center"><AlignCenter aria-hidden="true" /></ToggleGroupItem>
          <ToggleGroupItem value="right" size="icon" aria-label="Right"><AlignRight aria-hidden="true" /></ToggleGroupItem>
        </ToggleGroup>
      </div>
      <p
        className="rounded-control border border-border px-4 py-3 text-sm leading-6"
        style={{
          fontWeight: style.includes("bold") ? 600 : 400,
          fontStyle: style.includes("italic") ? "italic" : "normal",
          textDecoration: style.includes("underline") ? "underline" : "none",
          textAlign: (align[0] ?? "left") as "left" | "center" | "right",
        }}
      >
        Launch moves to Tuesday 28 October. The release note goes out at 09:00 CET.
      </p>
    </Panel>
  );
}

API reference

Toggle

component

Also accepts the native props of its root element and a ref.

PropTypeDefaultDescription
size"sm" | "md" | "lg" | "icon"—
asChildboolean—
loadingboolean—Shows a spinner over the label and swallows clicks while keeping focus and width.
feedbackKeystring—Animate a meaningful label/icon change, such as saving → saved.
pressedboolean—
defaultPressedbooleanfalse
onPressedChange((pressed: boolean) => void)—

ToggleGroup

component

Also accepts the native props of its root element and a ref.

PropTypeDefaultDescription
type"multiple" | "single""multiple"Single behaves like a radio group: one value, always one selected. Multiple lets any combination stay on.
valuestring[]—
defaultValuestring[][]
onValueChange((value: string[]) => void)—
label *string—Names the group for assistive technology, such as "Text style".
disabledboolean—

ToggleGroupItem

component

Also accepts the native props of its root element and a ref.

PropTypeDefaultDescription
size"sm" | "md" | "lg" | "icon"—
asChildboolean—
loadingboolean—Shows a spinner over the label and swallows clicks while keeping focus and width.
feedbackKeystring—Animate a meaningful label/icon change, such as saving → saved.
value *string—

Accessibility

It is built on native elements and keeps aria-pressed and aria-label in step with state.

KeyBehaviour
↓Moves to the next item or value.
↑Moves to the previous item or value.
HomeJumps to the first item or the minimum.
EndJumps to the last item or the maximum.

Motion

It declares no animation in its component source. Any transition comes from its stylesheet, which uses the duration tokens that collapse to instant under prefers-reduced-motion.

All Actions examples