@altano/use-element-observer 1.0.7 → 2.0.0
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.
- package/dist/Observer.d.ts +7 -0
- package/dist/index.js +22 -7
- package/dist/useElementObserver.d.ts +5 -1
- package/dist/useMountRef.d.ts +7 -0
- package/package.json +20 -11
package/dist/index.js
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
// src/useElementObserver.tsx
|
|
2
|
-
import
|
|
2
|
+
import React3 from "react";
|
|
3
3
|
import { useSet } from "@uidotdev/usehooks";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
|
|
5
|
+
// src/Observer.tsx
|
|
6
|
+
import React2, { useCallback, useContext } from "react";
|
|
7
|
+
|
|
8
|
+
// src/useMountRef.tsx
|
|
9
|
+
import { useEffect, useRef } from "react";
|
|
10
|
+
function useMountRef({
|
|
11
|
+
onMount,
|
|
12
|
+
onUnmount
|
|
13
|
+
}) {
|
|
7
14
|
const newRef = useRef(null);
|
|
8
15
|
useEffect(() => {
|
|
9
16
|
const ref = newRef.current;
|
|
@@ -16,6 +23,9 @@ function useMountRef({ onMount, onUnmount }) {
|
|
|
16
23
|
}, [newRef, onMount, onUnmount]);
|
|
17
24
|
return newRef;
|
|
18
25
|
}
|
|
26
|
+
|
|
27
|
+
// src/Observer.tsx
|
|
28
|
+
import { jsx } from "react/jsx-runtime";
|
|
19
29
|
function ObserveChildWithWrapperDiv({
|
|
20
30
|
children,
|
|
21
31
|
onMount,
|
|
@@ -30,8 +40,8 @@ function ObserveChild({
|
|
|
30
40
|
onUnmount
|
|
31
41
|
}) {
|
|
32
42
|
const newRef = useMountRef({ onMount, onUnmount });
|
|
33
|
-
const child =
|
|
34
|
-
return
|
|
43
|
+
const child = React2.Children.only(children);
|
|
44
|
+
return React2.cloneElement(child, { ref: newRef });
|
|
35
45
|
}
|
|
36
46
|
function Observer({
|
|
37
47
|
children,
|
|
@@ -58,15 +68,20 @@ function Observer({
|
|
|
58
68
|
const Component = useWrapperDiv ? ObserveChildWithWrapperDiv : ObserveChild;
|
|
59
69
|
return /* @__PURE__ */ jsx(Component, { onMount, onUnmount, children });
|
|
60
70
|
}
|
|
71
|
+
|
|
72
|
+
// src/useElementObserver.tsx
|
|
73
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
74
|
+
var ElementObserverContext = React3.createContext(null);
|
|
61
75
|
function useElementObserver({
|
|
62
76
|
tree,
|
|
63
77
|
selector,
|
|
64
78
|
useWrapperDiv = true
|
|
65
79
|
}) {
|
|
66
80
|
const mountedElements = useSet();
|
|
67
|
-
const observedTree = /* @__PURE__ */
|
|
81
|
+
const observedTree = /* @__PURE__ */ jsx2(ElementObserverContext.Provider, { value: { mountedElements }, children: /* @__PURE__ */ jsx2(Observer, { selector, useWrapperDiv, children: tree }) });
|
|
68
82
|
return [mountedElements, observedTree];
|
|
69
83
|
}
|
|
70
84
|
export {
|
|
85
|
+
ElementObserverContext,
|
|
71
86
|
useElementObserver
|
|
72
87
|
};
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
interface
|
|
2
|
+
interface Context {
|
|
3
|
+
mountedElements: Set<Element>;
|
|
4
|
+
}
|
|
5
|
+
export declare const ElementObserverContext: React.Context<Context | null>;
|
|
6
|
+
export interface Options {
|
|
3
7
|
selector: string;
|
|
4
8
|
tree: React.ReactNode;
|
|
5
9
|
useWrapperDiv?: boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@altano/use-element-observer",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Intersection-Observer-based Scroll Spy React Hook",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"email": "altano@gmail.com",
|
|
16
16
|
"url": "https://alan.norbauer.com"
|
|
17
17
|
},
|
|
18
|
-
"license": "
|
|
18
|
+
"license": "ISC",
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
21
21
|
"url": "https://github.com/altano/npm-packages",
|
|
@@ -29,28 +29,37 @@
|
|
|
29
29
|
"react": "^18"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
+
"@testing-library/jest-dom": "^6.4.2",
|
|
33
|
+
"@testing-library/react": "^14.2.2",
|
|
32
34
|
"@types/react": "^18.2.64",
|
|
33
35
|
"@types/react-dom": "^18.2.21",
|
|
36
|
+
"@vitejs/plugin-react": "^4.2.1",
|
|
37
|
+
"@vitest/coverage-v8": "^1.4.0",
|
|
34
38
|
"eslint": "^8.57.0",
|
|
39
|
+
"jsdom": "^24.0.0",
|
|
35
40
|
"prettier": "^3.2.5",
|
|
36
41
|
"react": "^18.2.0",
|
|
37
|
-
"tsup": "^8.0.2",
|
|
38
42
|
"typescript": "^5.4.2",
|
|
43
|
+
"vitest": "^1.4.0",
|
|
39
44
|
"@altano/tsconfig": "0.0.0",
|
|
40
|
-
"eslint-config-altano": "0.0.
|
|
45
|
+
"eslint-config-altano": "0.0.0"
|
|
41
46
|
},
|
|
42
47
|
"publishConfig": {
|
|
43
48
|
"access": "public"
|
|
44
49
|
},
|
|
45
50
|
"scripts": {
|
|
46
|
-
"build": "tsup
|
|
47
|
-
"build:types": "tsc --
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"lint:timing": "TIMING=1 eslint src/**/*.ts*",
|
|
51
|
-
"lint:fix": "TIMING=1 eslint src/**/*.ts* --fix",
|
|
51
|
+
"build": "tsup --config build-config/tsup.config.browser.ts",
|
|
52
|
+
"build:types": "tsc --project tsconfig.declarations.json",
|
|
53
|
+
"clean": "rm -rf .turbo && rm -rf .tsbuildinfo && rm -rf node_modules && rm -rf dist",
|
|
54
|
+
"dev": "pnpm run build --watch",
|
|
52
55
|
"format": "prettier --check src",
|
|
53
56
|
"format:fix": "prettier --write src",
|
|
54
|
-
"
|
|
57
|
+
"lint": "eslint package.json src/**/*.ts*",
|
|
58
|
+
"lint:fix": "TIMING=1 pnpm lint --fix",
|
|
59
|
+
"lint:timing": "TIMING=1 pnpm lint",
|
|
60
|
+
"test:unit": "vitest --run",
|
|
61
|
+
"test:unit:watch": "vitest",
|
|
62
|
+
"typecheck:src": "tsc --noEmit",
|
|
63
|
+
"typecheck:test": "tsc --noEmit --project ./tests/tsconfig.json"
|
|
55
64
|
}
|
|
56
65
|
}
|