UNPKG

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