Skip to content

Commit b4dbf10

Browse files
committed
chore(docs): New recipe NativeObserver
The recipe documents how to gracefully handle environments without IntersectionObserver creating a component to do so.
1 parent 57b94db commit b4dbf10

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,19 @@ Here's how to create an **element monitoring** component:
155155

156156
```jsx
157157
import React, { Component } from 'react';
158+
import PropTypes from 'prop-types';
158159
import Observer from '@researchgate/react-intersection-observer';
159160

160161
export default class ViewableMonitor extends Component {
162+
static propTypes = {
163+
tag: PropTypes.node,
164+
children: PropTypes.func.isRequired,
165+
}
166+
167+
static defaultProps = {
168+
tag: 'div',
169+
}
170+
161171
state = {
162172
isIntersecting: false,
163173
}
@@ -167,16 +177,13 @@ export default class ViewableMonitor extends Component {
167177
};
168178

169179
render() {
170-
const { children, mount: Tag, ...rest } = this.props;
171-
let element = children(this.state.isIntersecting);
172-
173-
if (Tag) {
174-
element = <Tag>{element}</Tag>;
175-
}
180+
const { tag: Tag, children, ...rest } = this.props;
176181

177182
return (
178183
<Observer {...rest} onChange={this.handleChange}>
179-
{element}
184+
<Tag>
185+
{children(this.state.isIntersecting)}
186+
</Tag>
180187
</Observer>
181188
);
182189
}

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ common situations that can be approached with ReactIntersectionObserver:
2020
* [Higher Order Component](docs/components/HigherOrderComponent/)
2121
* [Tracking Ad Impressions](docs/components/ImpressionTracking/)
2222
* [Viewable Monitor](docs/components/ViewableMonitor/)
23+
* [Graceful Degradation](docs/components/NativeObserver/)
2324

2425
### Can I submit a new recipe?
2526

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## Gracefully handling environments without IntersectionObserver support
2+
3+
This module assumes `IntersectionObserver` is available as a global - the polyfill is necessary. However, there may be
4+
situations where we want to do progressive enhancement and only observe elements if it's available.
5+
6+
To prevent an exception in such cases, let's create a component that will gracefully fail if `IntersectionObserver`
7+
doesn't exist, and instead render the passed children naturally.
8+
9+
### Source
10+
11+
```jsx
12+
import React from 'react';
13+
import Observer from '@researchgate/react-intersection-observer';
14+
15+
// Exits early if all IntersectionObserver and IntersectionObserverEntry
16+
// features are natively supported.
17+
const hasNativeSupport =
18+
'IntersectionObserver' in window &&
19+
'IntersectionObserverEntry' in window &&
20+
'intersectionRatio' in window.IntersectionObserverEntry.prototype;
21+
22+
export default function NativeObserver(props) {
23+
return hasNativeSupport ? <Observer {...props} /> : props.children;
24+
}
25+
```

0 commit comments

Comments
 (0)