Skip to content

Commit e272a67

Browse files
committed
chore(README): Improve structure and content
1 parent dbea038 commit e272a67

File tree

1 file changed

+66
-22
lines changed

1 file changed

+66
-22
lines changed

README.md

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ is fully declarative and takes care of all the imperative parts for you.
2828
<details>
2929
<summary><strong>Table of Contents</strong></summary>
3030

31-
* [What does IntersectionObserver do?](#what-does-intersectionobserver-do)
3231
* [Getting started](#getting-started)
3332
* [Installation](#installation)
3433
* [Inside your codebase](#inside-your-codebase)
35-
* [Why ReactIntersectionObserver?](#why-reactintersectionobserver)
34+
* [What does IntersectionObserver do?](#what-does-intersectionobserver-do)
35+
* [Why use this component?](#why-use-this-component)
3636
* [No bookkeeping](#no-bookkeeping)
3737
* [No extra markup](#no-extra-markup)
3838
* [Easy to adopt](#easy-to-adopt)
@@ -47,29 +47,12 @@ is fully declarative and takes care of all the imperative parts for you.
4747

4848
---
4949

50-
## What does IntersectionObserver do?
51-
52-
> IntersectionObservers calculate how much of a target element overlaps (or "intersects with") the visible portion of a
53-
> page, also known as the browser's "viewport":
54-
>
55-
> \- >
56-
> [Dan Callahan](https://hacks.mozilla.org/2017/08/intersection-observer-comes-to-firefox/)&nbsp;&middot;&nbsp;<a href="https://creativecommons.org/licenses/by-sa/3.0/"> >
57-
> <img id="licensebutton_slim" alt="Creative Commons License" src="https://i.creativecommons.org/l/by-sa/3.0/80x15.png" style="margin-right:10px;margin-bottom:4px; border: 0;"></a>
58-
59-
![Graphic example](https://hacks.mozilla.org/files/2017/08/Blank-Diagram-Page-1.png)
60-
6150
## Getting started
6251

6352
```shell
6453
npm install --save @researchgate/react-intersection-observer
6554
```
6655

67-
Optionally add the polyfill and make sure it's required on your dependendencies for unsupporting browsers:
68-
69-
```shell
70-
npm install --save intersection-observer
71-
```
72-
7356
Usage:
7457

7558
```jsx
@@ -100,7 +83,23 @@ class ExampleComponent extends React.Component {
10083
}
10184
```
10285

103-
## Why ReactIntersectionObserver?
86+
Optionally add the **polyfill** and make sure it's required on your dependendencies for unsupporting browsers:
87+
88+
```shell
89+
npm install --save intersection-observer
90+
```
91+
92+
## What does IntersectionObserver do?
93+
94+
> IntersectionObservers calculate how much of a target element overlaps (or "intersects with") the visible portion of a
95+
> page, also known as the browser's "viewport":
96+
>
97+
> [Dan Callahan](https://hacks.mozilla.org/2017/08/intersection-observer-comes-to-firefox/) · <a href="https://creativecommons.org/licenses/by-sa/3.0/">
98+
> <img id="licensebutton_slim" alt="Creative Commons License" src="https://i.creativecommons.org/l/by-sa/3.0/80x15.png" style="margin-right:10px;margin-bottom:4px; border: 0;"></a>
99+
100+
![Graphic example](https://hacks.mozilla.org/files/2017/08/Blank-Diagram-Page-1.png)
101+
102+
## Why use this component?
104103

105104
The motivation is to provide the easiest possible solution for observing elements that enter the viewport on your
106105
**React** codebase. It's fully declarative and all complexity is abstracted away, focusing on reusability, and low
@@ -150,6 +149,51 @@ Find multiple examples and usage guidelines under:
150149
Recipes are useful code snippets solutions to common problems, for example, how to use ReactIntersectionObserver within
151150
a
152151
[Higher Order Component](https://researchgate.github.io/react-intersection-observer/?selectedKind=Recipes&selectedStory=Higher%20Order%20Component).
152+
<br>
153+
Here's how to create an **element monitoring** component:
154+
155+
```jsx
156+
import React, { Component } from 'react';
157+
import Observer from '@researchgate/react-intersection-observer';
158+
159+
export default class ViewableMonitor extends Component {
160+
state = {
161+
isIntersecting: false,
162+
}
163+
164+
handleChange = ({ isIntersecting }) => {
165+
this.setState({ isIntersecting });
166+
};
167+
168+
render() {
169+
const { children, mount: Tag, ...rest } = this.props;
170+
let element = children(this.state.isIntersecting);
171+
172+
if (Tag) {
173+
element = <Tag>{element}</Tag>;
174+
}
175+
176+
return (
177+
<Observer {...rest} onChange={this.handleChange}>
178+
{element}
179+
</Observer>
180+
);
181+
}
182+
}
183+
```
184+
185+
```jsx
186+
import React from 'react';
187+
import ViewableMonitor from './ViewableMonitor';
188+
189+
export default () => (
190+
<ViewableMonitor>
191+
{isViewable =>
192+
isViewable ? 'I am viewable' : 'I am still hiding'
193+
}
194+
</ViewableMonitor>
195+
);
196+
```
153197

154198
Discover more recipes in our [examples section](docs/README.md).
155199

@@ -191,8 +235,8 @@ Single React component or element that is used as the target (observable).
191235
* Changes happen asynchronously, similar to the way `requestIdleCallback` works.
192236
* Although you can consider callbacks immediate - always below 1 second - you can also get an immediate response on an
193237
element's visibility with `observer.takeRecords()`.
194-
* The primitives `Map` an `Set` are required. You may need to include a polyfill
195-
for browsers lacking ES2015 support. If you're using babel, include `"babel-polyfill"` somewhere to your codebase.
238+
* The primitives `Map` an `Set` are required. You may need to include a polyfill for browsers lacking ES2015 support. If
239+
you're using babel, include `"babel-polyfill"` somewhere to your codebase.
196240

197241
## Polyfill
198242

0 commit comments

Comments
 (0)