this repo has no description
1import { debounce } from '@amp/web-app-components/src/utils/debounce';
2import { throttle } from '@amp/web-app-components/src/utils/throttle';
3/**
4 * Dynamically change header and bottom gradient style when scrolling within a modal, and on window resize
5 */
6export function updateScrollAndWindowDependentVisuals(node) {
7 let animationRequest;
8 const handleScroll = () => {
9 // Get scroll details
10 const { scrollHeight, scrollTop, offsetHeight } = node;
11 const maxScroll = scrollHeight - offsetHeight;
12
13 // Calculate whether content is scrolled
14 const contentIsScrolling = scrollTop > 1;
15
16 // Calculate if bottom gradient should be hidden
17 const scrollingNotPossible = maxScroll === 0;
18 const pastMaxScroll = scrollTop >= maxScroll;
19 const hideGradient = scrollingNotPossible || pastMaxScroll;
20
21 if (animationRequest) {
22 window.cancelAnimationFrame(animationRequest);
23 }
24
25 animationRequest = window.requestAnimationFrame(() =>
26 node.dispatchEvent(
27 new CustomEvent('scrollStatus', {
28 detail: { contentIsScrolling, hideGradient },
29 }),
30 ),
31 );
32 };
33
34 const onResize = throttle(handleScroll, 250);
35 const onScroll = debounce(handleScroll, 50);
36 node.addEventListener('scroll', onScroll, { capture: true, passive: true });
37 window.addEventListener('resize', onResize);
38
39 return {
40 destroy() {
41 node.removeEventListener('scroll', onScroll, { capture: true });
42 window.removeEventListener('resize', onResize);
43 if (animationRequest) {
44 window.cancelAnimationFrame(animationRequest);
45 }
46 },
47 };
48}