@dxos/react-hooks 0.8.4-main.72ec0f3 → 0.8.4-main.74a063c4e0
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/README.md +1 -0
- package/dist/lib/browser/index.mjs +60 -69
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +60 -69
- 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 -2
- 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/src/useViewportResize.d.ts +1 -1
- package/dist/types/src/useViewportResize.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +16 -12
- package/src/index.ts +3 -3
- package/src/useControlledState.ts +6 -6
- package/src/useDebugDeps.ts +17 -8
- package/src/useDynamicRef.ts +4 -5
- package/src/useForwardedRef.ts +1 -1
- package/src/useId.ts +3 -2
- package/src/useIsFocused.ts +1 -1
- package/src/useMulticastObservable.test.ts +1 -1
- package/src/useViewportResize.ts +3 -3
- package/dist/types/src/useSignals.d.ts +0 -10
- package/dist/types/src/useSignals.d.ts.map +0 -1
- package/src/useSignals.ts +0 -27
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.74a063c4e0",
|
|
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,24 @@
|
|
|
25
29
|
"src"
|
|
26
30
|
],
|
|
27
31
|
"dependencies": {
|
|
28
|
-
"@
|
|
29
|
-
"@radix-ui/react-
|
|
32
|
+
"@radix-ui/react-compose-refs": "1.1.1",
|
|
33
|
+
"@radix-ui/react-id": "1.1.0",
|
|
30
34
|
"alea": "^1.0.1",
|
|
31
35
|
"lodash.defaultsdeep": "^4.6.1",
|
|
32
36
|
"mini-virtual-list": "^0.3.2",
|
|
33
|
-
"@dxos/async": "0.8.4-main.
|
|
34
|
-
"@dxos/log": "0.8.4-main.
|
|
37
|
+
"@dxos/async": "0.8.4-main.74a063c4e0",
|
|
38
|
+
"@dxos/log": "0.8.4-main.74a063c4e0"
|
|
35
39
|
},
|
|
36
40
|
"devDependencies": {
|
|
37
41
|
"@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.
|
|
42
|
+
"@types/react": "~19.2.7",
|
|
43
|
+
"@types/react-dom": "~19.2.3",
|
|
44
|
+
"react": "~19.2.3",
|
|
45
|
+
"react-dom": "~19.2.3"
|
|
42
46
|
},
|
|
43
47
|
"peerDependencies": {
|
|
44
|
-
"react": "
|
|
45
|
-
"react-dom": "
|
|
48
|
+
"react": "~19.2.3",
|
|
49
|
+
"react-dom": "~19.2.3"
|
|
46
50
|
},
|
|
47
51
|
"publishConfig": {
|
|
48
52
|
"access": "public"
|
package/src/index.ts
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
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';
|
|
6
9
|
export * from './useAsyncState';
|
|
7
10
|
export * from './useControlledState';
|
|
@@ -17,8 +20,5 @@ export * from './useMediaQuery';
|
|
|
17
20
|
export * from './useMulticastObservable';
|
|
18
21
|
export * from './useRefCallback';
|
|
19
22
|
export * from './useViewportResize';
|
|
20
|
-
export * from './useSignals';
|
|
21
23
|
export * from './useTimeout';
|
|
22
24
|
export * from './useTransitions';
|
|
23
|
-
|
|
24
|
-
export { useSize, useScroller } from 'mini-virtual-list';
|
|
@@ -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
|
@@ -2,15 +2,14 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { useCallback } from '
|
|
6
|
-
import { type Dispatch, type RefObject, type SetStateAction, useEffect, useRef, useState } from 'react';
|
|
5
|
+
import { type Dispatch, type RefObject, type SetStateAction, useCallback, useEffect, useRef, useState } from 'react';
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* Like `useState` but with an additional dynamic value.
|
|
10
9
|
*/
|
|
11
|
-
export const useStateWithRef = <T>(
|
|
12
|
-
const [value, setValue] = useState<T>(
|
|
13
|
-
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);
|
|
14
13
|
const setter = useCallback<Dispatch<SetStateAction<T>>>((value) => {
|
|
15
14
|
if (typeof value === 'function') {
|
|
16
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)}`;
|
package/src/useIsFocused.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
// Based upon the useIsFocused hook which is part of the `rci` project:
|
|
6
|
-
/// https://github.com/leonardodino/rci/blob/main/packages/use-
|
|
6
|
+
/// https://github.com/leonardodino/rci/blob/main/packages/use-w-focused
|
|
7
7
|
|
|
8
8
|
import { type RefObject, useEffect, useRef, useState } from 'react';
|
|
9
9
|
|
|
@@ -15,7 +15,7 @@ describe('useMulticastObservable', () => {
|
|
|
15
15
|
const observable = MulticastObservable.from(event, 0);
|
|
16
16
|
const { result } = renderHook(() => useMulticastObservable(observable));
|
|
17
17
|
expect(result.current).toEqual(0);
|
|
18
|
-
act(() => event.emit(1));
|
|
18
|
+
await act(async () => event.emit(1));
|
|
19
19
|
await expect.poll(() => result.current).toEqual(1);
|
|
20
20
|
});
|
|
21
21
|
});
|
package/src/useViewportResize.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { useLayoutEffect, useMemo } from 'react';
|
|
6
6
|
|
|
7
7
|
export const useViewportResize = (
|
|
8
|
-
|
|
8
|
+
cb: (event?: Event) => void,
|
|
9
9
|
deps: Parameters<typeof useLayoutEffect>[1] = [],
|
|
10
10
|
delay: number = 800,
|
|
11
11
|
) => {
|
|
@@ -14,10 +14,10 @@ export const useViewportResize = (
|
|
|
14
14
|
return (event?: Event) => {
|
|
15
15
|
clearTimeout(timeout);
|
|
16
16
|
timeout = setTimeout(() => {
|
|
17
|
-
|
|
17
|
+
cb(event);
|
|
18
18
|
}, delay);
|
|
19
19
|
};
|
|
20
|
-
}, [
|
|
20
|
+
}, [cb, delay]);
|
|
21
21
|
|
|
22
22
|
return useLayoutEffect(() => {
|
|
23
23
|
window.visualViewport?.addEventListener('resize', debouncedHandler);
|
|
@@ -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":"AAMA,OAAO,EAAE,KAAK,cAAc,EAAsB,MAAM,OAAO,CAAC;AAEhE;;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,27 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2022 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { computed, effect } from '@preact-signals/safe-react';
|
|
6
|
-
import { useRef } from '@preact-signals/safe-react/react';
|
|
7
|
-
import { type DependencyList, useEffect, useMemo } from 'react';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Like `useEffect` but also tracks signals inside of the callback.
|
|
11
|
-
*/
|
|
12
|
-
export const useSignalsEffect = (cb: () => void | (() => void), deps?: DependencyList) => {
|
|
13
|
-
const callback = useRef(cb);
|
|
14
|
-
callback.current = cb;
|
|
15
|
-
useEffect(() => {
|
|
16
|
-
return effect(() => {
|
|
17
|
-
return callback.current();
|
|
18
|
-
});
|
|
19
|
-
}, deps ?? []);
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Like `useMemo` but also tracks signals inside of the callback.
|
|
24
|
-
*/
|
|
25
|
-
export const useSignalsMemo = <T>(cb: () => T, deps?: DependencyList) => {
|
|
26
|
-
return useMemo(() => computed(cb), deps ?? []).value;
|
|
27
|
-
};
|