Skip to content

Commit a5eb5d3

Browse files
mrosvikmimarz
andauthored
docs(checkbox): web-first (#4806)
Co-authored-by: Michael Marszalek <mimarz@gmail.com>
1 parent 6adb0d2 commit a5eb5d3

6 files changed

Lines changed: 132 additions & 216 deletions

File tree

apps/www/app/content/components/checkbox/checkbox.stories.tsx

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -355,15 +355,14 @@ export const Outline = () => {
355355

356356
return (
357357
<Fieldset>
358-
<Fieldset.Legend>
359-
Hvordan vil du helst at vi skal kontakte deg?
360-
</Fieldset.Legend>
358+
<Fieldset.Legend>Hvordan ønsker å bli kontaktet?</Fieldset.Legend>
361359
<Fieldset.Description>
362-
Velg alle alternativene som er relevante for deg.
360+
Velg hvordan du vi motta viktig informasjon om saken din.
363361
</Fieldset.Description>
364362
<Checkbox
365363
label='E-post'
366364
value='epost'
365+
description='Vi bruker e-postadressen du har oppgitt tidligere (********)'
367366
variant='outline'
368367
checked={value.includes('epost')}
369368
onChange={(e) => {
@@ -374,22 +373,10 @@ export const Outline = () => {
374373
}
375374
}}
376375
/>
377-
<Checkbox
378-
label='Telefon'
379-
value='telefon'
380-
variant='outline'
381-
checked={value.includes('telefon')}
382-
onChange={(e) => {
383-
if (e.target.checked) {
384-
setValue([...value, 'telefon']);
385-
} else {
386-
setValue(value.filter((v) => v !== 'telefon'));
387-
}
388-
}}
389-
/>
390376
<Checkbox
391377
label='SMS'
392378
value='sms'
379+
description='Vi bruker telefonnummeret du har oppgitt tidligere (*******)'
393380
variant='outline'
394381
checked={value.includes('sms')}
395382
onChange={(e) => {
@@ -405,17 +392,18 @@ export const Outline = () => {
405392
};
406393

407394
export const OutlineEn = () => {
408-
const [value, setValue] = useState<string[]>(['epost']);
395+
const [value, setValue] = useState<string[]>(['email']);
409396

410397
return (
411398
<Fieldset>
412-
<Fieldset.Legend>How would you prefer us to contact you?</Fieldset.Legend>
399+
<Fieldset.Legend>How would you like to be contacted?</Fieldset.Legend>
413400
<Fieldset.Description>
414-
Select all the options that are relevant to you.
401+
Choose how you want to receive important information about your case.
415402
</Fieldset.Description>
416403
<Checkbox
417404
label='E-mail'
418405
value='email'
406+
description='We use the e-mail address you have provided earlier (********)'
419407
variant='outline'
420408
checked={value.includes('email')}
421409
onChange={(e) => {
@@ -427,28 +415,16 @@ export const OutlineEn = () => {
427415
}}
428416
/>
429417
<Checkbox
430-
label='Phone'
431-
value='phone'
432-
variant='outline'
433-
checked={value.includes('phone')}
434-
onChange={(e) => {
435-
if (e.target.checked) {
436-
setValue([...value, 'phone']);
437-
} else {
438-
setValue(value.filter((v) => v !== 'phone'));
439-
}
440-
}}
441-
/>
442-
<Checkbox
443-
label='Text message'
444-
value='text'
418+
label='SMS'
419+
value='sms'
420+
description='We use the phone number you have provided earlier (*******)'
445421
variant='outline'
446-
checked={value.includes('text')}
422+
checked={value.includes('sms')}
447423
onChange={(e) => {
448424
if (e.target.checked) {
449-
setValue([...value, 'text']);
425+
setValue([...value, 'sms']);
450426
} else {
451-
setValue(value.filter((v) => v !== 'text'));
427+
setValue(value.filter((v) => v !== 'sms'));
452428
}
453429
}}
454430
/>
Lines changed: 33 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,54 @@
1-
<Story story="Preview" />
1+
<Story story="Preview" defaultOpen/>
22

3-
<ReactComponentDocs />
43

5-
## Usage
4+
## CSS variables and data attributes
5+
Sizes are controlled with [`data-size`](/en/fundamentals/code/sizes) and colors with [`data-color`](/en/fundamentals/code/colors). The component will inherit these from the nearest parent.
66

7-
```tsx
8-
import { Checkbox } from '@digdir/designsystemet-react';
7+
Checkbox is `ds-input` with `type="checkbox"`, so variables and data attributes that apply to [input](/en/components/docs/input/overview) also apply to checkbox.
8+
9+
<CssAttributes />
10+
11+
<CssVariables />
912

10-
<Checkbox label="Checkbox label" value="value" />
11-
```
1213

1314
## Examples
1415

1516
### Grouping
1617

17-
Use [`Fieldset`](/en/components/docs/fieldset/code) and `useCheckboxGroup` for grouping multiple options.
18+
Use [fieldset](/en/components/docs/fieldset/code) for grouping multiple choices.
1819

1920
<Story story="GroupEn" />
2021

22+
2123
### Outline
22-
`variant="outline"` adds extra padding and a border around the selection, increasing the clickable area.
24+
`data-variant="outline"` adds extra padding and a border around the selection to create a larger clickable area.
2325

2426
<Story story="OutlineEn" />
2527

26-
### Indeterminate checkbox
2728

28-
Add `allowIndeterminate: true` to `getCheckboxProps` to create a parent `Checkbox` that can select or clear all options.
29-
This activates an additional state, `indeterminate`, next to `checked` and `unchecked`.
30-
It is displayed with a horizontal line when one or more `Checkbox` are selected in the group.
31-
It will be displayed as a regular `Checkbox` if everyone in the group has the same state.
32-
33-
<Story story="InTableEn" />
34-
35-
## HTML
36-
37-
`Checkbox` is a composite component, so in HTML you have to build it from multiple components.
38-
We use [`Fieldset`](/en/components/docs/fieldset/code), [`Label`](/en/components/docs/label/code) and [`Input`](/en/components/docs/input/code) to create a `Checkbox`.
39-
40-
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.
41-
42-
```html
43-
<div class="ds-field">
44-
<input
45-
class="ds-input"
46-
type="checkbox"
47-
value="value"
48-
id="check-input"
49-
aria-describedby="description"
50-
>
51-
<label
52-
class="ds-label"
53-
data-weight="regular"
54-
for="check-input">Checkbox label</label>
55-
<div data-field="description" id="description">Description</div>
56-
</div>
57-
```
29+
## React
30+
31+
In React, building a checkbox is simpler, because we have a ready-made component that handles most of the logic and state management for us.
32+
33+
`Checkbox` is a composite component that uses [field](/en/components/docs/field/overview), [label](/en/components/docs/label/overview), [validation message](/en/components/docs/validation-message/overview), and [input](/en/components/docs/input/overview) to create a checkbox.
5834

59-
### In Fieldset
60-
61-
The code below is an example of how to use `Checkbox` inside `<fieldset>`.
62-
Note that we only show one `Checkbox' here, in practice this will often be several.
63-
64-
```html
65-
<fieldset class="ds-fieldset">
66-
<legend class="ds-label" data-weight="medium">How would you prefer us to contact you?</legend>
67-
<p class="ds-paragraph" data-variant="default">Select all the options that are relevant to you.</p>
68-
<div class="ds-field">
69-
<input
70-
class="ds-input"
71-
type="checkbox"
72-
value="email"
73-
id="check-input"
74-
>
75-
<label
76-
class="ds-label"
77-
data-weight="regular"
78-
for="check-input"
79-
>Email</label>
80-
</div>
81-
</fieldset>
35+
We also have a dedicated [useCheckboxGroup](/en/components/utilities/use-checkbox-group) React hook to make it easier to manage a group of Checkbox components.
36+
37+
```tsx
38+
import { Checkbox } from '@digdir/designsystemet-react';
39+
40+
<Checkbox label="Checkbox label" value="value" />
8241
```
8342

84-
## CSS variables and data attributes
85-
Checkbox is a composite component, so we show variables and data attributes from `ds-input` here.
86-
<CssVariables />
43+
<ReactComponentDocs />
8744

88-
<CssAttributes />
45+
46+
### Indeterminate checkbox
47+
48+
Use [useCheckboxGroup](/en/components/utilities/use-checkbox-group) to create a parent checkbox that can select or clear all options in the group.
49+
50+
This activates an additional state, `indeterminate`, alongside `checked` and `unchecked`. It is shown as a horizontal line when one or more checkboxes are selected in the group. It is shown as a regular checkbox when all checkboxes in the group share the same state.
51+
52+
This is enabled by setting `allowIndeterminate: true` in `getCheckboxProps`.
53+
54+
<Story story="InTableEn" language="react" />

apps/www/app/content/components/checkbox/en/overview.mdx

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,66 @@ search_terms: selection, multiselect, option, options, check, tick
44

55
<Story story="Preview" />
66

7-
**Use `Checkbox` when**
7+
**Use checkbox when**
88
- the user should be able to select one or more options within the same context
99
- the user needs to accept terms and conditions
1010

11-
**Avoid using `Checkbox` when**
12-
- the user should choose only one option; use a [`Radio`](/en/components/docs/radio/overview) instead
13-
- the user must choose from more than seven options; use a [`Select`](/en/components/docs/select/overview) or [`Suggestion`](/en/components/docs/suggestion/overview) instead
14-
- something should be turned on or off; use a [`Switch`](/en/components/docs/switch/overview) instead
11+
**Avoid checkbox when**
12+
- the user should choose only one option; use a [radio](/en/components/docs/radio/overview) instead
13+
- the user must choose from more than seven options; use a [select](/en/components/docs/select/overview) or [suggestion](/en/components/docs/suggestion/overview) instead
14+
- something should be turned on or off; use a [switch](/en/components/docs/switch/overview) instead
1515

1616
## Example
1717

18-
### Confirmation with `Checkbox`
18+
### Confirmation with checkbox
1919

20-
If the user must confirm something rather than choose between alternatives, a single `Checkbox` can stand alone. For example, when the user needs to confirm that something is correct or give consent to terms and conditions.
20+
If the user must confirm something rather than choose between alternatives, a single checkbox can stand alone. For example, when the user needs to confirm that something is correct or give consent to terms and conditions.
2121

22-
A `Checkbox` for consent must always require an active action, and you should never preselect the box. This ensures that the consent is given voluntarily.
22+
A checkbox for consent must always require an active action, and you should never preselect the box. This ensures that the consent is given voluntarily.
2323

2424
<Story story="OneOption" />
2525

2626
### Grouping
2727

28-
Most of the time, `Checkbox` is used in groups where the user can select more than one option. The `<fieldset>` around the group should have a legend that explains what the options relate to.
28+
Most of the time, checkbox is used in groups where the user can select more than one option.
2929

3030
<Story story="GroupEn" />
3131

32-
### Outline
33-
`variant="outline"` adds extra padding and a border around the selection, increasing the clickable area.
32+
### With error
33+
When checkboxes are used in groups, the error message should be shown collectively for the entire group.
3434

35-
<Story story="OutlineEn" />
35+
<Story story="WithError" />
3636

37-
### With error
37+
### Outline
38+
In some cases, it may be appropriate to emphasise options by using a larger click area around the checkbox.
3839

39-
When `Checkbox` is used in groups, the error message should be placed on the `Fieldset`, so it applies to the entire group.
40+
This can for example be appropriate when:
41+
- the option represents a clear decision point that should stand out in the interface
42+
- related information (such as descriptions and additional details) should be perceived as a single, coherent interaction
43+
- standard checkboxes would result in a visually weak layout
4044

41-
<Story story="WithError" />
45+
<Story story="OutlineEn" />
4246

4347
### Read-only checkbox
4448

45-
`Checkbox` supports the `readOnly` attribute to make the field non-editable and gives a visual indication that distinguishes it from editable fields. Although they cannot be changed, fields with `readOnly` are still part of the tab order, and the information is included when the form is submitted.
49+
Checkbox supports the `readOnly` attribute to make the field non-editable and gives a visual indication that distinguishes it from editable fields. Although they cannot be changed, fields with `readOnly` are still part of the tab order, and the information is included when the form is submitted.
4650

4751
We avoid using `readOnly` whenever possible because such fields can be confusing. Not all users will understand why they cannot change the content.
4852

4953
<Story story="ReadOnly" />
5054

5155
### Disabled
5256

53-
We should avoid disabling (`disabled`) `Checkbox`, because it can be difficult to perceive.
54-
Some users will not understand what the option means or why it is not clickable. If a `Checkbox` or group of `Checkbox` options is not relevant, it is usually better to remove them rather than disable them. If possible, give users an explanation of why options are unavailable.
57+
We should avoid disabling (`disabled`) checkbox, because it can be difficult to perceive.
58+
Some users will not understand what the option means or why it is not clickable. If a checkbox or group of checkbox options is not relevant, it is usually better to remove them rather than disable them. If possible, give users an explanation of why options are unavailable.
5559

5660
Nav has a helpful explanation of [why disabled states are problematic (nav.no)](https://aksel.nav.no/god-praksis/artikler/deaktiverte-tilstander) and which alternatives exist.
5761

5862
<Story story="Disabled" />
5963

6064
## Guidelines
6165

62-
Use `Checkbox` when the user can select one or more options.
66+
Use checkbox when the user can select one or more options.
6367

6468
### Sort the options
6569

@@ -71,10 +75,10 @@ Preferably place the response options vertically. This makes it easier to scan t
7175

7276
<div className="dodont-row">
7377
<DoDont story="DoPlacement">
74-
Place `Checkbox` vertically for improved readability.
78+
Place checkbox vertically for improved readability.
7579
</DoDont>
7680
<DoDont story="DontPlacement">
77-
Avoid placing `Checkbox` horizontally, as it reduces readability.
81+
Avoid placing checkbox horizontally, as it reduces readability.
7882
</DoDont>
7983
</div>
8084

apps/www/app/content/components/checkbox/metadata.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"no": {
33
"title": "Checkbox",
4-
"subtitle": "`Checkbox` gir brukerne mulighet til å velge ett eller flere alternativer. Den kan også brukes i tilfeller der brukeren skal bekrefte noe. "
4+
"subtitle": "Checkbox gir brukerne mulighet til å velge ett eller flere alternativer. Den kan også brukes i tilfeller der brukeren skal bekrefte noe. "
55
},
66
"en": {
77
"title": "Checkbox",
8-
"subtitle": "`Checkbox` allows users to select one or more options. It can also be used in situations where the user needs to confirm something."
8+
"subtitle": "Checkbox allows users to select one or more options. It can also be used in situations where the user needs to confirm something."
99
},
1010
"image": "Checkbox.svg",
1111
"cssFile": "input.css"

0 commit comments

Comments
 (0)