Image radio groups
Five visual choice groups: color swatches, inline-SVG preview thumbs, labelled color chips, initial avatars and large tiles. No remote image is ever fetched: every surface is a semantic token fill or an inline SVG. Each is self-contained, sized, token-driven and keyboard accessible with role radiogroup.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/radio-group-imageDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/radio-group-image.tsx"use client"
import * as React from "react"
import { Check } from "lucide-react"
import { motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Image radio-group family: 5 visual selection tiles. There are NO remote image
URLs - every surface is a token fill or an inline SVG. The variants: color
swatches, small preview tiles, labelled color chips, initial-based avatars and
large tiles. The selection is indicated not by color alone but also with a
checkmark. role="radiogroup" + role="radio", roving tabindex, arrow-key
navigation and selection. Color comes ONLY from tokens, via alpha color-mix.
Under reduced-motion the checkmark appears instantly. Renders with no props
too. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
type RadioOption = { value: string; label: React.ReactNode }
interface RadioGroupProps {
className?: string
size?: StyledSize
options?: RadioOption[]
value?: string
defaultValue?: string
onValueChange?: (v: string) => void
}
const DEFAULT_OPTIONS: RadioOption[] = [
{ value: "a", label: "Option A" },
{ value: "b", label: "Option B" },
{ value: "c", label: "Option C" },
]
const textSize: Record<StyledSize, string> = {
sm: "text-xs",
md: "text-sm",
lg: "text-base",
xl: "text-lg",
}
/* Swatch and avatar box: all of them stay above 24px. */
const swatchSize: Record<StyledSize, string> = {
sm: "size-7",
md: "size-8",
lg: "size-9",
xl: "size-10",
}
const tileSize: Record<StyledSize, string> = {
sm: "size-14",
md: "size-16",
lg: "size-20",
xl: "size-24",
}
/* Tile media strip: the width follows the tile scale, the height a fixed ratio. */
const tileMedia: Record<StyledSize, string> = {
sm: "h-10 min-w-24",
md: "h-12 min-w-28",
lg: "h-14 min-w-32",
xl: "h-16 min-w-36",
}
const checkSize: Record<StyledSize, string> = {
sm: "size-3",
md: "size-3.5",
lg: "size-4",
xl: "size-4",
}
/* Semantic tone surfaces - no hex, tokens only. */
const TONE_SURFACE = ["bg-brand", "bg-info", "bg-success", "bg-warning", "bg-danger"]
const TONE_ON = [
"text-brand-foreground",
"text-info-foreground",
"text-success-foreground",
"text-warning-foreground",
"text-danger-foreground",
]
const TONE_NAME = ["brand", "info", "success", "warning", "danger"]
function useRadioGroup(props: Pick<RadioGroupProps, "value" | "defaultValue" | "onValueChange" | "options">) {
const { value, defaultValue, onValueChange, options } = props
const opts = options && options.length > 0 ? options : DEFAULT_OPTIONS
const reduce = useReducedMotion()
const [internal, setInternal] = React.useState(defaultValue ?? opts[0]?.value)
const selected = value ?? internal
const select = React.useCallback(
(v: string) => {
if (value === undefined) setInternal(v)
onValueChange?.(v)
},
[value, onValueChange]
)
const spring = reduce
? { duration: 0 }
: ({ type: "spring" as const, stiffness: 520, damping: 30 })
return { opts, selected, select, reduce, spring }
}
/* Arrow-key navigation + selection (together with the roving tabindex). */
function useRadioKeys(opts: RadioOption[], select: (v: string) => void) {
const refs = React.useRef<(HTMLButtonElement | null)[]>([])
const setRef = (i: number) => (el: HTMLButtonElement | null) => {
refs.current[i] = el
}
const onKeyDown = (i: number) => (e: React.KeyboardEvent) => {
const last = opts.length - 1
let next = -1
if (e.key === "ArrowRight" || e.key === "ArrowDown") next = i === last ? 0 : i + 1
else if (e.key === "ArrowLeft" || e.key === "ArrowUp") next = i === 0 ? last : i - 1
if (next < 0) return
e.preventDefault()
const target = opts[next]
if (!target) return
select(target.value)
refs.current[next]?.focus()
}
return { setRef, onKeyDown }
}
const focusRing =
"outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50 focus-visible:ring-offset-2 focus-visible:ring-offset-background"
/* Secili yuzeyde beliren onay isareti. */
function CheckMark({ active, spring, className }: { active: boolean; spring: Record<string, unknown>; className?: string }) {
return (
<motion.span
initial={false}
animate={{ scale: active ? 1 : 0, opacity: active ? 1 : 0 }}
transition={spring}
className={cn("pointer-events-none inline-flex items-center justify-center", className)}
aria-hidden="true"
>
<Check className="size-full" strokeWidth={3} />
</motion.span>
)
}
/* Etiketten deterministik bas harf. */
function initial(label: React.ReactNode, i: number) {
if (typeof label === "string" && label.trim().length > 0) return label.trim().charAt(0).toUpperCase()
if (typeof label === "number") return String(label)
return String(i + 1)
}
/* An inline preview graphic - no remote image, drawn with currentColor. */
function ThumbArt({ index }: { index: number }) {
const variant = index % 3
return (
<svg viewBox="0 0 48 48" className="size-full" aria-hidden="true" focusable="false">
{variant === 0 ? (
<>
<circle cx="34" cy="14" r="6" fill="currentColor" opacity="0.55" />
<path d="M4 40 L18 20 L30 40 Z" fill="currentColor" opacity="0.85" />
<path d="M24 40 L34 26 L44 40 Z" fill="currentColor" opacity="0.5" />
</>
) : null}
{variant === 1 ? (
<>
<rect x="6" y="26" width="8" height="16" rx="2" fill="currentColor" opacity="0.5" />
<rect x="18" y="16" width="8" height="26" rx="2" fill="currentColor" opacity="0.75" />
<rect x="30" y="8" width="8" height="34" rx="2" fill="currentColor" opacity="0.9" />
</>
) : null}
{variant === 2 ? (
<>
<circle cx="24" cy="24" r="14" fill="none" stroke="currentColor" strokeWidth="3" opacity="0.5" />
<circle cx="24" cy="24" r="6" fill="currentColor" opacity="0.9" />
</>
) : null}
</svg>
)
}
/* Swatch: round semantic colour swatches; the selected one gets a ring plus a check. */
export function SwatchRadio({ className, size = "md", options, value, defaultValue, onValueChange }: RadioGroupProps) {
const { opts, selected, select, spring } = useRadioGroup({ options, value, defaultValue, onValueChange })
const { setRef, onKeyDown } = useRadioKeys(opts, select)
const activeIndex = opts.findIndex((o) => o.value === selected)
return (
<div
data-slot="styled-radio-group"
role="radiogroup"
aria-label="Choose a color"
className={cn("flex flex-wrap items-center gap-2.5", className)}
>
{opts.map((opt, i) => {
const active = opt.value === selected
const roving = active || (activeIndex < 0 && i === 0)
const tone = i % TONE_SURFACE.length
return (
<button
key={opt.value}
ref={setRef(i)}
type="button"
role="radio"
aria-checked={active}
aria-label={typeof opt.label === "string" ? opt.label : TONE_NAME[tone]}
tabIndex={roving ? 0 : -1}
onKeyDown={onKeyDown(i)}
onClick={() => select(opt.value)}
data-slot="styled-radio-item"
data-tone={TONE_NAME[tone]}
className={cn(
"relative flex cursor-pointer items-center justify-center rounded-full ring-offset-2 ring-offset-background transition-all",
focusRing,
swatchSize[size],
TONE_SURFACE[tone],
TONE_ON[tone],
active ? "ring-2 ring-foreground" : "ring-1 ring-[color-mix(in_oklab,var(--color-foreground)_15%,transparent)] hover:ring-[color-mix(in_oklab,var(--color-foreground)_45%,transparent)]"
)}
>
<CheckMark active={active} spring={spring} className={checkSize[size]} />
</button>
)
})}
</div>
)
}
/* Thumb: small tiles with an inline SVG preview plus a label underneath. */
export function ThumbRadio({ className, size = "md", options, value, defaultValue, onValueChange }: RadioGroupProps) {
const { opts, selected, select, spring } = useRadioGroup({ options, value, defaultValue, onValueChange })
const { setRef, onKeyDown } = useRadioKeys(opts, select)
const activeIndex = opts.findIndex((o) => o.value === selected)
return (
<div
data-slot="styled-radio-group"
role="radiogroup"
aria-label="Choose an option"
className={cn("flex flex-wrap items-start gap-3", className)}
>
{opts.map((opt, i) => {
const active = opt.value === selected
const roving = active || (activeIndex < 0 && i === 0)
return (
<button
key={opt.value}
ref={setRef(i)}
type="button"
role="radio"
aria-checked={active}
tabIndex={roving ? 0 : -1}
onKeyDown={onKeyDown(i)}
onClick={() => select(opt.value)}
data-slot="styled-radio-item"
className={cn(
"flex cursor-pointer flex-col items-center gap-1.5 rounded-lg font-medium transition-colors",
focusRing,
textSize[size],
active ? "text-foreground" : "text-muted-foreground hover:text-foreground"
)}
>
<span
className={cn(
"relative overflow-hidden rounded-lg border bg-surface-2 p-2 transition-colors",
tileSize[size],
active ? "border-primary text-primary" : "border-field-border text-muted-foreground"
)}
>
<ThumbArt index={i} />
<CheckMark
active={active}
spring={spring}
className={cn(
"absolute right-1 top-1 rounded-full bg-primary p-0.5 text-primary-foreground",
checkSize[size]
)}
/>
</span>
<span>{opt.label}</span>
</button>
)
})}
</div>
)
}
/* Color: labelled colour chips with a semantic tone fill on the left. */
export function ColorRadio({ className, size = "md", options, value, defaultValue, onValueChange }: RadioGroupProps) {
const { opts, selected, select, spring } = useRadioGroup({ options, value, defaultValue, onValueChange })
const { setRef, onKeyDown } = useRadioKeys(opts, select)
const activeIndex = opts.findIndex((o) => o.value === selected)
return (
<div
data-slot="styled-radio-group"
role="radiogroup"
aria-label="Choose a color"
className={cn("flex flex-wrap items-center gap-2", className)}
>
{opts.map((opt, i) => {
const active = opt.value === selected
const roving = active || (activeIndex < 0 && i === 0)
const tone = i % TONE_SURFACE.length
return (
<button
key={opt.value}
ref={setRef(i)}
type="button"
role="radio"
aria-checked={active}
tabIndex={roving ? 0 : -1}
onKeyDown={onKeyDown(i)}
onClick={() => select(opt.value)}
data-slot="styled-radio-item"
data-tone={TONE_NAME[tone]}
className={cn(
"flex cursor-pointer items-center gap-2 rounded-full border py-1 pl-1 pr-3 font-medium transition-colors",
focusRing,
textSize[size],
active
? "border-foreground text-foreground"
: "border-field-border text-muted-foreground hover:border-[color-mix(in_oklab,var(--color-foreground)_35%,transparent)] hover:text-foreground"
)}
>
<span
className={cn(
"flex items-center justify-center rounded-full",
swatchSize[size],
TONE_SURFACE[tone],
TONE_ON[tone]
)}
>
<CheckMark active={active} spring={spring} className={checkSize[size]} />
</span>
<span>{opt.label}</span>
</button>
)
})}
</div>
)
}
/* Avatar: bas harfli yuvarlak avatarlar; secili olan halka + rozet onay alir. */
export function AvatarRadio({ className, size = "md", options, value, defaultValue, onValueChange }: RadioGroupProps) {
const { opts, selected, select, spring } = useRadioGroup({ options, value, defaultValue, onValueChange })
const { setRef, onKeyDown } = useRadioKeys(opts, select)
const activeIndex = opts.findIndex((o) => o.value === selected)
return (
<div
data-slot="styled-radio-group"
role="radiogroup"
aria-label="Choose a person"
className={cn("flex flex-wrap items-center gap-3", className)}
>
{opts.map((opt, i) => {
const active = opt.value === selected
const roving = active || (activeIndex < 0 && i === 0)
const tone = i % TONE_SURFACE.length
return (
<button
key={opt.value}
ref={setRef(i)}
type="button"
role="radio"
aria-checked={active}
aria-label={typeof opt.label === "string" ? opt.label : undefined}
tabIndex={roving ? 0 : -1}
onKeyDown={onKeyDown(i)}
onClick={() => select(opt.value)}
data-slot="styled-radio-item"
data-tone={TONE_NAME[tone]}
className={cn("relative cursor-pointer rounded-full transition-transform", focusRing)}
>
<span
className={cn(
"flex items-center justify-center rounded-full font-semibold uppercase ring-offset-2 ring-offset-background transition-all",
swatchSize[size],
textSize[size],
TONE_SURFACE[tone],
TONE_ON[tone],
active ? "ring-2 ring-foreground" : "ring-1 ring-[color-mix(in_oklab,var(--color-foreground)_15%,transparent)]"
)}
>
{initial(opt.label, i)}
</span>
<CheckMark
active={active}
spring={spring}
className="absolute -bottom-0.5 -right-0.5 size-4 rounded-full border border-background bg-primary p-0.5 text-primary-foreground"
/>
</button>
)
})}
</div>
)
}
/* Tile: buyuk gradyan token karolari; secili olan kenarlik + kose onayi alir. */
export function TileRadio({ className, size = "md", options, value, defaultValue, onValueChange }: RadioGroupProps) {
const { opts, selected, select, spring } = useRadioGroup({ options, value, defaultValue, onValueChange })
const { setRef, onKeyDown } = useRadioKeys(opts, select)
const activeIndex = opts.findIndex((o) => o.value === selected)
return (
<div
data-slot="styled-radio-group"
role="radiogroup"
aria-label="Choose an option"
className={cn("flex flex-wrap items-stretch gap-3", className)}
>
{opts.map((opt, i) => {
const active = opt.value === selected
const roving = active || (activeIndex < 0 && i === 0)
const tone = i % TONE_SURFACE.length
return (
<button
key={opt.value}
ref={setRef(i)}
type="button"
role="radio"
aria-checked={active}
tabIndex={roving ? 0 : -1}
onKeyDown={onKeyDown(i)}
onClick={() => select(opt.value)}
data-slot="styled-radio-item"
data-tone={TONE_NAME[tone]}
className={cn(
"relative flex cursor-pointer flex-col overflow-hidden rounded-xl border text-left font-medium transition-colors",
focusRing,
textSize[size],
active
? "border-primary bg-card text-foreground"
: "border-field-border bg-card text-muted-foreground hover:text-foreground"
)}
>
<span
className={cn("flex w-full items-end p-2", tileMedia[size], TONE_SURFACE[tone])}
aria-hidden="true"
>
<span className={cn("block h-1.5 w-8 rounded-full bg-[color-mix(in_oklab,var(--color-background)_70%,transparent)]")} />
</span>
<span className="px-3 py-2">{opt.label}</span>
<CheckMark
active={active}
spring={spring}
className="absolute right-1.5 top-1.5 size-4 rounded-full bg-primary p-0.5 text-primary-foreground"
/>
</button>
)
})}
</div>
)
}Manual installs skip the @ai2/tokens theme, so add the token CSS from the theming guide or the tone colors will be missing.
Variations
5 takes on the same idea. Each is its own export, and every one accepts a size prop (sm, md, lg, xl) aligned to the base Button scale.
Swatch
Round semantic color swatches with a check on select.
import { SwatchRadio } from "@/components/ui/radio-group-image"
<SwatchRadio />Thumb
Preview tiles drawn with inline SVG, labelled underneath.
import { ThumbRadio } from "@/components/ui/radio-group-image"
<ThumbRadio />Color
Labelled color chips with a token fill.
import { ColorRadio } from "@/components/ui/radio-group-image"
<ColorRadio />Avatar
Initial avatars with a ring and a check badge.
import { AvatarRadio } from "@/components/ui/radio-group-image"
<AvatarRadio />Tile
Large media tiles with a corner check.
import { TileRadio } from "@/components/ui/radio-group-image"
<TileRadio />ai2 Image radio groups: 5 styled variations on the token system
The ai2 Image radio groups are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around visual single-choice tiles built from token surfaces and inline SVG. They are free and MIT licensed, and every color comes from a semantic token, so they theme with the rest of ai2 in light and dark.
Motion runs on framer-motion: framer-motion springs the check mark onto the selected tile. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the check mark appears instantly.
What is in the ai2 Image radio groups?
5 exports in one file: Swatch, Thumb, Color, Avatar and Tile. Each renders a native button and takes a size prop (sm, md, lg, xl) aligned to the base Button. They are separate from the base Button on purpose: the base keeps its clean variant, tone and size axes, while the styled layer carries the effects.
You own the file. Copy the one category file and you have all 5 variations, with no runtime dependency on ai2 itself.
Why use it
- On-system by construction: Every color resolves to an ai2 semantic token, so the buttons follow your theme in light and dark with no extra work.
- Effect without the sprawl: The decorations live in a dedicated styled file, so the base Button keeps its clean, predictable API.
- Accessible and honest: Each renders a real button element, keeps a visible focus ring, and respects prefers-reduced-motion.
Features
- Token-driven color: No hardcoded hex or oklch; the look recolors with your theme tokens.
- framer-motion: framer-motion springs the check mark onto the selected tile.
- Reduced-motion aware: Under prefers-reduced-motion, the check mark appears instantly.
- Size aligned to the base: Every variation takes sm, md, lg and xl matching the base Button height scale, so styled and base buttons line up in a row.
Production tips
- Use it for emphasis, not everywhere: Styled buttons draw the eye. Reserve them for the one action you want people to take on a screen, and use the base Button for the rest.
- Keep labels as verbs: The decoration adds weight, so a clear action label keeps the button scannable.
- Pick one variation per surface: The variations share a family; using two different ones in the same view competes for attention.
Works with the rest of ai2
The Image radio groups sit alongside the base Button and the rest of the @ai2 registry. They share the same token file, so a styled action next to a base button or a badge stays visually consistent in both modes.