@altano/use-element-observer 1.0.6 → 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 +33 -14
- package/dist/useElementObserver.d.ts +6 -2
- package/dist/useMountRef.d.ts +7 -0
- package/package.json +28 -18
package/dist/index.js
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
// src/useElementObserver.tsx
|
|
2
|
-
import
|
|
3
|
-
import { useSet } from "
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
import React3 from "react";
|
|
3
|
+
import { useSet } from "@uidotdev/usehooks";
|
|
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,
|
|
@@ -42,27 +52,36 @@ function Observer({
|
|
|
42
52
|
if (context == null) {
|
|
43
53
|
throw new Error("Observer context was null");
|
|
44
54
|
}
|
|
45
|
-
const {
|
|
46
|
-
const { add, reset } = methods;
|
|
55
|
+
const { mountedElements } = context;
|
|
47
56
|
const onMount = useCallback(
|
|
48
57
|
(ref) => {
|
|
49
|
-
Array.from(ref.querySelectorAll(selector)).forEach(
|
|
58
|
+
Array.from(ref.querySelectorAll(selector)).forEach(
|
|
59
|
+
(e) => mountedElements.add(e)
|
|
60
|
+
);
|
|
50
61
|
},
|
|
51
|
-
[selector,
|
|
62
|
+
[selector, mountedElements]
|
|
63
|
+
);
|
|
64
|
+
const onUnmount = useCallback(
|
|
65
|
+
() => mountedElements.clear(),
|
|
66
|
+
[mountedElements]
|
|
52
67
|
);
|
|
53
|
-
const onUnmount = useCallback(() => reset, [reset]);
|
|
54
68
|
const Component = useWrapperDiv ? ObserveChildWithWrapperDiv : ObserveChild;
|
|
55
69
|
return /* @__PURE__ */ jsx(Component, { onMount, onUnmount, children });
|
|
56
70
|
}
|
|
71
|
+
|
|
72
|
+
// src/useElementObserver.tsx
|
|
73
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
74
|
+
var ElementObserverContext = React3.createContext(null);
|
|
57
75
|
function useElementObserver({
|
|
58
76
|
tree,
|
|
59
77
|
selector,
|
|
60
78
|
useWrapperDiv = true
|
|
61
79
|
}) {
|
|
62
|
-
const
|
|
63
|
-
const observedTree = /* @__PURE__ */
|
|
80
|
+
const mountedElements = useSet();
|
|
81
|
+
const observedTree = /* @__PURE__ */ jsx2(ElementObserverContext.Provider, { value: { mountedElements }, children: /* @__PURE__ */ jsx2(Observer, { selector, useWrapperDiv, children: tree }) });
|
|
64
82
|
return [mountedElements, observedTree];
|
|
65
83
|
}
|
|
66
84
|
export {
|
|
85
|
+
ElementObserverContext,
|
|
67
86
|
useElementObserver
|
|
68
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;
|
|
@@ -19,7 +23,7 @@ interface Options {
|
|
|
19
23
|
* @param useWrapperDiv If `true`, a 'div' will wrap the observed children. If
|
|
20
24
|
* `false`, `children` must be a single child element that can take a ref (which
|
|
21
25
|
* will be overwritten).
|
|
22
|
-
* @returns A tuple of: (1)
|
|
26
|
+
* @returns A tuple of: (1) set of elements and (2) the observed tree to be
|
|
23
27
|
* rendered.
|
|
24
28
|
*/
|
|
25
29
|
export declare function useElementObserver({ tree, selector, useWrapperDiv, }: Options): [Set<Element>, React.ReactElement];
|
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",
|
|
@@ -23,19 +23,24 @@
|
|
|
23
23
|
},
|
|
24
24
|
"homepage": "https://github.com/altano/npm-packages/tree/main/packages/use-element-observer",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"
|
|
26
|
+
"@uidotdev/usehooks": "^2.4.1"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"react": "^18"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@
|
|
33
|
-
"@
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"react": "^
|
|
37
|
-
"
|
|
38
|
-
"
|
|
32
|
+
"@testing-library/jest-dom": "^6.4.2",
|
|
33
|
+
"@testing-library/react": "^14.2.2",
|
|
34
|
+
"@types/react": "^18.2.64",
|
|
35
|
+
"@types/react-dom": "^18.2.21",
|
|
36
|
+
"@vitejs/plugin-react": "^4.2.1",
|
|
37
|
+
"@vitest/coverage-v8": "^1.4.0",
|
|
38
|
+
"eslint": "^8.57.0",
|
|
39
|
+
"jsdom": "^24.0.0",
|
|
40
|
+
"prettier": "^3.2.5",
|
|
41
|
+
"react": "^18.2.0",
|
|
42
|
+
"typescript": "^5.4.2",
|
|
43
|
+
"vitest": "^1.4.0",
|
|
39
44
|
"@altano/tsconfig": "0.0.0",
|
|
40
45
|
"eslint-config-altano": "0.0.0"
|
|
41
46
|
},
|
|
@@ -43,13 +48,18 @@
|
|
|
43
48
|
"access": "public"
|
|
44
49
|
},
|
|
45
50
|
"scripts": {
|
|
46
|
-
"build": "tsup
|
|
47
|
-
"build:types": "tsc --
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
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",
|
|
55
|
+
"format": "prettier --check src",
|
|
56
|
+
"format:fix": "prettier --write src",
|
|
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"
|
|
54
64
|
}
|
|
55
65
|
}
|