All files / src/pages/flowsheet-page/economics/shared/model economicsScroll.ts

7.69% Statements 1/13
0% Branches 0/2
100% Functions 0/0
7.69% Lines 1/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 4476x                                                                                      
const SCROLLABLE_OVERFLOW_VALUES = new Set(["auto", "scroll", "overlay"]);
 
export function scrollElementIntoEconomicsContext(element: HTMLElement) {
  const scrollContainer = nearestScrollableAncestor(element);
  if (!scrollContainer) {
    element.scrollIntoView({
      block: "center",
      inline: "nearest",
      behavior: "smooth",
    });
    return;
  }
 
  const elementRect = element.getBoundingClientRect();
  const containerRect = scrollContainer.getBoundingClientRect();
  const elementTopInContainer =
    elementRect.top - containerRect.top + scrollContainer.scrollTop;
  const centeredScrollTop =
    elementTopInContainer -
    (scrollContainer.clientHeight - elementRect.height) / 2;
 
  scrollContainer.scrollTo({
    top: Math.max(0, centeredScrollTop),
    behavior: "smooth",
  });
}
 
function nearestScrollableAncestor(element: HTMLElement) {
  for (
    let parent = element.parentElement;
    parent !== null;
    parent = parent.parentElement
  ) {
    const style = window.getComputedStyle(parent);
    if (
      parent.scrollHeight > parent.clientHeight &&
      SCROLLABLE_OVERFLOW_VALUES.has(style.overflowY)
    ) {
      return parent;
    }
  }
  return null;
}