@altano/use-element-observer 1.0.7 → 2.0.2

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,32 @@
1
- export * from "./useElementObserver";
2
- //# sourceMappingURL=index.d.ts.map
1
+ import React from 'react';
2
+
3
+ interface Context {
4
+ mountedElements: Set<Element>;
5
+ }
6
+ declare const ElementObserverContext: React.Context<Context | null>;
7
+ interface Options {
8
+ selector: string;
9
+ tree: React.ReactNode;
10
+ useWrapperDiv?: boolean | undefined;
11
+ }
12
+ /**
13
+ * Allows observing mount/unmount of elements that match a given selector in the
14
+ * given React tree. Tree must be static: only the mounting of the tree itself
15
+ * is monitored. If the tree's DOM is dynamically changed those mounts and
16
+ * unmounts will not be detected.
17
+ *
18
+ * @param tree The React tree to observe.
19
+ * @param onMount The callback that will be passed any elements in the tree that
20
+ * match the given selector, as they mount.
21
+ * @param onUnmount The callback that will be passed any elements in the tree
22
+ * that match the given selector, as they unmount.
23
+ * @param selector The css selector used to select which elements are observed.
24
+ * @param useWrapperDiv If `true`, a 'div' will wrap the observed children. If
25
+ * `false`, `children` must be a single child element that can take a ref (which
26
+ * will be overwritten).
27
+ * @returns A tuple of: (1) set of elements and (2) the observed tree to be
28
+ * rendered.
29
+ */
30
+ declare function useElementObserver({ tree, selector, useWrapperDiv, }: Options): [Set<Element>, React.ReactElement];
31
+
32
+ export { ElementObserverContext, type Options, useElementObserver };
package/dist/index.js CHANGED
@@ -1,9 +1,16 @@
1
1
  // src/useElementObserver.tsx
2
- import React, { useCallback, useContext, useEffect, useRef } from "react";
2
+ import React3 from "react";
3
3
  import { useSet } from "@uidotdev/usehooks";
4
- import { jsx } from "react/jsx-runtime";
5
- var ElementObserverContext = React.createContext(null);
6
- function useMountRef({ onMount, onUnmount }) {
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 = React.Children.only(children);
34
- return React.cloneElement(child, { ref: newRef });
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__ */ jsx(ElementObserverContext.Provider, { value: { mountedElements }, children: /* @__PURE__ */ jsx(Observer, { selector, useWrapperDiv, children: tree }) });
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
  };
package/package.json CHANGED
@@ -1,56 +1,70 @@
1
1
  {
2
2
  "name": "@altano/use-element-observer",
3
- "version": "1.0.7",
3
+ "version": "2.0.2",
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": "UNLICENSED",
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": {
29
+ "@arethetypeswrong/cli": "^0.15.4",
30
+ "@testing-library/jest-dom": "^6.4.2",
31
+ "@testing-library/react": "^14.2.2",
32
32
  "@types/react": "^18.2.64",
33
33
  "@types/react-dom": "^18.2.21",
34
- "eslint": "^8.57.0",
35
- "prettier": "^3.2.5",
36
- "react": "^18.2.0",
37
- "tsup": "^8.0.2",
38
- "typescript": "^5.4.2",
34
+ "@vitest/coverage-v8": "^3.0.9",
35
+ "depcheck": "^1.4.7",
36
+ "eslint": "^9.22.0",
37
+ "jsdom": "^25.0.1",
38
+ "prettier": "^3.3.3",
39
+ "react": "^18.3.1",
40
+ "typescript": "^5.8.2",
41
+ "vite": "^6.2.3",
42
+ "vitest": "^3.0.9",
43
+ "@altano/build-config": "0.0.0",
44
+ "@altano/testing": "0.0.0",
39
45
  "@altano/tsconfig": "0.0.0",
40
- "eslint-config-altano": "0.0.1"
46
+ "@altano/vitest-plugins": "1.0.0"
47
+ },
48
+ "peerDependencies": {
49
+ "react": "18.x.x"
41
50
  },
42
51
  "publishConfig": {
43
52
  "access": "public"
44
53
  },
45
54
  "scripts": {
46
- "build": "tsup src/index.tsx --format esm --onSuccess \"pnpm build:types\"",
47
- "build:types": "tsc --emitDeclarationOnly --declaration",
48
- "dev": "tsup src/index.tsx --watch --format esm",
49
- "lint": "eslint src/**/*.ts*",
50
- "lint:timing": "TIMING=1 eslint src/**/*.ts*",
51
- "lint:fix": "TIMING=1 eslint src/**/*.ts* --fix",
55
+ "build": "tsup --config build-config/tsup.config.browser.ts",
56
+ "check:exports": "attw --pack . --ignore-rules=cjs-resolves-to-esm",
57
+ "check:types:src": "tsc --noEmit",
58
+ "check:types:test": "tsc --noEmit --project ./tests/tsconfig.json",
59
+ "check:unused-dependencies": "depcheck",
60
+ "clean": "rm -rf .turbo && rm -rf .tsbuildinfo && rm -rf node_modules && rm -rf dist",
61
+ "dev": "pnpm run build --watch",
52
62
  "format": "prettier --check src",
53
63
  "format:fix": "prettier --write src",
54
- "clean": "rm -rf .turbo && rm -rf .tsbuildinfo && rm -rf node_modules && rm -rf dist"
64
+ "lint": "eslint",
65
+ "lint:fix": "TIMING=1 pnpm lint --fix",
66
+ "lint:timing": "TIMING=1 pnpm lint",
67
+ "test:unit": "vitest --run",
68
+ "test:unit:watch": "vitest"
55
69
  }
56
70
  }
@@ -1,27 +0,0 @@
1
- import React from "react";
2
- interface Options {
3
- selector: string;
4
- tree: React.ReactNode;
5
- useWrapperDiv?: boolean;
6
- }
7
- /**
8
- * Allows observing mount/unmount of elements that match a given selector in the
9
- * given React tree. Tree must be static: only the mounting of the tree itself
10
- * is monitored. If the tree's DOM is dynamically changed those mounts and
11
- * unmounts will not be detected.
12
- *
13
- * @param tree The React tree to observe.
14
- * @param onMount The callback that will be passed any elements in the tree that
15
- * match the given selector, as they mount.
16
- * @param onUnmount The callback that will be passed any elements in the tree
17
- * that match the given selector, as they unmount.
18
- * @param selector The css selector used to select which elements are observed.
19
- * @param useWrapperDiv If `true`, a 'div' will wrap the observed children. If
20
- * `false`, `children` must be a single child element that can take a ref (which
21
- * will be overwritten).
22
- * @returns A tuple of: (1) set of elements and (2) the observed tree to be
23
- * rendered.
24
- */
25
- export declare function useElementObserver({ tree, selector, useWrapperDiv, }: Options): [Set<Element>, React.ReactElement];
26
- export {};
27
- //# sourceMappingURL=useElementObserver.d.ts.map