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
const Preview = () => { return ( <Checkbox label='Checkbox label' description='Description' value='value' /> ); }; render(<Preview />)
Usage
Examples
Grouping
Use Fieldset and useCheckboxGroup for grouping multiple options.
React
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
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 | 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 |