import { FieldItem, FieldLabel, FieldLegend, FieldSet } from "@/components/ui/field";import { Radio, RadioGroup } from "@/components/ui/radio";
export default function RadioBasic() { return ( <FieldSet> <FieldLegend>Density</FieldLegend> <RadioGroup defaultValue="comfortable"> <FieldItem> <Radio value="compact" /> <FieldLabel>Compact</FieldLabel> </FieldItem> <FieldItem> <Radio value="comfortable" /> <FieldLabel>Comfortable</FieldLabel> </FieldItem> <FieldItem> <Radio value="spacious" /> <FieldLabel>Spacious</FieldLabel> </FieldItem> </RadioGroup> </FieldSet> );}- Completed:Add the registryOnce per project
pnpm dlx shadcn@latest registry add @jarvis=https://ui.jarv.is/r/{name}.json - Completed:Add radio
pnpm dlx shadcn@latest add @jarvis/radio
Usage
Pair each Radio with a label in a FieldItem, and label the group with a FieldSet and a
FieldLegend, from Field.
import { FieldItem, FieldLabel, FieldLegend, FieldSet } from "@/components/ui/field";import { Radio, RadioGroup } from "@/components/ui/radio";
export function ThemePicker() { return ( <FieldSet> <FieldLegend>Theme</FieldLegend> <RadioGroup defaultValue="system"> <FieldItem> <Radio value="light" /> <FieldLabel>Light</FieldLabel> </FieldItem> <FieldItem> <Radio value="dark" /> <FieldLabel>Dark</FieldLabel> </FieldItem> <FieldItem> <Radio value="system" /> <FieldLabel>System</FieldLabel> </FieldItem> </RadioGroup> </FieldSet> );}Without a visible legend, give the RadioGroup an aria-label instead.
Composition
Examples
With descriptions
Wrap the label in a FieldContent with a FieldDescription when an option needs explaining.
import { FieldContent, FieldDescription, FieldItem, FieldLabel, FieldLegend, FieldSet,} from "@/components/ui/field";import { Radio, RadioGroup } from "@/components/ui/radio";
export default function RadioWithDescriptions() { return ( <FieldSet className="w-full max-w-sm"> <FieldLegend>Visibility</FieldLegend> <RadioGroup defaultValue="private"> <FieldItem> <Radio value="public" /> <FieldContent> <FieldLabel>Public</FieldLabel> <FieldDescription>Anyone with the link can view it.</FieldDescription> </FieldContent> </FieldItem> <FieldItem> <Radio value="private" /> <FieldContent> <FieldLabel>Private</FieldLabel> <FieldDescription>Only people you invite can view it.</FieldDescription> </FieldContent> </FieldItem> </RadioGroup> </FieldSet> );}Disabled
Set disabled on a Radio, or on the RadioGroup to disable all of them.
import { FieldContent, FieldDescription, FieldItem, FieldLabel, FieldLegend, FieldSet,} from "@/components/ui/field";import { Radio, RadioGroup } from "@/components/ui/radio";
export default function RadioDisabled() { return ( <FieldSet> <FieldLegend>Layout</FieldLegend> <RadioGroup defaultValue="list"> <FieldItem> <Radio value="list" /> <FieldLabel>List</FieldLabel> </FieldItem> <FieldItem> <Radio value="board" /> <FieldLabel>Board</FieldLabel> </FieldItem> <FieldItem disabled> <Radio value="timeline" /> <FieldContent> <FieldLabel>Timeline</FieldLabel> <FieldDescription>Not available on small screens.</FieldDescription> </FieldContent> </FieldItem> </RadioGroup> </FieldSet> );}Read-only
Set readOnly on the RadioGroup to show the selected option without letting anyone change it.
The radios stay focusable, and the value still submits with a form.
import { FieldItem, FieldLabel, FieldLegend, FieldSet } from "@/components/ui/field";import { Radio, RadioGroup } from "@/components/ui/radio";
export default function RadioReadOnly() { return ( <FieldSet> <FieldLegend>Data region</FieldLegend> <RadioGroup readOnly defaultValue="eu"> <FieldItem> <Radio value="us" /> <FieldLabel>United States</FieldLabel> </FieldItem> <FieldItem> <Radio value="eu" /> <FieldLabel>European Union</FieldLabel> </FieldItem> </RadioGroup> </FieldSet> );}Choice cards
Set variant="card" on each FieldItem to frame it as a card. Its FieldLabel makes the whole
card clickable.
import { FieldContent, FieldDescription, FieldItem, FieldLabel, FieldLegend, FieldSet,} from "@/components/ui/field";import { Radio, RadioGroup } from "@/components/ui/radio";
const plans = [ { value: "starter", name: "Starter", description: "Up to 3 projects and 5 members." }, { value: "team", name: "Team", description: "Unlimited projects and 25 members." }, { value: "enterprise", name: "Enterprise", description: "SSO, audit logs and priority support." },];
export default function RadioChoiceCards() { return ( <FieldSet className="w-full max-w-sm"> <FieldLegend>Plan</FieldLegend> <RadioGroup defaultValue="team"> {plans.map((plan) => ( <FieldItem key={plan.value} variant="card"> <FieldContent> <FieldLabel>{plan.name}</FieldLabel> <FieldDescription>{plan.description}</FieldDescription> </FieldContent> <Radio value={plan.value} /> </FieldItem> ))} </RadioGroup> </FieldSet> );}Invalid
Wrap the group in a Field, set invalid on it, and explain the problem
with a FieldError.
import { Field, FieldError, FieldItem, FieldLabel, FieldLegend, FieldSet,} from "@/components/ui/field";import { Radio, RadioGroup } from "@/components/ui/radio";
export default function RadioInvalid() { return ( <Field invalid className="w-fit"> <FieldSet> <FieldLegend>Shipping speed</FieldLegend> <RadioGroup> <FieldItem> <Radio value="standard" /> <FieldLabel>Standard</FieldLabel> </FieldItem> <FieldItem> <Radio value="express" /> <FieldLabel>Express</FieldLabel> </FieldItem> </RadioGroup> </FieldSet> <FieldError>Choose a shipping speed.</FieldError> </Field> );}In a form
Wrap the group in a Field with a name, and the form submits the selected radio's value under
it. Leave out defaultValue so that someone has to choose, and with required on the
RadioGroup, the form won't submit until they do, failing validation with valueMissing.
import { Button } from "@/components/ui/button";import { Field, FieldError, FieldItem, FieldLabel, FieldLegend, FieldSet, Form,} from "@/components/ui/field";import { Radio, RadioGroup } from "@/components/ui/radio";import { toast } from "@/components/ui/toast";
export default function RadioForm() { return ( <Form className="max-w-sm" onSubmit={(event) => { event.preventDefault(); const entries = Array.from(new FormData(event.currentTarget), ([name, value]) => typeof value === "string" ? `${name}=${value}` : name, ); toast.add({ title: "Form submitted", description: entries.join(", ") }); }} > <Field name="plan"> <FieldSet> <FieldLegend>Plan</FieldLegend> <RadioGroup required> <FieldItem> <Radio value="hobby" /> <FieldLabel>Hobby</FieldLabel> </FieldItem> <FieldItem> <Radio value="pro" /> <FieldLabel>Pro</FieldLabel> </FieldItem> <FieldItem> <Radio value="enterprise" /> <FieldLabel>Enterprise</FieldLabel> </FieldItem> </RadioGroup> </FieldSet> <FieldError match="valueMissing">Choose a plan.</FieldError> </Field> <Button type="submit" variant="primary" className="self-start"> Continue </Button> </Form> );}Props
radioVariants() returns the classes that draw the circle. Use them to show a selected state on an
element that isn't a control of its own.
Base UI's styling (opens in a new tab) and composition (opens in a new tab) guides say more about className, style and render.
Radio
| Prop | Type | Default |
|---|---|---|
valueThe unique identifying value of the radio in a group. | any | — |
disabledWhether the component should ignore user interaction. | boolean | — |
requiredWhether the user must choose a value before submitting a form. | boolean | — |
readOnlyWhether the user should be unable to select the radio button. | boolean | — |
inputRefA ref to access the hidden input element. | React.Ref<HTMLInputElement> | — |
nativeButtonWhether the component renders a native | boolean | false |
classNameCSS class applied to the element, or a function that returns a class based on the component's state. | string | ((state: RadioRootState) => string | undefined) | — |
styleStyle applied to the element, or a function that returns a style object based on the component's state. | React.CSSProperties | ((state: RadioRootState) => React.CSSProperties | undefined) | — |
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a | ComponentRenderFn<HTMLProps, RadioRootState> | React.ReactElement | — |
RadioGroup
| Prop | Type | Default |
|---|---|---|
disabledWhether the component should ignore user interaction. | boolean | false |
readOnlyWhether the user should be unable to select a different radio button in the group. | boolean | false |
requiredWhether the user must choose a value before submitting a form. | boolean | false |
nameIdentifies the field when a form is submitted. | string | — |
formIdentifies the form that owns the radio inputs. Useful when the radio group is rendered outside the form. | string | — |
valueThe controlled value of the radio item that should be currently selected. To render an uncontrolled radio group, use the | any | — |
defaultValueThe uncontrolled value of the radio button that should be initially selected. To render a controlled radio group, use the | any | — |
onValueChangeCallback fired when the value changes. | ((value: any, eventDetails: RadioGroup.ChangeEventDetails) => void) | — |
inputRefA ref to access the hidden input element. | React.Ref<HTMLInputElement> | — |
classNameCSS class applied to the element, or a function that returns a class based on the component's state. | string | ((state: RadioGroupState) => string | undefined) | — |
styleStyle applied to the element, or a function that returns a style object based on the component's state. | React.CSSProperties | ((state: RadioGroupState) => React.CSSProperties | undefined) | — |
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a | ComponentRenderFn<HTMLProps, RadioGroupState> | React.ReactElement | — |