Skip to content

Commit c17bb7b

Browse files
authored
docs(www): Alert and Details docs (#4180)
1 parent 597fff2 commit c17bb7b

File tree

14 files changed

+469
-159
lines changed

14 files changed

+469
-159
lines changed

apps/www/app/_components/css-attributes/css-attributes.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Table } from '@digdir/designsystemet-react';
1+
import { Paragraph, Table } from '@digdir/designsystemet-react';
22
import cl from 'clsx';
33
import { forwardRef } from 'react';
4+
import { useTranslation } from 'react-i18next';
45

56
type CssAttributesProps = {
67
vars: {
@@ -10,7 +11,12 @@ type CssAttributesProps = {
1011

1112
export const CssAttributes = forwardRef<HTMLTableElement, CssAttributesProps>(
1213
function CssAttributes({ vars, className, ...rest }, ref) {
13-
if (Object.keys(vars).length === 0) return null;
14+
const { t } = useTranslation();
15+
16+
if (Object.keys(vars).length === 0)
17+
return (
18+
<Paragraph>{t('components.no-relevant-data-attributes')}</Paragraph>
19+
);
1420
return (
1521
<Table
1622
className={cl('component-table', className)}
@@ -23,10 +29,15 @@ export const CssAttributes = forwardRef<HTMLTableElement, CssAttributesProps>(
2329
{...rest}
2430
ref={ref}
2531
>
32+
<caption>{t('components.data-attributes')}</caption>
2633
<Table.Head>
2734
<Table.Row>
28-
<Table.HeaderCell>Name</Table.HeaderCell>
29-
<Table.HeaderCell>Value(s)</Table.HeaderCell>
35+
<Table.HeaderCell>
36+
{t('components.css-variables.name')}
37+
</Table.HeaderCell>
38+
<Table.HeaderCell>
39+
{t('components.css-variables.value')}
40+
</Table.HeaderCell>
3041
</Table.Row>
3142
</Table.Head>
3243
<Table.Body>

apps/www/app/_components/css-variables/css-variables.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Table } from '@digdir/designsystemet-react';
22
import cl from 'clsx';
33
import { forwardRef } from 'react';
4+
import { useTranslation } from 'react-i18next';
45

56
type CssVariablesProps = {
67
vars: {
@@ -10,6 +11,8 @@ type CssVariablesProps = {
1011

1112
export const CssVariables = forwardRef<HTMLTableElement, CssVariablesProps>(
1213
function CssVariables({ vars, className, ...rest }, ref) {
14+
const { t } = useTranslation();
15+
1316
return (
1417
<Table
1518
zebra
@@ -22,10 +25,15 @@ export const CssVariables = forwardRef<HTMLTableElement, CssVariablesProps>(
2225
{...rest}
2326
ref={ref}
2427
>
28+
<caption>{t('components.css-variables.caption')}</caption>
2529
<Table.Head>
2630
<Table.Row>
27-
<Table.HeaderCell>Name</Table.HeaderCell>
28-
<Table.HeaderCell>Value</Table.HeaderCell>
31+
<Table.HeaderCell>
32+
{t('components.css-variables.name')}
33+
</Table.HeaderCell>
34+
<Table.HeaderCell>
35+
{t('components.css-variables.value')}
36+
</Table.HeaderCell>
2937
</Table.Row>
3038
</Table.Head>
3139
<Table.Body>

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { Alert, Heading, Link, Paragraph } from '@digdir/designsystemet-react';
1+
import {
2+
Alert,
3+
Button,
4+
Heading,
5+
Link,
6+
Paragraph,
7+
} from '@digdir/designsystemet-react';
8+
import { useState } from 'react';
29

310
export const Preview = () => {
411
return <Alert>En beskjed det er viktig at brukeren ser</Alert>;
@@ -118,3 +125,47 @@ export const MedLenke = () => (
118125
</Paragraph>
119126
</Alert>
120127
);
128+
129+
export const CorrectLiveRegionReact = () => {
130+
const [showAlert, setShowAlert] = useState(false);
131+
return (
132+
<>
133+
<Button
134+
data-size='sm'
135+
variant='secondary'
136+
onClick={() => setShowAlert((value) => !value)}
137+
>
138+
{showAlert ? 'Skjul varsel' : 'Handling som fører til varsel'}
139+
</Button>
140+
{/* Korrekt bruk: role="alert" ligger på elementet der varselet dukker opp */}
141+
<div role='alert'>
142+
{showAlert && (
143+
<Alert data-color='warning'>
144+
<Heading
145+
level={2}
146+
data-size='xs'
147+
style={{
148+
marginBottom: 'var(--ds-size-2)',
149+
}}
150+
>
151+
Vi klarer ikke lagre skjemaet
152+
</Heading>
153+
<Paragraph>
154+
Vi har mistet forbindelsen med serveren og får ikke lagret
155+
skjemaet. Vent litt og prøv en gang til.
156+
</Paragraph>
157+
</Alert>
158+
)}
159+
</div>
160+
</>
161+
);
162+
};
163+
164+
export const Variants = () => (
165+
<>
166+
<Alert data-color='info'>Dette er en info alert</Alert>
167+
<Alert data-color='success'>Dette er en success alert</Alert>
168+
<Alert data-color='warning'>Dette er en warning alert</Alert>
169+
<Alert data-color='danger'>Dette er en danger alert</Alert>
170+
</>
171+
);
Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,73 @@
11
<Story story="Preview" />
22

3-
## Usage
3+
4+
## Use
45

56
```tsx
6-
import { Button } from '@digdir/designsystemet-react';
7+
import { Alert } from '@digdir/designsystemet-react';
78

8-
<Button>Click me!</Button>;
9+
<Alert>You are using the Alert component!</Alert>;
910
```
1011

11-
## Code Examples
12+
### Props
13+
<ReactComponentDocs />
14+
15+
## Code examples
1216

13-
### Variants
17+
### Change alert
1418

19+
In code, you change the type of alert by changing `data-color`.
20+
The icon is controlled via CSS.
1521

1622
<Story story="Variants" />
1723

24+
### Alert in live region
25+
26+
If you want the alert to be read dynamically, you must place `Alert` in a live region.
27+
The region **should be around** the `Alert` component, not on it.
28+
29+
Out of the box, `Alert` is presented to screen reader users as regular static content. For alerts that occur dynamically, you can define a *live region* for screen readers, but be aware of the behavior this will cause.
30+
You can read more about this in the [MDN documentation on ARIA live regions](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Guides/Live_regions).
31+
32+
Note in particular that the live region — that is, the element with the attribute `role="alert"`, `role="status"`, or `aria-live` with the value `"assertive"` or `"polite"` — must exist on the page **before** the alert that is to be spoken.
33+
34+
For dynamic notifications, it is most appropriate to use `role="alert"` for critical notifications or `role="status"` for less critical ones, as described in the [pattern for system notifications](https://designsystemet.no/no/patterns/systemnotifications). See also [alert role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/alert_role) and [status role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/status_role) at MDN.
35+
36+
**Do not** combine `role="alert"` and `aria-live`, as [this causes the alert to be spoken twice in VoiceOver (mozilla.org)](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Guides/Live_regions#:~:text=adding%20both%20aria%2Dlive%20and%20role=%22alert%22%20causes%20double%20speaking%20issues%20in%20VoiceOver%20on%20iOS).
37+
38+
You **should** combine `role="status"` and `aria-live="polite"` [to maximize compatibility (mozilla.org)](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Guides/Live_regions#roles_with_implicit_live_region_attributes).
39+
40+
<Story story="CorrectLiveRegionReact" />
41+
42+
<Card data-color="info">
43+
For status messages, implemented using markup language, the following applies:
44+
45+
**The result of an action shall be encoded with one of the following:**
46+
47+
- role="status"
48+
- aria-live="polite"
49+
50+
**Waiting state shall be encoded with one of the following:**
51+
52+
- role="status"
53+
- aria-live="polite"
54+
55+
**Error shall be encoded with one of the following:**
56+
57+
- role="alert"
58+
- aria-live="polite"
59+
- aria-live="assertive"
60+
61+
**The progress of a process shall be encoded with one of the following:**
62+
63+
- role="progressbar"
64+
- the `<progress>` element
65+
- role="log"
66+
- role="status" and aria-atomic="false"
67+
- aria-live="polite" and aria-atomic="false"
68+
69+
Source: [4.1.3 Status Messages (Level AA) (uutilsynet.no)](https://www.uutilsynet.no/wcag-standarden/413-statusbeskjeder-niva-aa/152)
70+
</Card>
71+
72+
## CSS variables
73+
<CssVariables />

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

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,83 @@ Buttons let users perform actions.
22

33
<Story story="Preview" />
44

5-
## Usage
5+
## Examples
66

7-
```tsx
8-
import { Button } from '@digdir/designsystemet-react';
7+
`Alert` can be used for four different messages: Information, success, warnings and error messages.
98

10-
<Button>Click me!</Button>;
11-
```
9+
### Information
1210

13-
## Code Examples
11+
Use `info` when you want to provide the user with neutral and useful information.
1412

15-
### Variants
13+
<Story story="VariantInfo" />
1614

17-
Use `primary`, `secondary` or `tertiary` to weight the button correctly.
15+
### Success
1816

19-
<Story story="Variants" />
17+
Use `success` when you want to confirm that the user has completed a task, that the action was successful.
2018

19+
<Story story="VariantSuccess" />
20+
21+
### Warning
22+
23+
Use `warning` when you want the user to take a specific action or to warn them about something important.
24+
25+
<Story story="VariantWarning" />
26+
27+
### Error message
28+
29+
Use `danger` to inform about something that is critical or that prevents the user from moving forward.
30+
31+
<Story story="VariantDanger" />
32+
33+
### With and without heading
34+
35+
If the message is longer than a sentence, it can be useful to use a heading to highlight the most important thing. This can be done using the [Heading](/no/components/heading/overview) components. Remember to choose the correct heading level based on the space the alert has in the content structure of the page.
36+
37+
<Story story="WithHeading" />
38+
39+
If the title and description repeat the same thing, it is better to use a simple sentence without a heading.
40+
41+
<Story story="WithOnlyHeading" />
42+
43+
### With link
44+
45+
You can have a link in the `Alert` if it helps the user solve the task. But be aware that a link takes the user out of the service, so use links only when absolutely necessary, for example if you want the user to open a form or perform an important task.
46+
47+
<Story story="WithLink" />
48+
49+
## Guidelines
50+
`Alert` is used to display important messages that require attention, but not necessarily action. It can be used to inform the user about the status, changes, or problems in a solution. The message is displayed clearly and visually distinguished from the rest of the content.
51+
52+
Use the component with caution. Users can confuse alerts with advertising, and thus ignore them. If we use alerts too often, we can exacerbate this problem.
53+
54+
Make sure that `Alert` has the same look and feel across all services and products. This component should be recognizable everywhere, so we should not adjust it.
55+
56+
**Suitable for**
57+
- provide short and informative time-limited notifications
58+
- inform about errors that only affect one part of the system or a minor function
59+
- inform about connection problems or API errors that are likely to be resolved by reloading the page
60+
61+
**Not suitable for**
62+
- validate individual form elements, instead use the component's own error message (See [`ValidationMessage`](/no/components/validation-message/overview))
63+
- summarize multiple error messages in a form, instead use [`ErrorSummary`](/docs/komponenter-errorsummary--docs)
64+
- display errors that prevent all further use of the service, instead use an error page
65+
- display static information that should be displayed all the time, instead use [`Card`](/docs/komponenter-card--docs)
66+
67+
## Text
68+
69+
It is not always easy to understand the difference between the notifications, even though they have different icons and colors. Therefore, it is important that the text we write in the notification is clear and easy to understand.
70+
71+
If there is something users need to or can do to get through their task, the text should convey that. When the message is longer than a sentence, it may be a good idea to include a headline that highlights the most important points.
72+
73+
Here is a list of the types of information that a notification should contain:
74+
75+
- Tell them what happened
76+
- Example: "Couldn't connect to account."
77+
- Tell them why it happened
78+
- Example: "We were unable to connect to your account due to technical issues on our end."
79+
- Reassure the user
80+
- Example: "Your changes have been saved."
81+
- Give them a way out of the problem
82+
- Example: "If this problem occurs again, contact customer service."
83+
- Help them fix the problem themselves
84+
- Example: "Please try again."
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,49 @@
11
## Tilgjengelighet
2+
3+
### Alert i live region
4+
5+
Dersom du vil at varselet skal bli lest opp dynamisk, må du legge `Alert` i en live region.
6+
Regionen **skal være rundt** `Alert`-komponenten, ikke på den.
7+
8+
Ut av boksen blir `Alert` presentert for skjermleserbrukere som vanlig statisk innhold. For varsler som oppstår dynamisk kan du selv definere en *live region* for skjermlesere, men vær opperksom på hvilken oppførsel dette medfører.
9+
Du kan lese mer om dette i [MDN sin dokumentasjon om ARIA live regions](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Guides/Live_regions).
10+
11+
Legg spesielt merke til at live-regionen — altså elementet med attributt `role="alert"`, `role="status"` eller `aria-live` med verdi `"assertive"` eller `"polite"` — må eksistere på siden **før** varselet som skal leses opp.
12+
13+
For dynamiske varsler er det mest aktuelt å bruke `role="alert"` for kritiske varsel eller `role="status"` for mindre kritiske vasel, som beskrevet i [mønsteret for systemvarsler](https://designsystemet.no/no/patterns/systemnotifications). Se også [alert role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/alert_role) og [status role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/status_role) hos MDN.
14+
15+
**Ikke** kombiner `role="alert"` og `aria-live`, siden [dette fører at varselet leses opp dobbelt i VoiceOver (mozilla.org)](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Guides/Live_regions#:~:text=adding%20both%20aria%2Dlive%20and%20role=%22alert%22%20causes%20double%20speaking%20issues%20in%20VoiceOver%20on%20iOS).
16+
17+
Du **bør** kombinere `role="status"` og `aria-live="polite"` [for å maksimere kompatibilitet (mozilla.org)](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Guides/Live_regions#roles_with_implicit_live_region_attributes).
18+
19+
<Story story="CorrectLiveRegionReact" />
20+
21+
<Card data-color="info">
22+
For statusbeskjeder, som implementeres ved hjelp av oppmerkingsspråk, gjelder følgende:
23+
24+
**Resultatet av en handling skal kodes med én av følgende:**
25+
26+
- role="status"
27+
- aria-live="polite"
28+
29+
**Ventetilstand skal kodes med én av følgende:**
30+
31+
- role="status"
32+
- aria-live="polite"
33+
34+
**Feil skal kodes med én av følgende:**
35+
36+
- role="alert"
37+
- aria-live="polite"
38+
- aria-live="assertive"
39+
40+
**Fremdriften i en prosess skal kodes med én av følgende:**
41+
42+
- role="progressbar"
43+
- elementet `<progress>`
44+
- role="log"
45+
- role="status" og aria-atomic="false"
46+
- aria-live="polite" og aria-atomic="false"
47+
48+
Kilde: [4.1.3 Statusbeskjeder (Nivå AA) (uutilsynet.no)](https://www.uutilsynet.no/wcag-standarden/413-statusbeskjeder-niva-aa/152)
49+
</Card>

0 commit comments

Comments
 (0)