Components
Checkbox
Checkbox allows users to select one or more options. It can also be used in situations where the user needs to confirm something.
React
Unable to parse html
const Preview = () => { return ( <Checkbox label='Checkbox label' description='Description' value='value' /> ); }; render(<Preview />)
- disabled
- Description
Disables element @note Avoid using if possible for accessibility purposes
- Type
boolean
- readOnly
- Description
Toggle `readOnly`
- Type
boolean
- aria-label
- Description
Optional aria-label
- Type
string
- label
- Description
Checkbox 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
- aria-labelledby
- Type
string
| Name | Type | Default | Description |
|---|---|---|---|
| disabled | boolean | - | Disables element @note Avoid using if possible for accessibility purposes |
| readOnly | boolean | - | Toggle `readOnly` |
| aria-label | string | - | Optional aria-label |
| label | ReactNode | - | Checkbox label |
| description | ReactNode | - | Description for field |
| value | string | number | readonly string[] | - | Value of the `input` element |
| error | ReactNode | - | Error message for field |
| aria-labelledby | string | - | - |
Usage
Examples
Grouping
Use Fieldset and useCheckboxGroup for grouping multiple options.
React
Unable to parse html
const GroupEn = () => { const [value, setValue] = useState<string[]>(['epost']); return ( <Fieldset> <Fieldset.Legend>How would you prefer us to contact you?</Fieldset.Legend> <Fieldset.Description> Select all the options that are relevant to you. </Fieldset.Description> <Checkbox label='E-mail' value='email' checked={value.includes('email')} onChange={(e) => { if (e.target.checked) { setValue([...value, 'email']); } else { setValue(value.filter((v) => v !== 'email')); } }} /> <Checkbox label='Phone' value='phone' checked={value.includes('phone')} onChange={(e) => { if (e.target.checked) { setValue([...value, 'phone']); } else { setValue(value.filter((v) => v !== 'phone')); } }} /> <Checkbox label='Text message' value='text' checked={value.includes('text')} onChange={(e) => { if (e.target.checked) { setValue([...value, 'text']); } else { setValue(value.filter((v) => v !== 'text')); } }} /> </Fieldset> ); }; render(<GroupEn />)
Indeterminate checkbox
Add allowIndeterminate: true to getCheckboxProps to create a parent Checkbox that can select or clear all options.
This activates an additional state, indeterminate, next to checked and unchecked.
It is displayed with a horizontal line when one or more Checkbox are selected in the group.
It will be displayed as a regular Checkbox if everyone in the group has the same state.
React
Unable to parse html
const InTableEn = () => { const tableData = [ { id: 1, navn: 'Lise Nordmann', epost: 'lise@nordmann.no', telefon: '68051156', }, { id: 2, navn: 'Kari Nordmann', epost: 'kari@nordmann.no', telefon: '68059679', }, { id: 3, navn: 'Ola Nordmann', epost: 'ola@nordmann.no', telefon: '68055731', }, { id: 4, navn: 'Per Nordmann', epost: 'per@nordmann.no', telefon: '68059631', }, ]; const { getCheckboxProps } = useCheckboxGroup({ name: 'checkbox-table', value: ['2', '3'], }); return ( <Table> <colgroup> {/* ensure the first column only takes up the necessary space */} <col style={{ width: '1px' }} /> <col /> <col /> </colgroup> <Table.Head> <Table.Row> <Table.HeaderCell> <Checkbox aria-label='Select all' {...getCheckboxProps({ allowIndeterminate: true, value: 'all', })} /> </Table.HeaderCell> <Table.HeaderCell>Name</Table.HeaderCell> <Table.HeaderCell>E-mail</Table.HeaderCell> </Table.Row> </Table.Head> <Table.Body> {tableData.map((person) => ( <Table.Row key={person.id}> <Table.Cell> <Checkbox aria-labelledby={`checkbox-${person.id}-name`} {...getCheckboxProps(person.id.toString())} /> </Table.Cell> <Table.Cell id={`checkbox-${person.id}-name`}> {person.navn} </Table.Cell> <Table.Cell>{person.epost}</Table.Cell> </Table.Row> ))} </Table.Body> </Table> ); }; render(<InTableEn />)
HTML
Checkbox is a composite component, so in HTML you have to build it from multiple components.
We use Fieldset, Label and Input to create a Checkbox.
The code below shows how you can visually build a Checkbox in HTML, but you have to make sure that <input> is connected to <label> and description.
In Fieldset
The code below is an example of how to use Checkbox inside <fieldset>.
Note that we only show one `Checkbox' here, in practice this will often be several.
CSS variables and data attributes
Checkbox is a composite component, so we show variables and data attributes from ds-input here.
| Name | Value |
|---|---|
| --dsc-input-padding-block | var(--ds-size-2) |
| --dsc-input-padding-inline | var(--ds-size-3) |
| --dsc-input-padding | var(--dsc-input-padding-block) var(--dsc-input-padding-inline) |
| --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) |
| --dsc-input-line-height | var(--ds-line-height-md) |
| Name | Value |
|---|---|
| data-width | auto |