-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.ts
378 lines (340 loc) · 12.2 KB
/
hooks.ts
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* eslint-disable react-compiler/react-compiler */
import { useEffect, useInsertionEffect, useRef, useState } from "react";
type AnyFunction = (...args: unknown[]) => unknown;
/**
* Similar to useCallback, with a few subtle differences:
* - The returned function is a stable reference, and will always be the same between renders
* - No dependency lists required
* - Properties or state accessed within the callback will always be "current"
*/
export function useEvent<TCallback extends AnyFunction>(
callback: TCallback,
): TCallback {
// Keep track of the latest callback:
const latestRef = useRef<TCallback>(
process.env.NODE_ENV === "production"
? (useEvent_shouldNotBeInvokedBeforeMount as TCallback)
: (undefined as unknown as TCallback),
);
useInsertionEffect(() => {
latestRef.current = callback;
}, [callback]);
// Create a stable callback that always calls the latest callback:
// using useRef instead of useCallback avoids creating and empty array on every render
const stableRef = useRef<TCallback>(null as unknown as TCallback);
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!stableRef.current) {
stableRef.current = function (this: unknown) {
// eslint-disable-next-line prefer-rest-params
return latestRef.current.apply(this, arguments) as unknown;
} as TCallback;
}
return stableRef.current;
}
/**
* Render methods should be pure, especially when concurrency is used,
* so we will throw this error if the callback is called while rendering.
*/
function useEvent_shouldNotBeInvokedBeforeMount() {
throw new Error(
"INVALID_USEEVENT_INVOCATION: the callback from useEvent cannot be invoked before the component has mounted.",
);
}
const observerMap = new Map<
string,
{
id: string;
observer: IntersectionObserver;
elements: Map<Element, Array<ObserverInstanceCallback>>;
}
>();
const RootIds: WeakMap<Element | Document, string> = new WeakMap();
let rootId = 0;
/**
* Generate a unique ID for the root element
* @param root
*/
function getRootId(root: IntersectionObserverInit["root"]) {
if (!root) return "0";
if (RootIds.has(root)) return RootIds.get(root);
rootId += 1;
RootIds.set(root, rootId.toString());
return RootIds.get(root);
}
/**
* Convert the options to a string Id, based on the values.
* Ensures we can reuse the same observer when observing elements with the same options.
* @param options
*/
function optionsToId(options: IntersectionObserverInit) {
return Object.keys(options)
.sort()
.filter(
(key): key is keyof IntersectionObserverInit =>
options[key as keyof IntersectionObserverInit] !== undefined,
)
.map(
(key) =>
`${key}_${
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
key === "root"
? getRootId(options.root)
: options[key as keyof IntersectionObserverInit]
}`,
)
.toString();
}
function createObserver(options: IntersectionObserverInit) {
// Create a unique ID for this observer instance, based on the root, root margin and threshold.
const id = optionsToId(options);
let instance = observerMap.get(id);
if (!instance) {
// Create a map of elements this observer is going to observe. Each element has a list of callbacks that should be triggered, once it comes into view.
const elements = new Map<Element, Array<ObserverInstanceCallback>>();
let thresholds: number[] | readonly number[] = [];
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
// While it would be nice if you could just look at isIntersecting to determine if the component is inside the viewport, browsers can't agree on how to use it.
// -Firefox ignores `threshold` when considering `isIntersecting`, so it will never be false again if `threshold` is > 0
const inView =
entry.isIntersecting &&
thresholds.some((threshold) => entry.intersectionRatio >= threshold);
// @ts-expect-error - support IntersectionObserver v2.
if (options.trackVisibility && typeof entry.isVisible === "undefined") {
// @ts-expect-error - The browser doesn't support Intersection Observer v2, falling back to v1 behavior.
entry.isVisible = inView;
}
elements.get(entry.target)?.forEach((callback) => {
callback(inView, entry);
});
});
}, options);
// Ensure we have a valid thresholds array. If not, use the threshold from the options
thresholds =
observer.thresholds ||
(Array.isArray(options.threshold)
? options.threshold
: [options.threshold || 0]);
instance = {
id,
observer,
elements,
};
observerMap.set(id, instance);
}
return instance;
}
const unsupportedValue: boolean | undefined = undefined;
/**
* @param element - DOM Element to observe
* @param callback - Callback function to trigger when intersection status changes
* @param options - Intersection Observer options
* @param fallbackInView - Fallback inView value.
* @return Function - Cleanup function that should be triggered to unregister the observer
*/
function observe(
element: Element,
callback: ObserverInstanceCallback,
options: IntersectionObserverInit = {},
fallbackInView = unsupportedValue,
) {
if (
typeof window.IntersectionObserver === "undefined" &&
fallbackInView !== undefined
) {
const bounds = element.getBoundingClientRect();
callback(fallbackInView, {
isIntersecting: fallbackInView,
target: element,
intersectionRatio:
typeof options.threshold === "number" ? options.threshold : 0,
time: 0,
boundingClientRect: bounds,
intersectionRect: bounds,
rootBounds: bounds,
});
return () => {
// Nothing to cleanup
};
}
// An observer with the same options can be reused, so lets use this fact
const { id, observer, elements } = createObserver(options);
// Register the callback listener for this element
const callbacks = elements.get(element) || [];
if (!elements.has(element)) elements.set(element, callbacks);
callbacks.push(callback);
observer.observe(element);
return function unobserve() {
// Remove the callback from the callback list
callbacks.splice(callbacks.indexOf(callback), 1);
if (callbacks.length === 0) {
// No more callback exists for element, so destroy it
elements.delete(element);
observer.unobserve(element);
}
if (elements.size === 0) {
// No more elements are being observer by this instance, so destroy it
observer.disconnect();
observerMap.delete(id);
}
};
}
type ObserverInstanceCallback = (
inView: boolean,
entry: IntersectionObserverEntry,
) => void;
interface IntersectionOptions extends IntersectionObserverInit {
/** The IntersectionObserver interface's read-only `root` property identifies the Element or Document whose bounds are treated as the bounding box of the viewport for the element which is the observer's target. If the `root` is null, then the bounds of the actual document viewport are used.*/
root?: Element | null;
/** Margin around the root. Can have values similar to the CSS margin property, e.g. `10px 20px 30px 40px` (top, right, bottom, left). */
rootMargin?: string;
/** Number between `0` and `1` indicating the percentage that should be visible before triggering. Can also be an `array` of numbers, to create multiple trigger points. */
threshold?: number | number[];
/** Only trigger the inView callback once */
triggerOnce?: boolean;
/** Skip assigning the observer to the `ref` */
skip?: boolean;
/** Set the initial value of the `inView` boolean. This can be used if you expect the element to be in the viewport to start with, and you want to trigger something when it leaves. */
initialInView?: boolean;
/** Fallback to this inView state if the IntersectionObserver is unsupported, and a polyfill wasn't loaded */
fallbackInView?: boolean;
/** IntersectionObserver v2 - Track the actual visibility of the element */
trackVisibility?: boolean;
/** IntersectionObserver v2 - Set a minimum delay between notifications */
delay?: number;
/** Call this function whenever the in view state changes */
onChange?: (inView: boolean, entry: IntersectionObserverEntry) => void;
}
/**
* The Hook response supports both array and object destructing
*/
type InViewHookResponse = [
(node?: Element | null) => void,
boolean,
IntersectionObserverEntry | undefined,
] & {
ref: (node?: Element | null) => void;
inView: boolean;
entry?: IntersectionObserverEntry;
};
type State = {
inView: boolean;
entry?: IntersectionObserverEntry;
};
/**
* React Hooks make it easy to monitor the `inView` state of your components. Call
* the `useInView` hook with the (optional) [options](#options) you need. It will
* return an array containing a `ref`, the `inView` status and the current
* [`entry`](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry).
* Assign the `ref` to the DOM element you want to monitor, and the hook will
* report the status.
*
* @example
* ```jsx
* import React from 'react';
* import { useInView } from 'react-intersection-observer';
*
* const Component = () => {
* const { ref, inView, entry } = useInView({
* threshold: 0,
* });
*
* return (
* <div ref={ref}>
* <h2>{`Header inside viewport ${inView}.`}</h2>
* </div>
* );
* };
* ```
*/
export function useInView({
threshold,
delay,
trackVisibility,
rootMargin,
root,
triggerOnce,
skip,
initialInView,
fallbackInView,
onChange,
}: IntersectionOptions = {}): InViewHookResponse {
const [ref, setRef] = useState<Element | null>(null);
const callback = useRef<IntersectionOptions["onChange"]>(undefined);
const [state, setState] = useState<State>({
inView: Boolean(initialInView),
entry: undefined,
});
// Store the onChange callback in a `ref`, so we can access the latest instance
// inside the `useEffect`, but without triggering a rerender.
callback.current = onChange;
useEffect(
() => {
// Ensure we have node ref, and that we shouldn't skip observing
if (skip || !ref) return;
let unobserve: (() => void) | undefined;
unobserve = observe(
ref,
(inView, entry) => {
setState({
inView,
entry,
});
if (callback.current) callback.current(inView, entry);
if (entry.isIntersecting && triggerOnce && unobserve) {
// If it should only trigger once, unobserve the element after it's inView
unobserve();
unobserve = undefined;
}
},
{
root,
rootMargin,
threshold,
// @ts-expect-error - Support for the v2 API
trackVisibility,
delay,
},
fallbackInView,
);
return () => {
unobserve?.();
};
},
// We break the rule here, because we aren't including the actual `threshold` variable
// eslint-disable-next-line react-hooks/exhaustive-deps
[
// If the threshold is an array, convert it to a string, so it won't change between renders.
// eslint-disable-next-line react-hooks/exhaustive-deps
Array.isArray(threshold) ? threshold.toString() : threshold,
ref,
root,
rootMargin,
triggerOnce,
skip,
trackVisibility,
fallbackInView,
delay,
],
);
const entryTarget = state.entry?.target;
const previousEntryTarget = useRef<Element>(undefined);
if (
!ref &&
entryTarget &&
!triggerOnce &&
!skip &&
previousEntryTarget.current !== entryTarget
) {
// If we don't have a node ref, then reset the state (unless the hook is set to only `triggerOnce` or `skip`)
// This ensures we correctly reflect the current state - If you aren't observing anything, then nothing is inView
previousEntryTarget.current = entryTarget;
setState({ inView: Boolean(initialInView), entry: undefined });
}
const result = [setRef, state.inView, state.entry] as InViewHookResponse;
// Support object destructuring, by adding the specific values.
result.ref = result[0];
result.inView = result[1];
result.entry = result[2];
return result;
}