@absolutejs/beacon 0.6.2 → 0.6.4

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,12 +2734,37 @@ 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) {
2722
2755
  const modals = openModals();
2723
2756
  if (modals.length > 0 && !modals.some((modal) => modal.contains(target))) {
2724
- reportModalEscape(modals[modals.length - 1], target, "escaped");
2757
+ const modal = modals[modals.length - 1];
2758
+ const timer2 = setTimeout(() => {
2759
+ modalTimers.delete(timer2);
2760
+ if (!isRenderedModal(modal))
2761
+ return;
2762
+ const active = document.activeElement instanceof Element ? document.activeElement : null;
2763
+ if (active !== null && modal.contains(active))
2764
+ return;
2765
+ reportModalEscape(modal, active, "escaped");
2766
+ }, 0);
2767
+ modalTimers.add(timer2);
2725
2768
  }
2726
2769
  }
2727
2770
  lastDialogFocus = target instanceof Element && target.closest(DIALOG_SELECTOR) !== null ? target : null;
@@ -2746,26 +2789,39 @@ var createBeacon = (options) => {
2746
2789
  document.addEventListener("focusout", onFocusOut, true);
2747
2790
  let modalObserver;
2748
2791
  if (signals.modalFocusEscape !== false && typeof MutationObserver !== "undefined") {
2749
- for (const modal of openModals())
2750
- checkInitialModalFocus(modal);
2792
+ for (const modal of Array.from(document.querySelectorAll(MODAL_SELECTOR))) {
2793
+ updateModalVisibility(modal);
2794
+ }
2751
2795
  modalObserver = new MutationObserver((records) => {
2752
2796
  for (const record of records) {
2753
- if (record.type === "attributes" && record.target instanceof Element && record.target.matches(MODAL_SELECTOR)) {
2754
- checkInitialModalFocus(record.target);
2797
+ if (record.type === "attributes" && record.target instanceof Element) {
2798
+ const target = record.target;
2799
+ if (knownModals.has(target) || target.matches(MODAL_SELECTOR)) {
2800
+ updateModalVisibility(target);
2801
+ }
2802
+ for (const modal of knownModals) {
2803
+ if (modal !== target && target.contains(modal)) {
2804
+ updateModalVisibility(modal);
2805
+ }
2806
+ }
2755
2807
  }
2756
2808
  for (const added of Array.from(record.addedNodes)) {
2757
2809
  if (!(added instanceof Element))
2758
2810
  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
- }
2811
+ registerModals(added);
2764
2812
  }
2765
2813
  }
2766
2814
  });
2767
2815
  modalObserver.observe(document.documentElement, {
2768
- attributeFilter: ["aria-modal", "open"],
2816
+ attributeFilter: [
2817
+ "aria-hidden",
2818
+ "aria-modal",
2819
+ "class",
2820
+ "hidden",
2821
+ "inert",
2822
+ "open",
2823
+ "style"
2824
+ ],
2769
2825
  attributes: true,
2770
2826
  childList: true,
2771
2827
  subtree: true
@@ -2778,6 +2834,7 @@ var createBeacon = (options) => {
2778
2834
  for (const timer2 of modalTimers)
2779
2835
  clearTimeout(timer2);
2780
2836
  modalTimers.clear();
2837
+ knownModals.clear();
2781
2838
  });
2782
2839
  }
2783
2840
  if (signals.formFrustration !== false) {
@@ -3519,5 +3576,5 @@ export {
3519
3576
  BEACON_ATTRIBUTE
3520
3577
  };
3521
3578
 
3522
- //# debugId=E1FC30A16480777864756E2164756E21
3579
+ //# debugId=9A47E9967A8B159F64756E2164756E21
3523
3580
  //# sourceMappingURL=index.js.map