UNPKG

1.95 kBJavaScriptView Raw
1/**
2 * The helper functions here will make the target element as modal to screen readers, by placing aria-hidden on elements
3 * that are siblings to the target element and the target element's ancestors (because aria-hidden gets inherited).
4 * That way, all other elements on the page are hidden to the screen reader.
5 */
6import { getDocument } from './dom/getDocument';
7/**
8 * Call this on a target element to make it modal to screen readers.
9 * Returns a function that undoes the changes it made.
10 */
11export function modalize(target) {
12 var _a;
13 var affectedNodes = [];
14 var targetDocument = getDocument(target) || document;
15 // start at target, then recurse and do the same for parent, until we reach <body>
16 while (target !== targetDocument.body) {
17 // grab all siblings of current element
18 for (var _i = 0, _b = target.parentElement.children; _i < _b.length; _i++) {
19 var sibling = _b[_i];
20 // but ignore elements that are already aria-hidden
21 if (sibling !== target && ((_a = sibling.getAttribute('aria-hidden')) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== 'true') {
22 affectedNodes.push(sibling);
23 }
24 }
25 if (!target.parentElement) {
26 break;
27 }
28 target = target.parentElement;
29 }
30 // take all those elements and set aria-hidden=true on them
31 affectedNodes.forEach(function (node) {
32 node.setAttribute('aria-hidden', 'true');
33 });
34 return function () {
35 unmodalize(affectedNodes);
36 affectedNodes = []; // dispose
37 };
38}
39/**
40 * Undoes the changes that modalize() did.
41 */
42function unmodalize(affectedNodes) {
43 affectedNodes.forEach(function (node) {
44 // set instead of removing in case other components explicitly set aria-hidden and do =="true" or =="false"
45 node.setAttribute('aria-hidden', 'false');
46 });
47}
48//# sourceMappingURL=modalize.js.map
\No newline at end of file