All files / utils focusScrollUtils.js

100% Statements 14/14
100% Branches 5/5
100% Functions 3/3
100% Lines 13/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      2x   27x   2x 5x   2x 6x 6x 2x   4x 4x 6x         6x 6x                  
// Scrolls the game card named by the ?focus={gameId} 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,
};