All files / utils focusScrollUtils.js

100% Statements 21/21
100% Branches 8/8
100% Functions 3/3
100% Lines 21/21

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 331x 1x   1x   1x   1x 1x   1x 6x 6x 2x 2x 4x 6x 6x 6x 6x 6x 6x 6x 6x 6x                
// Scrolls the commons card named by the ?focus={commonsId} query parameter
// into view, leaving room for the sticky navbar so the whole card is visible.
 
const FOCUS_SCROLL_MARGIN = 16;
 
const getFocusTargetId = (search) => new URLSearchParams(search).get("focus");
 
const computeScrollTop = ({ elementTop, scrollY, navbarHeight }) =>
  elementTop + scrollY - navbarHeight - FOCUS_SCROLL_MARGIN;
 
const scrollToFocusTarget = ({ targetId, win = window }) => {
  const element = win.document.getElementById(targetId);
  if (!element) {
    return false;
  }
  const navbar = win.document.querySelector(".navbar");
  const navbarHeight = navbar ? navbar.getBoundingClientRect().height : 0;
  const top = computeScrollTop({
    elementTop: element.getBoundingClientRect().top,
    scrollY: win.scrollY,
    navbarHeight,
  });
  win.scrollTo({ top, behavior: "smooth" });
  return true;
};
 
export {
  FOCUS_SCROLL_MARGIN,
  getFocusTargetId,
  computeScrollTop,
  scrollToFocusTarget,
};