Skip to content

Commit 33efbcb

Browse files
authored
docs: update readme and recipes (#686)
* docs: update readme and recipes * Update Recipes.md
1 parent a6b92e9 commit 33efbcb

2 files changed

Lines changed: 25 additions & 31 deletions

File tree

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ const Component = () => (
123123
export default Component;
124124
```
125125

126-
> **Note**<br> When rendering a plain child, make sure you keep your HTML output
126+
> [!NOTE]
127+
> When rendering a plain child, make sure you keep your HTML output
127128
> semantic. Change the `as` to match the context, and add a `className` to style
128129
> the `<InView />`. The component does not support Ref Forwarding, so if you
129130
> need a `ref` to the HTML element, use the Render Props version instead.
@@ -138,7 +139,7 @@ Provide these as the options argument in the `useInView` hook or as props on the
138139
| Name | Type | Default | Description |
139140
| ---------------------- | ------------------------- | ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
140141
| **root** | `Element` | `document` | The Intersection Observer interface's read-only root property identifies the Element or Document whose bounds are treated as the bounding box of the viewport for the element which is the observer's target. If the root is `null`, then the bounds of the actual document viewport are used. |
141-
| **rootMargin** | `string` | `'0px'` | Margin around the root. Can have values similar to the CSS margin property, e.g. `"10px 20px 30px 40px"` (top, right, bottom, left). Also supports percentages, to check if an element intersects with the center of the viewport for example `"-50% 0% -50% 0%"`. |
142+
| **rootMargin** | `string` | `'0px'` | Margin around the root. Can have values similar to the CSS margin property, e.g. `"10px 20px 30px 40px"` (top, right, bottom, left). Also supports percentages, to check if an element intersects with the center of the viewport for example `"-50% 0% -50% 0%"`. |
142143
| **threshold** | `number` or `number[]` | `0` | Number between `0` and `1` indicating the percentage that should be visible before triggering. Can also be an array of numbers, to create multiple trigger points. |
143144
| **onChange** | `(inView, entry) => void` | `undefined` | Call this function whenever the in view state changes. It will receive the `inView` boolean, alongside the current `IntersectionObserverEntry`. |
144145
| **trackVisibility** 🧪 | `boolean` | `false` | A boolean indicating whether this Intersection Observer will track visibility changes on the target. |
@@ -191,7 +192,7 @@ few ideas for how you can use it.
191192
- [Lazy image load](docs/Recipes.md#lazy-image-load)
192193
- [Trigger animations](docs/Recipes.md#trigger-animations)
193194
- [Track impressions](docs/Recipes.md#track-impressions) _(Google Analytics, Tag
194-
Manager, etc)_
195+
Manager, etc.)_
195196
196197
## FAQ
197198
@@ -489,7 +490,8 @@ const destroy = observe(element, callback, options);
489490
The `observe` method returns an `unobserve` function, that you must call in
490491
order to destroy the observer again.
491492
492-
> **Warning**<br> You most likely won't need this, but it can be useful if you
493+
> [!IMPORTANT]
494+
> You most likely won't need this, but it can be useful if you
493495
> need to handle IntersectionObservers outside React, or need full control over
494496
> how instances are created.
495497

docs/Recipes.md

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,38 @@ build it according to your needs.
2525
- Either hide the `<img />` with CSS, or skip rendering it until it's inside the
2626
viewport.
2727

28-
> 🔥 The example has been expanded to include support for the new native
29-
> `loading` attribute on images. If it's supported, we can skip the `useInView`
30-
> hook and render the `<img>`.
28+
> [!TIP]
29+
> All modern browsers support the native `loading` attribute on `<img />` tags, so unless you need
30+
> fine-grained control, you can skip the `IntersectionObserver` and use `loading="lazy"` instead.
3131
>
32-
> See https://web.dev/native-lazy-loading for details about using the `loading`
33-
> attribute.
34-
>
35-
> [@charlietango/use-native-lazy-loading](https://www.npmjs.com/package/@charlietango/use-native-lazy-loading)
36-
> is a small hook that detects support for `loading` as a side effect.
32+
> https://web.dev/articles/browser-level-image-lazy-loading
3733
3834
```jsx
39-
import React from 'react';
40-
import useNativeLazyLoading from '@charlietango/use-native-lazy-loading';
41-
import { useInView } from 'react-intersection-observer';
35+
import React from "react";
36+
import { useInView } from "react-intersection-observer";
4237

4338
const LazyImage = ({ width, height, src, ...rest }) => {
44-
const supportsLazyLoading = useNativeLazyLoading();
4539
const { ref, inView } = useInView({
4640
triggerOnce: true,
47-
rootMargin: '200px 0px',
48-
skip: supportsLazyLoading !== false,
41+
rootMargin: "200px 0px",
4942
});
5043

5144
return (
5245
<div
5346
ref={ref}
5447
style={{
55-
position: 'relative',
48+
position: "relative",
5649
paddingBottom: `${(height / width) * 100}%`,
57-
background: '#2a4b7a',
50+
background: "#2a4b7a",
5851
}}
5952
>
60-
{inView || supportsLazyLoading ? (
53+
{inView ? (
6154
<img
6255
{...rest}
6356
src={src}
6457
width={width}
6558
height={height}
66-
loading="lazy"
67-
style={{ position: 'absolute', width: '100%', height: '100%' }}
59+
style={{ position: "absolute", width: "100%", height: "100%" }}
6860
/>
6961
) : null}
7062
</div>
@@ -89,19 +81,19 @@ for an IntersectionObserver.
8981
have it go inwards. You can also use a percentage value, instead of pixels.
9082

9183
```jsx
92-
import React from 'react';
93-
import { useInView } from 'react-intersection-observer';
84+
import React from "react";
85+
import { useInView } from "react-intersection-observer";
9486

9587
const LazyAnimation = () => {
9688
const { ref, inView } = useInView({
9789
triggerOnce: true,
98-
rootMargin: '-100px 0px',
90+
rootMargin: "-100px 0px",
9991
});
10092

10193
return (
10294
<div
10395
ref={ref}
104-
className={`transition-opacity ${inView ? 'opacity-1' : 'opacity-0'}`}
96+
className={`transition-opacity ${inView ? "opacity-1" : "opacity-0"}`}
10597
>
10698
<span aria-label="Wave">👋</span>
10799
</div>
@@ -126,17 +118,17 @@ fire an event on your tracking service.
126118
- You can use the `onChange` callback to trigger the tracking.
127119

128120
```jsx
129-
import * as React from 'react';
130-
import { useInView } from 'react-intersection-observer';
121+
import * as React from "react";
122+
import { useInView } from "react-intersection-observer";
131123

132124
const TrackImpression = () => {
133125
const { ref } = useInView({
134126
triggerOnce: true,
135-
rootMargin: '-100px 0',
127+
rootMargin: "-100px 0",
136128
onChange: (inView) => {
137129
if (inView) {
138130
// Fire a tracking event to your tracking service of choice.
139-
dataLayer.push('Section shown'); // Here's a GTM dataLayer push
131+
dataLayer.push("Section shown"); // Here's a GTM dataLayer push
140132
}
141133
},
142134
});

0 commit comments

Comments
 (0)