@altano/use-element-observer 2.0.0 → 3.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/index.d.ts CHANGED
@@ -1,2 +1,44 @@
1
- export * from "./useElementObserver";
2
- //# sourceMappingURL=index.d.ts.map
1
+ import React from 'react';
2
+
3
+ type ReactElementWithRef = React.ReactElement<{
4
+ ref: React.Ref<Element>;
5
+ }>;
6
+
7
+ interface Context {
8
+ mountedElements: Set<Element>;
9
+ }
10
+ declare const ElementObserverContext: React.Context<Context | null>;
11
+ type Options = ChildOptions & {
12
+ selector: string;
13
+ };
14
+ type ChildOptions = {
15
+ useWrapperDiv: false;
16
+ tree: ReactElementWithRef;
17
+ } | {
18
+ useWrapperDiv: true;
19
+ tree: React.ReactNode;
20
+ } | {
21
+ useWrapperDiv?: undefined;
22
+ tree: React.ReactNode;
23
+ };
24
+ /**
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 };
package/dist/index.js CHANGED
@@ -40,8 +40,7 @@ function ObserveChild({
40
40
  onUnmount
41
41
  }) {
42
42
  const newRef = useMountRef({ onMount, onUnmount });
43
- const child = React2.Children.only(children);
44
- return React2.cloneElement(child, { ref: newRef });
43
+ return React2.cloneElement(children, { ref: newRef });
45
44
  }
46
45
  function Observer({
47
46
  children,
@@ -65,8 +64,7 @@ function Observer({
65
64
  () => mountedElements.clear(),
66
65
  [mountedElements]
67
66
  );
68
- const Component = useWrapperDiv ? ObserveChildWithWrapperDiv : ObserveChild;
69
- return /* @__PURE__ */ jsx(Component, { onMount, onUnmount, children });
67
+ return useWrapperDiv ? /* @__PURE__ */ jsx(ObserveChildWithWrapperDiv, { onMount, onUnmount, children }) : /* @__PURE__ */ jsx(ObserveChild, { onMount, onUnmount, children });
70
68
  }
71
69
 
72
70
  // src/useElementObserver.tsx
@@ -75,10 +73,10 @@ var ElementObserverContext = React3.createContext(null);
75
73
  function useElementObserver({
76
74
  tree,
77
75
  selector,
78
- useWrapperDiv = true
76
+ useWrapperDiv
79
77
  }) {
80
78
  const mountedElements = useSet();
81
- const observedTree = /* @__PURE__ */ jsx2(ElementObserverContext.Provider, { value: { mountedElements }, children: /* @__PURE__ */ jsx2(Observer, { selector, useWrapperDiv, children: tree }) });
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 }) });
82
80
  return [mountedElements, observedTree];
83
81
  }
84
82
  export {
package/package.json CHANGED
@@ -1,65 +1,68 @@
1
1
  {
2
2
  "name": "@altano/use-element-observer",
3
- "version": "2.0.0",
3
+ "version": "3.0.0",
4
4
  "description": "Intersection-Observer-based Scroll Spy React Hook",
5
- "type": "module",
5
+ "homepage": "https://github.com/altano/npm-packages/tree/main/packages/use-element-observer",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/altano/npm-packages",
9
+ "directory": "packages/use-element-observer"
10
+ },
11
+ "license": "ISC",
12
+ "author": {
13
+ "name": "Alan Norbauer",
14
+ "email": "altano@gmail.com",
15
+ "url": "https://alan.norbauer.com"
16
+ },
6
17
  "sideEffects": false,
18
+ "type": "module",
7
19
  "exports": "./dist/index.js",
8
20
  "types": "./dist/index.d.ts",
9
21
  "files": [
10
22
  "dist/**/*.js",
11
23
  "dist/**/*.d.ts"
12
24
  ],
13
- "author": {
14
- "name": "Alan Norbauer",
15
- "email": "altano@gmail.com",
16
- "url": "https://alan.norbauer.com"
17
- },
18
- "license": "ISC",
19
- "repository": {
20
- "type": "git",
21
- "url": "https://github.com/altano/npm-packages",
22
- "directory": "packages/use-element-observer"
23
- },
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
- "peerDependencies": {
29
- "react": "^18"
30
- },
31
28
  "devDependencies": {
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",
29
+ "@arethetypeswrong/cli": "^0.17.4",
30
+ "@testing-library/react": "^16.2.0",
31
+ "@types/react": "^19.0.12",
32
+ "@types/react-dom": "^19.0.4",
33
+ "@vitest/coverage-v8": "^3.1.1",
34
+ "depcheck": "^1.4.7",
35
+ "eslint": "^9.23.0",
36
+ "jsdom": "^26.0.0",
37
+ "prettier": "^3.5.3",
38
+ "react": "^19.1.0",
39
+ "typescript": "^5.8.2",
40
+ "vite": "^6.2.4",
41
+ "vitest": "^3.1.1",
42
+ "@altano/build-config": "0.0.0",
44
43
  "@altano/tsconfig": "0.0.0",
45
- "eslint-config-altano": "0.0.0"
44
+ "@altano/testing": "0.0.0"
45
+ },
46
+ "peerDependencies": {
47
+ "react": "19.x.x"
46
48
  },
47
49
  "publishConfig": {
48
50
  "access": "public"
49
51
  },
50
52
  "scripts": {
51
53
  "build": "tsup --config build-config/tsup.config.browser.ts",
52
- "build:types": "tsc --project tsconfig.declarations.json",
54
+ "check:exports": "attw --pack . --ignore-rules=cjs-resolves-to-esm",
55
+ "check:types:src": "tsc --noEmit",
56
+ "check:types:test": "tsc --noEmit --project ./tests/tsconfig.json",
57
+ "check:unused-dependencies": "depcheck",
53
58
  "clean": "rm -rf .turbo && rm -rf .tsbuildinfo && rm -rf node_modules && rm -rf dist",
54
59
  "dev": "pnpm run build --watch",
55
60
  "format": "prettier --check src",
56
61
  "format:fix": "prettier --write src",
57
- "lint": "eslint package.json src/**/*.ts*",
62
+ "lint": "eslint",
58
63
  "lint:fix": "TIMING=1 pnpm lint --fix",
59
64
  "lint:timing": "TIMING=1 pnpm lint",
60
65
  "test:unit": "vitest --run",
61
- "test:unit:watch": "vitest",
62
- "typecheck:src": "tsc --noEmit",
63
- "typecheck:test": "tsc --noEmit --project ./tests/tsconfig.json"
66
+ "test:unit:watch": "vitest"
64
67
  }
65
68
  }
@@ -1,7 +0,0 @@
1
- import React from "react";
2
- export declare function Observer({ children, selector, useWrapperDiv, }: {
3
- children: React.ReactNode;
4
- selector: string;
5
- useWrapperDiv: boolean;
6
- }): React.ReactElement;
7
- //# sourceMappingURL=Observer.d.ts.map
@@ -1,31 +0,0 @@
1
- import React from "react";
2
- interface Context {
3
- mountedElements: Set<Element>;
4
- }
5
- export declare const ElementObserverContext: React.Context<Context | null>;
6
- export interface Options {
7
- selector: string;
8
- tree: React.ReactNode;
9
- useWrapperDiv?: boolean;
10
- }
11
- /**
12
- * Allows observing mount/unmount of elements that match a given selector in the
13
- * given React tree. Tree must be static: only the mounting of the tree itself
14
- * is monitored. If the tree's DOM is dynamically changed those mounts and
15
- * unmounts will not be detected.
16
- *
17
- * @param tree The React tree to observe.
18
- * @param onMount The callback that will be passed any elements in the tree that
19
- * match the given selector, as they mount.
20
- * @param onUnmount The callback that will be passed any elements in the tree
21
- * that match the given selector, as they unmount.
22
- * @param selector The css selector used to select which elements are observed.
23
- * @param useWrapperDiv If `true`, a 'div' will wrap the observed children. If
24
- * `false`, `children` must be a single child element that can take a ref (which
25
- * will be overwritten).
26
- * @returns A tuple of: (1) set of elements and (2) the observed tree to be
27
- * rendered.
28
- */
29
- export declare function useElementObserver({ tree, selector, useWrapperDiv, }: Options): [Set<Element>, React.ReactElement];
30
- export {};
31
- //# sourceMappingURL=useElementObserver.d.ts.map
@@ -1,7 +0,0 @@
1
- import React from "react";
2
- export interface Callbacks<T> {
3
- onMount: (e: T) => void;
4
- onUnmount: (e: T) => void;
5
- }
6
- export declare function useMountRef<T>({ onMount, onUnmount, }: Callbacks<T>): React.Ref<T>;
7
- //# sourceMappingURL=useMountRef.d.ts.map