Radio component allows users to choose a single option from a predefined set of options. To allow multiple choices, use the Checkbox component instead.

Open in new tab
import { Fieldset, Radio } from "@kesko/components";

export default () => (
  <Fieldset label="Delivery method">
    <Radio label="Home delivery" name="delivery" value="home" defaultChecked />
    <Radio label="Pickup from store" name="delivery" value="pickup" />
    <Radio label="Parcel locker" name="delivery" value="locker" />
  </Fieldset>
);
import { Fieldset, Radio } from "@kesko/components";

export default () => (
  <Fieldset label="Delivery method">
    <Radio label="Home delivery" name="delivery" value="home" description="9,90 € · Tomorrow between 8 and 21" />
    <Radio label="Pickup from store" name="delivery" value="pickup" description="Free · Ready in 2 hours" defaultChecked />
    <Radio label="Parcel locker" name="delivery" value="locker" description="4,90 € · In 1 to 3 days" />
  </Fieldset>
);
import { Fieldset, Radio } from "@kesko/components";

const secondary = {
  "--k-radio-fill": "var(--k-color-theme-secondary)",
  "--k-radio-fill-hover": "var(--k-color-theme-secondary-hover)",
};

export default () => (
  <Fieldset label="Receipt" style={secondary}>
    <Radio label="Email" name="receipt" value="email" defaultChecked />
    <Radio label="Paper" name="receipt" value="paper" />
  </Fieldset>
);
import { Fieldset, Radio } from "@kesko/components";

export default () => (
  <Fieldset label="Delivery method">
    <Radio label="Home delivery" name="delivery" value="home" disabled />
    <Radio label="Pickup from store" name="delivery" value="pickup" defaultChecked disabled />
  </Fieldset>
);
import { Fieldset, Radio, Stack } from "@kesko/components";

export default () => (
  <Fieldset label="Bag size">
    <Stack direction="row" gap="lg">
      <Radio label="Small" name="bag" value="s" />
      <Radio label="Medium" name="bag" value="m" defaultChecked />
      <Radio label="Large" name="bag" value="l" />
    </Stack>
  </Fieldset>
);
import { Fieldset, Radio } from "@kesko/components";

export default () => (
  <Fieldset label="Payment method" required>
    <Radio label="Card" name="payment" value="card" required hideRequired />
    <Radio label="MobilePay" name="payment" value="mobilepay" required hideRequired />
    <Radio label="Invoice" name="payment" value="invoice" required hideRequired />
  </Fieldset>
);
import { Button, Fieldset, Radio, Stack } from "@kesko/components";

export default () => (
  <form onSubmit={event => event.preventDefault()}>
    <Stack>
      <Fieldset label="Payment method" required>
        <Radio label="MobilePay" name="payment" value="mobilepay" required hideRequired />
        <Radio label="Invoice" name="payment" value="invoice" required hideRequired />
        <Radio
          label="Card ending in 4242"
          name="payment"
          value="card"
          required
          hideRequired
          error="This card has expired."
        />
      </Fieldset>
      <Button type="submit">Place order</Button>
    </Stack>
  </form>
);

Properties #

NameDescriptionTypeDefault
label

Label for the option. Always provide one, even when it is hidden with hideLabel.

ReactNode—
hideLabel

Hides the label visually while keeping it available to assistive technology.

booleanfalse
description

Assistive text shown below the option, such as what choosing it leads to.

ReactNode—
error

Optional error message for this option. Also marks the control invalid for assistive technology. An error about the whole group, such as nothing selected, goes on the Fieldset.

ReactNode—
name

The name of the group. Every option in the group shares it, and the value of the selected option is submitted under it.

string—
value

The value submitted under name while this option is selected.

string—
checked

Whether this option is selected. Pair with onChange to control the group.

boolean—
defaultChecked

Whether this option starts selected in an uncontrolled group.

boolean—
required

Requires one option of the group to be selected before the form submits. Draws the marker on the label and is announced to assistive technology. Put the marker on the Fieldset legend instead and pass hideRequired here when every option would otherwise carry one.

booleanfalse
hideRequired

Hides the required marker on the label while keeping required on the control.

booleanfalse
disabled

Disables the option. Blocks interaction and drops the value from the submitted data.

boolean—
form

The id of the form the control submits with. Only needed when the control sits outside that form.

string—

Events #

NameDescriptionType
onChange

Handler called when this option becomes selected.

(event: ChangeEvent<HTMLInputElement>) => void
onFocus

Handler called when the control receives focus.

(event: FocusEvent<HTMLInputElement>) => void
onBlur

Handler called when the control loses focus.

(event: FocusEvent<HTMLInputElement>) => void

Styles #

CSS custom properties provide finer-grained control than properties. See Styling API for more details. Prefer the Properties API when it suits your need.

NameDescriptionDefault
--k-radio-bg

Overrides the background color of the control.

var(--k-color-bg-base)
--k-radio-border

Overrides the border color of the unselected control in every state.

var(--k-color-border-control)
--k-radio-fill

Overrides the border and dot color of the selected control.

var(--k-color-theme-primary)
--k-radio-fill-hover

Overrides the border and dot color of the selected control on hover and focus.

var(--k-color-theme-primary-hover)
--k-radio-text

Overrides the label text color.

var(--k-color-text-base)
--k-radio-focus

Overrides the focus ring color.

var(--k-color-border-focus)

Accessibility #

Radio is built on a native <input type="radio"> and <label>, so it behaves as expected with assistive technologies.

  • Always pass a label, and keep it visible. A screen reader reads it as the name of the option, and clicking it selects the option. Options are read side by side, so a group of unlabelled circles cannot be compared.
  • Always put the group in a Fieldset. The options share one name, and the legend names the question they answer. A required group that is left empty shows its message on the Fieldset, not under every option.
  • Start with one option selected when there is a sensible default. Once an option is selected it can be changed but not cleared, so a group that starts empty asks for a decision. Leave it empty only when a default would be dishonest, such as a payment method, and mark the group required.
  • Pick the right control. Radio is for exactly one choice out of a few. Several independent options are a Checkbox group, and a setting that takes effect right away is a Switch.
  • Avoid disabled for validation. A disabled option leaves the tab order and cannot explain itself. Keep it enabled and let the message say what is wrong.
  • Check contrast if you override colors. The label needs 4.5:1 against the surface, and --k-radio-border and --k-radio-fill need 3:1 against it. Verify with the WebAIM contrast checker.

See the accessibility guidelines and the A11Y checklist for more details.


Usage #

There are two necessary parts to using the Radio component. The @kesko/components package and the CSS Framework are required. Everything else is optional:

npm install @kesko/components @kesko/css

Once installed, load the CSS Framework in your app’s entry point or root layout:

import "@kesko/css";

Then, you can import the Radio:

import { Radio } from "@kesko/components";

Projects without React can get the same styles from @kesko/css by importing the components stylesheet as well:

@import "@kesko/css";
@import "@kesko/css/components";

Resources #