@dxos/react-hooks 0.8.4-main.cb12b3f963 → 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.cb12b3f963",
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",
@@ -32,8 +32,8 @@
32
32
  "alea": "^1.0.1",
33
33
  "lodash.defaultsdeep": "^4.6.1",
34
34
  "mini-virtual-list": "^0.3.2",
35
- "@dxos/async": "0.8.4-main.cb12b3f963",
36
- "@dxos/log": "0.8.4-main.cb12b3f963"
35
+ "@dxos/async": "0.8.4-main.d05539e30a",
36
+ "@dxos/log": "0.8.4-main.d05539e30a"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@types/lodash.defaultsdeep": "^4.6.6",
@@ -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
  };