@byndyusoft-ui/use-intersection-observer 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ node_modules
2
+ dist
3
+ .turbo
package/README.md ADDED
@@ -0,0 +1,119 @@
1
+ # `@byndyusoft-ui/use-intersection-observer`
2
+ ---
3
+ > A React hook that provides a simple and flexible way to use the Intersection Observer API, allowing you to track the visibility of DOM elements and react to their intersection with a specified root element or the viewport.
4
+
5
+ ### Installation
6
+
7
+ ```
8
+ npm i @byndyusoft-ui/use-intersection-observer
9
+ ```
10
+
11
+ ### Usage `useIntersectionObserver` hook
12
+ ```js
13
+ // Use object destructuring, so you don't need to remember the exact order
14
+ const { isIntersecting, entry } = useIntersectionObserver(ref, options);
15
+
16
+ // Or array destructuring, making it easy to customize the field names
17
+ const [isIntersecting, entry] = useIntersectionObserver(ref, options);
18
+ ```
19
+
20
+ #### Default
21
+
22
+ ```jsx
23
+ import React, { useRef } from "react";
24
+ import {useIntersectionObserver} from "@byndyusoft-ui/use-intersection-observer";
25
+
26
+ const Component = () => {
27
+ const ref = useRef<HTMLDivElement | null>(null);
28
+ const { isIntersecting, entry } = useIntersectionObserver(ref);
29
+
30
+ return (
31
+ <div class="scroll-container">
32
+ <div ref={ref}>
33
+ {`isIntersecting: ${isIntersecting}`}
34
+ </div>
35
+ </div>
36
+ );
37
+ };
38
+ ```
39
+
40
+ ### Usage of `useIntersectionObserver` for Unmounted or Lazy-Loaded Components
41
+
42
+
43
+
44
+ ```jsx
45
+ import React, { useState } from "react";
46
+ import { useIntersectionObserver } from "@byndyusoft-ui/use-intersection-observer";
47
+
48
+ const Component = () => {
49
+ const [ref, setRef] = useState<HTMLDivElement | null>(null);
50
+ const [isVisible, setIsVisible] = useState<boolean>(false);
51
+
52
+ const { isIntersecting, entry } = useIntersectionObserver({ current: ref });
53
+
54
+ return (
55
+ <div class="scroll-container">
56
+ <button onClick={() => setIsVisible(p => !p)}>Toggle visible</button>
57
+ {isVisible && (
58
+ <div ref={setRef}>
59
+ {`isIntersecting: ${isIntersecting}`}
60
+ </div>
61
+ )}
62
+ </div>
63
+ );
64
+ };
65
+ ```
66
+
67
+ > The example below demonstrates a non-standard approach to using `useIntersectionObserver` with components that can be
68
+ > unmounted or lazy-loaded. This method involves using `useState` to manage the reference to the target element,
69
+ > which ensures that the intersection observer works correctly even when the target element is dynamically mounted or unmounted.
70
+
71
+
72
+ ### Usage `useIntersectionObserver` with options
73
+
74
+ ```jsx
75
+ import React from "react";
76
+ import { useIntersectionObserver } from "@byndyusoft-ui/use-intersection-observer";
77
+
78
+ const Component = () => {
79
+ const tergetRef = useRef<HTMLDivElement | null>(null);
80
+ const containerRef = useRef<HTMLDivElement | null>(null);
81
+
82
+ const { isIntersecting, entry } = useIntersectionObserver(tergetRef, {
83
+ root: scrollContainerRef.current,
84
+ rootMargin: "10px",
85
+ threshold: 0.5,
86
+ triggerOnce: false,
87
+ skip: false,
88
+ isIntersectingInitial: false,
89
+ isIntersectingFallback: false,
90
+ trackVisibility: false, // experimental
91
+ delay: 1500, // experimental
92
+ onChange: (isIntersecting, entry) => console.log(isIntersecting, entry),
93
+ });
94
+
95
+ return (
96
+ <div ref={containerRef} class="scroll-container">
97
+ <div ref={tergetRef}>
98
+ {`isIntersecting: ${isIntersecting}`}
99
+ </div>
100
+ </div>
101
+ );
102
+ };
103
+ ```
104
+ ### Options
105
+
106
+ Provide these as the options argument in the `useIntersectionObserver ` hook.
107
+
108
+ | Name | Type | Default | Description |
109
+ |------------------------------------|-----------------------------------| ----------- |---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
110
+ | **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. |
111
+ | **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%"`. |
112
+ | **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. |
113
+ | **onChange** | `(isIntersecting, entry) => void` | `undefined` | Call this function whenever the `isIntersecting` state changes. It will receive the `isIntersecting` boolean, alongside the current `IntersectionObserverEntry`. |
114
+ | **trackVisibility** (experimental) | `boolean` | `false` | A boolean indicating whether this Intersection Observer will track visibility changes on the target. |
115
+ | **delay** (experimental) | `number` | `undefined` | 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`. |
116
+ | **skip** | `boolean` | `false` | Skip creating the IntersectionObserver. You can use this to enable and disable the observer as needed. If `skip` is set while `isIntersecting`, the current state will still be kept. |
117
+ | **triggerOnce** | `boolean` | `false` | Only trigger the observer once. |
118
+ | **isIntersectingInitial** | `boolean` | `false` | Set the initial value of the `isIntersecting` 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. |
119
+ | **isIntersectingFallback** | `boolean` | `undefined` | 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 `isIntersecting` value will be set to this instead of failing. To set a global default, you can set it with the `defaultFallbackInView()` |
@@ -0,0 +1,4 @@
1
+ import useIntersectionObserver from './useIntersectionObserver';
2
+ export type { IUseIntersectionObserverOptions, IUseIntersectionObserverReturn } from './useIntersectionObserver.types';
3
+ export { useIntersectionObserver };
4
+ export default useIntersectionObserver;
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ import useIntersectionObserver from './useIntersectionObserver';
2
+ export { useIntersectionObserver };
3
+ export default useIntersectionObserver;
@@ -0,0 +1,3 @@
1
+ import { MutableRefObject } from 'react';
2
+ import type { IUseIntersectionObserverReturn, IUseIntersectionObserverOptions } from './useIntersectionObserver.types';
3
+ export default function useIntersectionObserver(ref: MutableRefObject<Element | null>, { threshold, delay, trackVisibility, rootMargin, root, triggerOnce, skip, isIntersectingInitial, isIntersectingFallback, onChange }?: IUseIntersectionObserverOptions): IUseIntersectionObserverReturn;
@@ -0,0 +1,61 @@
1
+ import { useEffect, useRef, useState } from 'react';
2
+ import { observe } from './utilities/useIntersectionObserver.utilities';
3
+ export default function useIntersectionObserver(ref, { threshold, delay, trackVisibility, rootMargin, root, triggerOnce, skip, isIntersectingInitial, isIntersectingFallback, onChange } = {}) {
4
+ const [isIntersecting, setIsIntersecting] = useState(Boolean(isIntersectingInitial));
5
+ const [entry, setEntry] = useState(undefined);
6
+ const callback = useRef();
7
+ const previousEntryTarget = useRef();
8
+ callback.current = onChange;
9
+ useEffect(() => {
10
+ if (skip || !ref?.current)
11
+ return;
12
+ let unobserve;
13
+ unobserve = observe({
14
+ element: ref.current,
15
+ options: {
16
+ root,
17
+ rootMargin,
18
+ threshold,
19
+ // @ts-expect-error experimental v2 api
20
+ trackVisibility,
21
+ delay
22
+ },
23
+ callback: (isIntersectingValue, entryValue) => {
24
+ setIsIntersecting(isIntersectingValue);
25
+ setEntry(entryValue);
26
+ if (callback.current)
27
+ callback.current(isIntersectingValue, entryValue);
28
+ if (entryValue.isIntersecting && triggerOnce && unobserve) {
29
+ unobserve();
30
+ unobserve = undefined;
31
+ }
32
+ },
33
+ isIntersectingFallback
34
+ });
35
+ return () => {
36
+ unobserve?.();
37
+ };
38
+ }, [
39
+ Array.isArray(threshold) ? threshold.toString() : threshold,
40
+ ref?.current,
41
+ root,
42
+ rootMargin,
43
+ triggerOnce,
44
+ skip,
45
+ trackVisibility,
46
+ isIntersectingFallback,
47
+ delay
48
+ ]);
49
+ const entryTarget = entry?.target;
50
+ if (!ref?.current && entryTarget && !triggerOnce && !skip && previousEntryTarget.current !== entryTarget) {
51
+ previousEntryTarget.current = entryTarget;
52
+ setIsIntersecting(Boolean(isIntersectingInitial));
53
+ setEntry(undefined);
54
+ }
55
+ const tupleReturn = [isIntersecting, entry];
56
+ const objectReturn = {
57
+ isIntersecting,
58
+ entry
59
+ };
60
+ return Object.assign(tupleReturn, objectReturn);
61
+ }
@@ -0,0 +1,40 @@
1
+ export type TObserverInstanceCallback = (isIntersecting: boolean, entry: IntersectionObserverEntry) => void;
2
+ export interface IObserveOptions {
3
+ element: Element;
4
+ callback: TObserverInstanceCallback;
5
+ options: IntersectionObserverInit;
6
+ isIntersectingFallback?: boolean;
7
+ }
8
+ export interface IObserverItem {
9
+ id: string;
10
+ observer: IntersectionObserver;
11
+ elements: Map<Element, Array<TObserverInstanceCallback>>;
12
+ }
13
+ export interface IUseIntersectionObserverOptions extends IntersectionObserverInit {
14
+ /** 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.*/
15
+ root?: Element | Document | null;
16
+ /** Margin around the root. Can have values similar to the CSS margin property, e.g. `10px 20px 30px 40px` (top, right, bottom, left). */
17
+ rootMargin?: string;
18
+ /** 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. */
19
+ threshold?: number | number[];
20
+ /** Only trigger the isIntersecting callback once */
21
+ triggerOnce?: boolean;
22
+ /** Skip assigning the observer to the `ref` */
23
+ skip?: boolean;
24
+ /** Set the initial value of the `isIntersecting` 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. */
25
+ isIntersectingInitial?: boolean;
26
+ /** Fallback to this isIntersecting state if the IntersectionObserver is unsupported, and a polyfill wasn't loaded */
27
+ isIntersectingFallback?: boolean;
28
+ /** IntersectionObserver v2 - Track the actual visibility of the element */
29
+ trackVisibility?: boolean;
30
+ /** IntersectionObserver v2 - Set a minimum delay between notifications */
31
+ delay?: number;
32
+ /** Call this function whenever the in view state changes */
33
+ onChange?: (isIntersecting: boolean, entry: IntersectionObserverEntry) => void;
34
+ }
35
+ export type IUseIntersectionObserverTuple = [boolean, IntersectionObserverEntry | undefined];
36
+ export type IUseIntersectionObserverObject = {
37
+ isIntersecting: boolean;
38
+ entry?: IntersectionObserverEntry;
39
+ };
40
+ export type IUseIntersectionObserverReturn = IUseIntersectionObserverTuple & IUseIntersectionObserverObject;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ import type { IObserveOptions, IObserverItem } from '../useIntersectionObserver.types';
2
+ /**
3
+ * Generate a unique ID for the root element
4
+ */
5
+ export declare function getRootId(root: IntersectionObserverInit['root']): string | undefined;
6
+ /**
7
+ * Convert the options to a string Id, based on the values.
8
+ * Ensures we can reuse the same observer when observing elements with the same options.
9
+ */
10
+ export declare function optionsToId(options: IntersectionObserverInit): string;
11
+ export declare function createObserver(options: IntersectionObserverInit): IObserverItem;
12
+ export declare function observe({ element, callback, options, isIntersectingFallback }: IObserveOptions): () => void;
@@ -0,0 +1,93 @@
1
+ const observerMap = new Map();
2
+ const rootIdsWeakMap = new WeakMap();
3
+ let rootId = 0;
4
+ /**
5
+ * Generate a unique ID for the root element
6
+ */
7
+ export function getRootId(root) {
8
+ if (!root)
9
+ return '0';
10
+ if (rootIdsWeakMap.has(root))
11
+ return rootIdsWeakMap.get(root);
12
+ rootId += 1;
13
+ rootIdsWeakMap.set(root, rootId.toString());
14
+ return rootIdsWeakMap.get(root);
15
+ }
16
+ /**
17
+ * Convert the options to a string Id, based on the values.
18
+ * Ensures we can reuse the same observer when observing elements with the same options.
19
+ */
20
+ export function optionsToId(options) {
21
+ return Object.keys(options)
22
+ .sort()
23
+ .filter(key => options[key] !== undefined)
24
+ .map(key => {
25
+ const value = key === 'root' ? getRootId(options.root) : options[key];
26
+ return `${key}_${value}`;
27
+ })
28
+ .toString();
29
+ }
30
+ export function createObserver(options) {
31
+ const id = optionsToId(options);
32
+ let instance = observerMap.get(id);
33
+ if (instance)
34
+ return instance;
35
+ const elements = new Map();
36
+ let thresholds = [];
37
+ const observer = new IntersectionObserver(entries => {
38
+ entries.forEach(entry => {
39
+ const isIntersecting = entry.isIntersecting && thresholds.some(threshold => entry.intersectionRatio >= threshold);
40
+ // @ts-expect-error support IntersectionObserver v2
41
+ if (options.trackVisibility && typeof entry.isVisible === 'undefined') {
42
+ // The browser doesn't support Intersection Observer v2, falling back to v1 behavior.
43
+ // @ts-expect-error
44
+ entry.isVisible = isIntersecting;
45
+ }
46
+ elements.get(entry.target)?.forEach(callback => {
47
+ callback(isIntersecting, entry);
48
+ });
49
+ });
50
+ }, options);
51
+ thresholds =
52
+ observer.thresholds || (Array.isArray(options.threshold) ? options.threshold : [options.threshold || 0]);
53
+ instance = {
54
+ id,
55
+ observer,
56
+ elements
57
+ };
58
+ observerMap.set(id, instance);
59
+ return instance;
60
+ }
61
+ export function observe({ element, callback, options = {}, isIntersectingFallback }) {
62
+ if (typeof window.IntersectionObserver === 'undefined' && isIntersectingFallback !== undefined) {
63
+ const bounds = element.getBoundingClientRect();
64
+ callback(isIntersectingFallback, {
65
+ isIntersecting: isIntersectingFallback,
66
+ target: element,
67
+ intersectionRatio: typeof options.threshold === 'number' ? options.threshold : 0,
68
+ time: 0,
69
+ boundingClientRect: bounds,
70
+ intersectionRect: bounds,
71
+ rootBounds: bounds
72
+ });
73
+ return () => { };
74
+ }
75
+ const { id, observer, elements } = createObserver(options);
76
+ const callbacks = elements.get(element) || [];
77
+ if (!elements.has(element)) {
78
+ elements.set(element, callbacks);
79
+ }
80
+ callbacks.push(callback);
81
+ observer.observe(element);
82
+ return function unobserve() {
83
+ callbacks.splice(callbacks.indexOf(callback), 1);
84
+ if (callbacks.length === 0) {
85
+ elements.delete(element);
86
+ observer.unobserve(element);
87
+ }
88
+ if (elements.size === 0) {
89
+ observer.disconnect();
90
+ observerMap.delete(id);
91
+ }
92
+ };
93
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@byndyusoft-ui/use-intersection-observer",
3
+ "version": "0.0.1",
4
+ "description": "Byndyusoft UI React Hook",
5
+ "keywords": [
6
+ "byndyusoft",
7
+ "byndyusoft-ui",
8
+ "react",
9
+ "hook",
10
+ "intersection-observer"
11
+ ],
12
+ "author": "Gleb Fomin <gleb.fom28@gmail.com>",
13
+ "homepage": "https://github.com/Byndyusoft/ui/tree/master/hooks/use-intersection-observer#readme",
14
+ "license": "Apache-2.0",
15
+ "main": "dist/index.js",
16
+ "types": "dist/index.d.ts",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/Byndyusoft/ui.git"
20
+ },
21
+ "scripts": {
22
+ "build": "tsc --project tsconfig.build.json",
23
+ "clean": "rimraf dist && rimraf .turbo && rimraf node_modules && rimraf package-lock.json",
24
+ "lint": "eslint src --config ../../eslint.config.js",
25
+ "test": "jest --config ../../jest.config.js --roots hooks/use-intersection-observer/src"
26
+ },
27
+ "bugs": {
28
+ "url": "https://github.com/Byndyusoft/ui/issues"
29
+ },
30
+ "publishConfig": {
31
+ "access": "public"
32
+ }
33
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "exclude": ["src/**/*.stories.*", "src/**/__stories__", "src/**/*.docs.*", "src/**/*.tests.*", "src/**/__tests__"]
4
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "declarationDir": "dist",
6
+ "outDir": "dist",
7
+ "module": "ESNext"
8
+ },
9
+ "include": ["../../types.d.ts", "src"]
10
+ }