← Components

Selection and input · Material 3: Radio button

Radio group

Choose exactly one option. The old dot shrinks toward the new choice and the new dot grows in.

Examples and variants

Basic

Use arrow keys to move.

Example
function RadioGroupBasic() {
  return (
    <RadioGroup defaultValue="calm" aria-label="Motion">
      {["still", "calm", "liquid"].map((value) => (
        <Label key={value} className="flex items-center gap-2 capitalize">
          <RadioGroupItem value={value} />
          {value}
        </Label>
      ))}
    </RadioGroup>
  )
}

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/radio-group.json
npx shadcn@latest add https://isotopi.com/r/label.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 @base-ui/react cn
  2. Copy these 2 files to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/radio-group.tsxradio-group
      components/ui/radio-group.tsx
      "use client"
      
      import * as React from "react"
      import { Radio as RadioPrimitive } from "@base-ui/react/radio"
      import { RadioGroup as RadioGroupPrimitive } from "@base-ui/react/radio-group"
      import { cn } from "cn"
      
      type Jump = {
        id: number
        x: number
        y: number
        dx: number
        dy: number
      }
      
      function allowsJump(node: HTMLElement) {
        if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return false
        const motion = node.closest<HTMLElement>("[data-motion]")?.dataset.motion
        return motion !== "calm" && motion !== "still"
      }
      
      function RadioGroup({ className, children, ...props }: RadioGroupPrimitive.Props) {
        const rootRef = React.useRef<HTMLDivElement>(null)
        const previous = React.useRef<HTMLElement | null>(null)
        const [jump, setJump] = React.useState<Jump | null>(null)
      
        React.useEffect(() => {
          const root = rootRef.current
          if (!root) return
          previous.current = root.querySelector<HTMLElement>("[data-checked]")
          const observer = new MutationObserver(() => {
            const next = root.querySelector<HTMLElement>("[data-checked]")
            const from = previous.current
            previous.current = next
            if (!from || !next || from === next || !allowsJump(root)) return
            const rootBox = root.getBoundingClientRect()
            const fromBox = from.getBoundingClientRect()
            const toBox = next.getBoundingClientRect()
            setJump({
              id: Date.now(),
              x: fromBox.left - rootBox.left + fromBox.width / 2,
              y: fromBox.top - rootBox.top + fromBox.height / 2,
              dx: toBox.left - fromBox.left,
              dy: toBox.top - fromBox.top,
            })
          })
          observer.observe(root, {
            subtree: true,
            attributes: true,
            attributeFilter: ["data-checked", "data-unchecked"],
          })
          return () => observer.disconnect()
        }, [])
      
        return (
          <RadioGroupPrimitive
            ref={rootRef}
            data-slot="radio-group"
            className={cn("relative grid gap-stack-sm", className)}
            {...props}
          >
            {children}
            {jump ? (
              <span
                key={jump.id}
                aria-hidden
                className="radio-jump pointer-events-none absolute size-inline-sm rounded-full bg-primary"
                style={{
                  left: jump.x,
                  top: jump.y,
                  "--jump-x": `${jump.dx}px`,
                  "--jump-y": `${jump.dy}px`,
                } as React.CSSProperties}
                onAnimationEnd={() => setJump(null)}
              />
            ) : null}
          </RadioGroupPrimitive>
        )
      }
      
      function RadioGroupItem({ className, ...props }: RadioPrimitive.Root.Props) {
        return (
          <RadioPrimitive.Root
            data-slot="radio-group-item"
            className={cn(
              "hit-target relative flex size-checkbox shrink-0 items-center justify-center rounded-full border-0 bg-(--radio-container) shadow-1 outline-none transition-colors duration-(--duration-fast) ease-(--ease-out) hover:bg-(--radio-container-hover) focus-visible:shadow-(--focus-ring) data-checked:bg-(--radio-container-checked) data-disabled:cursor-not-allowed data-disabled:bg-(--radio-container-disabled) data-disabled:shadow-none data-checked:data-disabled:bg-(--radio-container-checked-disabled)",
              className
            )}
            {...props}
          >
            <RadioPrimitive.Indicator
              data-slot="radio-group-indicator"
              className="radio-dot size-inline-sm rounded-full bg-(--radio-dot)"
            />
          </RadioPrimitive.Root>
        )
      }
      
      export { RadioGroup, RadioGroupItem }
      
    • components/ui/label.tsxlabel
      components/ui/label.tsx
      "use client"
      
      import * as React from "react"
      import { cn } from "cn"
      
      function Label({ className, ...props }: React.ComponentProps<"label">) {
        return (
          <label
            data-slot="label"
            className={cn(
              "flex items-center gap-2 text-sm leading-none font-medium select-none group-data-[disabled=true]:pointer-events-none group-data-[disabled=true]:text-(--field-label-disabled) group-data-[invalid=true]/field:text-(--color-danger-fg) peer-disabled:cursor-not-allowed peer-disabled:text-(--field-label-disabled)",
              className
            )}
            {...props}
          />
        )
      }
      
      export { Label }
      

Imports

What the examples on this page import:

Imports
import { Label } from "@/components/ui/label"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"

Anatomy

  1. 1Group · Lays options out vertically or horizontally.
  2. 2Radio · --radio-size circle; checked dot --radio-dot.
  3. 3Label · Wrap each radio in Label.

Props

PropTypeDefaultDescription
value / defaultValuestring—Selected option.
onValueChange(value: string) => void—Called on change.

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
--checkbox-containervar(--surface-container)oklch(0.965 0.005 95)oklch(0.12 0 0)
--checkbox-container-checkedvar(--color-brand)oklch(0.18 0.008 260)oklch(0.98 0 0)
--checkbox-container-hovercolor-mix(in oklch, var(--checkbox-container), var(--on-surface) var(--state-hover-mix))color-mix(in oklch, var(--checkbox-container), var(--on-surface) var(--state-hover-mix))same
--checkbox-container-disabledvar(--color-bg-disabled)color-mix(in oklch, var(--color-fg-default) var(--state-disabled-container-mix), transparent)same
--checkbox-container-checked-disabledcolor-mix(in oklch, var(--color-fg-disabled) 55%, transparent)color-mix(in oklch, var(--color-fg-disabled) 55%, transparent)same
--radio-sizevar(--size-checkbox)16pxsame
--radio-dotvar(--color-fg-on-accent)oklch(0.97 0.004 95)oklch(0.14 0 0)
--radio-containervar(--surface-container)oklch(0.965 0.005 95)oklch(0.12 0 0)
--radio-container-checkedvar(--color-brand)oklch(0.18 0.008 260)oklch(0.98 0 0)
--radio-container-hovercolor-mix(in oklch, var(--radio-container), var(--on-surface) var(--state-hover-mix))color-mix(in oklch, var(--radio-container), var(--on-surface) var(--state-hover-mix))same
--radio-container-disabledvar(--color-bg-disabled)color-mix(in oklch, var(--color-fg-default) var(--state-disabled-container-mix), transparent)same
--radio-container-checked-disabledcolor-mix(in oklch, var(--color-fg-disabled) 55%, transparent)color-mix(in oklch, var(--color-fg-disabled) 55%, transparent)same

Motion

TriggerWhat happensTokens
SelectThe dot jumps from the old option to the new one.--duration-fast, --ease-bounce

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

Accessibility

  • role=radiogroup; arrow keys move and select.
  • Give the group a label.

Usage

Do

  • 2 to 6 options that are all visible.

Don’t

  • Don't use for long lists (use Select).

Code

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

import * as React from "react"
import { Radio as RadioPrimitive } from "@base-ui/react/radio"
import { RadioGroup as RadioGroupPrimitive } from "@base-ui/react/radio-group"
import { cn } from "cn"

type Jump = {
  id: number
  x: number
  y: number
  dx: number
  dy: number
}

function allowsJump(node: HTMLElement) {
  if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return false
  const motion = node.closest<HTMLElement>("[data-motion]")?.dataset.motion
  return motion !== "calm" && motion !== "still"
}

function RadioGroup({ className, children, ...props }: RadioGroupPrimitive.Props) {
  const rootRef = React.useRef<HTMLDivElement>(null)
  const previous = React.useRef<HTMLElement | null>(null)
  const [jump, setJump] = React.useState<Jump | null>(null)

  React.useEffect(() => {
    const root = rootRef.current
    if (!root) return
    previous.current = root.querySelector<HTMLElement>("[data-checked]")
    const observer = new MutationObserver(() => {
      const next = root.querySelector<HTMLElement>("[data-checked]")
      const from = previous.current
      previous.current = next
      if (!from || !next || from === next || !allowsJump(root)) return
      const rootBox = root.getBoundingClientRect()
      const fromBox = from.getBoundingClientRect()
      const toBox = next.getBoundingClientRect()
      setJump({
        id: Date.now(),
        x: fromBox.left - rootBox.left + fromBox.width / 2,
        y: fromBox.top - rootBox.top + fromBox.height / 2,
        dx: toBox.left - fromBox.left,
        dy: toBox.top - fromBox.top,
      })
    })
    observer.observe(root, {
      subtree: true,
      attributes: true,
      attributeFilter: ["data-checked", "data-unchecked"],
    })
    return () => observer.disconnect()
  }, [])

  return (
    <RadioGroupPrimitive
      ref={rootRef}
      data-slot="radio-group"
      className={cn("relative grid gap-stack-sm", className)}
      {...props}
    >
      {children}
      {jump ? (
        <span
          key={jump.id}
          aria-hidden
          className="radio-jump pointer-events-none absolute size-inline-sm rounded-full bg-primary"
          style={{
            left: jump.x,
            top: jump.y,
            "--jump-x": `${jump.dx}px`,
            "--jump-y": `${jump.dy}px`,
          } as React.CSSProperties}
          onAnimationEnd={() => setJump(null)}
        />
      ) : null}
    </RadioGroupPrimitive>
  )
}

function RadioGroupItem({ className, ...props }: RadioPrimitive.Root.Props) {
  return (
    <RadioPrimitive.Root
      data-slot="radio-group-item"
      className={cn(
        "hit-target relative flex size-checkbox shrink-0 items-center justify-center rounded-full border-0 bg-(--radio-container) shadow-1 outline-none transition-colors duration-(--duration-fast) ease-(--ease-out) hover:bg-(--radio-container-hover) focus-visible:shadow-(--focus-ring) data-checked:bg-(--radio-container-checked) data-disabled:cursor-not-allowed data-disabled:bg-(--radio-container-disabled) data-disabled:shadow-none data-checked:data-disabled:bg-(--radio-container-checked-disabled)",
        className
      )}
      {...props}
    >
      <RadioPrimitive.Indicator
        data-slot="radio-group-indicator"
        className="radio-dot size-inline-sm rounded-full bg-(--radio-dot)"
      />
    </RadioPrimitive.Root>
  )
}

export { RadioGroup, RadioGroupItem }