@dxos/react-hooks 0.8.4-main.7ace549 → 0.8.4-main.937b3ca
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 +51 -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 +51 -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 +0 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/useControlledState.d.ts +2 -2
- package/dist/types/src/useControlledState.d.ts.map +1 -1
- package/dist/types/src/useDebugDeps.d.ts +1 -1
- package/dist/types/src/useDebugDeps.d.ts.map +1 -1
- package/dist/types/src/useDynamicRef.d.ts +1 -1
- package/dist/types/src/useDynamicRef.d.ts.map +1 -1
- package/dist/types/src/useId.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +15 -12
- package/src/index.ts +0 -1
- package/src/useControlledState.ts +6 -6
- package/src/useDebugDeps.ts +17 -8
- package/src/useDynamicRef.ts +3 -3
- package/src/useForwardedRef.ts +1 -1
- package/src/useId.ts +3 -2
- package/dist/types/src/useSignals.d.ts +0 -10
- package/dist/types/src/useSignals.d.ts.map +0 -1
- package/src/useSignals.ts +0 -26
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-hooks",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.937b3ca",
|
|
4
4
|
"description": "React hooks supporting DXOS React primitives.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"author": "DXOS.org",
|
|
9
|
-
"sideEffects":
|
|
13
|
+
"sideEffects": false,
|
|
10
14
|
"type": "module",
|
|
11
15
|
"exports": {
|
|
12
16
|
".": {
|
|
@@ -25,24 +29,23 @@
|
|
|
25
29
|
"src"
|
|
26
30
|
],
|
|
27
31
|
"dependencies": {
|
|
28
|
-
"@
|
|
29
|
-
"@radix-ui/react-use-controllable-state": "^1.2.2",
|
|
32
|
+
"@radix-ui/react-id": "1.1.0",
|
|
30
33
|
"alea": "^1.0.1",
|
|
31
34
|
"lodash.defaultsdeep": "^4.6.1",
|
|
32
35
|
"mini-virtual-list": "^0.3.2",
|
|
33
|
-
"@dxos/async": "0.8.4-main.
|
|
34
|
-
"@dxos/log": "0.8.4-main.
|
|
36
|
+
"@dxos/async": "0.8.4-main.937b3ca",
|
|
37
|
+
"@dxos/log": "0.8.4-main.937b3ca"
|
|
35
38
|
},
|
|
36
39
|
"devDependencies": {
|
|
37
40
|
"@types/lodash.defaultsdeep": "^4.6.6",
|
|
38
|
-
"@types/react": "~19.2.
|
|
39
|
-
"@types/react-dom": "~19.2.
|
|
40
|
-
"react": "~19.2.
|
|
41
|
-
"react-dom": "~19.2.
|
|
41
|
+
"@types/react": "~19.2.7",
|
|
42
|
+
"@types/react-dom": "~19.2.3",
|
|
43
|
+
"react": "~19.2.3",
|
|
44
|
+
"react-dom": "~19.2.3"
|
|
42
45
|
},
|
|
43
46
|
"peerDependencies": {
|
|
44
|
-
"react": "
|
|
45
|
-
"react-dom": "
|
|
47
|
+
"react": "~19.2.3",
|
|
48
|
+
"react-dom": "~19.2.3"
|
|
46
49
|
},
|
|
47
50
|
"publishConfig": {
|
|
48
51
|
"access": "public"
|
package/src/index.ts
CHANGED
|
@@ -8,19 +8,19 @@ import { useDynamicRef } from './useDynamicRef';
|
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* A stateful hook with a controlled value.
|
|
11
|
-
*
|
|
11
|
+
* NOTE: Consider using Radix's `useControllableState`.
|
|
12
12
|
*/
|
|
13
13
|
export const useControlledState = <T>(
|
|
14
|
-
|
|
14
|
+
valueProp: T,
|
|
15
15
|
onChange?: (value: T) => void,
|
|
16
16
|
): [T, Dispatch<SetStateAction<T>>] => {
|
|
17
|
-
const [value, setControlledValue] = useState(
|
|
17
|
+
const [value, setControlledValue] = useState(valueProp);
|
|
18
18
|
useEffect(() => {
|
|
19
|
-
setControlledValue(
|
|
20
|
-
}, [
|
|
19
|
+
setControlledValue(valueProp);
|
|
20
|
+
}, [valueProp]);
|
|
21
21
|
|
|
22
22
|
const onChangeRef = useRef(onChange);
|
|
23
|
-
const valueRef = useDynamicRef(
|
|
23
|
+
const valueRef = useDynamicRef(valueProp);
|
|
24
24
|
const setValue = useCallback<Dispatch<SetStateAction<T>>>(
|
|
25
25
|
(nextValue) => {
|
|
26
26
|
const value = isFunction(nextValue) ? nextValue(valueRef.current) : nextValue;
|
package/src/useDebugDeps.ts
CHANGED
|
@@ -4,23 +4,32 @@
|
|
|
4
4
|
|
|
5
5
|
import { type DependencyList, useEffect, useRef } from 'react';
|
|
6
6
|
|
|
7
|
+
import { log } from '@dxos/log';
|
|
8
|
+
|
|
7
9
|
/**
|
|
8
10
|
* Util to log deps that have changed.
|
|
9
11
|
*/
|
|
10
|
-
export const useDebugDeps = (deps: DependencyList = [], active = true) => {
|
|
12
|
+
export const useDebugDeps = (deps: DependencyList = [], label = 'useDebugDeps', active = true) => {
|
|
11
13
|
const lastDeps = useRef<DependencyList>([]);
|
|
12
14
|
useEffect(() => {
|
|
13
|
-
|
|
15
|
+
if (!active) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const diff: Record<number, { previous: any; current: any }> = {};
|
|
14
20
|
for (let i = 0; i < Math.max(lastDeps.current.length ?? 0, deps.length ?? 0); i++) {
|
|
15
|
-
if (lastDeps.current[i] !== deps[i]
|
|
16
|
-
|
|
17
|
-
index: i,
|
|
21
|
+
if (lastDeps.current[i] !== deps[i] || i > lastDeps.current.length) {
|
|
22
|
+
diff[i] = {
|
|
18
23
|
previous: lastDeps.current[i],
|
|
19
24
|
current: deps[i],
|
|
20
|
-
}
|
|
25
|
+
};
|
|
21
26
|
}
|
|
22
27
|
}
|
|
23
|
-
|
|
28
|
+
|
|
29
|
+
if (Object.keys(diff).length > 0) {
|
|
30
|
+
log.warn(`Updated: ${label} [${lastDeps.current.length}/${deps.length}]`, diff);
|
|
31
|
+
}
|
|
32
|
+
|
|
24
33
|
lastDeps.current = deps;
|
|
25
|
-
}, deps);
|
|
34
|
+
}, [...deps, active]);
|
|
26
35
|
};
|
package/src/useDynamicRef.ts
CHANGED
|
@@ -7,9 +7,9 @@ import { type Dispatch, type RefObject, type SetStateAction, useCallback, useEff
|
|
|
7
7
|
/**
|
|
8
8
|
* Like `useState` but with an additional dynamic value.
|
|
9
9
|
*/
|
|
10
|
-
export const useStateWithRef = <T>(
|
|
11
|
-
const [value, setValue] = useState<T>(
|
|
12
|
-
const valueRef = useRef<T>(
|
|
10
|
+
export const useStateWithRef = <T>(valueProp: T): [T, Dispatch<SetStateAction<T>>, RefObject<T>] => {
|
|
11
|
+
const [value, setValue] = useState<T>(valueProp);
|
|
12
|
+
const valueRef = useRef<T>(valueProp);
|
|
13
13
|
const setter = useCallback<Dispatch<SetStateAction<T>>>((value) => {
|
|
14
14
|
if (typeof value === 'function') {
|
|
15
15
|
setValue((current) => {
|
package/src/useForwardedRef.ts
CHANGED
|
@@ -57,5 +57,5 @@ export const mergeRefs = <T>(refs: (Ref<T> | undefined)[]): Ref<T> => {
|
|
|
57
57
|
* The returned ref is memoized and only changes when the refs array changes.
|
|
58
58
|
*/
|
|
59
59
|
export const useMergeRefs = <T>(refs: (Ref<T> | undefined)[]): Ref<T> => {
|
|
60
|
-
return useMemo(() => mergeRefs(refs), refs);
|
|
60
|
+
return useMemo(() => mergeRefs(refs), [...refs]);
|
|
61
61
|
};
|
package/src/useId.ts
CHANGED
|
@@ -19,8 +19,9 @@ export const randomString = (n = 4) =>
|
|
|
19
19
|
.toString(16)
|
|
20
20
|
.slice(2, n + 2);
|
|
21
21
|
|
|
22
|
-
export const useId = (namespace: string, propsId?: string, opts?: Partial<{ n: number }>) =>
|
|
23
|
-
useMemo(() => makeId(namespace, propsId, opts), [propsId]);
|
|
22
|
+
export const useId = (namespace: string, propsId?: string, opts?: Partial<{ n: number }>) => {
|
|
23
|
+
return useMemo(() => makeId(namespace, propsId, opts), [propsId]);
|
|
24
|
+
};
|
|
24
25
|
|
|
25
26
|
export const makeId = (namespace: string, propsId?: string, opts?: Partial<{ n: number }>) =>
|
|
26
27
|
propsId ?? `${namespace}-${randomString(opts?.n ?? 4)}`;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { type DependencyList } from 'react';
|
|
2
|
-
/**
|
|
3
|
-
* Like `useEffect` but also tracks signals inside of the callback.
|
|
4
|
-
*/
|
|
5
|
-
export declare const useSignalsEffect: (cb: () => void | (() => void), deps?: DependencyList) => void;
|
|
6
|
-
/**
|
|
7
|
-
* Like `useMemo` but also tracks signals inside of the callback.
|
|
8
|
-
*/
|
|
9
|
-
export declare const useSignalsMemo: <T>(cb: () => T, deps?: DependencyList) => T;
|
|
10
|
-
//# sourceMappingURL=useSignals.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useSignals.d.ts","sourceRoot":"","sources":["../../../src/useSignals.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,cAAc,EAA8B,MAAM,OAAO,CAAC;AAExE;;GAEG;AACH,eAAO,MAAM,gBAAgB,GAAI,IAAI,MAAM,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC,EAAE,OAAO,cAAc,SAQpF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,CAAC,EAAE,IAAI,MAAM,CAAC,EAAE,OAAO,cAAc,MAEnE,CAAC"}
|
package/src/useSignals.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2022 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { computed, effect } from '@preact-signals/safe-react';
|
|
6
|
-
import { type DependencyList, useEffect, useMemo, useRef } from 'react';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Like `useEffect` but also tracks signals inside of the callback.
|
|
10
|
-
*/
|
|
11
|
-
export const useSignalsEffect = (cb: () => void | (() => void), deps?: DependencyList) => {
|
|
12
|
-
const callback = useRef(cb);
|
|
13
|
-
callback.current = cb;
|
|
14
|
-
useEffect(() => {
|
|
15
|
-
return effect(() => {
|
|
16
|
-
return callback.current();
|
|
17
|
-
});
|
|
18
|
-
}, deps ?? []);
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Like `useMemo` but also tracks signals inside of the callback.
|
|
23
|
-
*/
|
|
24
|
-
export const useSignalsMemo = <T>(cb: () => T, deps?: DependencyList) => {
|
|
25
|
-
return useMemo(() => computed(cb), deps ?? []).value;
|
|
26
|
-
};
|