-
Notifications
You must be signed in to change notification settings - Fork 63
/
aria-live-region.js
81 lines (69 loc) · 1.83 KB
/
aria-live-region.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
(function (MemoryGame) {
/**
* Aria live region for reading to screen reader.
*
* @class H5P.MemoryGame.Popup
*/
MemoryGame.AriaLiveRegion = function () {
let readText, timeout = null;
// Build dom with defaults
const dom = document.createElement('div');
dom.classList.add('h5p-memory-aria-live-region');
dom.setAttribute('aria-live', 'polite');
dom.style.height = '1px';
dom.style.overflow = 'hidden';
dom.style.position = 'absolute';
dom.style.textIndent = '1px';
dom.style.top = '-1px';
dom.style.width = '1px';
/**
* Get DOM of aria live region.
*
* @returns {HTMLElement} DOM of aria live region.
*/
this.getDOM = function () {
return dom;
}
/**
* Set class if default CSS values do not suffice.
*
* @param {string} className Class name to set. Add CSS elsewhere.
*/
this.setClass = function(className) {
if (typeof className !== 'string') {
return;
}
// Remove default values
dom.style.height = '';
dom.style.overflow = '';
dom.style.position = '';
dom.style.textIndent = '';
dom.style.top = '';
dom.style.width = '';
dom.classList = className;
}
/**
* Read text via aria live region.
*
* @param {string} text Text to read.
*/
this.read = function (text) {
if (readText) {
const lastChar = readText
.substring(readText.length - 1);
readText =
[`${readText}${lastChar === '.' ? '' : '.'}`, text]
.join(' ');
}
else {
readText = text;
}
dom.innerText = readText;
window.clearTimeout(timeout);
timeout = window.setTimeout(function () {
readText = null;
dom.innerText = '';
}, 100);
}
}
})(H5P.MemoryGame);