@dxos/react-hooks 0.8.4-main.c85a9c8dae → 0.8.4-main.d05539e30a

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/react-hooks",
3
- "version": "0.8.4-main.c85a9c8dae",
3
+ "version": "0.8.4-main.d05539e30a",
4
4
  "description": "React hooks supporting DXOS React primitives.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -8,7 +8,7 @@
8
8
  "type": "git",
9
9
  "url": "https://github.com/dxos/dxos"
10
10
  },
11
- "license": "MIT",
11
+ "license": "FSL-1.1-Apache-2.0",
12
12
  "author": "DXOS.org",
13
13
  "sideEffects": false,
14
14
  "type": "module",
@@ -21,20 +21,19 @@
21
21
  }
22
22
  },
23
23
  "types": "dist/types/src/index.d.ts",
24
- "typesVersions": {
25
- "*": {}
26
- },
27
24
  "files": [
28
25
  "dist",
29
26
  "src"
30
27
  ],
31
28
  "dependencies": {
29
+ "@effect-atom/atom-react": "^0.5.0",
30
+ "@radix-ui/react-compose-refs": "1.1.1",
32
31
  "@radix-ui/react-id": "1.1.0",
33
32
  "alea": "^1.0.1",
34
33
  "lodash.defaultsdeep": "^4.6.1",
35
34
  "mini-virtual-list": "^0.3.2",
36
- "@dxos/async": "0.8.4-main.c85a9c8dae",
37
- "@dxos/log": "0.8.4-main.c85a9c8dae"
35
+ "@dxos/async": "0.8.4-main.d05539e30a",
36
+ "@dxos/log": "0.8.4-main.d05539e30a"
38
37
  },
39
38
  "devDependencies": {
40
39
  "@types/lodash.defaultsdeep": "^4.6.6",
package/src/index.ts CHANGED
@@ -2,7 +2,11 @@
2
2
  // Copyright 2022 DXOS.org
3
3
  //
4
4
 
5
+ export { useComposedRefs } from '@radix-ui/react-compose-refs';
6
+ export { useSize, useScroller } from 'mini-virtual-list';
7
+
5
8
  export * from './useAsyncEffect';
9
+ export * from './useAtomState';
6
10
  export * from './useAsyncState';
7
11
  export * from './useControlledState';
8
12
  export * from './useDebugDeps';
@@ -19,5 +23,3 @@ export * from './useRefCallback';
19
23
  export * from './useViewportResize';
20
24
  export * from './useTimeout';
21
25
  export * from './useTransitions';
22
-
23
- export { useSize, useScroller } from 'mini-virtual-list';
@@ -0,0 +1,23 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { Atom, useAtomSet, useAtomValue } from '@effect-atom/atom-react';
6
+ import { useMemo, useState } from 'react';
7
+
8
+ export type AtomState<T> = {
9
+ atom: Atom.Writable<T>;
10
+ value: T;
11
+ set: (value: T | ((value: T) => T)) => void;
12
+ };
13
+
14
+ /**
15
+ * Wraps a writable atom together with its current value and setter.
16
+ * The atom is created once on first render; `initialValue` is only used to seed it.
17
+ */
18
+ export const useAtomState = <T>(initialValue: T): AtomState<T> => {
19
+ const [atom] = useState(() => Atom.make(initialValue));
20
+ const value = useAtomValue(atom);
21
+ const set = useAtomSet(atom);
22
+ return useMemo(() => ({ atom, value, set }), [atom, value, set]);
23
+ };
@@ -47,7 +47,9 @@ export const mergeRefs = <T>(refs: (Ref<T> | undefined)[]): Ref<T> => {
47
47
  }
48
48
 
49
49
  return () => {
50
- for (const cleanup of cleanups) cleanup();
50
+ for (const cleanup of cleanups) {
51
+ cleanup();
52
+ }
51
53
  };
52
54
  };
53
55
  };
@@ -9,19 +9,37 @@ export const useViewportResize = (
9
9
  deps: Parameters<typeof useLayoutEffect>[1] = [],
10
10
  delay: number = 800,
11
11
  ) => {
12
- const debouncedHandler = useMemo(() => {
13
- let timeout: ReturnType<typeof setTimeout>;
14
- return (event?: Event) => {
15
- clearTimeout(timeout);
16
- timeout = setTimeout(() => {
17
- cb(event);
18
- }, delay);
12
+ // The cleanup must cancel the pending debounce timeout. Otherwise, if the
13
+ // component unmounts during the `delay` window (common in jsdom/happy-dom
14
+ // test teardown), the callback fires against a torn-down DOM and surfaces
15
+ // as `ReferenceError: getComputedStyle is not defined`.
16
+ const { handler: debouncedHandler, cancel } = useMemo(() => {
17
+ let timeout: ReturnType<typeof setTimeout> | undefined;
18
+ return {
19
+ handler: (event?: Event) => {
20
+ if (timeout !== undefined) {
21
+ clearTimeout(timeout);
22
+ }
23
+ timeout = setTimeout(() => {
24
+ timeout = undefined;
25
+ cb(event);
26
+ }, delay);
27
+ },
28
+ cancel: () => {
29
+ if (timeout !== undefined) {
30
+ clearTimeout(timeout);
31
+ timeout = undefined;
32
+ }
33
+ },
19
34
  };
20
35
  }, [cb, delay]);
21
36
 
22
37
  return useLayoutEffect(() => {
23
38
  window.visualViewport?.addEventListener('resize', debouncedHandler);
24
39
  debouncedHandler();
25
- return () => window.visualViewport?.removeEventListener('resize', debouncedHandler);
26
- }, [debouncedHandler, ...deps]);
40
+ return () => {
41
+ window.visualViewport?.removeEventListener('resize', debouncedHandler);
42
+ cancel();
43
+ };
44
+ }, [debouncedHandler, cancel, ...deps]);
27
45
  };