forked from nodejs/nodejs.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifyWhenStickyHeadersChange.ts
More file actions
178 lines (165 loc) · 5.25 KB
/
notifyWhenStickyHeadersChange.ts
File metadata and controls
178 lines (165 loc) · 5.25 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { StickyChange, SentinelObserverSetupOptions } from '../types';
/**
* Dispatches a `stickychange` custom event.
*/
const fireStickyChange = (stuck: boolean, target: HTMLElement): void => {
const detail: StickyChange = { stuck, target };
const event: CustomEvent<StickyChange> = new CustomEvent('stickychange', {
detail,
});
document.dispatchEvent(event);
};
/**
* Adds DOM nodes (aka sentinels) in each sticky section to act as waypoints for
* figuring out scroll position.
*/
const addSentinels = (
container: HTMLElement,
stickyElementsClassName: string,
className: string
): HTMLDivElement[] => {
const stickyElements: HTMLElement[] = Array.from(
container.querySelectorAll(`.${stickyElementsClassName}`)
) as HTMLElement[];
const sentinels: HTMLDivElement[] = stickyElements.map(
(stickyElement: HTMLElement) => {
const sentinel: HTMLDivElement = document.createElement('div');
sentinel.classList.add('sticky-sentinel', className);
const appendedSentinel: HTMLDivElement = stickyElement.parentElement!.appendChild(
sentinel
);
return appendedSentinel;
}
);
return sentinels;
};
/**
* Sets up an intersection observer to notify when elements with the class
* `.sticky-sentinel--top` become visible/invisible at the top of the container.
*/
const observeTopSentinels = ({
container,
stickyElementsClassName,
root = container,
headerRootMargin,
}: SentinelObserverSetupOptions): void => {
const callback: IntersectionObserverCallback = (
entries: IntersectionObserverEntry[]
) => {
entries.forEach((entry: IntersectionObserverEntry) => {
const targetBoundsInfo: ClientRect = entry.boundingClientRect;
const targetParentElement: HTMLElement | null =
entry.target.parentElement;
const stickyElement: HTMLElement | null =
targetParentElement &&
(targetParentElement.querySelector(
`.${stickyElementsClassName}`
) as HTMLElement);
const rootBoundsInfo: ClientRect = entry.rootBounds;
// Started sticking
if (stickyElement && targetBoundsInfo.bottom < rootBoundsInfo.top) {
fireStickyChange(true, stickyElement);
}
// Stopped sticking
if (
stickyElement &&
targetBoundsInfo.bottom >= rootBoundsInfo.top &&
targetBoundsInfo.bottom < rootBoundsInfo.bottom
) {
fireStickyChange(false, stickyElement);
}
});
};
const options: IntersectionObserverInit = {
root,
rootMargin: headerRootMargin || '0px',
threshold: [0],
};
const observer: IntersectionObserver = new IntersectionObserver(
callback,
options
);
// Add the top sentinels to each section and attach an observer
const topSentinels: HTMLDivElement[] = addSentinels(
container,
stickyElementsClassName,
'sticky-sentinel--top'
);
topSentinels.forEach((sentinel: HTMLDivElement) =>
observer.observe(sentinel)
);
};
/**
* Sets up an intersection observer to notify when elements with the class
* `.sticky-sentinel--bottom` become visible/invisible at the bottom of the
* container.
*/
const observeBottomSentinels = ({
container,
stickyElementsClassName,
root = container,
footerRootMargin,
}: SentinelObserverSetupOptions): void => {
const callback: IntersectionObserverCallback = (
entries: IntersectionObserverEntry[]
) => {
entries.forEach((entry: IntersectionObserverEntry) => {
const targetBoundsInfo: ClientRect = entry.boundingClientRect;
const targetParentElement: HTMLElement | null =
entry.target.parentElement;
const stickyElement: HTMLElement | null =
targetParentElement &&
(targetParentElement.querySelector(
`.${stickyElementsClassName}`
) as HTMLElement);
const rootBoundsInfo: ClientRect = entry.rootBounds;
const ratio: number = entry.intersectionRatio;
// Started sticking
if (
stickyElement &&
targetBoundsInfo.bottom > rootBoundsInfo.top &&
ratio === 1
) {
fireStickyChange(true, stickyElement);
}
// Stopped sticking
if (
stickyElement &&
targetBoundsInfo.top < rootBoundsInfo.top &&
targetBoundsInfo.bottom < rootBoundsInfo.bottom
) {
fireStickyChange(false, stickyElement);
}
});
};
const options: IntersectionObserverInit = {
root,
rootMargin: footerRootMargin || '0px',
// Get callback slightly before element is 100% visible/invisible
threshold: [1],
};
const observer: IntersectionObserver = new IntersectionObserver(
callback,
options
);
// Add the bottom sentinels to each section and attach an observer
const bottomSentinels: HTMLDivElement[] = addSentinels(
container,
stickyElementsClassName,
'sticky-sentinel--bottom'
);
bottomSentinels.forEach((sentinel: HTMLDivElement) =>
observer.observe(sentinel)
);
};
/**
* Notifies when elements that have the class `stickyElementsClassName`
* (specified in `setupOptions`) begin to stick or not.
* Note: these should be children of the `container` element.
*/
export const notifyWhenStickyHeadersChange = (
setupOptions: SentinelObserverSetupOptions
): void => {
observeTopSentinels(setupOptions);
observeBottomSentinels(setupOptions);
};