summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/lib/utils/sticky.js
blob: a6d53358cb82af5633f7d8954ccb2e6937d1a177 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
export const createPlaceholder = () => {
  const placeholder = document.createElement('div');
  placeholder.classList.add('sticky-placeholder');

  return placeholder;
};

export const isSticky = (el, scrollY, stickyTop, insertPlaceholder) => {
  const top = Math.floor(el.offsetTop - scrollY);

  if (top <= stickyTop && !el.classList.contains('is-stuck')) {
    const placeholder = insertPlaceholder ? createPlaceholder() : null;
    const heightBefore = el.offsetHeight;

    el.classList.add('is-stuck');

    if (insertPlaceholder) {
      el.parentNode.insertBefore(placeholder, el.nextElementSibling);

      placeholder.style.height = `${heightBefore - el.offsetHeight}px`;
    }
  } else if (top > stickyTop && el.classList.contains('is-stuck')) {
    el.classList.remove('is-stuck');

    if (
      insertPlaceholder &&
      el.nextElementSibling &&
      el.nextElementSibling.classList.contains('sticky-placeholder')
    ) {
      el.nextElementSibling.remove();
    }
  }
};

/**
 * Create a listener that will toggle a 'is-stuck' class, based on the current scroll position.
 *
 * - If the current environment does not support `position: sticky`, do nothing.
 *
 * @param {HTMLElement} el The `position: sticky` element.
 * @param {Number} stickyTop Used to determine when an element is stuck.
 * @param {Boolean} insertPlaceholder Should a placeholder element be created when element is stuck?
 */
export const stickyMonitor = (el, stickyTop, insertPlaceholder = true) => {
  if (!el) return;

  if (
    typeof CSS === 'undefined' ||
    !CSS.supports('(position: -webkit-sticky) or (position: sticky)')
  )
    return;

  document.addEventListener(
    'scroll',
    () => isSticky(el, window.scrollY, stickyTop, insertPlaceholder),
    {
      passive: true,
    },
  );
};