-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
30 lines (26 loc) · 1.04 KB
/
index.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
if (window.ResizeObserver) {
// basic debouncer from https://codeburst.io/throttling-and-debouncing-in-javascript-b01cad5c8edf
const debounce = (func, delay) => {
let inDebounce;
return function() {
const context = this;
const args = arguments;
window.clearTimeout(inDebounce);
inDebounce = window.setTimeout(() => func.apply(context, args), delay);
};
};
const callback = entries => {
entries.forEach(({ target }) => {
const { scrollWidth, scrollHeight, clientWidth, clientHeight } = target;
if (scrollWidth > clientWidth || scrollHeight > clientHeight) {
target.tabIndex = 0;
target.setAttribute('role', 'group');
target.setAttribute('aria-label', 'Preformatted text that overflows visually');
} else {
['tabindex', 'role', 'aria-label'].forEach(attr => target.removeAttribute(attr));
}
});
};
const resizeObserver = new ResizeObserver(debounce(callback, 250));
document.querySelectorAll('pre').forEach(pre => resizeObserver.observe(pre));
}