This repository was archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathdom.js
65 lines (56 loc) · 2.01 KB
/
dom.js
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
61
62
63
64
65
import gsap from "gsap";
import { ScrollToPlugin } from "gsap/ScrollToPlugin";
// We have to include this so that Webpack doesn't tree-shake the plugin out of
// the production bundle.
gsap.registerPlugin(ScrollToPlugin);
export const smoothScrollTo = (node, options = {}, onCompleteFn) => {
const offset = 0;
const { duration = 0.55 } = options;
gsap.to(window, {
duration,
scrollTo: {
y: node.offsetTop + offset,
},
onComplete: onCompleteFn,
});
};
/**
* findActiveNode expects a list of react refs and whether we're scrolling down
* or not, and returns which node is most likely to be in focus.
* @param {array} possibleNodes A list of react refs, sorted by vertical position
* @param {bool} isScrollingDown Whether the user is scrolling down
* @return {ref} The node most likely to be active
*/
export const findActiveNode = (possibleNodes, isScrollingDown) => {
const topEdge = window.innerHeight * 0.125;
const bottomEdge = window.innerHeight * 0.5;
const isReachedBottom =
window.innerHeight + window.scrollY >= document.body.offsetHeight - topEdge;
return possibleNodes.find((x) => {
if (!x.current) {
return false;
}
const { top, bottom } = x.current.getBoundingClientRect();
if (isScrollingDown) {
// For Edge case
// The last two items on https://developers.stellar.org/docs/
// Has equal hiearachy when scrolled all the way down
// Display the last item when scrolled all the way to the bottom
return isReachedBottom
? bottom + bottomEdge > window.innerHeight - topEdge
: top < bottomEdge && top >= 0;
}
return bottom > topEdge && bottom < window.innerHeight;
});
};
export const isInViewport = (elem) => {
const bounding = elem.getBoundingClientRect();
return (
bounding.top >= 0 &&
bounding.left >= 0 &&
bounding.bottom <=
(window.innerHeight || document.documentElement.clientHeight) &&
bounding.right <=
(window.innerWidth || document.documentElement.clientWidth)
);
};