Skip to content

Commit d6bf2e7

Browse files
committed
feat: update useOnInView implementation and adjust package limits
1 parent 75f9f28 commit d6bf2e7

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

docs/Recipes.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ export default LazyAnimation;
106106
## Track impressions
107107

108108
You can use `IntersectionObserver` to track when a user views your element, and
109-
fire an event on your tracking service.
109+
fire an event on your tracking service. Consider using the `useOnInView` to
110+
trigger changes via a callback.
110111

111112
- Set `triggerOnce`, to only trigger an event the first time the element enters
112113
the viewport.
@@ -115,22 +116,20 @@ fire an event on your tracking service.
115116
- Instead of `threshold`, you can use `rootMargin` to have a fixed amount be
116117
visible before triggering. Use a negative margin value, like `-100px 0px`, to
117118
have it go inwards. You can also use a percentage value, instead of pixels.
118-
- You can use the `onChange` callback to trigger the tracking.
119119

120120
```jsx
121121
import * as React from "react";
122-
import { useInView } from "react-intersection-observer";
122+
import { useOnInView } from "react-intersection-observer";
123123

124124
const TrackImpression = () => {
125-
const { ref } = useInView({
126-
triggerOnce: true,
127-
rootMargin: "-100px 0",
128-
onChange: (inView) => {
125+
const ref = useOnInView((inView) => {
129126
if (inView) {
130-
// Fire a tracking event to your tracking service of choice.
131-
dataLayer.push("Section shown"); // Here's a GTM dataLayer push
127+
// Fire a tracking event to your tracking service of choice.
128+
dataLayer.push("Section shown"); // Here's a GTM dataLayer push
132129
}
133-
},
130+
}, {
131+
triggerOnce: true,
132+
rootMargin: "-100px 0",
134133
});
135134

136135
return (

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,25 @@
9494
"path": "dist/index.mjs",
9595
"name": "InView",
9696
"import": "{ InView }",
97-
"limit": "1.8 kB"
97+
"limit": "1.5 kB"
9898
},
9999
{
100100
"path": "dist/index.mjs",
101101
"name": "useInView",
102102
"import": "{ useInView }",
103103
"limit": "1.3 kB"
104104
},
105+
{
106+
"path": "dist/index.mjs",
107+
"name": "useOnInView",
108+
"import": "{ useOnInView }",
109+
"limit": "1.1 kB"
110+
},
105111
{
106112
"path": "dist/index.mjs",
107113
"name": "observe",
108114
"import": "{ observe }",
109-
"limit": "1 kB"
115+
"limit": "0.9 kB"
110116
}
111117
],
112118
"peerDependencies": {

src/useOnInView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ export const useOnInView = <TElement extends Element>(
8383
}
8484

8585
if (!element || skip) {
86-
observedElementRef.current = element ?? null;
8786
cleanupExisting();
87+
observedElementRef.current = null;
8888
return;
8989
}
9090

0 commit comments

Comments
 (0)