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.
React
const Preview = () => { return <Radio label='Radio' value='value' name='name' />; }; render(<Preview />)
Usage
Examples
Grouping
Use Fieldset around a group of radio buttons to provide a common prompt (<legend>) and optionally a description (description).
React
const GroupEn = () => { return ( <Fieldset> <Fieldset.Legend>How would you like us to contact you?</Fieldset.Legend> <Fieldset.Description> Choose the method that works best for you. We use this only to send important updates about your case. </Fieldset.Description> <Radio label='Email' description='We will use the email address you provided earlier (name@example.com)' value='email' name='contact' /> <Radio label='SMS' description='We will use the phone number you provided earlier (99 99 99 99)' value='sms' name='contact' /> <Radio label='Letter' description='Delivery may take 3-5 working days, depending on the postal service.' value='letter' name='contact' /> </Fieldset> ); }; render(<GroupEn />)
With Error
Here we must use Fieldset, because it activates the correct style and ensures that the content has the correct connections for accessibility.
React
const WithErrorEn = () => { return ( <Fieldset> <Fieldset.Legend>Which district do you live in?</Fieldset.Legend> <Fieldset.Description> Trondheim is divided into four districts </Fieldset.Description> <Radio label='Østbyen' value='ostbyen' name='city' aria-invalid='true' /> <Radio label='Lerkendal' value='lerkendal' name='city' aria-invalid='true' /> <Radio label='Heimdal' value='heimdal' name='city' aria-invalid='true' /> <Radio label='Midtbyen' value='midtbyen' name='city' aria-invalid='true' /> <ValidationMessage data-color='danger'> You must choose a district before you can continue. </ValidationMessage> </Fieldset> ); }; render(<WithErrorEn />)
Readonly
Fields with the readonly attribute are included in the tab order. Users can copy the content but not edit it. Information is included when the form is submitted.
Readonly fields can be confusing for some users. Not everyone will understand why they cannot change the content of the field. We therefore recommend avoiding readonly as much as possible.
React
const ReadOnlyEn = () => { return ( <Fieldset> <Fieldset.Legend>Which district do you live in?</Fieldset.Legend> <Fieldset.Description> Trondheim is divided into four districts </Fieldset.Description> <Radio label='Østbyen' value='ostbyen' name='city' readOnly /> <Radio label='Lerkendal' value='lerkendal' name='city' readOnly /> <Radio label='Heimdal' value='heimdal' name='city' readOnly checked /> <Radio label='Midtbyen' value='midtbyen' name='city' readOnly /> </Fieldset> ); }; render(<ReadOnlyEn />)
Inline
Radio can be positioned horizontally with flex, but should generally be placed vertically.
React
const InlineEn = () => { return ( <Fieldset> <Fieldset.Legend>Contact by email?</Fieldset.Legend> <Fieldset.Description> Confirm whether you would like to be contacted by email. </Fieldset.Description> <div style={{ display: 'flex', flexWrap: 'wrap', gap: 'var(--ds-size-6)' }} > <Radio name='my-inline' label='Yes' value='yes' /> <Radio name='my-inline' label='No' value='no' /> </div> </Fieldset> ); }; render(<InlineEn />)
HTML
Radio is a composite component, so in HTML you have to build it from multiple components.
We use Fieldset, Label and Input to create a Radio.
The code below shows how you can visually build a Radio in HTML, but you have to make sure that <input> is connected with <label> and description.
In Fieldset
The code below is an example of how to use multiple Radio inside <fieldset>.
CSS variables and data attributes
Checkbox is a composite component, and therefore we show variables and data attributes from ds-input here.
| Name | Value |
|---|---|
| --dsc-input-padding | var(--ds-size-2) var(--ds-size-3) |
| --dsc-input-size--toggle | var(--ds-size-6) |
| --dsc-input-size | var(--ds-size-12) |
| --dsc-input-background--readonly | var(--ds-color-neutral-surface-tinted) |
| --dsc-input-background | var(--ds-color-neutral-surface-default) |
| --dsc-input-border-color--readonly | var(--ds-color-neutral-border-subtle) |
| --dsc-input-border-color | var(--ds-color-neutral-border-default) |
| --dsc-input-border-style | solid |
| --dsc-input-border-width--toggle | max(var(--ds-border-width-default),calc(var(--ds-size-1)/2)) |
| --dsc-input-border-width | var(--ds-border-width-default) |
| --dsc-input-outline-color--hover | var(--ds-color-neutral-border-default) |
| --dsc-input-outline-color--toggle--hover | var(--dsc-input-accent-color) |
| --dsc-input-outline-width--hover | var(--ds-border-width-default) |
| --dsc-input-outline-style--hover | solid |
| --dsc-input-color--readonly | var(--ds-color-neutral-text-default) |
| --dsc-input-color | var(--ds-color-neutral-text-default) |
| --dsc-input-stroke-color | var(--ds-color-base-contrast-default) |
| --dsc-input-stroke-color--invalid | var(--ds-color-danger-base-contrast-default) |
| --dsc-input-stroke-width | 0.05em |
| --dsc-input-accent-color | var(--ds-color-base-default) |
| --dsc-input-accent-color--invalid | var(--ds-color-danger-text-subtle) |
| Name | Value |
|---|---|
| data-width | auto |