Radio Group
Drawn rings; choosing one inks a dot in the middle with a tight spiral.
import { Field, FieldContent, FieldDescription, FieldLabel, FieldTitle } from "@/components/ui/field";
import { Label } from "@/components/ui/label";
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
export default function RadioGroupDemo() {
return (
<div className="flex w-full max-w-lg flex-col gap-10">
<RadioGroup defaultValue="blue" aria-label="Ink">
{[
["blue", "Blue ballpoint"],
["black", "Black fineliner"],
["pencil", "Pencil"],
].map(([value, label]) => (
<Label key={value}>
<RadioGroupItem value={value} seed={`radio-${value}`} />
{label}
</Label>
))}
</RadioGroup>
<RadioGroup defaultValue="cream" aria-label="Paper" className="grid-cols-1 gap-5 sm:grid-cols-2">
{[
["cream", "Cream", "Warm, like a notebook."],
["night", "Night", "Navy, for the lamp."],
].map(([value, title, description]) => (
<FieldLabel key={value}>
<Field orientation="horizontal">
<RadioGroupItem value={value} seed={`paper-${value}`} />
<FieldContent>
<FieldTitle>{title}</FieldTitle>
<FieldDescription>{description}</FieldDescription>
</FieldContent>
</Field>
</FieldLabel>
))}
</RadioGroup>
</div>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ radio-group"use client";
import { Radio as RadioPrimitive } from "@base-ui/react/radio";
import { RadioGroup as RadioGroupPrimitive } from "@base-ui/react/radio-group";
import { usePen, type Pen } from "@/hooks/use-ink-box";
import { inkClassName, Stroke } from "@/lib/ink";
import { InkOutline } from "@/lib/ink-outline";
import { dotStroke } from "@/lib/ink-sketch";
/** The options as a column, or (`orientation="horizontal"`) in a row that wraps. */
function RadioGroup({ className, orientation = "vertical", ...props }: RadioGroupPrimitive.Props & { orientation?: "vertical" | "horizontal" }) {
return (
<RadioGroupPrimitive
data-slot="radio-group"
data-orientation={orientation}
className={inkClassName("w-full gap-3 data-[orientation=horizontal]:flex data-[orientation=horizontal]:flex-wrap data-[orientation=horizontal]:gap-x-6 data-[orientation=vertical]:grid", className)}
{...props}
/>
);
}
/** A drawn ring; choosing it inks a dot in the middle with a tight spiral. */
function RadioGroupItem({
className,
seed,
passes,
draw,
weight,
speed,
...props
}: RadioPrimitive.Root.Props & Pick<Pen, "passes" | "draw" | "weight" | "speed"> & { seed?: string | number }) {
const pen = usePen({ passes, draw, weight, speed });
return (
<RadioPrimitive.Root
data-slot="radio-group-item"
className={inkClassName(
"peer group/radio relative inline-flex size-5 shrink-0 cursor-pointer items-center justify-center rounded-full outline-none",
"after:absolute after:-inset-x-3 after:-inset-y-2",
"text-ink-3 transition-colors duration-(--dur-hover) not-data-checked:hover:text-ink data-checked:text-ink",
"aria-invalid:text-destructive data-invalid:text-destructive",
"focus-visible:outline-solid focus-visible:outline-[1.5px] focus-visible:outline-offset-4 focus-visible:outline-ring",
"data-disabled:cursor-not-allowed data-disabled:opacity-50",
className,
)}
{...props}
>
<InkOutline pen={pen} seed={seed} estimate={[20, 20]} shape="ring" pad={6}>
{({ w, h, s }) => (
<g transform={`translate(${w / 2} ${h / 2})`}>
<Stroke d={dotStroke(s + 40, w * 0.22)} draw="checked" duration={260} width={w * 0.17} />
</g>
)}
</InkOutline>
</RadioPrimitive.Root>
);
}
export { RadioGroup, RadioGroupItem };Usage
import { Label } from "@/components/ui/label"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
<RadioGroup defaultValue="blue">
<Label>
<RadioGroupItem value="blue" />
Blue ballpoint
</Label>
<Label>
<RadioGroupItem value="pencil" />
Pencil
</Label>
</RadioGroup>Examples
States
A disabled option, and a group with no choice made yet and an error.
import { Field, FieldDescription, FieldError, FieldLabel, FieldLegend, FieldSet } from "@/components/ui/field";
import { Label } from "@/components/ui/label";
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
export default function RadioGroupStates() {
return (
<div className="grid w-full max-w-md gap-8">
<RadioGroup defaultValue="day" aria-label="Time" className="flex flex-wrap gap-6">
<Label>
<RadioGroupItem value="day" seed="rs-day" />
Day
</Label>
<Label>
<RadioGroupItem value="night" seed="rs-night" />
Night
</Label>
<Label>
<RadioGroupItem value="dusk" disabled seed="rs-dusk" />
Dusk
</Label>
</RadioGroup>
<FieldSet data-invalid>
<FieldLegend variant="label">Delivery</FieldLegend>
<FieldDescription>Choose one before you continue.</FieldDescription>
<RadioGroup aria-label="Delivery" aria-invalid>
{["Post", "Courier"].map((option) => (
<Field key={option} orientation="horizontal" data-invalid>
<RadioGroupItem value={option.toLowerCase()} id={`rs-${option}`} aria-invalid seed={`rs-${option}`} />
<FieldLabel htmlFor={`rs-${option}`}>{option}</FieldLabel>
</Field>
))}
</RadioGroup>
<FieldError>Pick a delivery option.</FieldError>
</FieldSet>
</div>
);
}Choice cards
Wrap a Field in FieldLabel and the whole row is the target.
import { Field, FieldContent, FieldDescription, FieldLabel, FieldTitle } from "@/components/ui/field";
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
const papers = [
["cream", "Cream", "Warm, like old letters."],
["white", "White", "Crisp, for drawings."],
["legal", "Legal pad", "Yellow, for lists and plans."],
] as const;
export default function RadioGroupCards() {
return (
<RadioGroup defaultValue="cream" aria-label="Paper" className="max-w-md">
{papers.map(([value, title, note]) => (
<FieldLabel key={value} htmlFor={`rc-${value}`}>
<Field orientation="horizontal">
<FieldContent>
<FieldTitle>{title}</FieldTitle>
<FieldDescription>{note}</FieldDescription>
</FieldContent>
<RadioGroupItem value={value} id={`rc-${value}`} seed={`rc-${value}`} />
</Field>
</FieldLabel>
))}
</RadioGroup>
);
}Horizontal
The options in a row that wraps, under a heading.
Notebook size
import { Label } from "@/components/ui/label";
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
const sizes = ["A6", "A5", "A4", "Letter"];
export default function RadioGroupHorizontal() {
return (
<div className="flex flex-col gap-3">
<span id="rh-size" className="font-bold">
Notebook size
</span>
<RadioGroup orientation="horizontal" defaultValue="A5" aria-labelledby="rh-size">
{sizes.map((size) => (
<Label key={size}>
<RadioGroupItem value={size} seed={`rh-${size}`} />
{size}
</Label>
))}
</RadioGroup>
</div>
);
}A scale
A 1 to 5 rating with a word at each end, read back as you choose.
How did the pen feel?
ScratchySmooth
Pick one.
"use client";
import { useState } from "react";
import { Label } from "@/components/ui/label";
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
export default function RadioGroupScale() {
const [score, setScore] = useState("");
return (
<div className="flex w-full max-w-md flex-col gap-3">
<span id="rsc-q" className="font-bold">
How did the pen feel?
</span>
<RadioGroup orientation="horizontal" value={score} onValueChange={(v) => setScore(v as string)} aria-labelledby="rsc-q" className="justify-between gap-0">
{[1, 2, 3, 4, 5].map((n) => (
<Label key={n} className="flex-col gap-2">
<RadioGroupItem value={String(n)} seed={`rsc-${n}`} />
{n}
</Label>
))}
</RadioGroup>
<div className="flex justify-between text-sm text-ink-3">
<span>Scratchy</span>
<span>Smooth</span>
</div>
<p className="text-ink-2">{score ? `You gave it a ${score}.` : "Pick one."}</p>
</div>
);
}API reference
- orientationdefault
"vertical" "vertical" | "horizontal"The options as a column, or in a row that wraps.
- RadioGroupItem disabled
booleanDims the option and takes it out of the choice.
- RadioGroupItem seed
string | numberPins the drawing.
Every other prop goes to Base UI's Radio.
Pen settings
The drawing also takes draw, passes, weight and speed, as props or from the nearest InkProvider. What each one does.