Skip to content

Commit 4bbabca

Browse files
committed
feat(recipes): new recipes section with custom Readme files
1 parent 520eb19 commit 4bbabca

File tree

7 files changed

+65
-9
lines changed

7 files changed

+65
-9
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ ReactIntersectionObserver is good at:
2626
+ [No bookkeeping](#no-bookkeeping)
2727
+ [No extra markup](#no-extra-markup)
2828
+ [Easy to adopt](#easy-to-adopt)
29-
* [Demo](#demo)
29+
* [Examples](#examples)
3030
* [Options](#options)
3131
* [Notes](#notes)
3232
* [Polyfill](#polyfill)
@@ -122,12 +122,20 @@ Some of the things you may want to use ReactIntersectionObserver for:
122122
* Occlusion culling - Don't render an object until is close to the viewport edges
123123
* Sentinel Scrolling - Infinite scroller with a recycled Sentinel
124124

125-
## Demo
125+
## Examples
126126

127-
Find multiple examples and usage guidelines in: [https://researchgate.github.io/react-intersection-observer/](https://researchgate.github.io/react-intersection-observer/)
127+
### Demos
128+
129+
Find multiple examples and usage guidelines under: [https://researchgate.github.io/react-intersection-observer/](https://researchgate.github.io/react-intersection-observer/)
128130

129131
[![demo](https://github.com/researchgate/react-intersection-observer/blob/master/.github/demo.gif?raw=true)](https://researchgate.github.io/react-intersection-observer/)
130132

133+
### Recipes
134+
135+
Recipes are useful code snippets solutions to common problems, for example, how to use ReactIntersectionObserver within a [Higher Order Component](https://researchgate.github.io/react-intersection-observer/?selectedKind=Recipes&selectedStory=Higher%20Order%20Component).
136+
137+
Discover more recipes in our [examples section](examples/README.md).
138+
131139
## Options
132140

133141
### root

examples/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Examples
2+
3+
Welcome to the code examples section!
4+
5+
Here you'll find usage examples and recipes. For a full list of examples, take a look in our [storybook page](https://researchgate.github.io/react-intersection-observer/?selectedKind=Examples).
6+
7+
* [Basic usage on Window](https://researchgate.github.io/react-intersection-observer/?selectedKind=Examples&selectedStory=Window)
8+
* [Usage within a scroller/frame](https://researchgate.github.io/react-intersection-observer/?selectedKind=Examples&selectedStory=Frame)
9+
* [Using thresholds](https://researchgate.github.io/react-intersection-observer/?selectedKind=Examples&selectedStory=Thresholds)
10+
* [Using the rootMargin option](https://researchgate.github.io/react-intersection-observer/?selectedKind=Examples&selectedStory=Margin)
11+
* [Using the onlyOnce option](https://researchgate.github.io/react-intersection-observer/?selectedKind=Examples&selectedStory=Once)
12+
13+
## Recipes
14+
15+
Here you [will find useful code snippets](https://researchgate.github.io/react-intersection-observer/?selectedKind=Recipes) to common situations that can be approached with ReactIntersectionObserver:
16+
17+
* [Higher Order Component](https://researchgate.github.io/react-intersection-observer/?selectedKind=Recipes&selectedStory=Higher%20Order%20Component)
18+
* [Tracking Ad Impressions](https://researchgate.github.io/react-intersection-observer/?selectedKind=Recipes&selectedStory=Ad%20Impressions)
19+
20+
### Can I submit a new recipe?
21+
22+
Yes, of course!
23+
24+
1. Fork the code repo.
25+
2. Create your new recipe in the correct subfolder within `./examples/components/` (create a new folder if it doesn't already exist).
26+
3. Make sure you have included a README as well as your source file.
27+
4. Submit a PR.
28+
29+
_If you haven't yet, please read our [contribution guidelines](https://github.com/researchgate/react-intersection-observer/blob/master/.github/CONTRIBUTING.md)._
30+
31+
### What license are the recipes released under?
32+
33+
By default, all newly submitted code is licensed under the MIT license.
34+
35+
### How else can I contribute?
36+
37+
Recipes don't always have to be code - great documentation, tutorials, general tips and even general improvements to our examples folder are greatly appreciated.

examples/components/HigherOrderComponent/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Higher order component
22

3+
A common way to avoid repetiton and dynamic option setting is to use _HOC_. Since our component reuses instances based on **all** the options, you don't have to worry about bookkeeping of instances. Create as many HOC as you need; they can be as flexible as you need them to be.
4+
5+
The next example illustrates in a simple way how a _HOC_ is used to wrap target elements for occlusion culling:
6+
37
### withIntersectionObserver.js
48
```jsx
59
import React, { Component } from 'react';

examples/components/OnlyOnce/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Basic usage of **IntersectionObserver** with **onlyOnce** that will only trigger an event once as soon as **isIntersecting** is true:
1+
The option `onlyOnce` applied to the component will only trigger an event once, when the target detects `isIntersecting` is truthy. This is specially useful when you need a disposable observer, and you want to prevent re-rendering the element after that:
22

33
```jsx
44
import React, { Component } from 'react';
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Usage within a scrollable frame that changes the className of the observed **<div />** element when the visibility changes.
2-
3-
For more see the **<Window />** example.
1+
Usage within a scrollable frame or even iframes should behave as expected. Note that in the example we're using Window as the root, which indicates the observed target triggers changes when intersecting with the viewport, in this case Window.
2+
3+
However, we can always use a different root. Check out the [rootMargin example](https://researchgate.github.io/react-intersection-observer/?selectedKind=Examples&selectedStory=Margin), where we use a different approach to set the `root` option.

examples/components/WithRootMargin/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
Usage of **IntersectionObserver** with negative bottom rootMargin:
1+
The option `rootMargin`'s syntax follows that of the `margin` CSS property. The unit(s) however **must** be provided in either `px` or `%` or both. It can also contain _negative values_, which are very useful to shrink the viewport size. Interestingly a percentage value works great to determine if an item is visible on very different screen sizes, e.g.: mobile vs desktop screens.
2+
3+
Note that in the example below, we conciously show how to set a root to DOM reference using the `disable` option. This option accepts types `Element` and `String`. A more effective way is then to pass a valid `querySelector` string instead.
24

35
```jsx
46
import React, { Component } from 'react';

examples/components/WithThresholds/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
Usage of **IntersectionObserver** with tresholds, triggering an event for each defined points in the array:
1+
The option `threshold` is either a single number or an array of numbers which indicate at what percentage of the target's visibility the observer's callback should be executed. If you only want to detect when visibility passes the 50% mark, you can use a value of 0.5. If you want the callback run every time visibility passes another 25%, you would specify the array [0, 0.25, 0.5, 0.75, 1].
2+
3+
The default is 0 (meaning as soon as even one pixel is visible, the callback will be run).
4+
A value of 1.0 means that the threshold isn't considered passed until every pixel is visible.
5+
6+
Note that when comparing old and new props, our component compares the value of the `treshold` array for convenience, so you don't have to necessarily keep a reference to it. This is specially useful when using within a functional component.
27

38
```jsx
49
import React, { Component } from 'react';

0 commit comments

Comments
 (0)