Skip to main content

What are you looking for?

Try searching for…

Components

Radio

A Radio is an option the user can select. Use multiple radio to show a list of options. Users can switch between the options, but can only select one.

HTML

Press Enter to start editing
Unable to parse html
const Preview = () => {
  return <Radio label='Radio' value='value' name='name' />;
};

render(<Preview />)

Usage

Use the class ds-input on <input> together with type="radio".

For a valid form element, we recommend using <ds-field> together with a <label>.

CSS variables and data attributes

Sizes are controlled with data-size and colors with data-color. The component will inherit from the closest parent where these are set.

Radio is ds-input with type="radio", so variables and data attributes that apply to input also apply to radio.

CSS Variables
NameValue
--dsc-input-padding-blockvar(--ds-size-2)
--dsc-input-padding-inlinevar(--ds-size-3)
--dsc-input-paddingvar(--dsc-input-padding-block) var(--dsc-input-padding-inline)
--dsc-input-size--togglevar(--ds-size-6)
--dsc-input-sizevar(--ds-size-12)
--dsc-input-background--readonlyvar(--ds-color-neutral-surface-tinted)
--dsc-input-backgroundvar(--ds-color-neutral-surface-default)
--dsc-input-border-color--readonlyvar(--ds-color-neutral-border-subtle)
--dsc-input-border-colorvar(--ds-color-neutral-border-default)
--dsc-input-border-stylesolid
--dsc-input-border-width--togglemax(var(--ds-border-width-default),calc(var(--ds-size-1)/2))
--dsc-input-border-widthvar(--ds-border-width-default)
--dsc-input-outline-color--hovervar(--ds-color-neutral-border-default)
--dsc-input-outline-color--toggle--hovervar(--dsc-input-accent-color)
--dsc-input-outline-width--hovervar(--ds-border-width-default)
--dsc-input-outline-style--hoversolid
--dsc-input-color--readonlyvar(--ds-color-neutral-text-default)
--dsc-input-colorvar(--ds-color-neutral-text-default)
--dsc-input-stroke-colorvar(--ds-color-base-contrast-default)
--dsc-input-stroke-color--invalidvar(--ds-color-danger-base-contrast-default)
--dsc-input-stroke-width0.05em
--dsc-input-accent-colorvar(--ds-color-base-default)
--dsc-input-accent-color--invalidvar(--ds-color-danger-text-subtle)
--dsc-input-line-heightvar(--ds-line-height-md)
Data Attributes
NameValue
data-widthauto

Examples

Grouping

Use fieldset for grouping multiple choices.

Outline

data-variant="outline" adds extra padding and a border around the option to create a larger clickable area.

Inline

Radio can be positioned horizontally with flex, but should generally be placed vertically.

ReadOnly

type="radio" is always :read-only and does not support this in the same way as input types you can type into.

If you set readonly on radio, we ensure that it behaves as if it has read-only access by stopping onClick and onChange.

The user will still be able to focus on the element.

React

In React, building a radio is easier, because we have a ready-made component that handles most of the logic and state management for us.

Radio is a composite component that uses field, label, validation message, and input to create a radio.

In addition, we have a dedicated useRadioGroup React hook to make it easier to manage a group of Radio components.

disabled
Description

Disables element @note Avoid using if possible for accessibility purposes

Type
boolean
readOnly
Description

Toggle `readOnly`

Type
boolean
data-indeterminate
Description

Indeterminate state for checkbox inputs Only works when used inside `Field` component

Type
boolean
Default
false
aria-label
Description

Optional aria-label

Type
string
label
Description

Radio label

Type
ReactNode
description
Description

Description for field

Type
ReactNode
value
Description

Value of the `input` element

Type
string | number | readonly string[]
error
Description

Error message for field

Type
ReactNode
variant
Description

If outline, the radio will have a border.

Type
"outline"
aria-labelledby
Type
string
NameTypeDefaultDescription
disabledboolean-

Disables element @note Avoid using if possible for accessibility purposes

readOnlyboolean-

Toggle `readOnly`

data-indeterminatebooleanfalse

Indeterminate state for checkbox inputs Only works when used inside `Field` component

aria-labelstring-

Optional aria-label

labelReactNode-

Radio label

descriptionReactNode-

Description for field

valuestring | number | readonly string[]-

Value of the `input` element

errorReactNode-

Error message for field

variant"outline"-

If outline, the radio will have a border.

aria-labelledbystring-

-

Edit this page on github.com (opens in a new tab)