Skip to content

Commit 17d7339

Browse files
VSrimukhSreemukh
andauthored
feat(curriculum): update aria-live lesson with interactive code examples (freeCodeCamp#63591)
Co-authored-by: Srimukh <vsreemukh4@gmail.com>
1 parent 37ce12a commit 17d7339

1 file changed

Lines changed: 115 additions & 3 deletions

File tree

curriculum/challenges/english/blocks/lecture-understanding-aria-expanded-aria-live-and-common-aria-states/672a54dff9dc439089f1a219.md

Lines changed: 115 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ challengeType: 19
55
dashedName: what-is-the-aria-live-attribute
66
---
77

8-
# --description--
8+
# --interactive--
99

1010
The `aria-live` attribute creates a live region on your page which can be used to notify screen reader users of dynamic content changes in the live region as they occur.
1111

@@ -19,22 +19,134 @@ If you set `aria-live` to the value `assertive` that means that the update is ve
1919

2020
This means that the screen reader will interrupt any announcement it is currently making to announce the content change in the live region. Such interruptions can be extremely disruptive, so the `assertive` value should only be used for time-sensitive or critical notifications.
2121

22+
:::interactive_editor
23+
2224
```html
23-
<div aria-live="assertive">
25+
<link rel="stylesheet" href="styles.css">
26+
27+
<div class="session-warning" aria-live="assertive">
2428
<p>Your session will expire in 30 seconds.</p>
2529
</div>
30+
31+
<script src="index.js"></script>
32+
33+
```
34+
35+
```css
36+
.session-warning {
37+
background-color: #ffcc00;
38+
color: #000;
39+
font-family: system-ui, sans-serif;
40+
font-weight: 500;
41+
padding: 1em 1.5em;
42+
border-radius: 6px;
43+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
44+
opacity: 0;
45+
transform: translateY(20px);
46+
transition: opacity 0.4s ease, transform 0.4s ease;
47+
}
48+
49+
.session-warning.visible {
50+
opacity: 1;
51+
transform: translateY(0);
52+
}
53+
54+
.session-warning.fade-out {
55+
opacity: 0;
56+
transform: translateY(10px);
57+
}
58+
2659
```
2760

61+
```js
62+
document.addEventListener("DOMContentLoaded", () => {
63+
const warning = document.querySelector(".session-warning");
64+
65+
setTimeout(() => {
66+
warning.classList.add("visible");
67+
}, 100);
68+
69+
setTimeout(() => {
70+
warning.classList.add("fade-out");
71+
}, 8000);
72+
73+
warning.addEventListener("transitionend", () => {
74+
if (warning.classList.contains("fade-out")) {
75+
warning.remove();
76+
}
77+
});
78+
});
79+
80+
```
81+
82+
:::
83+
2884
The next value in order of priority is `polite`.
2985

3086
This value means that the update is not urgent, so the screen reader can wait until any current announcement is finished or until the user stops typing before announcing the update. Most live regions will use the `polite` value.
3187

88+
:::interactive_editor
89+
3290
```html
33-
<div aria-live="polite">
91+
<link rel="stylesheet" href="styles.css">
92+
93+
<div class="upload-success" aria-live="polite">
3494
<p>File successfully uploaded</p>
3595
</div>
96+
97+
<script src="index.js"></script>
98+
99+
```
100+
101+
```css
102+
.upload-success {
103+
background-color: #4caf50;
104+
color: #fff;
105+
font-family: system-ui, sans-serif;
106+
font-weight: 500;
107+
padding: 1em 1.5em;
108+
border-radius: 6px;
109+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
110+
opacity: 0;
111+
transform: translateY(-10px);
112+
transition: opacity 0.4s ease, transform 0.4s ease;
113+
}
114+
115+
.upload-success.visible {
116+
opacity: 1;
117+
transform: translateY(0);
118+
}
119+
120+
.upload-success.fade-out {
121+
opacity: 0;
122+
transform: translateY(-10px);
123+
}
124+
36125
```
37126

127+
```js
128+
document.addEventListener("DOMContentLoaded", () => {
129+
const success = document.querySelector(".upload-success");
130+
131+
setTimeout(() => {
132+
success.classList.add("visible");
133+
}, 100);
134+
135+
setTimeout(() => {
136+
success.classList.add("fade-out");
137+
}, 8000);
138+
139+
success.addEventListener("transitionend", () => {
140+
if (success.classList.contains("fade-out")) {
141+
success.remove();
142+
}
143+
});
144+
});
145+
146+
```
147+
148+
:::
149+
38150
The lowest priority value for `aria-live` is `off` which means that the update will not be announced unless the content is in an element that currently has keyboard focus. Realistically, this value is almost never used as the use case is very narrow and it is not implemented consistently (if at all) across screen readers. If you need live regions, plan on using `polite` for everything except critical messages that need `assertive`.
39151

40152
It's also important to note that the `aria-live` attribute is not required if the updated information is contained in an element with an ARIA role of `alert`, `log`, `marquee`, `status`, or `timer`, as these roles already have default `aria-live` values. But the default value can be changed by explicitly setting `aria-live` on the element.

0 commit comments

Comments
 (0)