@absolutejs/beacon 0.6.2 → 0.6.3

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
@@ -2690,6 +2690,24 @@ var createBeacon = (options) => {
2690
2690
  const reportedFocusLoss = new Set;
2691
2691
  const reportedModalEscapes = new WeakSet;
2692
2692
  const modalTimers = new Set;
2693
+ const modalVisibility = new WeakMap;
2694
+ const knownModals = new Set;
2695
+ const isRenderedModal = (modal) => {
2696
+ if (!modal.isConnected || !modal.matches(MODAL_SELECTOR))
2697
+ return false;
2698
+ let current = modal;
2699
+ while (current !== null) {
2700
+ if (current.hasAttribute("hidden") || current.hasAttribute("inert") || current.getAttribute("aria-hidden")?.toLowerCase() === "true") {
2701
+ return false;
2702
+ }
2703
+ const style = getComputedStyle(current);
2704
+ if (style.display === "none" || style.visibility === "hidden" || style.visibility === "collapse" || style.contentVisibility === "hidden") {
2705
+ return false;
2706
+ }
2707
+ current = current.parentElement;
2708
+ }
2709
+ return true;
2710
+ };
2693
2711
  const reportModalEscape = (modal, active, reason) => {
2694
2712
  if (signals.modalFocusEscape === false || reportedModalEscapes.has(modal) || document.visibilityState === "hidden") {
2695
2713
  return;
@@ -2703,11 +2721,11 @@ var createBeacon = (options) => {
2703
2721
  target: active === null ? "none" : describeElement(active)
2704
2722
  });
2705
2723
  };
2706
- const openModals = () => Array.from(document.querySelectorAll(MODAL_SELECTOR));
2724
+ const openModals = () => Array.from(document.querySelectorAll(MODAL_SELECTOR)).filter(isRenderedModal);
2707
2725
  const checkInitialModalFocus = (modal) => {
2708
2726
  const timer2 = setTimeout(() => {
2709
2727
  modalTimers.delete(timer2);
2710
- if (!modal.isConnected || !modal.matches(MODAL_SELECTOR))
2728
+ if (!isRenderedModal(modal))
2711
2729
  return;
2712
2730
  const active = document.activeElement instanceof Element ? document.activeElement : null;
2713
2731
  if (active !== null && modal.contains(active))
@@ -2716,6 +2734,21 @@ var createBeacon = (options) => {
2716
2734
  }, MODAL_FOCUS_SETTLE_MS);
2717
2735
  modalTimers.add(timer2);
2718
2736
  };
2737
+ const updateModalVisibility = (modal) => {
2738
+ knownModals.add(modal);
2739
+ const visible = isRenderedModal(modal);
2740
+ const wasVisible = modalVisibility.get(modal) === true;
2741
+ modalVisibility.set(modal, visible);
2742
+ if (visible && !wasVisible)
2743
+ checkInitialModalFocus(modal);
2744
+ };
2745
+ const registerModals = (root) => {
2746
+ if (root.matches(MODAL_SELECTOR))
2747
+ updateModalVisibility(root);
2748
+ for (const modal of Array.from(root.querySelectorAll(MODAL_SELECTOR))) {
2749
+ updateModalVisibility(modal);
2750
+ }
2751
+ };
2719
2752
  const onFocusIn = (event) => {
2720
2753
  const target = event.target;
2721
2754
  if (signals.modalFocusEscape !== false && target instanceof Element) {
@@ -2746,26 +2779,39 @@ var createBeacon = (options) => {
2746
2779
  document.addEventListener("focusout", onFocusOut, true);
2747
2780
  let modalObserver;
2748
2781
  if (signals.modalFocusEscape !== false && typeof MutationObserver !== "undefined") {
2749
- for (const modal of openModals())
2750
- checkInitialModalFocus(modal);
2782
+ for (const modal of Array.from(document.querySelectorAll(MODAL_SELECTOR))) {
2783
+ updateModalVisibility(modal);
2784
+ }
2751
2785
  modalObserver = new MutationObserver((records) => {
2752
2786
  for (const record of records) {
2753
- if (record.type === "attributes" && record.target instanceof Element && record.target.matches(MODAL_SELECTOR)) {
2754
- checkInitialModalFocus(record.target);
2787
+ if (record.type === "attributes" && record.target instanceof Element) {
2788
+ const target = record.target;
2789
+ if (knownModals.has(target) || target.matches(MODAL_SELECTOR)) {
2790
+ updateModalVisibility(target);
2791
+ }
2792
+ for (const modal of knownModals) {
2793
+ if (modal !== target && target.contains(modal)) {
2794
+ updateModalVisibility(modal);
2795
+ }
2796
+ }
2755
2797
  }
2756
2798
  for (const added of Array.from(record.addedNodes)) {
2757
2799
  if (!(added instanceof Element))
2758
2800
  continue;
2759
- if (added.matches(MODAL_SELECTOR))
2760
- checkInitialModalFocus(added);
2761
- for (const modal of Array.from(added.querySelectorAll(MODAL_SELECTOR))) {
2762
- checkInitialModalFocus(modal);
2763
- }
2801
+ registerModals(added);
2764
2802
  }
2765
2803
  }
2766
2804
  });
2767
2805
  modalObserver.observe(document.documentElement, {
2768
- attributeFilter: ["aria-modal", "open"],
2806
+ attributeFilter: [
2807
+ "aria-hidden",
2808
+ "aria-modal",
2809
+ "class",
2810
+ "hidden",
2811
+ "inert",
2812
+ "open",
2813
+ "style"
2814
+ ],
2769
2815
  attributes: true,
2770
2816
  childList: true,
2771
2817
  subtree: true
@@ -2778,6 +2824,7 @@ var createBeacon = (options) => {
2778
2824
  for (const timer2 of modalTimers)
2779
2825
  clearTimeout(timer2);
2780
2826
  modalTimers.clear();
2827
+ knownModals.clear();
2781
2828
  });
2782
2829
  }
2783
2830
  if (signals.formFrustration !== false) {
@@ -3519,5 +3566,5 @@ export {
3519
3566
  BEACON_ATTRIBUTE
3520
3567
  };
3521
3568
 
3522
- //# debugId=E1FC30A16480777864756E2164756E21
3569
+ //# debugId=92AD5D46269906F564756E2164756E21
3523
3570
  //# sourceMappingURL=index.js.map