react-intersection-observer
Advanced tools
Comparing version 9.0.0 to 9.1.0
@@ -31,2 +31,4 @@ import * as React from 'react'; | ||
delay?: number; | ||
/** Call this function whenever the in view state changes */ | ||
onChange?: (inView: boolean, entry: IntersectionObserverEntry) => void; | ||
} | ||
@@ -40,4 +42,2 @@ export interface IntersectionObserverProps extends IntersectionOptions { | ||
children: (fields: RenderProps) => React.ReactNode; | ||
/** Call this function whenever the in view state changes */ | ||
onChange?: (inView: boolean, entry: IntersectionObserverEntry) => void; | ||
} | ||
@@ -44,0 +44,0 @@ /** |
{ | ||
"name": "react-intersection-observer", | ||
"version": "9.0.0", | ||
"version": "9.1.0", | ||
"description": "Monitor if a component is inside the viewport, using IntersectionObserver API", | ||
@@ -5,0 +5,0 @@ "source": "src/index.tsx", |
@@ -385,8 +385,12 @@ import * as React from 'react'; | ||
initialInView, | ||
fallbackInView | ||
fallbackInView, | ||
onChange | ||
} = {}) { | ||
const unobserve = React.useRef(); | ||
const callback = React.useRef(); | ||
const [state, setState] = React.useState({ | ||
inView: !!initialInView | ||
}); | ||
}); // Store the onChange callback in a `ref`, so we can access the latest instance inside the `useCallback`. | ||
callback.current = onChange; | ||
const setRef = React.useCallback(node => { | ||
@@ -407,2 +411,3 @@ if (unobserve.current !== undefined) { | ||
}); | ||
if (callback.current) callback.current(inView, entry); | ||
@@ -409,0 +414,0 @@ if (entry.isIntersecting && triggerOnce && unobserve.current) { |
@@ -439,5 +439,7 @@ var React = require('react'); | ||
initialInView = _ref.initialInView, | ||
fallbackInView = _ref.fallbackInView; | ||
fallbackInView = _ref.fallbackInView, | ||
onChange = _ref.onChange; | ||
var unobserve = React__namespace.useRef(); | ||
var callback = React__namespace.useRef(); | ||
@@ -448,4 +450,6 @@ var _React$useState = React__namespace.useState({ | ||
state = _React$useState[0], | ||
setState = _React$useState[1]; | ||
setState = _React$useState[1]; // Store the onChange callback in a `ref`, so we can access the latest instance inside the `useCallback`. | ||
callback.current = onChange; | ||
var setRef = React__namespace.useCallback(function (node) { | ||
@@ -466,2 +470,3 @@ if (unobserve.current !== undefined) { | ||
}); | ||
if (callback.current) callback.current(inView, entry); | ||
@@ -468,0 +473,0 @@ if (entry.isIntersecting && triggerOnce && unobserve.current) { |
@@ -419,5 +419,7 @@ import * as React from 'react'; | ||
initialInView = _ref.initialInView, | ||
fallbackInView = _ref.fallbackInView; | ||
fallbackInView = _ref.fallbackInView, | ||
onChange = _ref.onChange; | ||
var unobserve = React.useRef(); | ||
var callback = React.useRef(); | ||
@@ -428,4 +430,6 @@ var _React$useState = React.useState({ | ||
state = _React$useState[0], | ||
setState = _React$useState[1]; | ||
setState = _React$useState[1]; // Store the onChange callback in a `ref`, so we can access the latest instance inside the `useCallback`. | ||
callback.current = onChange; | ||
var setRef = React.useCallback(function (node) { | ||
@@ -446,2 +450,3 @@ if (unobserve.current !== undefined) { | ||
}); | ||
if (callback.current) callback.current(inView, entry); | ||
@@ -448,0 +453,0 @@ if (entry.isIntersecting && triggerOnce && unobserve.current) { |
@@ -442,5 +442,7 @@ (function (global, factory) { | ||
initialInView = _ref.initialInView, | ||
fallbackInView = _ref.fallbackInView; | ||
fallbackInView = _ref.fallbackInView, | ||
onChange = _ref.onChange; | ||
var unobserve = React__namespace.useRef(); | ||
var callback = React__namespace.useRef(); | ||
@@ -451,4 +453,6 @@ var _React$useState = React__namespace.useState({ | ||
state = _React$useState[0], | ||
setState = _React$useState[1]; | ||
setState = _React$useState[1]; // Store the onChange callback in a `ref`, so we can access the latest instance inside the `useCallback`. | ||
callback.current = onChange; | ||
var setRef = React__namespace.useCallback(function (node) { | ||
@@ -469,2 +473,3 @@ if (unobserve.current !== undefined) { | ||
}); | ||
if (callback.current) callback.current(inView, entry); | ||
@@ -471,0 +476,0 @@ if (entry.isIntersecting && triggerOnce && unobserve.current) { |
@@ -5,4 +5,3 @@ # react-intersection-observer | ||
[![GZipped size][npm-minzip-svg]][bundlephobia-url] | ||
[![Test][test-image]][test-url] | ||
[![License][license-image]][license-url] | ||
[![Test][test-image]][test-url] [![License][license-image]][license-url] | ||
[![Downloads][downloads-image]][downloads-url] | ||
@@ -26,7 +25,9 @@ | ||
- ⚙️ **Matches native API** - Intuitive to use | ||
- 🛠 **Written in TypeScript** - It'll fit right into your existing TypeScript project | ||
- 🛠 **Written in TypeScript** - It'll fit right into your existing TypeScript | ||
project | ||
- 🧪 **Ready to test** - Mocks the Intersection Observer for easy testing with | ||
[Jest](https://jestjs.io/) | ||
- 🌳 **Tree-shakeable** - Only include the parts you use | ||
- 💥 **Tiny bundle** [~1.8 kB gzipped][bundlephobia-url] | ||
- 💥 **Tiny bundle** - Around **~1.15kB** for `useInView` and **~1.6kB** for | ||
`<InView>` | ||
@@ -151,13 +152,14 @@ ## Installation | ||
| Name | Type | Default | Required | Description | | ||
| ---------------------- | ---------------------- | --------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| **root** | `Element` | document | false | The IntersectionObserver 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. | | ||
| **rootMargin** | `string` | '0px' | false | Margin around the root. Can have values similar to the CSS margin property, e.g. "10px 20px 30px 40px" (top, right, bottom, left). | | ||
| **threshold** | `number` \| `number[]` | 0 | false | 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. | | ||
| **trackVisibility** 🧪 | `boolean` | false | false | A boolean indicating whether this IntersectionObserver will track visibility changes on the target. | | ||
| **delay** 🧪 | `number` | undefined | false | A number indicating the minimum delay in milliseconds between notifications from this observer for a given target. This must be set to at least `100` if `trackVisibility` is `true`. | | ||
| **skip** | `boolean` | false | false | Skip creating the IntersectionObserver. You can use this to enable and disable the observer as needed. If `skip` is set while `inView`, the current state will still be kept. | | ||
| **triggerOnce** | `boolean` | false | false | Only trigger the observer once. | | ||
| **initialInView** | `boolean` | false | false | Set the initial value of the `inView` boolean. This can be used if you expect the element to be in the viewport to start with, and you want to trigger something when it leaves. | | ||
| **fallbackInView** | `boolean` | undefined | false | If the `IntersectionObserver` API isn't available in the client, the default behavior is to throw an Error. You can set a specific fallback behavior, and the `inView` value will be set to this instead of failing. To set a global default, you can set it with the `defaultFallbackInView()` | | ||
| Name | Type | Default | Required | Description | | ||
| ---------------------- | ------------------------- | --------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| **root** | `Element` | document | false | The IntersectionObserver 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. | | ||
| **rootMargin** | `string` | '0px' | false | Margin around the root. Can have values similar to the CSS margin property, e.g. "10px 20px 30px 40px" (top, right, bottom, left). | | ||
| **threshold** | `number` or `number[]` | 0 | false | 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. | | ||
| **onChange** | `(inView, entry) => void` | undefined | false | Call this function whenever the in view state changes. It will receive the `inView` boolean, alongside the current `IntersectionObserverEntry`. | | ||
| **trackVisibility** 🧪 | `boolean` | false | false | A boolean indicating whether this IntersectionObserver will track visibility changes on the target. | | ||
| **delay** 🧪 | `number` | undefined | false | A number indicating the minimum delay in milliseconds between notifications from this observer for a given target. This must be set to at least `100` if `trackVisibility` is `true`. | | ||
| **skip** | `boolean` | false | false | Skip creating the IntersectionObserver. You can use this to enable and disable the observer as needed. If `skip` is set while `inView`, the current state will still be kept. | | ||
| **triggerOnce** | `boolean` | false | false | Only trigger the observer once. | | ||
| **initialInView** | `boolean` | false | false | Set the initial value of the `inView` boolean. This can be used if you expect the element to be in the viewport to start with, and you want to trigger something when it leaves. | | ||
| **fallbackInView** | `boolean` | undefined | false | If the `IntersectionObserver` API isn't available in the client, the default behavior is to throw an Error. You can set a specific fallback behavior, and the `inView` value will be set to this instead of failing. To set a global default, you can set it with the `defaultFallbackInView()` | | ||
@@ -168,7 +170,6 @@ ### InView Props | ||
| Name | Type | Default | Required | Description | | ||
| ------------ | -------------------------------------------------------- | ------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| **as** | `string` | 'div' | false | Render the wrapping element as this element. Defaults to `div`. | | ||
| **children** | `({ref, inView, entry}) => React.ReactNode`, `ReactNode` | | true | Children expects a function that receives an object containing the `inView` boolean and a `ref` that should be assigned to the element root. Alternatively pass a plain child, to have the `<InView />` deal with the wrapping element. You will also get the `IntersectionObserverEntry` as `entry, giving you more details. | | ||
| **onChange** | `(inView, entry) => void` | | false | Call this function whenever the in view state changes. It will receive the `inView` boolean, alongside the current `IntersectionObserverEntry`. | | ||
| Name | Type | Default | Required | Description | | ||
| ------------ | ---------------------------------------------------- | --------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| **as** | `string` | 'div' | false | Render the wrapping element as this element. Defaults to `div`. | | ||
| **children** | `({ref, inView, entry}) => ReactNode` or `ReactNode` | undefined | true | Children expects a function that receives an object containing the `inView` boolean and a `ref` that should be assigned to the element root. Alternatively pass a plain child, to have the `<InView />` deal with the wrapping element. You will also get the `IntersectionObserverEntry` as `entry, giving you more details. | | ||
@@ -175,0 +176,0 @@ ### IntersectionObserver v2 🧪 |
@@ -28,2 +28,2 @@ import type { InViewHookResponse, IntersectionOptions } from './index'; | ||
*/ | ||
export declare function useInView({ threshold, delay, trackVisibility, rootMargin, root, triggerOnce, skip, initialInView, fallbackInView, }?: IntersectionOptions): InViewHookResponse; | ||
export declare function useInView({ threshold, delay, trackVisibility, rootMargin, root, triggerOnce, skip, initialInView, fallbackInView, onChange, }?: IntersectionOptions): InViewHookResponse; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
206996
1985
468