@wistia/ui 3.2.1-beta.864cc3a8.62d3583 → 3.2.1-beta.a4e7d6e9.3eb4984

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 CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
  /*
3
- * @license @wistia/ui v3.2.1-beta.864cc3a8.62d3583
3
+ * @license @wistia/ui v3.2.1-beta.a4e7d6e9.3eb4984
4
4
  *
5
5
  * Copyright (c) 2024-2026, Wistia, Inc. and its affiliates.
6
6
  *
@@ -1546,21 +1546,34 @@ const scopeTab = (node, event) => {
1546
1546
  const target = tabbable[event.shiftKey ? tabbable.length - 1 : 0];
1547
1547
  if (target) target.focus();
1548
1548
  };
1549
+ const ariaHiderState = /* @__PURE__ */ new WeakMap();
1549
1550
  const createAriaHider = (containerNode, selector = "body > :not(script)") => {
1550
- const rootNodes = [...document.querySelectorAll(selector)].map((node) => {
1551
- if (node.contains(containerNode)) return;
1552
- const ariaHidden = node.getAttribute("aria-hidden");
1553
- if (ariaHidden === null || ariaHidden === "false") node.setAttribute("aria-hidden", "true");
1554
- return {
1555
- node,
1556
- ariaHidden
1557
- };
1551
+ const hiddenNodes = [...document.querySelectorAll(selector)].filter((node) => !node.contains(containerNode));
1552
+ hiddenNodes.forEach((node) => {
1553
+ const state = ariaHiderState.get(node);
1554
+ if (state) {
1555
+ state.count += 1;
1556
+ return;
1557
+ }
1558
+ const originalAriaHidden = node.getAttribute("aria-hidden");
1559
+ ariaHiderState.set(node, {
1560
+ count: 1,
1561
+ originalAriaHidden
1562
+ });
1563
+ if (originalAriaHidden === null || originalAriaHidden === "false") node.setAttribute("aria-hidden", "true");
1558
1564
  });
1565
+ let isRestored = false;
1559
1566
  return () => {
1560
- rootNodes.forEach((item) => {
1561
- if (!item) return;
1562
- if (item.ariaHidden === null) item.node.removeAttribute("aria-hidden");
1563
- else item.node.setAttribute("aria-hidden", item.ariaHidden);
1567
+ if (isRestored) return;
1568
+ isRestored = true;
1569
+ hiddenNodes.forEach((node) => {
1570
+ const state = ariaHiderState.get(node);
1571
+ if (!state) return;
1572
+ state.count -= 1;
1573
+ if (state.count > 0) return;
1574
+ ariaHiderState.delete(node);
1575
+ if (state.originalAriaHidden === null) node.removeAttribute("aria-hidden");
1576
+ else node.setAttribute("aria-hidden", state.originalAriaHidden);
1564
1577
  });
1565
1578
  };
1566
1579
  };
@@ -1570,47 +1583,53 @@ const isRef = (val) => {
1570
1583
  return val !== null && typeof val === "object" && "current" in val;
1571
1584
  };
1572
1585
  const useFocusTrap = (active = true, options = {}) => {
1573
- const ref = useRef(null);
1574
- const restoreAriaRef = useRef(null);
1575
- const setRef = useCallback((node) => {
1576
- if (restoreAriaRef.current !== null) restoreAriaRef.current();
1577
- if (ref.current) {
1586
+ const { disableAriaHider = false, focusSelector } = options;
1587
+ const [node, setNode] = useState(null);
1588
+ const nodeRef = useRef(null);
1589
+ const setRef = useCallback((nextNode) => {
1590
+ nodeRef.current = nextNode ?? null;
1591
+ setNode(nextNode ?? null);
1592
+ }, []);
1593
+ useLayoutEffect(() => {
1594
+ if (!active || node === null) return;
1595
+ setupScopedFocus(node);
1596
+ markForFocusLater();
1597
+ let restoreAria = null;
1598
+ const processNode = () => {
1599
+ restoreAria = disableAriaHider ? null : createAriaHider(node);
1600
+ let focusElement = null;
1601
+ if (isNotUndefined(focusSelector)) {
1602
+ if (isRef(focusSelector)) focusElement = focusSelector.current;
1603
+ else focusElement = typeof focusSelector === "string" ? node.querySelector(focusSelector) : focusSelector;
1604
+ }
1605
+ if (!focusElement) {
1606
+ const children = [...node.querySelectorAll(FOCUSABLE_ELEMENT_SELECTORS)];
1607
+ focusElement = children.find((element) => isTabbableElement(element)) ?? children.find((element) => isFocusableElement(element)) ?? null;
1608
+ if (!focusElement && isFocusableElement(node)) focusElement = node;
1609
+ }
1610
+ if (focusElement) focusElement.focus();
1611
+ if (!focusElement) console.warn("[useFocusTrap]: Failed to find a focusable element after activating the focus trap. Make sure to include at an element that can recieve focus. As a fallback, you can also set \"tabIndex={-1}\" on the focus trap node.", node);
1612
+ };
1613
+ const timeoutId = setTimeout(() => {
1614
+ if (node.ownerDocument) processNode();
1615
+ if (!node.ownerDocument) console.warn("[useFocusTrap]: The focus trap is not part of the DOM yet, so it is unable to correctly set focus. Make sure to render the ref node.", node);
1616
+ }, 0);
1617
+ return () => {
1618
+ clearTimeout(timeoutId);
1619
+ restoreAria?.();
1578
1620
  returnFocus();
1579
1621
  teardownScopedFocus();
1580
- }
1581
- if (active && node !== null && node !== void 0) {
1582
- setupScopedFocus(node);
1583
- markForFocusLater();
1584
- const processNode = (node2) => {
1585
- restoreAriaRef.current = !(options.disableAriaHider ?? false) ? createAriaHider(node2) : null;
1586
- let focusElement = null;
1587
- if (isNotUndefined(options.focusSelector)) {
1588
- if (isRef(options.focusSelector)) focusElement = options.focusSelector.current;
1589
- else focusElement = typeof options.focusSelector === "string" ? node2.querySelector(options.focusSelector) : options.focusSelector;
1590
- }
1591
- if (!focusElement) {
1592
- const children = [...node2.querySelectorAll(FOCUSABLE_ELEMENT_SELECTORS)];
1593
- focusElement = children.find((element) => isTabbableElement(element)) ?? children.find((element) => isFocusableElement(element)) ?? null;
1594
- if (!focusElement && isFocusableElement(node2)) focusElement = node2;
1595
- }
1596
- if (focusElement) focusElement.focus();
1597
- if (!focusElement) console.warn("[useFocusTrap]: Failed to find a focusable element after activating the focus trap. Make sure to include at an element that can recieve focus. As a fallback, you can also set \"tabIndex={-1}\" on the focus trap node.", node2);
1598
- };
1599
- setTimeout(() => {
1600
- if (node.ownerDocument) processNode(node);
1601
- if (!node.ownerDocument) console.warn("[useFocusTrap]: The focus trap is not part of the DOM yet, so it is unable to correctly set focus. Make sure to render the ref node.", node);
1602
- }, 0);
1603
- ref.current = node;
1604
- } else ref.current = null;
1622
+ };
1605
1623
  }, [
1606
1624
  active,
1607
- options.focusSelector,
1608
- options.disableAriaHider
1625
+ node,
1626
+ focusSelector,
1627
+ disableAriaHider
1609
1628
  ]);
1610
1629
  useEffect(() => {
1611
1630
  if (!active) return;
1612
1631
  const handleKeyDown = (event) => {
1613
- if (event.key === "Tab" && ref.current) scopeTab(ref.current, event);
1632
+ if (event.key === "Tab" && nodeRef.current) scopeTab(nodeRef.current, event);
1614
1633
  };
1615
1634
  document.addEventListener("keydown", handleKeyDown);
1616
1635
  return () => {