-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathIntersectionObserver.ts
More file actions
103 lines (82 loc) · 2.43 KB
/
IntersectionObserver.ts
File metadata and controls
103 lines (82 loc) · 2.43 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
import React from 'react';
import { createObserver } from './observer';
import {
shallowCompare,
toString,
} from './utils';
import { ChangeHandler, Options, Instance } from './types';
const observerOptions = <const>['root', 'rootMargin', 'threshold'];
const observableProps = <const>['root', 'rootMargin', 'threshold', 'disabled'];
export const getOptions = (props: Props) => {
return observerOptions.reduce<IntersectionObserverInit>((options, key) => {
const isRootString =
key === 'root' && toString.call(props.root) === '[object String]';
return Object.assign(options, {
[key]: isRootString
? document.querySelector(props[key] as string)
: props[key],
});
}, {});
};
interface Props extends Options {
/**
* The element that is used as the target to observe.
*/
children?: React.ReactElement | null;
/**
* Function that will be invoked whenever the intersection value for this element changes.
*/
onChange: ChangeHandler;
}
export default class ReactIntersectionObserver extends React.Component<Props> implements Instance {
static displayName = 'IntersectionObserver';
private targetNode?: Element;
public observer?: IntersectionObserver;
handleChange = (event: IntersectionObserverEntry, unobserve: () => void) => {
this.props.onChange(event, unobserve);
};
componentDidMount() {
if (typeof window !== 'undefined') {
this.observe();
}
}
componentDidUpdate(prevProps: Props) {
const relatedPropsChanged = observableProps.some(
(prop) => shallowCompare(this.props[prop], prevProps[prop])
);
if (relatedPropsChanged) {
this.unobserve();
this.observe();
}
}
componentWillUnmount() {
this.unobserve();
}
observe() {
if (!this.targetNode || this.props.disabled) return;
const options = getOptions(this.props);
this.observer = createObserver(options);
this.observer.observe(this.targetNode);
}
unobserve() {
if (this.observer && this.targetNode) {
this.observer.unobserve(this.targetNode);
this.observer = undefined;
}
}
handleNode = (node: Element | null) => {
if (node) {
this.targetNode = node;
} else {
this.targetNode = undefined;
}
};
render() {
const { children } = this.props;
if (!children) return null;
return React.cloneElement(React.Children.only(children), {
ref: this.handleNode,
});
}
}
export * from './types';