@altano/use-element-observer 3.0.0 → 4.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.
package/dist/index.d.ts CHANGED
@@ -1,44 +1,51 @@
1
- import React from 'react';
1
+ import React from "react";
2
2
 
3
+ //#region src/types.d.ts
3
4
  type ReactElementWithRef = React.ReactElement<{
4
- ref: React.Ref<Element>;
5
+ ref: React.Ref<Element>;
5
6
  }>;
6
-
7
+ //#endregion
8
+ //#region src/useElementObserver.d.ts
7
9
  interface Context {
8
- mountedElements: Set<Element>;
10
+ mountedElements: Set<Element>;
9
11
  }
10
12
  declare const ElementObserverContext: React.Context<Context | null>;
11
13
  type Options = ChildOptions & {
12
- selector: string;
14
+ selector: string;
13
15
  };
14
16
  type ChildOptions = {
15
- useWrapperDiv: false;
16
- tree: ReactElementWithRef;
17
+ useWrapperDiv: false;
18
+ tree: ReactElementWithRef;
17
19
  } | {
18
- useWrapperDiv: true;
19
- tree: React.ReactNode;
20
+ useWrapperDiv: true;
21
+ tree: React.ReactNode;
20
22
  } | {
21
- useWrapperDiv?: undefined;
22
- tree: React.ReactNode;
23
+ useWrapperDiv?: undefined;
24
+ tree: React.ReactNode;
23
25
  };
24
26
  /**
25
- * Allows observing mount/unmount of elements that match a given selector in the
26
- * given React tree. Tree must be static: only the mounting of the tree itself
27
- * is monitored. If the tree's DOM is dynamically changed those mounts and
28
- * unmounts will not be detected.
29
- *
30
- * @param tree The React tree to observe.
31
- * @param onMount The callback that will be passed any elements in the tree that
32
- * match the given selector, as they mount.
33
- * @param onUnmount The callback that will be passed any elements in the tree
34
- * that match the given selector, as they unmount.
35
- * @param selector The css selector used to select which elements are observed.
36
- * @param useWrapperDiv If `true`, a 'div' will wrap the observed children. If
37
- * `false`, `children` must be a single child element that can take a ref (which
38
- * will be overwritten).
39
- * @returns A tuple of: (1) set of elements and (2) the observed tree to be
40
- * rendered.
41
- */
42
- declare function useElementObserver({ tree, selector, useWrapperDiv, }: Options): [Set<Element>, React.ReactElement];
43
-
44
- export { type ChildOptions, ElementObserverContext, type Options, type ReactElementWithRef, useElementObserver };
27
+ * Allows observing mount/unmount of elements that match a given selector in the
28
+ * given React tree. Tree must be static: only the mounting of the tree itself
29
+ * is monitored. If the tree's DOM is dynamically changed those mounts and
30
+ * unmounts will not be detected.
31
+ *
32
+ * @param tree The React tree to observe.
33
+ * @param onMount The callback that will be passed any elements in the tree that
34
+ * match the given selector, as they mount.
35
+ * @param onUnmount The callback that will be passed any elements in the tree
36
+ * that match the given selector, as they unmount.
37
+ * @param selector The css selector used to select which elements are observed.
38
+ * @param useWrapperDiv If `true`, a 'div' will wrap the observed children. If
39
+ * `false`, `children` must be a single child element that can take a ref (which
40
+ * will be overwritten).
41
+ * @returns A tuple of: (1) set of elements and (2) the observed tree to be
42
+ * rendered.
43
+ */
44
+ declare function useElementObserver({
45
+ tree,
46
+ selector,
47
+ useWrapperDiv
48
+ }: Options): [Set<Element>, React.ReactElement];
49
+ //#endregion
50
+ export { ChildOptions, ElementObserverContext, Options, ReactElementWithRef, useElementObserver };
51
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,85 +1,100 @@
1
- // src/useElementObserver.tsx
2
- import React3 from "react";
1
+ import React, { useCallback, useContext, useEffect, useRef } from "react";
3
2
  import { useSet } from "@uidotdev/usehooks";
3
+ import { jsx } from "react/jsx-runtime";
4
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
- }) {
14
- const newRef = useRef(null);
15
- useEffect(() => {
16
- const ref = newRef.current;
17
- if (ref == null) {
18
- return () => {
19
- };
20
- }
21
- onMount(ref);
22
- return () => onUnmount(ref);
23
- }, [newRef, onMount, onUnmount]);
24
- return newRef;
5
+ //#region src/useMountRef.tsx
6
+ function useMountRef({ onMount, onUnmount }) {
7
+ const newRef = useRef(null);
8
+ useEffect(() => {
9
+ const ref = newRef.current;
10
+ if (ref == null) return () => {};
11
+ onMount(ref);
12
+ return () => onUnmount(ref);
13
+ }, [
14
+ newRef,
15
+ onMount,
16
+ onUnmount
17
+ ]);
18
+ return newRef;
25
19
  }
26
20
 
27
- // src/Observer.tsx
28
- import { jsx } from "react/jsx-runtime";
29
- function ObserveChildWithWrapperDiv({
30
- children,
31
- onMount,
32
- onUnmount
33
- }) {
34
- const ref = useMountRef({ onMount, onUnmount });
35
- return /* @__PURE__ */ jsx("div", { style: { display: "inherit" }, ref, children });
21
+ //#endregion
22
+ //#region src/Observer.tsx
23
+ function ObserveChildWithWrapperDiv({ children, onMount, onUnmount }) {
24
+ const ref = useMountRef({
25
+ onMount,
26
+ onUnmount
27
+ });
28
+ return /* @__PURE__ */ jsx("div", {
29
+ style: { display: "inherit" },
30
+ ref,
31
+ children
32
+ });
36
33
  }
37
- function ObserveChild({
38
- children,
39
- onMount,
40
- onUnmount
41
- }) {
42
- const newRef = useMountRef({ onMount, onUnmount });
43
- return React2.cloneElement(children, { ref: newRef });
34
+ function ObserveChild({ children, onMount, onUnmount }) {
35
+ const newRef = useMountRef({
36
+ onMount,
37
+ onUnmount
38
+ });
39
+ return React.cloneElement(children, { ref: newRef });
44
40
  }
45
- function Observer({
46
- children,
47
- selector,
48
- useWrapperDiv
49
- }) {
50
- const context = useContext(ElementObserverContext);
51
- if (context == null) {
52
- throw new Error("Observer context was null");
53
- }
54
- const { mountedElements } = context;
55
- const onMount = useCallback(
56
- (ref) => {
57
- Array.from(ref.querySelectorAll(selector)).forEach(
58
- (e) => mountedElements.add(e)
59
- );
60
- },
61
- [selector, mountedElements]
62
- );
63
- const onUnmount = useCallback(
64
- () => mountedElements.clear(),
65
- [mountedElements]
66
- );
67
- return useWrapperDiv ? /* @__PURE__ */ jsx(ObserveChildWithWrapperDiv, { onMount, onUnmount, children }) : /* @__PURE__ */ jsx(ObserveChild, { onMount, onUnmount, children });
41
+ function Observer({ children, selector, useWrapperDiv }) {
42
+ const context = useContext(ElementObserverContext);
43
+ if (context == null) throw new Error("Observer context was null");
44
+ const { mountedElements } = context;
45
+ const onMount = useCallback((ref) => {
46
+ Array.from(ref.querySelectorAll(selector)).forEach((e) => mountedElements.add(e));
47
+ }, [selector, mountedElements]);
48
+ const onUnmount = useCallback(() => mountedElements.clear(), [mountedElements]);
49
+ return useWrapperDiv ? /* @__PURE__ */ jsx(ObserveChildWithWrapperDiv, {
50
+ onMount,
51
+ onUnmount,
52
+ children
53
+ }) : /* @__PURE__ */ jsx(ObserveChild, {
54
+ onMount,
55
+ onUnmount,
56
+ children
57
+ });
68
58
  }
69
59
 
70
- // src/useElementObserver.tsx
71
- import { jsx as jsx2 } from "react/jsx-runtime";
72
- var ElementObserverContext = React3.createContext(null);
73
- function useElementObserver({
74
- tree,
75
- selector,
76
- useWrapperDiv
77
- }) {
78
- const mountedElements = useSet();
79
- const observedTree = /* @__PURE__ */ jsx2(ElementObserverContext.Provider, { value: { mountedElements }, children: useWrapperDiv == null || useWrapperDiv === true ? /* @__PURE__ */ jsx2(Observer, { selector, useWrapperDiv: true, children: tree }) : /* @__PURE__ */ jsx2(Observer, { selector, useWrapperDiv: false, children: tree }) });
80
- return [mountedElements, observedTree];
60
+ //#endregion
61
+ //#region src/useElementObserver.tsx
62
+ const ElementObserverContext = React.createContext(null);
63
+ /**
64
+ * Allows observing mount/unmount of elements that match a given selector in the
65
+ * given React tree. Tree must be static: only the mounting of the tree itself
66
+ * is monitored. If the tree's DOM is dynamically changed those mounts and
67
+ * unmounts will not be detected.
68
+ *
69
+ * @param tree The React tree to observe.
70
+ * @param onMount The callback that will be passed any elements in the tree that
71
+ * match the given selector, as they mount.
72
+ * @param onUnmount The callback that will be passed any elements in the tree
73
+ * that match the given selector, as they unmount.
74
+ * @param selector The css selector used to select which elements are observed.
75
+ * @param useWrapperDiv If `true`, a 'div' will wrap the observed children. If
76
+ * `false`, `children` must be a single child element that can take a ref (which
77
+ * will be overwritten).
78
+ * @returns A tuple of: (1) set of elements and (2) the observed tree to be
79
+ * rendered.
80
+ */
81
+ function useElementObserver({ tree, selector, useWrapperDiv }) {
82
+ const mountedElements = useSet();
83
+ const observedTree = /* @__PURE__ */ jsx(ElementObserverContext.Provider, {
84
+ value: { mountedElements },
85
+ children: useWrapperDiv == null || useWrapperDiv === true ? /* @__PURE__ */ jsx(Observer, {
86
+ selector,
87
+ useWrapperDiv: true,
88
+ children: tree
89
+ }) : /* @__PURE__ */ jsx(Observer, {
90
+ selector,
91
+ useWrapperDiv: false,
92
+ children: tree
93
+ })
94
+ });
95
+ return [mountedElements, observedTree];
81
96
  }
82
- export {
83
- ElementObserverContext,
84
- useElementObserver
85
- };
97
+
98
+ //#endregion
99
+ export { ElementObserverContext, useElementObserver };
100
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["ref: Element","ElementObserverContext: React.Context<Context | null>"],"sources":["../src/useMountRef.tsx","../src/Observer.tsx","../src/useElementObserver.tsx"],"sourcesContent":["import React, { useEffect, useRef } from \"react\";\n\nexport interface Callbacks<T> {\n onMount: (e: T) => void;\n onUnmount: (e: T) => void;\n}\n\nexport function useMountRef<T>({\n onMount,\n onUnmount,\n}: Callbacks<T>): React.Ref<T> {\n const newRef = useRef<T>(null);\n useEffect(() => {\n const ref = newRef.current;\n if (ref == null) {\n return (): void => {};\n }\n onMount(ref);\n return (): void => onUnmount(ref);\n }, [newRef, onMount, onUnmount]);\n return newRef;\n}\n","import React, { useCallback, useContext } from \"react\";\nimport { useMountRef, type Callbacks } from \"./useMountRef\";\nimport { ElementObserverContext } from \"./useElementObserver\";\nimport type { ReactElementWithRef } from \"./types\";\n\ninterface ChildrenAndCallbacks<T, ChildType> extends Callbacks<T> {\n children: ChildType;\n}\n\nfunction ObserveChildWithWrapperDiv({\n children,\n onMount,\n onUnmount,\n}: ChildrenAndCallbacks<Element, React.ReactNode>): React.ReactElement {\n const ref = useMountRef<HTMLDivElement>({ onMount, onUnmount });\n return (\n <div style={{ display: \"inherit\" }} ref={ref}>\n {children}\n </div>\n );\n}\n\nfunction ObserveChild({\n children,\n onMount,\n onUnmount,\n}: ChildrenAndCallbacks<Element, ReactElementWithRef>): React.ReactElement {\n const newRef = useMountRef({ onMount, onUnmount });\n return React.cloneElement(children, { ref: newRef });\n}\n\ntype Options =\n | {\n // If we're using a wrapper div...\n useWrapperDiv: true;\n // ... the children can be any ReactNode\n children: React.ReactNode;\n selector: string;\n }\n | {\n // But if we're not using a wrapper div...\n useWrapperDiv: false;\n // The child must be a single ReactElement that takes a ref\n children: ReactElementWithRef;\n selector: string;\n };\n\nexport function Observer({\n children,\n selector,\n useWrapperDiv,\n}: Options): React.ReactElement {\n const context = useContext(ElementObserverContext);\n if (context == null) {\n throw new Error(\"Observer context was null\");\n }\n const { mountedElements } = context;\n const onMount = useCallback(\n (ref: Element) => {\n Array.from(ref.querySelectorAll(selector)).forEach((e) =>\n mountedElements.add(e),\n );\n },\n [selector, mountedElements],\n );\n const onUnmount = useCallback(\n () => mountedElements.clear(),\n [mountedElements],\n );\n return useWrapperDiv ? (\n <ObserveChildWithWrapperDiv onMount={onMount} onUnmount={onUnmount}>\n {children}\n </ObserveChildWithWrapperDiv>\n ) : (\n <ObserveChild onMount={onMount} onUnmount={onUnmount}>\n {children}\n </ObserveChild>\n );\n}\n","import React from \"react\";\nimport { useSet } from \"@uidotdev/usehooks\";\nimport { Observer } from \"./Observer\";\nimport type { ReactElementWithRef } from \"./types\";\n\nexport type { ReactElementWithRef };\n\ninterface Context {\n mountedElements: Set<Element>;\n}\n\nexport const ElementObserverContext: React.Context<Context | null> =\n React.createContext<Context | null>(null);\n\nexport type Options = ChildOptions & {\n selector: string;\n};\n\nexport type ChildOptions =\n | {\n useWrapperDiv: false;\n tree: ReactElementWithRef;\n }\n | {\n useWrapperDiv: true;\n tree: React.ReactNode;\n }\n | {\n useWrapperDiv?: undefined;\n tree: React.ReactNode;\n };\n\n/**\n * Allows observing mount/unmount of elements that match a given selector in the\n * given React tree. Tree must be static: only the mounting of the tree itself\n * is monitored. If the tree's DOM is dynamically changed those mounts and\n * unmounts will not be detected.\n *\n * @param tree The React tree to observe.\n * @param onMount The callback that will be passed any elements in the tree that\n * match the given selector, as they mount.\n * @param onUnmount The callback that will be passed any elements in the tree\n * that match the given selector, as they unmount.\n * @param selector The css selector used to select which elements are observed.\n * @param useWrapperDiv If `true`, a 'div' will wrap the observed children. If\n * `false`, `children` must be a single child element that can take a ref (which\n * will be overwritten).\n * @returns A tuple of: (1) set of elements and (2) the observed tree to be\n * rendered.\n */\nexport function useElementObserver({\n tree,\n selector,\n useWrapperDiv,\n}: Options): [Set<Element>, React.ReactElement] {\n // TODO: Re-compute on every render to catch mutations? Add mutation observer\n // to catch changes to grandchildren?\n const mountedElements = useSet<Element>();\n const observedTree = (\n <ElementObserverContext.Provider value={{ mountedElements }}>\n {useWrapperDiv == null || useWrapperDiv === true ? (\n <Observer selector={selector} useWrapperDiv={true}>\n {tree}\n </Observer>\n ) : (\n <Observer selector={selector} useWrapperDiv={false}>\n {tree}\n </Observer>\n )}\n </ElementObserverContext.Provider>\n );\n return [mountedElements, observedTree];\n}\n"],"mappings":";;;;;AAOA,SAAgB,YAAe,EAC7B,SACA,WACa,EAAgB;CAC7B,MAAM,SAAS,OAAU,KAAK;AAC9B,WAAU,MAAM;EACd,MAAM,MAAM,OAAO;AACnB,MAAI,OAAO,KACT,QAAO,MAAY,CAAE;AAEvB,UAAQ,IAAI;AACZ,SAAO,MAAY,UAAU,IAAI;CAClC,GAAE;EAAC;EAAQ;EAAS;CAAU,EAAC;AAChC,QAAO;AACR;;;;ACZD,SAAS,2BAA2B,EAClC,UACA,SACA,WAC+C,EAAsB;CACrE,MAAM,MAAM,YAA4B;EAAE;EAAS;CAAW,EAAC;AAC/D,wBACE,IAAC;EAAI,OAAO,EAAE,SAAS,UAAW;EAAO;EACtC;GACG;AAET;AAED,SAAS,aAAa,EACpB,UACA,SACA,WACmD,EAAsB;CACzE,MAAM,SAAS,YAAY;EAAE;EAAS;CAAW,EAAC;AAClD,QAAO,MAAM,aAAa,UAAU,EAAE,KAAK,OAAQ,EAAC;AACrD;AAkBD,SAAgB,SAAS,EACvB,UACA,UACA,eACQ,EAAsB;CAC9B,MAAM,UAAU,WAAW,uBAAuB;AAClD,KAAI,WAAW,KACb,OAAM,IAAI,MAAM;CAElB,MAAM,EAAE,iBAAiB,GAAG;CAC5B,MAAM,UAAU,YACd,CAACA,QAAiB;AAChB,QAAM,KAAK,IAAI,iBAAiB,SAAS,CAAC,CAAC,QAAQ,CAAC,MAClD,gBAAgB,IAAI,EAAE,CACvB;CACF,GACD,CAAC,UAAU,eAAgB,EAC5B;CACD,MAAM,YAAY,YAChB,MAAM,gBAAgB,OAAO,EAC7B,CAAC,eAAgB,EAClB;AACD,QAAO,gCACL,IAAC;EAAoC;EAAoB;EACtD;GAC0B,mBAE7B,IAAC;EAAsB;EAAoB;EACxC;GACY;AAElB;;;;ACnED,MAAaC,yBACX,MAAM,cAA8B,KAAK;;;;;;;;;;;;;;;;;;;AAsC3C,SAAgB,mBAAmB,EACjC,MACA,UACA,eACQ,EAAsC;CAG9C,MAAM,kBAAkB,QAAiB;CACzC,MAAM,+BACJ,IAAC,uBAAuB;EAAS,OAAO,EAAE,gBAAiB;YACxD,iBAAiB,QAAQ,kBAAkB,uBAC1C,IAAC;GAAmB;GAAU,eAAe;aAC1C;IACQ,mBAEX,IAAC;GAAmB;GAAU,eAAe;aAC1C;IACQ;GAEmB;AAEpC,QAAO,CAAC,iBAAiB,YAAa;AACvC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@altano/use-element-observer",
3
- "version": "3.0.0",
3
+ "version": "4.0.1",
4
4
  "description": "Intersection-Observer-based Scroll Spy React Hook",
5
5
  "homepage": "https://github.com/altano/npm-packages/tree/main/packages/use-element-observer",
6
6
  "repository": {
@@ -20,6 +20,7 @@
20
20
  "types": "./dist/index.d.ts",
21
21
  "files": [
22
22
  "dist/**/*.js",
23
+ "dist/**/*.js.map",
23
24
  "dist/**/*.d.ts"
24
25
  ],
25
26
  "dependencies": {
@@ -30,18 +31,19 @@
30
31
  "@testing-library/react": "^16.2.0",
31
32
  "@types/react": "^19.0.12",
32
33
  "@types/react-dom": "^19.0.4",
33
- "@vitest/coverage-v8": "^3.1.1",
34
+ "@vitest/coverage-v8": "^3.1.4",
34
35
  "depcheck": "^1.4.7",
35
36
  "eslint": "^9.23.0",
36
37
  "jsdom": "^26.0.0",
37
38
  "prettier": "^3.5.3",
38
39
  "react": "^19.1.0",
40
+ "tsdown": "^0.12.9",
39
41
  "typescript": "^5.8.2",
40
- "vite": "^6.2.4",
41
- "vitest": "^3.1.1",
42
- "@altano/build-config": "0.0.0",
43
- "@altano/tsconfig": "0.0.0",
44
- "@altano/testing": "0.0.0"
42
+ "vite": "^6.2.6",
43
+ "vitest": "^3.1.4",
44
+ "@altano/build-config": "0.0.1",
45
+ "@altano/testing": "0.0.1",
46
+ "@altano/tsconfig": "0.0.1"
45
47
  },
46
48
  "peerDependencies": {
47
49
  "react": "19.x.x"
@@ -50,8 +52,8 @@
50
52
  "access": "public"
51
53
  },
52
54
  "scripts": {
53
- "build": "tsup --config build-config/tsup.config.browser.ts",
54
- "check:exports": "attw --pack . --ignore-rules=cjs-resolves-to-esm",
55
+ "build": "tsdown --config ../build-config/tsdown.config.browser.ts",
56
+ "check:exports": "attw --pack . --profile esm-only --quiet",
55
57
  "check:types:src": "tsc --noEmit",
56
58
  "check:types:test": "tsc --noEmit --project ./tests/tsconfig.json",
57
59
  "check:unused-dependencies": "depcheck",