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