Skip to content

Commit 896a746

Browse files
Alert component: Add role, id, and hidden attributes (#1914)
1 parent 4384ecc commit 896a746

File tree

4 files changed

+157
-13
lines changed

4 files changed

+157
-13
lines changed

.changeset/thick-sloths-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cloudfour/patterns': minor
3+
---
4+
5+
Updates the Alert component by adding `role`, `id` and `hidden` template properties

src/components/alert/alert.stories.mdx

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,72 @@
1-
import { Canvas, Meta, Story } from '@storybook/addon-docs';
1+
import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs';
22
import template from './alert.twig';
33

44
<Meta
55
title="Components/Alert"
66
argTypes={{
7-
message: { type: { name: 'string' } },
8-
dismissable: { type: { name: 'boolean' } },
7+
class: {
8+
type: 'string',
9+
description: 'CSS class(es) to append to the root element.',
10+
},
11+
message: {
12+
type: 'string',
13+
description: 'The message within the alert.',
14+
table: {
15+
defaultValue: { summary: 'Hello world!!!' },
16+
},
17+
},
18+
dismissable: {
19+
type: 'boolean',
20+
description: 'Adds a close button to the right of the message.',
21+
table: {
22+
defaultValue: { summary: false },
23+
},
24+
},
25+
icon: {
26+
options: [
27+
'',
28+
'bell',
29+
'check',
30+
'cloud-slash',
31+
'brands/github',
32+
'brands/twitter',
33+
],
34+
type: 'string',
35+
description:
36+
'An option to add one of our [icons](/?path=/docs/design-icons--page).',
37+
control: {
38+
type: 'select',
39+
},
40+
},
41+
floating: {
42+
type: 'boolean',
43+
description: 'Adds a light border and shadow for a floating effect.',
44+
table: {
45+
defaultValue: { summary: false },
46+
},
47+
},
48+
tag_name: {
49+
type: 'string',
50+
description: 'The root element',
51+
table: {
52+
defaultValue: { summary: 'div' },
53+
},
54+
},
55+
id: {
56+
type: 'string',
57+
description: 'Adds an `id` attribute to the root element',
58+
},
59+
hidden: {
60+
type: 'boolean',
61+
description: 'Adds a `hidden` attribute to the root element.',
62+
table: {
63+
defaultValue: { summary: false },
64+
},
65+
},
66+
role: {
67+
type: 'string',
68+
description: 'Adds a `role` attribute on the root element.',
69+
},
970
}}
1071
/>
1172

@@ -30,6 +91,8 @@ This component is still a work in progress. The following features are not yet i
3091
</Story>
3192
</Canvas>
3293

94+
<ArgsTable story="Basic" />
95+
3396
## Dismissable
3497

3598
When `dismissable` is `true`, a close button will display to the right of the message.
@@ -119,15 +182,6 @@ When more contrast is desired, you may also attach a theme class to the the floa
119182
</Story>
120183
</Canvas>
121184

122-
## Template Properties
123-
124-
- `class` (string)
125-
- `icon` (string)
126-
- `dismissable` (boolean, default `false`)
127-
- `floating` (boolean, default `false`)
128-
- `message` (string, default `'Hello world!'`)
129-
- `tag_name` (string, default `'div'`)
130-
131185
## Template Blocks
132186

133187
- `content`

src/components/alert/alert.test.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import path from 'path';
2+
3+
import type { ElementHandle } from 'pleasantest';
4+
import { getAccessibilityTree, withBrowser } from 'pleasantest';
5+
6+
import { loadTwigTemplate } from '../../../test-utils.js';
7+
8+
/** Helper to load the Twig template file */
9+
const template = loadTwigTemplate(path.join(__dirname, './alert.twig'));
10+
11+
describe('Alert component', () => {
12+
test(
13+
'should have no role by default',
14+
withBrowser(async ({ utils, page }) => {
15+
await utils.injectHTML(
16+
await template({
17+
message: '¡Hola!',
18+
})
19+
);
20+
21+
const body = await page.evaluateHandle<ElementHandle>(
22+
() => document.body
23+
);
24+
expect(await getAccessibilityTree(body)).toMatchInlineSnapshot(
25+
`text "¡Hola!"`
26+
);
27+
})
28+
);
29+
30+
test(
31+
'should be able to set a role',
32+
withBrowser(async ({ utils, page }) => {
33+
await utils.injectHTML(
34+
await template({
35+
role: 'status',
36+
})
37+
);
38+
39+
const body = await page.evaluateHandle<ElementHandle>(
40+
() => document.body
41+
);
42+
expect(await getAccessibilityTree(body)).toMatchInlineSnapshot(`
43+
status
44+
text "Hello world!"
45+
`);
46+
})
47+
);
48+
49+
test(
50+
'should respect `hidden` template property',
51+
withBrowser(async ({ utils, page }) => {
52+
await utils.injectHTML(
53+
await template({
54+
hidden: true,
55+
})
56+
);
57+
58+
const body = await page.evaluateHandle<ElementHandle>(
59+
() => document.body
60+
);
61+
// Nothing to see if the `hidden` attribute is added to the alert
62+
expect(await getAccessibilityTree(body)).toMatchInlineSnapshot(``);
63+
})
64+
);
65+
66+
test(
67+
'should set an `id` attribute',
68+
withBrowser(async ({ utils, screen }) => {
69+
await utils.injectHTML(
70+
await template({
71+
id: 'my-id',
72+
role: 'status',
73+
})
74+
);
75+
76+
const alert = await screen.getByRole('status');
77+
await expect(alert).toHaveAttribute('id', 'my-id');
78+
})
79+
);
80+
});

src/components/alert/alert.twig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
{% endblock %}
1111
{% endset %}
1212

13-
<{{ tag_name }} class="c-alert{% if floating %} c-alert--floating{% endif %}{% if class %} {{ class }}{% endif %}">
13+
<{{ tag_name }}
14+
{% if id %}id="{{id}}"{% endif %}
15+
class="c-alert{% if floating %} c-alert--floating{% endif %}{% if class %} {{ class }}{% endif %}"
16+
{% if hidden %}hidden{% endif %}
17+
{% if role %}role="{{ role }}"{% endif %}
18+
>
1419
<div class="c-alert__inner">
1520
{% if _icon_block|trim is not empty %}
1621
<span class="c-alert__extra">

0 commit comments

Comments
 (0)