-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathInView.tsx
More file actions
184 lines (157 loc) · 4.93 KB
/
InView.tsx
File metadata and controls
184 lines (157 loc) · 4.93 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
179
180
181
182
183
184
import * as React from "react";
import type { IntersectionObserverProps, PlainChildrenProps } from "./index";
import { observe } from "./observe";
type State = {
inView: boolean;
entry?: IntersectionObserverEntry;
};
function isPlainChildren(
props: IntersectionObserverProps | PlainChildrenProps,
): props is PlainChildrenProps {
return typeof props.children !== "function";
}
/**
## Render props
To use the `<InView>` component, you pass it a function. It will be called
whenever the state changes, with the new value of `inView`. In addition to the
`inView` prop, children also receive a `ref` that should be set on the
containing DOM element. This is the element that the IntersectionObserver will
monitor.
If you need it, you can also access the
[`IntersectionObserverEntry`](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry)
on `entry`, giving you access to all the details about the current intersection
state.
```jsx
import { InView } from 'react-intersection-observer';
const Component = () => (
<InView>
{({ inView, ref, entry }) => (
<div ref={ref}>
<h2>{`Header inside viewport ${inView}.`}</h2>
</div>
)}
</InView>
);
export default Component;
```
## Plain children
You can pass any element to the `<InView />`, and it will handle creating the
wrapping DOM element. Add a handler to the `onChange` method, and control the
state in your own component. Any extra props you add to `<InView>` will be
passed to the HTML element, allowing you set the `className`, `style`, etc.
```jsx
import { InView } from 'react-intersection-observer';
const Component = () => (
<InView as="div" onChange={(inView, entry) => console.log('Inview:', inView)}>
<h2>Plain children are always rendered. Use onChange to monitor state.</h2>
</InView>
);
export default Component;
```
*/
export class InView extends React.Component<
IntersectionObserverProps | PlainChildrenProps,
State
> {
node: Element | null = null;
_unobserveCb: (() => void) | null = null;
constructor(props: IntersectionObserverProps | PlainChildrenProps) {
super(props);
this.state = {
inView: !!props.initialInView,
entry: undefined,
};
}
componentDidMount() {
this.unobserve();
this.observeNode();
}
componentDidUpdate(prevProps: IntersectionObserverProps) {
// If a IntersectionObserver option changed, reinit the observer
if (
prevProps.rootMargin !== this.props.rootMargin ||
prevProps.root !== this.props.root ||
prevProps.threshold !== this.props.threshold ||
prevProps.skip !== this.props.skip ||
prevProps.trackVisibility !== this.props.trackVisibility ||
prevProps.delay !== this.props.delay
) {
this.unobserve();
this.observeNode();
}
}
componentWillUnmount() {
this.unobserve();
}
observeNode() {
if (!this.node || this.props.skip) return;
const { threshold, root, rootMargin, trackVisibility, delay } = this.props;
this._unobserveCb = observe(this.node, this.handleChange, {
threshold,
root,
rootMargin,
// @ts-ignore
trackVisibility,
// @ts-ignore
delay,
});
}
unobserve() {
if (this._unobserveCb) {
this._unobserveCb();
this._unobserveCb = null;
}
}
handleNode = (node?: Element | null) => {
if (this.node) {
// Clear the old observer, before we start observing a new element
this.unobserve();
if (!node && !this.props.triggerOnce && !this.props.skip) {
// Reset the state if we get a new node, and we aren't ignoring updates
this.setState({ inView: !!this.props.initialInView, entry: undefined });
}
}
this.node = node ? node : null;
this.observeNode();
};
handleChange = (inView: boolean, entry: IntersectionObserverEntry) => {
if (inView && this.props.triggerOnce) {
// If `triggerOnce` is true, we should stop observing the element.
this.unobserve();
}
if (!isPlainChildren(this.props)) {
// Store the current State, so we can pass it to the children in the next render update
// There's no reason to update the state for plain children, since it's not used in the rendering.
this.setState({ inView, entry });
}
if (this.props.onChange) {
// If the user is actively listening for onChange, always trigger it
this.props.onChange(inView, entry);
}
};
render() {
const { children } = this.props;
if (typeof children === "function") {
const { inView, entry } = this.state;
return children({ inView, entry, ref: this.handleNode });
}
const {
as,
triggerOnce,
threshold,
root,
rootMargin,
onChange,
skip,
trackVisibility,
delay,
initialInView,
...props
} = this.props as PlainChildrenProps;
return React.createElement(
as || "div",
{ ref: this.handleNode, ...props },
children,
);
}
}