Command palette

@yasinsensoy/web

Web · Components

Radio

Radio button.

6 exports

Live example

Preview

Plan

You can change the plan any time.

Code

import { RadioField } from "@yasinsensoy/web";

<RadioField label="Plan" name="plan" variant="card" description="You can change the plan any time." required
  options={[
    { value: "free", label: "Free", description: "3 projects", aside: "$0" },
    { value: "pro", label: "Pro", description: "Unlimited projects", aside: "$9/mo", icon: "heart" },
    { value: "enterprise", label: "Enterprise", disabled: true, tooltip: "Coming soon" },
  ]}
  messages={{ valueMissing: "Pick a plan to continue" }}
  onValueChange={setPlan} />

Events

  1. Events appear here when you interact with the preview.

Radio + group: the same group is linked, only one is selected

Delivery

Payment (separate group)

Segment: view and range picker

Selected view: list

Horizontal list and sizes

Optional selection: deselectable

Value: "" (click the selected one again)

Import

import { Radio, RadioGroup } from "@yasinsensoy/web";

Radio

componentServer component

Radio button. Only one of the options in the same group can be selected: selecting one clears the others. Built on a real <input type="radio">; works with forms, FormData, arrow keys and screen readers even before JavaScript loads. Usable in server components.

To render options from an array, manage the value in one place and get card or segment looks use RadioGroup; for a form field with label and validation use RadioField. Radio is useful when you need to place options in separate spots on the page.

On touch screens the touch area is enlarged to 44×44 px without changing the look.

Radio({ group, name, variant: variantProp, size, label, description, icon, aside, tooltip, effect, onCheckedChange, onValueChange, onChange, className, "aria-describedby": describedByProp, ...props }: RadioProps): ReactNode

Examples

<Radio group="delivery" value="shipping" defaultChecked label="Shipping" />
<Radio group="delivery" value="store" label="Pick up in store"
  description="You get an SMS when it's ready." />

<Radio group="plan" value="pro" variant="card" icon="star"
  label="Pro" description="Unlimited projects" aside="$9/mo" />

<Radio group="sort" value="newest" checked={sort === "newest"} onValueChange={setSort}
  label="Newest" />

RadioGroup

componentClient component

Group where only one of the options can be selected. Options are given with the options array; the value is managed controlled (value) or uncontrolled (defaultValue). For a form field with label and validation use RadioField.

  • Real <input type="radio"> elements are used: arrow-key navigation, form submission and form reset work as in the browser.
  • Three looks: list with round buttons (default), cards with descriptions (card) and a tab-like strip (segment). See RadioVariant.
  • With deselectable clicking the selected option again clears it.
  • On touch screens each option gets at least a 44 px touch area.
RadioGroup({ options, name, value, defaultValue, onValueChange, variant: variantProp, effect, size, orientation, disabled, required, deselectable, form, id, className, ...props }: RadioGroupProps): JSX.Element

Examples

<RadioGroup name="size" defaultValue="M" options={["S", "M", "L", "XL"]}
  orientation="horizontal" aria-label="Size" />

<RadioGroup name="plan" variant="card" orientation="horizontal" defaultValue="pro"
  aria-label="Plan"
  options={[
    { value: "free", label: "Free", description: "3 projects", aside: "$0" },
    { value: "pro", label: "Pro", description: "Unlimited projects", aside: "$9/mo", icon: "star" },
  ]} />

<RadioGroup variant="segment" value={view} onValueChange={setView} aria-label="View"
  options={[{ value: "list", label: "List" }, { value: "grid", label: "Grid" }]} />

Look of the radio button:

  • default: round button with a label next to it.
  • card: bordered card, fully clickable; highlighted when selected. For choices with descriptions such as plan, delivery or payment method.
  • segment: the button is hidden and the label looks like a tab piece. Used with RadioGroup, not alone (it gathers the pieces into a strip).
type RadioVariant = "default" | "card" | "segment"

RadioProps

interface

Props of the Radio component.

Extends: Omit<ComponentProps<"input">, "type" | "size">

NameTypeDefaultDescription
group?stringOption group. Only one of the options with the same group value can be selected; selecting one clears the others. Arrow keys navigate within the group. Submitted in the form as the group name (name); if name is given too, that is used.
value?stringValue submitted in the form if selected.
variant?RadioVariant | CustomVariant<"radio">"default"Look: built-in, or a custom one added with defineAppearance (its classes are added to the option label; laid out like default unless extends is given). If omitted, UIProvider appearance.defaults.radio.variant. See RadioVariant.
size?Size"md"
label?ReactNodeLabel next to the button; fully clickable. If given, className is applied to the label itself.
description?ReactNodeDescription below the label; screen readers read it with the button.
icon?IconPropIcon before the label (name or element). Especially useful in the card look.
aside?ReactNodeExtra right-aligned content at the end of the label (e.g. price, "Recommended" badge, delivery time).
tooltip?TooltipPropTooltip shown on hover, keyboard focus or long press on touch screens. Opens from the whole label and screen readers read it with the button. See TooltipProp.
effect?EffectPropEffect or effects. Applied to the whole label in the card and segment looks (e.g. ["lift", "tilt"]), to the round button in the plain look (e.g. "pulse"). If omitted, UIProvider appearance.defaults.radio.effect. See BuiltinEffect.
onCheckedChange?(checked: boolean) => voidCalled when the selection changes. A radio button only changes when selected (true).
onValueChange?(value: string) => voidCalled with its own value when this option is selected.

RadioOption

interface

Option of RadioGroup. If only text is given, it is both value and label.

NameTypeDescription
valuestringValue submitted in the form; must be unique in the group.
label?ReactNodeVisible label. If omitted, value is shown.
description?ReactNodeDescription below the label.
icon?IconPropIcon before the label (name or element).
aside?ReactNodeExtra right-aligned content (e.g. price, badge).
disabled?booleanDisables only this option.
tooltip?TooltipPropTooltip of the option. See TooltipProp.
aria-label?stringAccessible name for icon-only segments that have no label.

Props of the RadioGroup component.

Extends: Omit<ComponentProps<"div">, "onChange" | "defaultValue" | "children">

NameTypeDefaultDescription
optionsreadonly (string | RadioOption)[]Options: text or RadioOption.
name?stringGroup name submitted in the form. If omitted, a name is generated (needed for arrow-key navigation); pass it if you read it in the form.
value?stringSelected value (controlled). "" if none is selected.
defaultValue?string""Initially selected value (uncontrolled).
onValueChange?(value: string, option: RadioOption | undefined) => voidCalled with the new value and option when the selection changes. "" when cleared.
variant?RadioVariant | CustomVariant<"radio">"default"Look: built-in, or a custom one added with defineAppearance (its classes are added to the option label; laid out like default unless extends is given). If omitted, UIProvider appearance.defaults.radio.variant. See RadioVariant.
effect?EffectPropEffect of each option (e.g. ["lift", "glow"] in the card look). See RadioProps.effect.
size?Size"md"
orientation?"vertical" | "horizontal""vertical"Layout of the options. Horizontal cards spread over as many columns as fit, and stack on narrow screens. segment is always horizontal.
disabled?booleanDisables all options.
required?booleanThe form isn't submitted without a selection.
deselectable?booleanClicking the selected option again (or pressing space) clears the selection. Lets optional choices return to "none".
form?stringId of the form to attach to if the options are outside the form.