@dloizides/ui-layout 1.63.0 → 1.64.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 CHANGED
@@ -3537,7 +3537,6 @@ var SectionIndex = ({ sections, activeKey, heading, itemHint, onSelect, testID,
3537
3537
  // src/SectionIndexLayout/scrollSpy.ts
3538
3538
  var SPY_ACTIVATION_RATIO = 0.3;
3539
3539
  var SPY_CLICK_LOCK_MS = 900;
3540
- var PERCENT2 = 100;
3541
3540
  function resolveSticky(value) {
3542
3541
  if (value === void 0 || value === false) return { sticky: false, spy: false, activationRatio: SPY_ACTIVATION_RATIO, clickLockMs: SPY_CLICK_LOCK_MS };
3543
3542
  const config = value === true ? {} : value;
@@ -3551,8 +3550,9 @@ function resolveSticky(value) {
3551
3550
  function pickActiveIndex(tops, line) {
3552
3551
  return tops.reduce((picked, top, index) => top <= line ? index : picked, 0);
3553
3552
  }
3554
- function spyRootMargin(activationRatio) {
3555
- return `0px 0px -${Math.round((1 - activationRatio) * PERCENT2)}% 0px`;
3553
+ function pickSpyIndex(tops, viewport, activationRatio) {
3554
+ if (viewport.atEnd) return Math.max(tops.length - 1, 0);
3555
+ return pickActiveIndex(tops, viewport.top + viewport.height * activationRatio);
3556
3556
  }
3557
3557
 
3558
3558
  // src/SectionIndexLayout/sectionKeys.ts
@@ -3570,11 +3570,35 @@ function scrollToSection(anchorId) {
3570
3570
  const target = document.getElementById(anchorId);
3571
3571
  target?.scrollIntoView?.({ behavior: "smooth", block: "start" });
3572
3572
  }
3573
+
3574
+ // src/SectionIndexLayout/scrollViewport.ts
3575
+ var SCROLLABLE_OVERFLOW = /^(auto|scroll|overlay)$/;
3576
+ var END_TOLERANCE_PX = 2;
3577
+ function scrollParent(element) {
3578
+ for (let node = element.parentElement; node !== null; node = node.parentElement) {
3579
+ if (SCROLLABLE_OVERFLOW.test(getComputedStyle(node).overflowY) && node.scrollHeight > node.clientHeight) return node;
3580
+ }
3581
+ return null;
3582
+ }
3583
+ function isAtEnd(scrolled, visible, total) {
3584
+ return scrolled > 0 && scrolled + visible >= total - END_TOLERANCE_PX;
3585
+ }
3586
+ function scrollViewport(anchor) {
3587
+ const parent = scrollParent(anchor);
3588
+ if (parent === null) {
3589
+ const root = document.scrollingElement ?? document.documentElement;
3590
+ return { top: 0, height: window.innerHeight, atEnd: isAtEnd(window.scrollY, window.innerHeight, root.scrollHeight) };
3591
+ }
3592
+ return { top: parent.getBoundingClientRect().top, height: parent.clientHeight, atEnd: isAtEnd(parent.scrollTop, parent.clientHeight, parent.scrollHeight) };
3593
+ }
3594
+
3595
+ // src/SectionIndexLayout/useScrollSpy.ts
3573
3596
  var UNPRIMED = -2;
3574
3597
  var STALE = -1;
3575
3598
  var ID_SEPARATOR = "\n";
3599
+ var LISTEN_OPTIONS = { capture: true, passive: true };
3576
3600
  function canObserve() {
3577
- return typeof document !== "undefined" && typeof window !== "undefined" && typeof IntersectionObserver !== "undefined";
3601
+ return typeof document !== "undefined" && typeof window !== "undefined";
3578
3602
  }
3579
3603
  function findAnchors(ids) {
3580
3604
  return ids.flatMap((id, index) => {
@@ -3591,20 +3615,26 @@ function useScrollSpy({ anchorIds, enabled, activationRatio, onActive }) {
3591
3615
  React16.useEffect(() => {
3592
3616
  if (!enabled || !canObserve()) return void 0;
3593
3617
  const anchors = findAnchors(idsKey.split(ID_SEPARATOR));
3594
- if (anchors.length < 2) return void 0;
3618
+ const first = anchors[0];
3619
+ if (first === void 0 || anchors.length < 2) return void 0;
3595
3620
  last.current = UNPRIMED;
3596
3621
  const evaluate = () => {
3597
3622
  if (Date.now() < pausedUntil.current) return;
3598
- const picked = pickActiveIndex(anchors.map((anchor) => anchor.element.getBoundingClientRect().top), window.innerHeight * activationRatio);
3623
+ const tops = anchors.map((anchor) => anchor.element.getBoundingClientRect().top);
3624
+ const picked = pickSpyIndex(tops, scrollViewport(first.element), activationRatio);
3599
3625
  const index = anchors[picked]?.index ?? 0;
3600
3626
  const primed = last.current !== UNPRIMED;
3601
3627
  if (index === last.current) return;
3602
3628
  last.current = index;
3603
3629
  if (primed || index > 0) onActiveRef.current(index);
3604
3630
  };
3605
- const observer = new IntersectionObserver(evaluate, { rootMargin: spyRootMargin(activationRatio), threshold: 0 });
3606
- anchors.forEach((anchor) => observer.observe(anchor.element));
3607
- return () => observer.disconnect();
3631
+ document.addEventListener("scroll", evaluate, LISTEN_OPTIONS);
3632
+ window.addEventListener("resize", evaluate, LISTEN_OPTIONS);
3633
+ evaluate();
3634
+ return () => {
3635
+ document.removeEventListener("scroll", evaluate, LISTEN_OPTIONS);
3636
+ window.removeEventListener("resize", evaluate, LISTEN_OPTIONS);
3637
+ };
3608
3638
  }, [enabled, activationRatio, idsKey]);
3609
3639
  const pause = React16.useCallback((ms) => {
3610
3640
  pausedUntil.current = Date.now() + ms;