-
-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Improve useIntersectionObserver (#464)
* ✨ Improve useIntersectionObserver * 🔖 Add changeset * 🐛 Typo
- Loading branch information
Showing
4 changed files
with
189 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'usehooks-ts': minor | ||
--- | ||
|
||
Updated `useIntersectionObserver` API and fixed #395, #271 and #182, see #464. |
12 changes: 6 additions & 6 deletions
12
packages/usehooks-ts/src/useIntersectionObserver/useIntersectionObserver.demo.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 16 additions & 7 deletions
23
packages/usehooks-ts/src/useIntersectionObserver/useIntersectionObserver.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,24 @@ | ||
This React Hook detects visibility of a component on the viewport using the [`IntersectionObserver` API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) natively present in the browser. | ||
|
||
It can be very useful to lazy-loading of images, implementing "infinite scrolling" or starting animations for example. | ||
It can be very useful to lazy-loading of images, implementing "infinite scrolling", tracking view in GA or starting animations for example. | ||
|
||
Your must pass the ref element (from `useRef()`). | ||
### Option properties | ||
|
||
It takes optionally `root`, `rootMargin` and `threshold` arguments from the [native `IntersectionObserver` API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) and `freezeOnceVisible` to only catch the first appearance too. | ||
- `threshold` (optional, default: `0`): A threshold indicating the percentage of the target's visibility needed to trigger the callback. Can be a single number or an array of numbers. | ||
- `root` (optional, default: `null`): The element that is used as the viewport for checking visibility of the target. It can be an Element, Document, or null. | ||
- `rootMargin` (optional, default: `'0%'`): A margin around the root. It specifies the size of the root's margin area. | ||
- `freezeOnceVisible` (optional, default: `false`): If true, freezes the intersection state once the element becomes visible. Once the element enters the viewport and triggers the callback, further changes in intersection will not update the state. | ||
- `onChange` (optional): A callback function to be invoked when the intersection state changes. It receives two parameters: `isIntersecting` (a boolean indicating if the element is intersecting) and `entry` (an IntersectionObserverEntry object representing the state of the intersection). | ||
- `initialIsIntersecting` (optional, default: `false`): The initial state of the intersection. If set to true, indicates that the element is intersecting initially. | ||
|
||
It returns the full IntersectionObserver's `entry` object. | ||
**Note:** This interface extends the native `IntersectionObserverInit` interface, which provides the base options for configuring the Intersection Observer. | ||
|
||
<br /> | ||
For more information on the Intersection Observer API and its options, refer to the [MDN Intersection Observer API documentation](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API). | ||
|
||
**Source:** | ||
### Return | ||
|
||
I discovered this way of using `IntersectionObserver` via this [post medium](https://medium.com/the-non-traditional-developer/how-to-use-an-intersectionobserver-in-a-react-hook-9fb061ac6cb5) while playing to build a [lazy-loaded collection of images](https://react-gallery.juliencaron.com/). | ||
The `IntersectionResult` type supports both array and object destructuring and includes the following properties: | ||
|
||
- `ref`: A function that can be used as a ref callback to set the target element. | ||
- `isIntersecting`: A boolean indicating if the target element is intersecting with the viewport. | ||
- `entry`: An optional `IntersectionObserverEntry` object representing the state of the intersection. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters