@dloizides/ui-layout 1.63.0 → 1.65.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,10 @@ 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.atStart) return 0;
3549
+ if (viewport.atEnd) return Math.max(tops.length - 1, 0);
3550
+ return pickActiveIndex(tops, viewport.top + viewport.height * activationRatio);
3550
3551
  }
3551
3552
 
3552
3553
  // src/SectionIndexLayout/sectionKeys.ts
@@ -3564,11 +3565,38 @@ function scrollToSection(anchorId) {
3564
3565
  const target = document.getElementById(anchorId);
3565
3566
  target?.scrollIntoView?.({ behavior: "smooth", block: "start" });
3566
3567
  }
3568
+
3569
+ // src/SectionIndexLayout/scrollViewport.ts
3570
+ var SCROLLABLE_OVERFLOW = /^(auto|scroll|overlay)$/;
3571
+ var END_TOLERANCE_PX = 2;
3572
+ function scrollParent(element) {
3573
+ for (let node = element.parentElement; node !== null; node = node.parentElement) {
3574
+ if (SCROLLABLE_OVERFLOW.test(getComputedStyle(node).overflowY) && node.scrollHeight > node.clientHeight) return node;
3575
+ }
3576
+ return null;
3577
+ }
3578
+ function isAtStart(scrolled) {
3579
+ return scrolled <= 0;
3580
+ }
3581
+ function isAtEnd(scrolled, visible, total) {
3582
+ return scrolled > 0 && scrolled + visible >= total - END_TOLERANCE_PX;
3583
+ }
3584
+ function scrollViewport(anchor) {
3585
+ const parent = scrollParent(anchor);
3586
+ if (parent === null) {
3587
+ const root = document.scrollingElement ?? document.documentElement;
3588
+ return { top: 0, height: window.innerHeight, atStart: isAtStart(window.scrollY), atEnd: isAtEnd(window.scrollY, window.innerHeight, root.scrollHeight) };
3589
+ }
3590
+ return { top: parent.getBoundingClientRect().top, height: parent.clientHeight, atStart: isAtStart(parent.scrollTop), atEnd: isAtEnd(parent.scrollTop, parent.clientHeight, parent.scrollHeight) };
3591
+ }
3592
+
3593
+ // src/SectionIndexLayout/useScrollSpy.ts
3567
3594
  var UNPRIMED = -2;
3568
3595
  var STALE = -1;
3569
3596
  var ID_SEPARATOR = "\n";
3597
+ var LISTEN_OPTIONS = { capture: true, passive: true };
3570
3598
  function canObserve() {
3571
- return typeof document !== "undefined" && typeof window !== "undefined" && typeof IntersectionObserver !== "undefined";
3599
+ return typeof document !== "undefined" && typeof window !== "undefined";
3572
3600
  }
3573
3601
  function findAnchors(ids) {
3574
3602
  return ids.flatMap((id, index) => {
@@ -3585,20 +3613,25 @@ function useScrollSpy({ anchorIds, enabled, activationRatio, onActive }) {
3585
3613
  useEffect(() => {
3586
3614
  if (!enabled || !canObserve()) return void 0;
3587
3615
  const anchors = findAnchors(idsKey.split(ID_SEPARATOR));
3588
- if (anchors.length < 2) return void 0;
3616
+ const first = anchors[0];
3617
+ if (first === void 0 || anchors.length < 2) return void 0;
3589
3618
  last.current = UNPRIMED;
3590
3619
  const evaluate = () => {
3591
3620
  if (Date.now() < pausedUntil.current) return;
3592
- const picked = pickActiveIndex(anchors.map((anchor) => anchor.element.getBoundingClientRect().top), window.innerHeight * activationRatio);
3621
+ const tops = anchors.map((anchor) => anchor.element.getBoundingClientRect().top);
3622
+ const picked = pickSpyIndex(tops, scrollViewport(first.element), activationRatio);
3593
3623
  const index = anchors[picked]?.index ?? 0;
3594
- const primed = last.current !== UNPRIMED;
3595
3624
  if (index === last.current) return;
3596
3625
  last.current = index;
3597
- if (primed || index > 0) onActiveRef.current(index);
3626
+ onActiveRef.current(index);
3627
+ };
3628
+ document.addEventListener("scroll", evaluate, LISTEN_OPTIONS);
3629
+ window.addEventListener("resize", evaluate, LISTEN_OPTIONS);
3630
+ evaluate();
3631
+ return () => {
3632
+ document.removeEventListener("scroll", evaluate, LISTEN_OPTIONS);
3633
+ window.removeEventListener("resize", evaluate, LISTEN_OPTIONS);
3598
3634
  };
3599
- const observer = new IntersectionObserver(evaluate, { rootMargin: spyRootMargin(activationRatio), threshold: 0 });
3600
- anchors.forEach((anchor) => observer.observe(anchor.element));
3601
- return () => observer.disconnect();
3602
3635
  }, [enabled, activationRatio, idsKey]);
3603
3636
  const pause = useCallback((ms) => {
3604
3637
  pausedUntil.current = Date.now() + ms;
@@ -3659,7 +3692,7 @@ var SectionIndexLayout = ({
3659
3692
  const wide = useIsWideViewport(fold.wide);
3660
3693
  const stacked = useIsWideViewport(fold.stackFrom);
3661
3694
  const sticky = resolveSticky(stickyIndex);
3662
- const preferredKey = wide ? initialKey : collapsedKey ?? initialKey;
3695
+ const preferredKey = wide ? initialKey : collapsedKey;
3663
3696
  const { activeKey, pick, select } = useSectionSelection({ sections, testID, preferredKey, spyEnabled: wide && sections.length > 1, sticky });
3664
3697
  const dense = density === "dense" /* Dense */;
3665
3698
  const gap = { gap: dense ? DENSE_GAP : COMFORTABLE_GAP };