@zag-js/aria-hidden 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +32 -5
- package/dist/index.mjs +83 -0
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -1,17 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
1
20
|
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
ariaHidden: () => ariaHidden
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
2
26
|
var elementCountMap = /* @__PURE__ */ new WeakMap();
|
|
3
27
|
function isLiveRegion(node, win) {
|
|
4
28
|
return node instanceof win.HTMLElement && node.dataset.liveAnnouncer === "true";
|
|
5
29
|
}
|
|
6
30
|
function ariaHidden(targets, rootEl) {
|
|
31
|
+
var _a;
|
|
7
32
|
const exclude = targets.filter(Boolean);
|
|
8
33
|
if (exclude.length === 0)
|
|
9
34
|
return;
|
|
10
35
|
const doc = exclude[0].ownerDocument || document;
|
|
11
|
-
const win = doc.defaultView
|
|
36
|
+
const win = (_a = doc.defaultView) != null ? _a : window;
|
|
12
37
|
const visibleNodes = new Set(exclude);
|
|
13
38
|
const hiddenNodes = /* @__PURE__ */ new Set();
|
|
14
|
-
const root = rootEl
|
|
39
|
+
const root = rootEl != null ? rootEl : doc.body;
|
|
15
40
|
const walker = doc.createTreeWalker(root, NodeFilter.SHOW_ELEMENT, {
|
|
16
41
|
acceptNode(node2) {
|
|
17
42
|
if (isLiveRegion(node2, win)) {
|
|
@@ -30,7 +55,8 @@ function ariaHidden(targets, rootEl) {
|
|
|
30
55
|
}
|
|
31
56
|
});
|
|
32
57
|
const hide = (node2) => {
|
|
33
|
-
|
|
58
|
+
var _a2;
|
|
59
|
+
let refCount = (_a2 = elementCountMap.get(node2)) != null ? _a2 : 0;
|
|
34
60
|
if (node2.getAttribute("aria-hidden") === "true" && refCount === 0) {
|
|
35
61
|
return;
|
|
36
62
|
}
|
|
@@ -76,6 +102,7 @@ function ariaHidden(targets, rootEl) {
|
|
|
76
102
|
}
|
|
77
103
|
};
|
|
78
104
|
}
|
|
79
|
-
export
|
|
105
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
106
|
+
0 && (module.exports = {
|
|
80
107
|
ariaHidden
|
|
81
|
-
};
|
|
108
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var elementCountMap = /* @__PURE__ */ new WeakMap();
|
|
3
|
+
function isLiveRegion(node, win) {
|
|
4
|
+
return node instanceof win.HTMLElement && node.dataset.liveAnnouncer === "true";
|
|
5
|
+
}
|
|
6
|
+
function ariaHidden(targets, rootEl) {
|
|
7
|
+
var _a;
|
|
8
|
+
const exclude = targets.filter(Boolean);
|
|
9
|
+
if (exclude.length === 0)
|
|
10
|
+
return;
|
|
11
|
+
const doc = exclude[0].ownerDocument || document;
|
|
12
|
+
const win = (_a = doc.defaultView) != null ? _a : window;
|
|
13
|
+
const visibleNodes = new Set(exclude);
|
|
14
|
+
const hiddenNodes = /* @__PURE__ */ new Set();
|
|
15
|
+
const root = rootEl != null ? rootEl : doc.body;
|
|
16
|
+
const walker = doc.createTreeWalker(root, NodeFilter.SHOW_ELEMENT, {
|
|
17
|
+
acceptNode(node2) {
|
|
18
|
+
if (isLiveRegion(node2, win)) {
|
|
19
|
+
visibleNodes.add(node2);
|
|
20
|
+
}
|
|
21
|
+
if (visibleNodes.has(node2) || hiddenNodes.has(node2.parentElement)) {
|
|
22
|
+
return NodeFilter.FILTER_REJECT;
|
|
23
|
+
}
|
|
24
|
+
if (node2 instanceof win.HTMLElement && node2.getAttribute("role") === "row") {
|
|
25
|
+
return NodeFilter.FILTER_SKIP;
|
|
26
|
+
}
|
|
27
|
+
if (exclude.some((target) => node2.contains(target))) {
|
|
28
|
+
return NodeFilter.FILTER_SKIP;
|
|
29
|
+
}
|
|
30
|
+
return NodeFilter.FILTER_ACCEPT;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
const hide = (node2) => {
|
|
34
|
+
var _a2;
|
|
35
|
+
let refCount = (_a2 = elementCountMap.get(node2)) != null ? _a2 : 0;
|
|
36
|
+
if (node2.getAttribute("aria-hidden") === "true" && refCount === 0) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (refCount === 0) {
|
|
40
|
+
node2.setAttribute("aria-hidden", "true");
|
|
41
|
+
}
|
|
42
|
+
hiddenNodes.add(node2);
|
|
43
|
+
elementCountMap.set(node2, refCount + 1);
|
|
44
|
+
};
|
|
45
|
+
let node = walker.nextNode();
|
|
46
|
+
while (node != null) {
|
|
47
|
+
hide(node);
|
|
48
|
+
node = walker.nextNode();
|
|
49
|
+
}
|
|
50
|
+
const observer = new win.MutationObserver((changes) => {
|
|
51
|
+
for (let change of changes) {
|
|
52
|
+
if (change.type !== "childList" || change.addedNodes.length === 0)
|
|
53
|
+
continue;
|
|
54
|
+
if (![...visibleNodes, ...hiddenNodes].some((node2) => node2.contains(change.target))) {
|
|
55
|
+
for (const node2 of change.addedNodes) {
|
|
56
|
+
if (isLiveRegion(node2, win)) {
|
|
57
|
+
visibleNodes.add(node2);
|
|
58
|
+
} else if (node2 instanceof win.Element) {
|
|
59
|
+
hide(node2);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
observer.observe(root, { childList: true, subtree: true });
|
|
66
|
+
return () => {
|
|
67
|
+
observer.disconnect();
|
|
68
|
+
for (let node2 of hiddenNodes) {
|
|
69
|
+
let count = elementCountMap.get(node2);
|
|
70
|
+
if (count === 1) {
|
|
71
|
+
node2.removeAttribute("aria-hidden");
|
|
72
|
+
elementCountMap.delete(node2);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (count !== void 0) {
|
|
76
|
+
elementCountMap.set(node2, count - 1);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export {
|
|
82
|
+
ariaHidden
|
|
83
|
+
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
|
-
"type": "module",
|
|
3
2
|
"name": "@zag-js/aria-hidden",
|
|
4
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
5
4
|
"description": "Hide targets from screen readers",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
6
8
|
"keywords": [
|
|
7
9
|
"js",
|
|
8
10
|
"utils",
|
|
@@ -11,8 +13,6 @@
|
|
|
11
13
|
"author": "Segun Adebayo <sage@adebayosegun.com>",
|
|
12
14
|
"homepage": "https://github.com/chakra-ui/zag#readme",
|
|
13
15
|
"license": "MIT",
|
|
14
|
-
"main": "dist/index.js",
|
|
15
|
-
"types": "dist/index.d.ts",
|
|
16
16
|
"repository": "https://github.com/chakra-ui/zag/tree/main/packages/utilities/aria-hidden",
|
|
17
17
|
"sideEffects": false,
|
|
18
18
|
"files": [
|
|
@@ -25,9 +25,9 @@
|
|
|
25
25
|
"url": "https://github.com/chakra-ui/zag/issues"
|
|
26
26
|
},
|
|
27
27
|
"scripts": {
|
|
28
|
-
"build-fast": "tsup src/index.ts --format=esm",
|
|
28
|
+
"build-fast": "tsup src/index.ts --format=esm,cjs",
|
|
29
29
|
"start": "pnpm build --watch",
|
|
30
|
-
"build": "tsup src/index.ts --format=esm --dts",
|
|
30
|
+
"build": "tsup src/index.ts --format=esm,cjs --dts",
|
|
31
31
|
"test": "jest --config ../../../jest.config.js --rootDir tests",
|
|
32
32
|
"lint": "eslint src --ext .ts,.tsx",
|
|
33
33
|
"test-ci": "pnpm test --ci --runInBand -u",
|