@zag-js/aria-hidden
Advanced tools
Comparing version 0.78.1 to 0.78.2
'use strict'; | ||
var ariaHidden$1 = require('aria-hidden'); | ||
// src/walk-tree-outside.ts | ||
var counterMap = /* @__PURE__ */ new WeakMap(); | ||
var uncontrolledNodes = /* @__PURE__ */ new WeakMap(); | ||
var markerMap = {}; | ||
var lockCount = 0; | ||
var unwrapHost = (node) => node && (node.host || unwrapHost(node.parentNode)); | ||
var correctTargets = (parent, targets) => targets.map((target) => { | ||
if (parent.contains(target)) return target; | ||
const correctedTarget = unwrapHost(target); | ||
if (correctedTarget && parent.contains(correctedTarget)) { | ||
return correctedTarget; | ||
} | ||
console.error("[zag-js > ariaHidden] target", target, "in not contained inside", parent, ". Doing nothing"); | ||
return null; | ||
}).filter((x) => Boolean(x)); | ||
var isIgnoredNode = (node) => { | ||
if (node.localName === "next-route-announcer") return true; | ||
if (node.localName === "script") return true; | ||
if (node.hasAttribute("aria-live")) return true; | ||
return node.matches("[data-live-announcer]"); | ||
}; | ||
var walkTreeOutside = (originalTarget, props) => { | ||
const { parentNode, markerName, controlAttribute } = props; | ||
const targets = correctTargets(parentNode, Array.isArray(originalTarget) ? originalTarget : [originalTarget]); | ||
markerMap[markerName] || (markerMap[markerName] = /* @__PURE__ */ new WeakMap()); | ||
const markerCounter = markerMap[markerName]; | ||
const hiddenNodes = []; | ||
const elementsToKeep = /* @__PURE__ */ new Set(); | ||
const elementsToStop = new Set(targets); | ||
const keep = (el) => { | ||
if (!el || elementsToKeep.has(el)) return; | ||
elementsToKeep.add(el); | ||
keep(el.parentNode); | ||
}; | ||
targets.forEach(keep); | ||
const deep = (parent) => { | ||
if (!parent || elementsToStop.has(parent)) { | ||
return; | ||
} | ||
Array.prototype.forEach.call(parent.children, (node) => { | ||
if (elementsToKeep.has(node)) { | ||
deep(node); | ||
} else { | ||
try { | ||
if (isIgnoredNode(node)) return; | ||
const attr = node.getAttribute(controlAttribute); | ||
const alreadyHidden = attr !== null && attr !== "false"; | ||
const counterValue = (counterMap.get(node) || 0) + 1; | ||
const markerValue = (markerCounter.get(node) || 0) + 1; | ||
counterMap.set(node, counterValue); | ||
markerCounter.set(node, markerValue); | ||
hiddenNodes.push(node); | ||
if (counterValue === 1 && alreadyHidden) { | ||
uncontrolledNodes.set(node, true); | ||
} | ||
if (markerValue === 1) { | ||
node.setAttribute(markerName, ""); | ||
} | ||
if (!alreadyHidden) { | ||
node.setAttribute(controlAttribute, ""); | ||
} | ||
} catch (e) { | ||
console.error("[zag-js > ariaHidden] cannot operate on ", node, e); | ||
} | ||
} | ||
}); | ||
}; | ||
deep(parentNode); | ||
elementsToKeep.clear(); | ||
lockCount++; | ||
return () => { | ||
hiddenNodes.forEach((node) => { | ||
const counterValue = counterMap.get(node) - 1; | ||
const markerValue = markerCounter.get(node) - 1; | ||
counterMap.set(node, counterValue); | ||
markerCounter.set(node, markerValue); | ||
if (!counterValue) { | ||
if (!uncontrolledNodes.has(node)) { | ||
node.removeAttribute(controlAttribute); | ||
} | ||
uncontrolledNodes.delete(node); | ||
} | ||
if (!markerValue) { | ||
node.removeAttribute(markerName); | ||
} | ||
}); | ||
lockCount--; | ||
if (!lockCount) { | ||
counterMap = /* @__PURE__ */ new WeakMap(); | ||
counterMap = /* @__PURE__ */ new WeakMap(); | ||
uncontrolledNodes = /* @__PURE__ */ new WeakMap(); | ||
markerMap = {}; | ||
} | ||
}; | ||
}; | ||
// src/aria-hidden.ts | ||
var getParentNode = (originalTarget) => { | ||
const target = Array.isArray(originalTarget) ? originalTarget[0] : originalTarget; | ||
return target.ownerDocument.body; | ||
}; | ||
var hideOthers = (originalTarget, parentNode = getParentNode(originalTarget), markerName = "data-aria-hidden") => { | ||
if (!parentNode) return; | ||
return walkTreeOutside(originalTarget, { | ||
parentNode, | ||
markerName, | ||
controlAttribute: "aria-hidden" | ||
}); | ||
}; | ||
// src/index.ts | ||
@@ -19,3 +127,3 @@ var raf = (fn) => { | ||
if (elements.length === 0) return; | ||
cleanups.push(ariaHidden$1.hideOthers(elements)); | ||
cleanups.push(hideOthers(elements)); | ||
}) | ||
@@ -22,0 +130,0 @@ ); |
{ | ||
"name": "@zag-js/aria-hidden", | ||
"version": "0.78.1", | ||
"version": "0.78.2", | ||
"description": "Hide targets from screen readers", | ||
@@ -26,5 +26,2 @@ "keywords": [ | ||
"main": "dist/index.js", | ||
"dependencies": { | ||
"aria-hidden": "1.2.4" | ||
}, | ||
"devDependencies": { | ||
@@ -31,0 +28,0 @@ "clean-package": "2.2.0" |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
12253
0
269
1
- Removedaria-hidden@1.2.4
- Removedaria-hidden@1.2.4(transitive)
- Removedtslib@2.8.1(transitive)