@dxos/react-hooks 0.8.3-staging.0fa589b → 0.8.4-main.28f8d3d
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/lib/browser/index.mjs +86 -60
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +86 -60
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/useAsyncEffect.d.ts +1 -31
- package/dist/types/src/useAsyncEffect.d.ts.map +1 -1
- package/dist/types/src/useAsyncState.d.ts.map +1 -1
- package/dist/types/src/useControlledState.d.ts +1 -1
- package/dist/types/src/useControlledState.d.ts.map +1 -1
- package/dist/types/src/useDefaultValue.d.ts.map +1 -1
- package/dist/types/src/useDefaults.d.ts +6 -0
- package/dist/types/src/useDefaults.d.ts.map +1 -0
- package/dist/types/src/useForwardedRef.d.ts +1 -1
- package/dist/types/src/useIsFocused.d.ts.map +1 -1
- package/dist/types/src/useTimeout.d.ts +2 -0
- package/dist/types/src/useTimeout.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +6 -3
- package/src/index.ts +2 -0
- package/src/useAsyncEffect.ts +6 -54
- package/src/useAsyncState.ts +2 -1
- package/src/useControlledState.ts +9 -1
- package/src/useDefaultValue.ts +1 -2
- package/src/useDefaults.ts +14 -0
- package/src/useForwardedRef.ts +2 -2
- package/src/useIsFocused.ts +1 -1
- package/src/useTimeout.ts +21 -0
- package/src/useTrackProps.ts +1 -1
- package/src/useTransitions.ts +1 -1
- package/dist/lib/node/index.cjs +0 -413
- package/dist/lib/node/index.cjs.map +0 -7
- package/dist/lib/node/meta.json +0 -1
- package/dist/types/src/useAsyncEffect.test.d.ts +0 -2
- package/dist/types/src/useAsyncEffect.test.d.ts.map +0 -1
- package/src/useAsyncEffect.test.tsx +0 -51
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-hooks",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.4-main.28f8d3d",
|
|
4
4
|
"description": "React hooks supporting DXOS React primitives.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"type": "module",
|
|
11
11
|
"exports": {
|
|
12
12
|
".": {
|
|
13
|
+
"source": "./src/index.ts",
|
|
13
14
|
"types": "./dist/types/src/index.d.ts",
|
|
14
15
|
"browser": "./dist/lib/browser/index.mjs",
|
|
15
16
|
"node": "./dist/lib/node-esm/index.mjs"
|
|
@@ -26,10 +27,12 @@
|
|
|
26
27
|
"dependencies": {
|
|
27
28
|
"@preact-signals/safe-react": "^0.9.0",
|
|
28
29
|
"alea": "^1.0.1",
|
|
29
|
-
"
|
|
30
|
-
"@dxos/
|
|
30
|
+
"lodash.defaultsdeep": "^4.6.1",
|
|
31
|
+
"@dxos/async": "0.8.4-main.28f8d3d",
|
|
32
|
+
"@dxos/log": "0.8.4-main.28f8d3d"
|
|
31
33
|
},
|
|
32
34
|
"devDependencies": {
|
|
35
|
+
"@types/lodash.defaultsdeep": "^4.6.6",
|
|
33
36
|
"@types/react": "~18.2.0",
|
|
34
37
|
"@types/react-dom": "~18.2.0",
|
|
35
38
|
"react": "~18.2.0",
|
package/src/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ export * from './useAsyncState';
|
|
|
7
7
|
export * from './useControlledState';
|
|
8
8
|
export * from './useDebugReactDeps';
|
|
9
9
|
export * from './useDefaultValue';
|
|
10
|
+
export * from './useDefaults';
|
|
10
11
|
export * from './useDynamicRef';
|
|
11
12
|
export * from './useFileDownload';
|
|
12
13
|
export * from './useForwardedRef';
|
|
@@ -16,5 +17,6 @@ export * from './useMediaQuery';
|
|
|
16
17
|
export * from './useMulticastObservable';
|
|
17
18
|
export * from './useRefCallback';
|
|
18
19
|
export * from './useResize';
|
|
20
|
+
export * from './useTimeout';
|
|
19
21
|
export * from './useTrackProps';
|
|
20
22
|
export * from './useTransitions';
|
package/src/useAsyncEffect.ts
CHANGED
|
@@ -4,59 +4,11 @@
|
|
|
4
4
|
|
|
5
5
|
import { useEffect } from 'react';
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Process async event with optional non-async destructor.
|
|
11
|
-
* Inspired by: https://github.com/rauldeheer/use-async-effect/blob/master/index.js
|
|
12
|
-
*
|
|
13
|
-
* ```tsx
|
|
14
|
-
* useAsyncEffect(async () => {
|
|
15
|
-
* await test();
|
|
16
|
-
* }, []);
|
|
17
|
-
* ```
|
|
18
|
-
*
|
|
19
|
-
* The callback may check of the component is still mounted before doing state updates.
|
|
20
|
-
*
|
|
21
|
-
* ```tsx
|
|
22
|
-
* const [value, setValue] = useState<string>();
|
|
23
|
-
* useAsyncEffect<string>(async (isMounted) => {
|
|
24
|
-
* const value = await test();
|
|
25
|
-
* if (!isMounted()) {
|
|
26
|
-
* setValue(value);
|
|
27
|
-
* }
|
|
28
|
-
* }, () => console.log('Unmounted'), []);
|
|
29
|
-
* ```
|
|
30
|
-
*
|
|
31
|
-
* @param callback Receives a getter function that determines if the component is still mounted.
|
|
32
|
-
* @param destructor Receives the value returned from the callback.
|
|
33
|
-
* @param deps
|
|
34
|
-
*
|
|
35
|
-
* NOTE: This effect does not cancel the async operation if the component is unmounted.
|
|
36
|
-
*
|
|
37
|
-
* @deprecated
|
|
38
|
-
*/
|
|
39
|
-
export const useAsyncEffect = <T>(
|
|
40
|
-
callback: (isMounted: () => boolean) => Promise<T> | undefined,
|
|
41
|
-
destructor?: ((value?: T) => void) | any[],
|
|
42
|
-
deps?: any[],
|
|
43
|
-
) => {
|
|
44
|
-
const [effectDestructor, effectDeps] =
|
|
45
|
-
typeof destructor === 'function' ? [destructor, deps] : [undefined, destructor];
|
|
46
|
-
|
|
7
|
+
export const useAsyncEffect = <T>(callback: () => Promise<T> | void, deps?: any[]) => {
|
|
47
8
|
useEffect(() => {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
value = result;
|
|
54
|
-
})
|
|
55
|
-
.catch(log.catch);
|
|
56
|
-
|
|
57
|
-
return () => {
|
|
58
|
-
mounted = false;
|
|
59
|
-
effectDestructor?.(value);
|
|
60
|
-
};
|
|
61
|
-
}, effectDeps);
|
|
9
|
+
const t = setTimeout(() => {
|
|
10
|
+
void callback();
|
|
11
|
+
});
|
|
12
|
+
return () => clearTimeout(t);
|
|
13
|
+
}, deps);
|
|
62
14
|
};
|
package/src/useAsyncState.ts
CHANGED
|
@@ -14,7 +14,7 @@ export const useAsyncState = <T>(
|
|
|
14
14
|
const [value, setValue] = useState<T | undefined>();
|
|
15
15
|
useEffect(() => {
|
|
16
16
|
let disposed = false;
|
|
17
|
-
|
|
17
|
+
const t = setTimeout(async () => {
|
|
18
18
|
const data = await cb();
|
|
19
19
|
if (!disposed) {
|
|
20
20
|
setValue(data);
|
|
@@ -23,6 +23,7 @@ export const useAsyncState = <T>(
|
|
|
23
23
|
|
|
24
24
|
return () => {
|
|
25
25
|
disposed = true;
|
|
26
|
+
clearTimeout(t);
|
|
26
27
|
};
|
|
27
28
|
}, deps);
|
|
28
29
|
|
|
@@ -7,7 +7,11 @@ import { type Dispatch, type SetStateAction, useEffect, useState } from 'react';
|
|
|
7
7
|
/**
|
|
8
8
|
* A stateful hook with a controlled value.
|
|
9
9
|
*/
|
|
10
|
-
export const useControlledState = <T>(
|
|
10
|
+
export const useControlledState = <T>(
|
|
11
|
+
controlledValue: T,
|
|
12
|
+
onChange?: (value: T) => void,
|
|
13
|
+
...deps: any[]
|
|
14
|
+
): [T, Dispatch<SetStateAction<T>>] => {
|
|
11
15
|
const [value, setValue] = useState<T>(controlledValue);
|
|
12
16
|
useEffect(() => {
|
|
13
17
|
if (controlledValue !== undefined) {
|
|
@@ -15,5 +19,9 @@ export const useControlledState = <T>(controlledValue: T, ...deps: any[]): [T, D
|
|
|
15
19
|
}
|
|
16
20
|
}, [controlledValue, ...deps]);
|
|
17
21
|
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
onChange?.(value);
|
|
24
|
+
}, [value, onChange]);
|
|
25
|
+
|
|
18
26
|
return [value, setValue];
|
|
19
27
|
};
|
package/src/useDefaultValue.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { useEffect,
|
|
5
|
+
import { useEffect, useMemo, useState } from 'react';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* A custom React hook that provides a stable default value for a potentially undefined reactive value.
|
|
@@ -21,7 +21,6 @@ export const useDefaultValue = <T>(reactiveValue: T | undefined | null, getDefau
|
|
|
21
21
|
// regardless of whether the defaultValue changes.
|
|
22
22
|
const stableDefaultValue = useMemo(getDefaultValue, []);
|
|
23
23
|
const [value, setValue] = useState(reactiveValue ?? stableDefaultValue);
|
|
24
|
-
|
|
25
24
|
useEffect(() => {
|
|
26
25
|
setValue(reactiveValue ?? stableDefaultValue);
|
|
27
26
|
}, [reactiveValue, stableDefaultValue]);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import defaultsDeep from 'lodash.defaultsdeep';
|
|
6
|
+
import { useMemo } from 'react';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Returns a memo-ized deep-merged object of the default and value.
|
|
10
|
+
* If value is undefined or null, then returns the default.
|
|
11
|
+
*/
|
|
12
|
+
export const useDefaults = <T>(value: T | undefined | null, defaults: T): T => {
|
|
13
|
+
return useMemo(() => defaultsDeep({}, defaults, value), [value, defaults]);
|
|
14
|
+
};
|
package/src/useForwardedRef.ts
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { type ForwardedRef,
|
|
5
|
+
import { type ForwardedRef, useEffect, useRef } from 'react';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Combines a possibly undefined forwarded ref with a locally defined ref.
|
|
9
|
-
*
|
|
9
|
+
* @deprecated Use @radix-ui/react-compose-refs
|
|
10
10
|
*/
|
|
11
11
|
export const useForwardedRef = <T>(ref: ForwardedRef<T>) => {
|
|
12
12
|
const innerRef = useRef<T>(null);
|
package/src/useIsFocused.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// Based upon the useIsFocused hook which is part of the `rci` project:
|
|
6
6
|
/// https://github.com/leonardodino/rci/blob/main/packages/use-is-focused
|
|
7
7
|
|
|
8
|
-
import { useEffect, useRef, useState
|
|
8
|
+
import { type RefObject, useEffect, useRef, useState } from 'react';
|
|
9
9
|
|
|
10
10
|
export const useIsFocused = (inputRef: RefObject<HTMLInputElement>) => {
|
|
11
11
|
const [isFocused, setIsFocused] = useState<boolean | undefined>(undefined);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { useEffect, useRef } from 'react';
|
|
6
|
+
|
|
7
|
+
export const useTimeout = (callback: (() => Promise<void>) | undefined, delay = 0, deps: any[] = []) => {
|
|
8
|
+
const callbackRef = useRef(callback);
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
callbackRef.current = callback;
|
|
11
|
+
}, [callback]);
|
|
12
|
+
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (delay == null) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const timeout = setTimeout(() => callbackRef.current?.(), delay);
|
|
19
|
+
return () => clearTimeout(timeout);
|
|
20
|
+
}, [delay, ...deps]);
|
|
21
|
+
};
|
package/src/useTrackProps.ts
CHANGED
package/src/useTransitions.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { useEffect, useRef, useState } from 'react';
|
|
6
6
|
|
|
7
7
|
const isFunction = <T>(functionToCheck: any): functionToCheck is (value: T) => boolean => {
|
|
8
8
|
return functionToCheck instanceof Function;
|
package/dist/lib/node/index.cjs
DELETED
|
@@ -1,413 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
var node_exports = {};
|
|
30
|
-
__export(node_exports, {
|
|
31
|
-
makeId: () => makeId,
|
|
32
|
-
randomString: () => randomString,
|
|
33
|
-
useAsyncEffect: () => useAsyncEffect,
|
|
34
|
-
useAsyncState: () => useAsyncState,
|
|
35
|
-
useControlledState: () => useControlledState,
|
|
36
|
-
useDebugReactDeps: () => useDebugReactDeps,
|
|
37
|
-
useDefaultValue: () => useDefaultValue,
|
|
38
|
-
useDidTransition: () => useDidTransition,
|
|
39
|
-
useDynamicRef: () => useDynamicRef,
|
|
40
|
-
useFileDownload: () => useFileDownload,
|
|
41
|
-
useForwardedRef: () => useForwardedRef,
|
|
42
|
-
useId: () => useId,
|
|
43
|
-
useIsFocused: () => useIsFocused,
|
|
44
|
-
useMediaQuery: () => useMediaQuery,
|
|
45
|
-
useMulticastObservable: () => useMulticastObservable,
|
|
46
|
-
useOnTransition: () => useOnTransition,
|
|
47
|
-
useRefCallback: () => useRefCallback,
|
|
48
|
-
useResize: () => useResize,
|
|
49
|
-
useTrackProps: () => useTrackProps
|
|
50
|
-
});
|
|
51
|
-
module.exports = __toCommonJS(node_exports);
|
|
52
|
-
var import_react = require("react");
|
|
53
|
-
var import_log = require("@dxos/log");
|
|
54
|
-
var import_react2 = require("react");
|
|
55
|
-
var import_react3 = require("react");
|
|
56
|
-
var import_react4 = require("react");
|
|
57
|
-
var import_react5 = require("react");
|
|
58
|
-
var import_react6 = require("react");
|
|
59
|
-
var import_react7 = require("react");
|
|
60
|
-
var import_react8 = require("react");
|
|
61
|
-
var import_alea = __toESM(require("alea"));
|
|
62
|
-
var import_react9 = require("react");
|
|
63
|
-
var import_react10 = require("react");
|
|
64
|
-
var import_react11 = require("react");
|
|
65
|
-
var import_react12 = require("react");
|
|
66
|
-
var import_react13 = require("react");
|
|
67
|
-
var import_react14 = require("react");
|
|
68
|
-
var import_react15 = require("react");
|
|
69
|
-
var import_log2 = require("@dxos/log");
|
|
70
|
-
var import_react16 = require("react");
|
|
71
|
-
var useAsyncEffect = (callback, destructor, deps) => {
|
|
72
|
-
const [effectDestructor, effectDeps] = typeof destructor === "function" ? [
|
|
73
|
-
destructor,
|
|
74
|
-
deps
|
|
75
|
-
] : [
|
|
76
|
-
void 0,
|
|
77
|
-
destructor
|
|
78
|
-
];
|
|
79
|
-
(0, import_react.useEffect)(() => {
|
|
80
|
-
let mounted = true;
|
|
81
|
-
let value;
|
|
82
|
-
const asyncResult = callback(() => mounted);
|
|
83
|
-
void Promise.resolve(asyncResult).then((result) => {
|
|
84
|
-
value = result;
|
|
85
|
-
}).catch(import_log.log.catch);
|
|
86
|
-
return () => {
|
|
87
|
-
mounted = false;
|
|
88
|
-
effectDestructor?.(value);
|
|
89
|
-
};
|
|
90
|
-
}, effectDeps);
|
|
91
|
-
};
|
|
92
|
-
var useAsyncState = (cb, deps = []) => {
|
|
93
|
-
const [value, setValue] = (0, import_react2.useState)();
|
|
94
|
-
(0, import_react2.useEffect)(() => {
|
|
95
|
-
let disposed = false;
|
|
96
|
-
queueMicrotask(async () => {
|
|
97
|
-
const data = await cb();
|
|
98
|
-
if (!disposed) {
|
|
99
|
-
setValue(data);
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
return () => {
|
|
103
|
-
disposed = true;
|
|
104
|
-
};
|
|
105
|
-
}, deps);
|
|
106
|
-
return [
|
|
107
|
-
value,
|
|
108
|
-
setValue
|
|
109
|
-
];
|
|
110
|
-
};
|
|
111
|
-
var useControlledState = (controlledValue, ...deps) => {
|
|
112
|
-
const [value, setValue] = (0, import_react3.useState)(controlledValue);
|
|
113
|
-
(0, import_react3.useEffect)(() => {
|
|
114
|
-
if (controlledValue !== void 0) {
|
|
115
|
-
setValue(controlledValue);
|
|
116
|
-
}
|
|
117
|
-
}, [
|
|
118
|
-
controlledValue,
|
|
119
|
-
...deps
|
|
120
|
-
]);
|
|
121
|
-
return [
|
|
122
|
-
value,
|
|
123
|
-
setValue
|
|
124
|
-
];
|
|
125
|
-
};
|
|
126
|
-
var useDebugReactDeps = (deps = []) => {
|
|
127
|
-
const lastDeps = (0, import_react4.useRef)([]);
|
|
128
|
-
(0, import_react4.useEffect)(() => {
|
|
129
|
-
console.group("deps changed", {
|
|
130
|
-
old: lastDeps.current.length,
|
|
131
|
-
new: deps.length
|
|
132
|
-
});
|
|
133
|
-
for (let i = 0; i < Math.max(lastDeps.current.length ?? 0, deps.length ?? 0); i++) {
|
|
134
|
-
console.log(i, lastDeps.current[i] === deps[i] ? "SAME" : "CHANGED", {
|
|
135
|
-
previous: lastDeps.current[i],
|
|
136
|
-
current: deps[i]
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
console.groupEnd();
|
|
140
|
-
lastDeps.current = deps;
|
|
141
|
-
}, deps);
|
|
142
|
-
};
|
|
143
|
-
var useDefaultValue = (reactiveValue, getDefaultValue) => {
|
|
144
|
-
const stableDefaultValue = (0, import_react5.useMemo)(getDefaultValue, []);
|
|
145
|
-
const [value, setValue] = (0, import_react5.useState)(reactiveValue ?? stableDefaultValue);
|
|
146
|
-
(0, import_react5.useEffect)(() => {
|
|
147
|
-
setValue(reactiveValue ?? stableDefaultValue);
|
|
148
|
-
}, [
|
|
149
|
-
reactiveValue,
|
|
150
|
-
stableDefaultValue
|
|
151
|
-
]);
|
|
152
|
-
return value;
|
|
153
|
-
};
|
|
154
|
-
var useDynamicRef = (value) => {
|
|
155
|
-
const ref = (0, import_react6.useRef)(value);
|
|
156
|
-
(0, import_react6.useEffect)(() => {
|
|
157
|
-
ref.current = value;
|
|
158
|
-
}, [
|
|
159
|
-
value
|
|
160
|
-
]);
|
|
161
|
-
return ref;
|
|
162
|
-
};
|
|
163
|
-
var useFileDownload = () => {
|
|
164
|
-
return (0, import_react7.useMemo)(() => (data, filename) => {
|
|
165
|
-
const url = typeof data === "string" ? data : URL.createObjectURL(data);
|
|
166
|
-
const element = document.createElement("a");
|
|
167
|
-
element.setAttribute("href", url);
|
|
168
|
-
element.setAttribute("download", filename);
|
|
169
|
-
element.setAttribute("target", "download");
|
|
170
|
-
element.click();
|
|
171
|
-
}, []);
|
|
172
|
-
};
|
|
173
|
-
var useForwardedRef = (ref) => {
|
|
174
|
-
const innerRef = (0, import_react8.useRef)(null);
|
|
175
|
-
(0, import_react8.useEffect)(() => {
|
|
176
|
-
if (!ref) {
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
if (typeof ref === "function") {
|
|
180
|
-
ref(innerRef.current);
|
|
181
|
-
} else {
|
|
182
|
-
ref.current = innerRef.current;
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
return innerRef;
|
|
186
|
-
};
|
|
187
|
-
var Alea = import_alea.default;
|
|
188
|
-
var prng = new Alea("@dxos/react-hooks");
|
|
189
|
-
var randomString = (n = 4) => prng().toString(16).slice(2, n + 2);
|
|
190
|
-
var useId = (namespace, propsId, opts) => (0, import_react9.useMemo)(() => makeId(namespace, propsId, opts), [
|
|
191
|
-
propsId
|
|
192
|
-
]);
|
|
193
|
-
var makeId = (namespace, propsId, opts) => propsId ?? `${namespace}-${randomString(opts?.n ?? 4)}`;
|
|
194
|
-
var useIsFocused = (inputRef) => {
|
|
195
|
-
const [isFocused, setIsFocused] = (0, import_react10.useState)(void 0);
|
|
196
|
-
const isFocusedRef = (0, import_react10.useRef)(isFocused);
|
|
197
|
-
isFocusedRef.current = isFocused;
|
|
198
|
-
(0, import_react10.useEffect)(() => {
|
|
199
|
-
const input = inputRef.current;
|
|
200
|
-
if (!input) {
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
const onFocus = () => setIsFocused(true);
|
|
204
|
-
const onBlur = () => setIsFocused(false);
|
|
205
|
-
input.addEventListener("focus", onFocus);
|
|
206
|
-
input.addEventListener("blur", onBlur);
|
|
207
|
-
if (isFocusedRef.current === void 0) {
|
|
208
|
-
setIsFocused(document.activeElement === input);
|
|
209
|
-
}
|
|
210
|
-
return () => {
|
|
211
|
-
input.removeEventListener("focus", onFocus);
|
|
212
|
-
input.removeEventListener("blur", onBlur);
|
|
213
|
-
};
|
|
214
|
-
}, [
|
|
215
|
-
inputRef,
|
|
216
|
-
setIsFocused
|
|
217
|
-
]);
|
|
218
|
-
return isFocused;
|
|
219
|
-
};
|
|
220
|
-
var breakpointMediaQueries = {
|
|
221
|
-
sm: "(min-width: 640px)",
|
|
222
|
-
md: "(min-width: 768px)",
|
|
223
|
-
lg: "(min-width: 1024px)",
|
|
224
|
-
xl: "(min-width: 1280px)",
|
|
225
|
-
"2xl": "(min-width: 1536px)"
|
|
226
|
-
};
|
|
227
|
-
var useMediaQuery = (query, options = {}) => {
|
|
228
|
-
const { ssr = true, fallback } = options;
|
|
229
|
-
const queries = (Array.isArray(query) ? query : [
|
|
230
|
-
query
|
|
231
|
-
]).map((query2) => query2 in breakpointMediaQueries ? breakpointMediaQueries[query2] : query2);
|
|
232
|
-
let fallbackValues = Array.isArray(fallback) ? fallback : [
|
|
233
|
-
fallback
|
|
234
|
-
];
|
|
235
|
-
fallbackValues = fallbackValues.filter((v) => v != null);
|
|
236
|
-
const [value, setValue] = (0, import_react11.useState)(() => {
|
|
237
|
-
return queries.map((query2, index) => ({
|
|
238
|
-
media: query2,
|
|
239
|
-
matches: ssr ? !!fallbackValues[index] : document.defaultView?.matchMedia(query2).matches
|
|
240
|
-
}));
|
|
241
|
-
});
|
|
242
|
-
(0, import_react11.useEffect)(() => {
|
|
243
|
-
setValue(queries.map((query2) => ({
|
|
244
|
-
media: query2,
|
|
245
|
-
matches: document.defaultView?.matchMedia(query2).matches
|
|
246
|
-
})));
|
|
247
|
-
const mql = queries.map((query2) => document.defaultView?.matchMedia(query2));
|
|
248
|
-
const handler = (evt) => {
|
|
249
|
-
setValue((prev) => {
|
|
250
|
-
return prev.slice().map((item) => {
|
|
251
|
-
if (item.media === evt.media) {
|
|
252
|
-
return {
|
|
253
|
-
...item,
|
|
254
|
-
matches: evt.matches
|
|
255
|
-
};
|
|
256
|
-
}
|
|
257
|
-
return item;
|
|
258
|
-
});
|
|
259
|
-
});
|
|
260
|
-
};
|
|
261
|
-
mql.forEach((mql2) => {
|
|
262
|
-
if (typeof mql2?.addListener === "function") {
|
|
263
|
-
mql2?.addListener(handler);
|
|
264
|
-
} else {
|
|
265
|
-
mql2?.addEventListener("change", handler);
|
|
266
|
-
}
|
|
267
|
-
});
|
|
268
|
-
return () => {
|
|
269
|
-
mql.forEach((mql2) => {
|
|
270
|
-
if (typeof mql2?.removeListener === "function") {
|
|
271
|
-
mql2?.removeListener(handler);
|
|
272
|
-
} else {
|
|
273
|
-
mql2?.removeEventListener("change", handler);
|
|
274
|
-
}
|
|
275
|
-
});
|
|
276
|
-
};
|
|
277
|
-
}, [
|
|
278
|
-
document.defaultView
|
|
279
|
-
]);
|
|
280
|
-
return value.map((item) => !!item.matches);
|
|
281
|
-
};
|
|
282
|
-
var useMulticastObservable = (observable) => {
|
|
283
|
-
const subscribeFn = (0, import_react12.useMemo)(() => (listener) => {
|
|
284
|
-
const subscription = observable.subscribe(listener);
|
|
285
|
-
return () => subscription.unsubscribe();
|
|
286
|
-
}, [
|
|
287
|
-
observable
|
|
288
|
-
]);
|
|
289
|
-
return (0, import_react12.useSyncExternalStore)(subscribeFn, () => observable.get());
|
|
290
|
-
};
|
|
291
|
-
var useRefCallback = () => {
|
|
292
|
-
const [value, setValue] = (0, import_react13.useState)(null);
|
|
293
|
-
return {
|
|
294
|
-
refCallback: (value2) => setValue(value2),
|
|
295
|
-
value
|
|
296
|
-
};
|
|
297
|
-
};
|
|
298
|
-
var useResize = (handler, deps = [], delay = 800) => {
|
|
299
|
-
const debouncedHandler = (0, import_react14.useMemo)(() => {
|
|
300
|
-
let timeout;
|
|
301
|
-
return (event) => {
|
|
302
|
-
clearTimeout(timeout);
|
|
303
|
-
timeout = setTimeout(() => {
|
|
304
|
-
handler(event);
|
|
305
|
-
}, delay);
|
|
306
|
-
};
|
|
307
|
-
}, [
|
|
308
|
-
handler,
|
|
309
|
-
delay
|
|
310
|
-
]);
|
|
311
|
-
return (0, import_react14.useLayoutEffect)(() => {
|
|
312
|
-
window.visualViewport?.addEventListener("resize", debouncedHandler);
|
|
313
|
-
debouncedHandler();
|
|
314
|
-
return () => window.visualViewport?.removeEventListener("resize", debouncedHandler);
|
|
315
|
-
}, [
|
|
316
|
-
debouncedHandler,
|
|
317
|
-
...deps
|
|
318
|
-
]);
|
|
319
|
-
};
|
|
320
|
-
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/ui/primitives/react-hooks/src/useTrackProps.ts";
|
|
321
|
-
var useTrackProps = (props, componentName = "Component", active = true) => {
|
|
322
|
-
const prevProps = (0, import_react15.useRef)(props);
|
|
323
|
-
(0, import_react15.useEffect)(() => {
|
|
324
|
-
const changes = Object.entries(props).filter(([key]) => props[key] !== prevProps.current[key]);
|
|
325
|
-
if (changes.length > 0) {
|
|
326
|
-
if (active) {
|
|
327
|
-
import_log2.log.info("props changed", {
|
|
328
|
-
componentName,
|
|
329
|
-
keys: changes.map(([key]) => key).join(","),
|
|
330
|
-
props: Object.fromEntries(changes.map(([key]) => [
|
|
331
|
-
key,
|
|
332
|
-
{
|
|
333
|
-
from: prevProps.current[key],
|
|
334
|
-
to: props[key]
|
|
335
|
-
}
|
|
336
|
-
]))
|
|
337
|
-
}, {
|
|
338
|
-
F: __dxlog_file,
|
|
339
|
-
L: 22,
|
|
340
|
-
S: void 0,
|
|
341
|
-
C: (f, a) => f(...a)
|
|
342
|
-
});
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
prevProps.current = props;
|
|
346
|
-
});
|
|
347
|
-
};
|
|
348
|
-
var isFunction = (functionToCheck) => {
|
|
349
|
-
return functionToCheck instanceof Function;
|
|
350
|
-
};
|
|
351
|
-
var useDidTransition = (currentValue, fromValue, toValue) => {
|
|
352
|
-
const [hasTransitioned, setHasTransitioned] = (0, import_react16.useState)(false);
|
|
353
|
-
const previousValue = (0, import_react16.useRef)(currentValue);
|
|
354
|
-
(0, import_react16.useEffect)(() => {
|
|
355
|
-
const toValueValid = isFunction(toValue) ? toValue(currentValue) : toValue === currentValue;
|
|
356
|
-
const fromValueValid = isFunction(fromValue) ? fromValue(previousValue.current) : fromValue === previousValue.current;
|
|
357
|
-
if (fromValueValid && toValueValid && !hasTransitioned) {
|
|
358
|
-
setHasTransitioned(true);
|
|
359
|
-
} else if ((!fromValueValid || !toValueValid) && hasTransitioned) {
|
|
360
|
-
setHasTransitioned(false);
|
|
361
|
-
}
|
|
362
|
-
previousValue.current = currentValue;
|
|
363
|
-
}, [
|
|
364
|
-
currentValue,
|
|
365
|
-
fromValue,
|
|
366
|
-
toValue,
|
|
367
|
-
hasTransitioned
|
|
368
|
-
]);
|
|
369
|
-
return hasTransitioned;
|
|
370
|
-
};
|
|
371
|
-
var useOnTransition = (currentValue, fromValue, toValue, callback) => {
|
|
372
|
-
const dirty = (0, import_react16.useRef)(false);
|
|
373
|
-
const hasTransitioned = useDidTransition(currentValue, fromValue, toValue);
|
|
374
|
-
(0, import_react16.useEffect)(() => {
|
|
375
|
-
dirty.current = false;
|
|
376
|
-
}, [
|
|
377
|
-
currentValue,
|
|
378
|
-
dirty
|
|
379
|
-
]);
|
|
380
|
-
(0, import_react16.useEffect)(() => {
|
|
381
|
-
if (hasTransitioned && !dirty.current) {
|
|
382
|
-
callback();
|
|
383
|
-
dirty.current = true;
|
|
384
|
-
}
|
|
385
|
-
}, [
|
|
386
|
-
hasTransitioned,
|
|
387
|
-
dirty,
|
|
388
|
-
callback
|
|
389
|
-
]);
|
|
390
|
-
};
|
|
391
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
392
|
-
0 && (module.exports = {
|
|
393
|
-
makeId,
|
|
394
|
-
randomString,
|
|
395
|
-
useAsyncEffect,
|
|
396
|
-
useAsyncState,
|
|
397
|
-
useControlledState,
|
|
398
|
-
useDebugReactDeps,
|
|
399
|
-
useDefaultValue,
|
|
400
|
-
useDidTransition,
|
|
401
|
-
useDynamicRef,
|
|
402
|
-
useFileDownload,
|
|
403
|
-
useForwardedRef,
|
|
404
|
-
useId,
|
|
405
|
-
useIsFocused,
|
|
406
|
-
useMediaQuery,
|
|
407
|
-
useMulticastObservable,
|
|
408
|
-
useOnTransition,
|
|
409
|
-
useRefCallback,
|
|
410
|
-
useResize,
|
|
411
|
-
useTrackProps
|
|
412
|
-
});
|
|
413
|
-
//# sourceMappingURL=index.cjs.map
|