Radio component allows users to choose a single option from a predefined set of options. To allow multiple choices, use the Checkbox component instead.
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>
);<fieldset class="k-fieldset">
<legend class="k-fieldset-legend">Delivery method</legend>
<k-radio>
<input type="radio" id="home" name="delivery" value="home" checked />
<label for="home">Home delivery</label>
</k-radio>
<k-radio>
<input type="radio" id="pickup" name="delivery" value="pickup" />
<label for="pickup">Pickup from store</label>
</k-radio>
<k-radio>
<input type="radio" id="locker" name="delivery" value="locker" />
<label for="locker">Parcel locker</label>
</k-radio>
</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>
);<fieldset class="k-fieldset" aria-describedby="delivery-description">
<legend class="k-fieldset-legend">Delivery method</legend>
<k-radio>
<input type="radio" id="home" name="delivery" value="home" aria-describedby="home-description" />
<label for="home">Home delivery</label>
<p class="k-field-description" id="home-description">9,90 € · Tomorrow between 8 and 21</p>
</k-radio>
<k-radio>
<input type="radio" id="pickup" name="delivery" value="pickup" aria-describedby="pickup-description" checked />
<label for="pickup">Pickup from store</label>
<p class="k-field-description" id="pickup-description">Free · Ready in 2 hours</p>
</k-radio>
<k-radio>
<input type="radio" id="locker" name="delivery" value="locker" aria-describedby="locker-description" />
<label for="locker">Parcel locker</label>
<p class="k-field-description" id="locker-description">4,90 € · In 1 to 3 days</p>
</k-radio>
</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>
);<fieldset
class="k-fieldset"
style="
--k-radio-fill: var(--k-color-theme-secondary);
--k-radio-fill-hover: var(--k-color-theme-secondary-hover);"
>
<legend class="k-fieldset-legend">Receipt</legend>
<k-radio>
<input type="radio" id="email" name="receipt" value="email" checked />
<label for="email">Email</label>
</k-radio>
<k-radio>
<input type="radio" id="paper" name="receipt" value="paper" />
<label for="paper">Paper</label>
</k-radio>
</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>
);<fieldset class="k-fieldset">
<legend class="k-fieldset-legend">Delivery method</legend>
<k-radio>
<input type="radio" id="home" name="delivery" value="home" disabled />
<label for="home">Home delivery</label>
</k-radio>
<k-radio>
<input type="radio" id="pickup" name="delivery" value="pickup" checked disabled />
<label for="pickup">Pickup from store</label>
</k-radio>
</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>
);<fieldset class="k-fieldset">
<legend class="k-fieldset-legend">Bag size</legend>
<k-stack direction="row" gap="lg">
<k-radio>
<input type="radio" id="bag-s" name="bag" value="s" />
<label for="bag-s">Small</label>
</k-radio>
<k-radio>
<input type="radio" id="bag-m" name="bag" value="m" checked />
<label for="bag-m">Medium</label>
</k-radio>
<k-radio>
<input type="radio" id="bag-l" name="bag" value="l" />
<label for="bag-l">Large</label>
</k-radio>
</k-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>
);<fieldset class="k-fieldset">
<legend class="k-fieldset-legend">Payment method<span class="k-label-required" aria-hidden="true">*</span></legend>
<k-radio>
<input type="radio" id="card" name="payment" value="card" required />
<label for="card">Card</label>
</k-radio>
<k-radio>
<input type="radio" id="mobilepay" name="payment" value="mobilepay" required />
<label for="mobilepay">MobilePay</label>
</k-radio>
<k-radio>
<input type="radio" id="invoice" name="payment" value="invoice" required />
<label for="invoice">Invoice</label>
</k-radio>
</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>
);<form>
<k-stack>
<fieldset class="k-fieldset">
<legend class="k-fieldset-legend">Payment method<span class="k-label-required" aria-hidden="true">*</span></legend>
<k-radio>
<input type="radio" id="mobilepay" name="payment" value="mobilepay" required />
<label for="mobilepay">MobilePay</label>
</k-radio>
<k-radio>
<input type="radio" id="invoice" name="payment" value="invoice" required />
<label for="invoice">Invoice</label>
</k-radio>
<k-radio>
<input type="radio" id="card" name="payment" value="card" required aria-describedby="card-message" aria-invalid="true" />
<label for="card">Card ending in 4242</label>
<k-field-message variant="error" role="alert"><svg viewBox="0 0 24 24" aria-hidden="true" xmlns="http://www.w3.org/2000/svg"><path d="M22.113 18.023L13.745 3.08c-.75-1.343-2.738-1.338-3.49 0L1.888 18.023C1.153 19.332 2.13 21 3.633 21h16.735c1.503 0 2.476-1.666 1.745-2.978zM20.367 19.5H3.633a.504.504 0 0 1-.435-.745l8.364-14.94a.506.506 0 0 1 .875 0l8.368 14.942a.501.501 0 0 1-.438.743zm-8.885-5.66l-.31-4.785c0-.115.08-.195.194-.195h1.265c.116 0 .196.08.196.195l-.3 4.613c0 .127-.058.217-.208.242l-.61.102c-.128.023-.217-.035-.23-.172zM13 16.14c0 .552-.403.978-1 .978s-1-.425-1-.978.403-.965 1-.965 1 .425 1 .965z" fill="currentColor" /></svg><span id="card-message">This card has expired.</span></k-field-message>
</k-radio>
</fieldset>
<button class="k-button" type="submit">Place order</button>
</k-stack>
</form>Properties #
| Name | Description | Type | Default |
|---|---|---|---|
label | Label for the option. Always provide one, even when it is hidden with | ReactNode | — |
hideLabel | Hides the label visually while keeping it available to assistive technology. | boolean | false |
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 | string | — |
checked | Whether this option is selected. Pair with | 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 | boolean | false |
hideRequired | Hides the required marker on the label while keeping | boolean | false |
disabled | Disables the option. Blocks interaction and drops the value from the submitted data. | boolean | — |
form | The | string | — |
Events #
| Name | Description | Type |
|---|---|---|
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.
| Name | Description | Default |
|---|---|---|
--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
disabledfor 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-borderand--k-radio-fillneed 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/csspnpm add @kesko/components @kesko/cssyarn add @kesko/components @kesko/cssbun add @kesko/components @kesko/cssOnce 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 #
Usage guidelines
Learn how to use the component library with React, Next.js, or basic HTML.
Localization
How to translate the components for multiple languages.
CSS Framework
Utilities and tokens for everything the components don’t cover.
Themes
How to customize the component library theme.
Accessibility checklist
Interactive, browser-based Kesko accessibility checklist.