Radio group

One choice from a few: a list, or cards.

Shipping
Plan
Delivery

Installation

Terminal
npx shadcn@latest add https://fabrickit.org/r/radio.json

This copies the component into components/fabricui and its engine into lib/fabricui. It's your code from then on. First time? Add the theme once: see Installation.

Code

components/fabricui/radio.tsx
"use client";

import * as React from "react";
import { GooSlide } from "@/lib/fabricui/goo-slide";
import "./radio.css";

const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;

export type RadioOption = {
  value: string;
  label: React.ReactNode;
  /** a dim line under the label (cards) */
  hint?: React.ReactNode;
  /** on the right: a price, say */
  meta?: React.ReactNode;
  disabled?: boolean;
components/fabricui/radio.css
/*
 * Radio group — native radio inputs (so arrow keys, labels and forms just
 * work), drawn three ways. Shared by the Radio group page.
 *
 * The dot (.radio): the round checkbox's sibling. Choosing it, white spreads
 * from the centre of the circle (never from outside), then a dark dot pops in
 * the middle. The old choice draws back to its centre.
 *
 *   <label class="radio-row">
 *     <span class="radio"><input class="radio__input" type="radio" name="ship" /><span class="radio__ring"><span class="radio__ink"></span></span><span class="radio__dot"></span></span>
 *     …
 *   </label>
 *
 * Cards (.radio-cards) have one highlight that slides to the chosen card,
 * the library's gooey way (goo-slide.js). For a segmented look, use Tabs.
 */

Usage

Usage
"use client";

import * as React from "react";
import { RadioGroup } from "@/components/fabricui/radio";

export function Plan() {
  const [plan, setPlan] = React.useState("pro");
  return (
    <RadioGroup
      variant="cards"
      aria-label="Plan"
      value={plan}
      onValueChange={setPlan}
      options={[
        { value: "starter", label: "Starter", hint: "For trying things out", meta: "$0" },
        { value: "pro", label: "Pro", hint: "For one person shipping often", meta: "$12" },
        { value: "team", label: "Team", hint: "Shared workspace, up to 10", meta: "$29", disabled: true },
      ]}
    />
  );
}

Props

PropTypeDefault
options

The choices. hint is a dim line under the label; meta sits on the right (a price, say).

{ value, label, hint?, meta?, disabled? }[]—
value

The chosen value, if you control it.

string—
defaultValue

The starting choice when you don't.

string—
onValueChange

Called when the choice changes.

(value: string) => void—
variant

Rows with a line between them, or cards with a highlight that slides to the chosen one.

"list" | "cards""list"
name

The radios' form name. One is made up if you leave it out.

string—
Radio

The dot on its own (a native radio input), for your own layouts. Takes every <input> prop.

component—