@dxos/react-hooks 0.6.13-main.ed424a1 → 0.6.13
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 +26 -28
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +24 -26
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/types/src/index.d.ts +1 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/useAsyncCallback.d.ts +5 -0
- package/dist/types/src/useAsyncCallback.d.ts.map +1 -0
- package/dist/types/src/useAsyncEffect.test.d.ts +1 -1
- package/dist/types/src/useAsyncEffect.test.d.ts.map +1 -1
- package/dist/types/src/useControlledValue.d.ts +1 -1
- package/dist/types/src/useControlledValue.d.ts.map +1 -1
- package/dist/types/src/useDefaultValue.d.ts +1 -1
- package/dist/types/src/useDefaultValue.d.ts.map +1 -1
- package/dist/types/src/useTransitions.d.ts +1 -1
- package/dist/types/src/useTransitions.d.ts.map +1 -1
- package/package.json +6 -8
- package/src/index.ts +1 -1
- package/src/{useAsyncState.ts → useAsyncCallback.ts} +4 -7
- package/src/useAsyncEffect.test.tsx +9 -3
- package/src/useControlledValue.ts +2 -5
- package/src/useDefaultValue.ts +2 -2
- package/src/useTransitions.ts +7 -4
- package/dist/lib/node-esm/index.mjs +0 -328
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/types/src/useAsyncState.d.ts +0 -6
- package/dist/types/src/useAsyncState.d.ts.map +0 -1
- package/dist/types/src/useMulticastObservable.test.d.ts +0 -2
- package/dist/types/src/useMulticastObservable.test.d.ts.map +0 -1
- package/src/useMulticastObservable.test.tsx +0 -23
package/dist/lib/node/meta.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/ui/primitives/react-hooks/src/
|
|
1
|
+
{"inputs":{"packages/ui/primitives/react-hooks/src/useAsyncCallback.ts":{"bytes":2022,"imports":[{"path":"react","kind":"import-statement","external":true}],"format":"esm"},"packages/ui/primitives/react-hooks/src/useAsyncEffect.ts":{"bytes":5018,"imports":[{"path":"react","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true}],"format":"esm"},"packages/ui/primitives/react-hooks/src/useControlledValue.ts":{"bytes":1974,"imports":[{"path":"react","kind":"import-statement","external":true}],"format":"esm"},"packages/ui/primitives/react-hooks/src/useDebugReactDeps.ts":{"bytes":3192,"imports":[{"path":"react","kind":"import-statement","external":true}],"format":"esm"},"packages/ui/primitives/react-hooks/src/useDefaultValue.ts":{"bytes":4104,"imports":[{"path":"react","kind":"import-statement","external":true}],"format":"esm"},"packages/ui/primitives/react-hooks/src/useDynamicRef.ts":{"bytes":1251,"imports":[{"path":"react","kind":"import-statement","external":true}],"format":"esm"},"packages/ui/primitives/react-hooks/src/useFileDownload.ts":{"bytes":2740,"imports":[{"path":"react","kind":"import-statement","external":true}],"format":"esm"},"packages/ui/primitives/react-hooks/src/useForwardedRef.ts":{"bytes":1783,"imports":[{"path":"react","kind":"import-statement","external":true}],"format":"esm"},"packages/ui/primitives/react-hooks/src/useId.ts":{"bytes":2163,"imports":[{"path":"alea","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true}],"format":"esm"},"packages/ui/primitives/react-hooks/src/useIsFocused.ts":{"bytes":3990,"imports":[{"path":"react","kind":"import-statement","external":true}],"format":"esm"},"packages/ui/primitives/react-hooks/src/useMediaQuery.ts":{"bytes":9390,"imports":[{"path":"react","kind":"import-statement","external":true}],"format":"esm"},"packages/ui/primitives/react-hooks/src/useMulticastObservable.ts":{"bytes":3033,"imports":[{"path":"react","kind":"import-statement","external":true}],"format":"esm"},"packages/ui/primitives/react-hooks/src/useRefCallback.ts":{"bytes":1879,"imports":[{"path":"react","kind":"import-statement","external":true}],"format":"esm"},"packages/ui/primitives/react-hooks/src/useTransitions.ts":{"bytes":7797,"imports":[{"path":"react","kind":"import-statement","external":true}],"format":"esm"},"packages/ui/primitives/react-hooks/src/index.ts":{"bytes":1913,"imports":[{"path":"packages/ui/primitives/react-hooks/src/useAsyncCallback.ts","kind":"import-statement","original":"./useAsyncCallback"},{"path":"packages/ui/primitives/react-hooks/src/useAsyncEffect.ts","kind":"import-statement","original":"./useAsyncEffect"},{"path":"packages/ui/primitives/react-hooks/src/useControlledValue.ts","kind":"import-statement","original":"./useControlledValue"},{"path":"packages/ui/primitives/react-hooks/src/useDebugReactDeps.ts","kind":"import-statement","original":"./useDebugReactDeps"},{"path":"packages/ui/primitives/react-hooks/src/useDefaultValue.ts","kind":"import-statement","original":"./useDefaultValue"},{"path":"packages/ui/primitives/react-hooks/src/useDynamicRef.ts","kind":"import-statement","original":"./useDynamicRef"},{"path":"packages/ui/primitives/react-hooks/src/useFileDownload.ts","kind":"import-statement","original":"./useFileDownload"},{"path":"packages/ui/primitives/react-hooks/src/useForwardedRef.ts","kind":"import-statement","original":"./useForwardedRef"},{"path":"packages/ui/primitives/react-hooks/src/useId.ts","kind":"import-statement","original":"./useId"},{"path":"packages/ui/primitives/react-hooks/src/useIsFocused.ts","kind":"import-statement","original":"./useIsFocused"},{"path":"packages/ui/primitives/react-hooks/src/useMediaQuery.ts","kind":"import-statement","original":"./useMediaQuery"},{"path":"packages/ui/primitives/react-hooks/src/useMulticastObservable.ts","kind":"import-statement","original":"./useMulticastObservable"},{"path":"packages/ui/primitives/react-hooks/src/useRefCallback.ts","kind":"import-statement","original":"./useRefCallback"},{"path":"packages/ui/primitives/react-hooks/src/useTransitions.ts","kind":"import-statement","original":"./useTransitions"}],"format":"esm"}},"outputs":{"packages/ui/primitives/react-hooks/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":24207},"packages/ui/primitives/react-hooks/dist/lib/node/index.cjs":{"imports":[{"path":"react","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"alea","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true}],"exports":["randomString","useAsyncCallback","useAsyncEffect","useControlledValue","useDebugReactDeps","useDefaultValue","useDidTransition","useDynamicRef","useFileDownload","useForwardedRef","useId","useIsFocused","useMediaQuery","useMulticastObservable","useOnTransition","useRefCallback"],"entryPoint":"packages/ui/primitives/react-hooks/src/index.ts","inputs":{"packages/ui/primitives/react-hooks/src/useAsyncCallback.ts":{"bytesInOutput":301},"packages/ui/primitives/react-hooks/src/index.ts":{"bytesInOutput":0},"packages/ui/primitives/react-hooks/src/useAsyncEffect.ts":{"bytesInOutput":596},"packages/ui/primitives/react-hooks/src/useControlledValue.ts":{"bytesInOutput":350},"packages/ui/primitives/react-hooks/src/useDebugReactDeps.ts":{"bytesInOutput":567},"packages/ui/primitives/react-hooks/src/useDefaultValue.ts":{"bytesInOutput":422},"packages/ui/primitives/react-hooks/src/useDynamicRef.ts":{"bytesInOutput":217},"packages/ui/primitives/react-hooks/src/useFileDownload.ts":{"bytesInOutput":416},"packages/ui/primitives/react-hooks/src/useForwardedRef.ts":{"bytesInOutput":343},"packages/ui/primitives/react-hooks/src/useId.ts":{"bytesInOutput":326},"packages/ui/primitives/react-hooks/src/useIsFocused.ts":{"bytesInOutput":833},"packages/ui/primitives/react-hooks/src/useMediaQuery.ts":{"bytesInOutput":1940},"packages/ui/primitives/react-hooks/src/useMulticastObservable.ts":{"bytesInOutput":368},"packages/ui/primitives/react-hooks/src/useRefCallback.ts":{"bytesInOutput":197},"packages/ui/primitives/react-hooks/src/useTransitions.ts":{"bytesInOutput":1388}},"bytes":9466}}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,SAAS,CAAC;AACxB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAsyncCallback.d.ts","sourceRoot":"","sources":["../../../src/useAsyncCallback.ts"],"names":[],"mappings":"AAMA;;GAEG;AACH,eAAO,MAAM,gBAAgB,GAAI,CAAC,MAAM,MAAM,OAAO,CAAC,CAAC,CAAC,KAAG,CAAC,GAAG,SAY9D,CAAC"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import 'raf/polyfill';
|
|
2
2
|
//# sourceMappingURL=useAsyncEffect.test.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAsyncEffect.test.d.ts","sourceRoot":"","sources":["../../../src/useAsyncEffect.test.tsx"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"useAsyncEffect.test.d.ts","sourceRoot":"","sources":["../../../src/useAsyncEffect.test.tsx"],"names":[],"mappings":"AAKA,OAAO,cAAc,CAAC"}
|
|
@@ -2,5 +2,5 @@ import { type Dispatch, type SetStateAction } from 'react';
|
|
|
2
2
|
/**
|
|
3
3
|
* A stateful hook with a controlled value.
|
|
4
4
|
*/
|
|
5
|
-
export declare const useControlledValue: <TValue>(controlledValue: TValue
|
|
5
|
+
export declare const useControlledValue: <TValue>(controlledValue: TValue) => [TValue, Dispatch<SetStateAction<TValue>>];
|
|
6
6
|
//# sourceMappingURL=useControlledValue.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useControlledValue.d.ts","sourceRoot":"","sources":["../../../src/useControlledValue.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,cAAc,EAAuB,MAAM,OAAO,CAAC;AAEhF;;GAEG;AACH,eAAO,MAAM,kBAAkB,GAAI,MAAM,
|
|
1
|
+
{"version":3,"file":"useControlledValue.d.ts","sourceRoot":"","sources":["../../../src/useControlledValue.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,cAAc,EAAuB,MAAM,OAAO,CAAC;AAEhF;;GAEG;AACH,eAAO,MAAM,kBAAkB,GAAI,MAAM,mBAAmB,MAAM,KAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CAS7G,CAAC"}
|
|
@@ -9,5 +9,5 @@
|
|
|
9
9
|
* @param defaultValue - The initial value used when the reactiveValue is undefined. This value is not reactive.
|
|
10
10
|
* @returns - The reactiveValue if it's defined, otherwise the defaultValue.
|
|
11
11
|
*/
|
|
12
|
-
export declare const useDefaultValue: <T>(reactiveValue: T | undefined | null,
|
|
12
|
+
export declare const useDefaultValue: <T>(reactiveValue: T | undefined | null, defaultValue: T) => T;
|
|
13
13
|
//# sourceMappingURL=useDefaultValue.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDefaultValue.d.ts","sourceRoot":"","sources":["../../../src/useDefaultValue.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,eAAe,GAAI,CAAC,iBAAiB,CAAC,GAAG,SAAS,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"useDefaultValue.d.ts","sourceRoot":"","sources":["../../../src/useDefaultValue.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,eAAe,GAAI,CAAC,iBAAiB,CAAC,GAAG,SAAS,GAAG,IAAI,gBAAgB,CAAC,KAAG,CAYzF,CAAC"}
|
|
@@ -3,5 +3,5 @@
|
|
|
3
3
|
*
|
|
4
4
|
* This function utilizes the `useDidTransition` hook to monitor changes in `currentValue`.
|
|
5
5
|
* When `currentValue` transitions from `fromValue` to `toValue`, the provided `callback` function is executed. */
|
|
6
|
-
export declare const useOnTransition: <T>(currentValue: T, fromValue: T | ((value: T) => boolean), toValue:
|
|
6
|
+
export declare const useOnTransition: <T>(currentValue: T, fromValue: T | ((value: T) => boolean), toValue: ((value: T) => boolean) | T, callback: () => void) => void;
|
|
7
7
|
//# sourceMappingURL=useTransitions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTransitions.d.ts","sourceRoot":"","sources":["../../../src/useTransitions.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useTransitions.d.ts","sourceRoot":"","sources":["../../../src/useTransitions.ts"],"names":[],"mappings":"AAiDA;;;;kHAIkH;AAClH,eAAO,MAAM,eAAe,GAAI,CAAC,gBACjB,CAAC,aACJ,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,CAAC,WAC7B,CAAC,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,CAAC,GAAG,CAAC,YAC1B,MAAM,IAAI,SAerB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-hooks",
|
|
3
|
-
"version": "0.6.13
|
|
3
|
+
"version": "0.6.13",
|
|
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,8 +10,7 @@
|
|
|
10
10
|
".": {
|
|
11
11
|
"browser": "./dist/lib/browser/index.mjs",
|
|
12
12
|
"node": {
|
|
13
|
-
"
|
|
14
|
-
"default": "./dist/lib/node-esm/index.mjs"
|
|
13
|
+
"default": "./dist/lib/node/index.cjs"
|
|
15
14
|
},
|
|
16
15
|
"types": "./dist/types/src/index.d.ts"
|
|
17
16
|
}
|
|
@@ -26,19 +25,18 @@
|
|
|
26
25
|
],
|
|
27
26
|
"dependencies": {
|
|
28
27
|
"alea": "^1.0.1",
|
|
29
|
-
"@dxos/async": "0.6.13
|
|
30
|
-
"@dxos/log": "0.6.13
|
|
28
|
+
"@dxos/async": "0.6.13",
|
|
29
|
+
"@dxos/log": "0.6.13"
|
|
31
30
|
},
|
|
32
31
|
"devDependencies": {
|
|
33
|
-
"@testing-library/react": "^13.4.0",
|
|
34
32
|
"@types/react": "~18.2.0",
|
|
35
33
|
"@types/react-dom": "~18.2.0",
|
|
36
34
|
"react": "~18.2.0",
|
|
37
35
|
"react-dom": "~18.2.0"
|
|
38
36
|
},
|
|
39
37
|
"peerDependencies": {
|
|
40
|
-
"react": "
|
|
41
|
-
"react-dom": "
|
|
38
|
+
"react": "^18.0.0",
|
|
39
|
+
"react-dom": "^18.0.0"
|
|
42
40
|
},
|
|
43
41
|
"publishConfig": {
|
|
44
42
|
"access": "public"
|
package/src/index.ts
CHANGED
|
@@ -2,15 +2,12 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { useEffect, useState } from 'react';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* NOTE: Use with care and when necessary to be able to cancel an async operation when unmounting.
|
|
9
9
|
*/
|
|
10
|
-
export const
|
|
11
|
-
cb: () => Promise<T | undefined>,
|
|
12
|
-
deps: any[] = [],
|
|
13
|
-
): [T | undefined, Dispatch<SetStateAction<T | undefined>>] => {
|
|
10
|
+
export const useAsyncCallback = <T>(cb: () => Promise<T>): T | undefined => {
|
|
14
11
|
const [value, setValue] = useState<T | undefined>();
|
|
15
12
|
useEffect(() => {
|
|
16
13
|
const t = setTimeout(async () => {
|
|
@@ -19,7 +16,7 @@ export const useAsyncState = <T>(
|
|
|
19
16
|
});
|
|
20
17
|
|
|
21
18
|
return () => clearTimeout(t);
|
|
22
|
-
},
|
|
19
|
+
}, []);
|
|
23
20
|
|
|
24
|
-
return
|
|
21
|
+
return value;
|
|
25
22
|
};
|
|
@@ -2,10 +2,14 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import expect from 'expect';
|
|
6
|
+
import 'raf/polyfill';
|
|
5
7
|
import React, { useState } from 'react';
|
|
6
8
|
import { createRoot } from 'react-dom/client';
|
|
7
9
|
import { act } from 'react-dom/test-utils';
|
|
8
|
-
import
|
|
10
|
+
import waitForExpect from 'wait-for-expect';
|
|
11
|
+
|
|
12
|
+
import { afterEach, beforeEach, describe, test } from '@dxos/test';
|
|
9
13
|
|
|
10
14
|
import { useAsyncEffect } from './useAsyncEffect';
|
|
11
15
|
|
|
@@ -41,11 +45,13 @@ describe('useAsyncEffect', () => {
|
|
|
41
45
|
});
|
|
42
46
|
|
|
43
47
|
test('gets async value.', async () => {
|
|
44
|
-
|
|
48
|
+
void act(() => {
|
|
45
49
|
createRoot(rootContainer).render(<Test />);
|
|
46
50
|
});
|
|
47
51
|
|
|
48
52
|
const h1 = rootContainer.querySelector('h1');
|
|
49
|
-
|
|
53
|
+
await waitForExpect(() => {
|
|
54
|
+
expect(h1?.textContent).toEqual('DXOS');
|
|
55
|
+
});
|
|
50
56
|
});
|
|
51
57
|
});
|
|
@@ -7,16 +7,13 @@ 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 useControlledValue = <TValue>(
|
|
11
|
-
controlledValue: TValue,
|
|
12
|
-
...deps: any[]
|
|
13
|
-
): [TValue, Dispatch<SetStateAction<TValue>>] => {
|
|
10
|
+
export const useControlledValue = <TValue>(controlledValue: TValue): [TValue, Dispatch<SetStateAction<TValue>>] => {
|
|
14
11
|
const [value, setValue] = useState<TValue>(controlledValue);
|
|
15
12
|
useEffect(() => {
|
|
16
13
|
if (controlledValue !== undefined) {
|
|
17
14
|
setValue(controlledValue);
|
|
18
15
|
}
|
|
19
|
-
}, [controlledValue
|
|
16
|
+
}, [controlledValue]);
|
|
20
17
|
|
|
21
18
|
return [value, setValue];
|
|
22
19
|
};
|
package/src/useDefaultValue.ts
CHANGED
|
@@ -15,11 +15,11 @@ import { useEffect, useState, useMemo } from 'react';
|
|
|
15
15
|
* @param defaultValue - The initial value used when the reactiveValue is undefined. This value is not reactive.
|
|
16
16
|
* @returns - The reactiveValue if it's defined, otherwise the defaultValue.
|
|
17
17
|
*/
|
|
18
|
-
export const useDefaultValue = <T>(reactiveValue: T | undefined | null,
|
|
18
|
+
export const useDefaultValue = <T>(reactiveValue: T | undefined | null, defaultValue: T): T => {
|
|
19
19
|
// Memoize defaultValue with an empty dependency array.
|
|
20
20
|
// This ensures that the defaultValue instance remains stable across all re-renders,
|
|
21
21
|
// regardless of whether the defaultValue changes.
|
|
22
|
-
const stableDefaultValue = useMemo(
|
|
22
|
+
const stableDefaultValue = useMemo(() => defaultValue, []);
|
|
23
23
|
const [value, setValue] = useState(reactiveValue ?? stableDefaultValue);
|
|
24
24
|
|
|
25
25
|
useEffect(() => {
|
package/src/useTransitions.ts
CHANGED
|
@@ -28,18 +28,21 @@ export const useDidTransition = <T>(
|
|
|
28
28
|
|
|
29
29
|
useEffect(() => {
|
|
30
30
|
const toValueValid = isFunction<T>(toValue) ? toValue(currentValue) : toValue === currentValue;
|
|
31
|
+
|
|
31
32
|
const fromValueValid = isFunction<T>(fromValue)
|
|
32
33
|
? fromValue(previousValue.current)
|
|
33
34
|
: fromValue === previousValue.current;
|
|
34
35
|
|
|
35
|
-
|
|
36
|
+
const transitioned = fromValueValid && toValueValid;
|
|
37
|
+
|
|
38
|
+
if (transitioned) {
|
|
36
39
|
setHasTransitioned(true);
|
|
37
|
-
} else
|
|
40
|
+
} else {
|
|
38
41
|
setHasTransitioned(false);
|
|
39
42
|
}
|
|
40
43
|
|
|
41
44
|
previousValue.current = currentValue;
|
|
42
|
-
}, [currentValue, fromValue, toValue,
|
|
45
|
+
}, [currentValue, fromValue, toValue, setHasTransitioned, previousValue]);
|
|
43
46
|
|
|
44
47
|
return hasTransitioned;
|
|
45
48
|
};
|
|
@@ -52,7 +55,7 @@ export const useDidTransition = <T>(
|
|
|
52
55
|
export const useOnTransition = <T>(
|
|
53
56
|
currentValue: T,
|
|
54
57
|
fromValue: T | ((value: T) => boolean),
|
|
55
|
-
toValue:
|
|
58
|
+
toValue: ((value: T) => boolean) | T,
|
|
56
59
|
callback: () => void,
|
|
57
60
|
) => {
|
|
58
61
|
const dirty = useRef(false);
|
|
@@ -1,328 +0,0 @@
|
|
|
1
|
-
import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
|
|
2
|
-
|
|
3
|
-
// packages/ui/primitives/react-hooks/src/useAsyncEffect.ts
|
|
4
|
-
import { useEffect } from "react";
|
|
5
|
-
import { log } from "@dxos/log";
|
|
6
|
-
var useAsyncEffect = (callback, destructor, deps) => {
|
|
7
|
-
const [effectDestructor, effectDeps] = typeof destructor === "function" ? [
|
|
8
|
-
destructor,
|
|
9
|
-
deps
|
|
10
|
-
] : [
|
|
11
|
-
void 0,
|
|
12
|
-
destructor
|
|
13
|
-
];
|
|
14
|
-
useEffect(() => {
|
|
15
|
-
let mounted = true;
|
|
16
|
-
let value;
|
|
17
|
-
const asyncResult = callback(() => mounted);
|
|
18
|
-
void Promise.resolve(asyncResult).then((result) => {
|
|
19
|
-
value = result;
|
|
20
|
-
}).catch(log.catch);
|
|
21
|
-
return () => {
|
|
22
|
-
mounted = false;
|
|
23
|
-
effectDestructor?.(value);
|
|
24
|
-
};
|
|
25
|
-
}, effectDeps);
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
// packages/ui/primitives/react-hooks/src/useAsyncState.ts
|
|
29
|
-
import { useEffect as useEffect2, useState } from "react";
|
|
30
|
-
var useAsyncState = (cb, deps = []) => {
|
|
31
|
-
const [value, setValue] = useState();
|
|
32
|
-
useEffect2(() => {
|
|
33
|
-
const t = setTimeout(async () => {
|
|
34
|
-
const data = await cb();
|
|
35
|
-
setValue(data);
|
|
36
|
-
});
|
|
37
|
-
return () => clearTimeout(t);
|
|
38
|
-
}, deps);
|
|
39
|
-
return [
|
|
40
|
-
value,
|
|
41
|
-
setValue
|
|
42
|
-
];
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
// packages/ui/primitives/react-hooks/src/useControlledValue.ts
|
|
46
|
-
import { useEffect as useEffect3, useState as useState2 } from "react";
|
|
47
|
-
var useControlledValue = (controlledValue, ...deps) => {
|
|
48
|
-
const [value, setValue] = useState2(controlledValue);
|
|
49
|
-
useEffect3(() => {
|
|
50
|
-
if (controlledValue !== void 0) {
|
|
51
|
-
setValue(controlledValue);
|
|
52
|
-
}
|
|
53
|
-
}, [
|
|
54
|
-
controlledValue,
|
|
55
|
-
...deps
|
|
56
|
-
]);
|
|
57
|
-
return [
|
|
58
|
-
value,
|
|
59
|
-
setValue
|
|
60
|
-
];
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
// packages/ui/primitives/react-hooks/src/useDebugReactDeps.ts
|
|
64
|
-
import { useEffect as useEffect4, useRef } from "react";
|
|
65
|
-
var useDebugReactDeps = (deps = []) => {
|
|
66
|
-
const lastDeps = useRef([]);
|
|
67
|
-
useEffect4(() => {
|
|
68
|
-
console.group("deps changed", {
|
|
69
|
-
old: lastDeps.current.length,
|
|
70
|
-
new: deps.length
|
|
71
|
-
});
|
|
72
|
-
for (let i = 0; i < Math.max(lastDeps.current.length ?? 0, deps.length ?? 0); i++) {
|
|
73
|
-
console.log(i, lastDeps.current[i] === deps[i] ? "SAME" : "CHANGED", {
|
|
74
|
-
previous: lastDeps.current[i],
|
|
75
|
-
current: deps[i]
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
console.groupEnd();
|
|
79
|
-
lastDeps.current = deps;
|
|
80
|
-
}, deps);
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
// packages/ui/primitives/react-hooks/src/useDefaultValue.ts
|
|
84
|
-
import { useEffect as useEffect5, useState as useState3, useMemo } from "react";
|
|
85
|
-
var useDefaultValue = (reactiveValue, getDefaultValue) => {
|
|
86
|
-
const stableDefaultValue = useMemo(getDefaultValue, []);
|
|
87
|
-
const [value, setValue] = useState3(reactiveValue ?? stableDefaultValue);
|
|
88
|
-
useEffect5(() => {
|
|
89
|
-
setValue(reactiveValue ?? stableDefaultValue);
|
|
90
|
-
}, [
|
|
91
|
-
reactiveValue,
|
|
92
|
-
stableDefaultValue
|
|
93
|
-
]);
|
|
94
|
-
return value;
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
// packages/ui/primitives/react-hooks/src/useDynamicRef.ts
|
|
98
|
-
import { useEffect as useEffect6, useRef as useRef2 } from "react";
|
|
99
|
-
var useDynamicRef = (value) => {
|
|
100
|
-
const ref = useRef2(value);
|
|
101
|
-
useEffect6(() => {
|
|
102
|
-
ref.current = value;
|
|
103
|
-
}, [
|
|
104
|
-
value
|
|
105
|
-
]);
|
|
106
|
-
return ref;
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
// packages/ui/primitives/react-hooks/src/useFileDownload.ts
|
|
110
|
-
import { useMemo as useMemo2 } from "react";
|
|
111
|
-
var useFileDownload = () => {
|
|
112
|
-
return useMemo2(() => (data, filename) => {
|
|
113
|
-
const url = typeof data === "string" ? data : URL.createObjectURL(data);
|
|
114
|
-
const element = document.createElement("a");
|
|
115
|
-
element.setAttribute("href", url);
|
|
116
|
-
element.setAttribute("download", filename);
|
|
117
|
-
element.setAttribute("target", "download");
|
|
118
|
-
element.click();
|
|
119
|
-
}, []);
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
// packages/ui/primitives/react-hooks/src/useForwardedRef.ts
|
|
123
|
-
import { useRef as useRef3, useEffect as useEffect7 } from "react";
|
|
124
|
-
var useForwardedRef = (ref) => {
|
|
125
|
-
const innerRef = useRef3(null);
|
|
126
|
-
useEffect7(() => {
|
|
127
|
-
if (!ref) {
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
if (typeof ref === "function") {
|
|
131
|
-
ref(innerRef.current);
|
|
132
|
-
} else {
|
|
133
|
-
ref.current = innerRef.current;
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
return innerRef;
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
// packages/ui/primitives/react-hooks/src/useId.ts
|
|
140
|
-
import alea from "alea";
|
|
141
|
-
import { useMemo as useMemo3 } from "react";
|
|
142
|
-
var Alea = alea;
|
|
143
|
-
var prng = new Alea("@dxos/react-hooks");
|
|
144
|
-
var randomString = (n = 4) => prng().toString(16).slice(2, n + 2);
|
|
145
|
-
var useId = (namespace, propsId, opts) => useMemo3(() => propsId ?? `${namespace}-${randomString(opts?.n ?? 4)}`, [
|
|
146
|
-
propsId
|
|
147
|
-
]);
|
|
148
|
-
|
|
149
|
-
// packages/ui/primitives/react-hooks/src/useIsFocused.ts
|
|
150
|
-
import { useEffect as useEffect8, useRef as useRef4, useState as useState4 } from "react";
|
|
151
|
-
var useIsFocused = (inputRef) => {
|
|
152
|
-
const [isFocused, setIsFocused] = useState4(void 0);
|
|
153
|
-
const isFocusedRef = useRef4(isFocused);
|
|
154
|
-
isFocusedRef.current = isFocused;
|
|
155
|
-
useEffect8(() => {
|
|
156
|
-
const input = inputRef.current;
|
|
157
|
-
if (!input) {
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
const onFocus = () => setIsFocused(true);
|
|
161
|
-
const onBlur = () => setIsFocused(false);
|
|
162
|
-
input.addEventListener("focus", onFocus);
|
|
163
|
-
input.addEventListener("blur", onBlur);
|
|
164
|
-
if (isFocusedRef.current === void 0) {
|
|
165
|
-
setIsFocused(document.activeElement === input);
|
|
166
|
-
}
|
|
167
|
-
return () => {
|
|
168
|
-
input.removeEventListener("focus", onFocus);
|
|
169
|
-
input.removeEventListener("blur", onBlur);
|
|
170
|
-
};
|
|
171
|
-
}, [
|
|
172
|
-
inputRef,
|
|
173
|
-
setIsFocused
|
|
174
|
-
]);
|
|
175
|
-
return isFocused;
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
// packages/ui/primitives/react-hooks/src/useMediaQuery.ts
|
|
179
|
-
import { useEffect as useEffect9, useState as useState5 } from "react";
|
|
180
|
-
var breakpointMediaQueries = {
|
|
181
|
-
sm: "(min-width: 640px)",
|
|
182
|
-
md: "(min-width: 768px)",
|
|
183
|
-
lg: "(min-width: 1024px)",
|
|
184
|
-
xl: "(min-width: 1280px)",
|
|
185
|
-
"2xl": "(min-width: 1536px)"
|
|
186
|
-
};
|
|
187
|
-
var useMediaQuery = (query, options = {}) => {
|
|
188
|
-
const { ssr = true, fallback } = options;
|
|
189
|
-
const queries = (Array.isArray(query) ? query : [
|
|
190
|
-
query
|
|
191
|
-
]).map((query2) => query2 in breakpointMediaQueries ? breakpointMediaQueries[query2] : query2);
|
|
192
|
-
let fallbackValues = Array.isArray(fallback) ? fallback : [
|
|
193
|
-
fallback
|
|
194
|
-
];
|
|
195
|
-
fallbackValues = fallbackValues.filter((v) => v != null);
|
|
196
|
-
const [value, setValue] = useState5(() => {
|
|
197
|
-
return queries.map((query2, index) => ({
|
|
198
|
-
media: query2,
|
|
199
|
-
matches: ssr ? !!fallbackValues[index] : document.defaultView?.matchMedia(query2).matches
|
|
200
|
-
}));
|
|
201
|
-
});
|
|
202
|
-
useEffect9(() => {
|
|
203
|
-
setValue(queries.map((query2) => ({
|
|
204
|
-
media: query2,
|
|
205
|
-
matches: document.defaultView?.matchMedia(query2).matches
|
|
206
|
-
})));
|
|
207
|
-
const mql = queries.map((query2) => document.defaultView?.matchMedia(query2));
|
|
208
|
-
const handler = (evt) => {
|
|
209
|
-
setValue((prev) => {
|
|
210
|
-
return prev.slice().map((item) => {
|
|
211
|
-
if (item.media === evt.media) {
|
|
212
|
-
return {
|
|
213
|
-
...item,
|
|
214
|
-
matches: evt.matches
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
return item;
|
|
218
|
-
});
|
|
219
|
-
});
|
|
220
|
-
};
|
|
221
|
-
mql.forEach((mql2) => {
|
|
222
|
-
if (typeof mql2?.addListener === "function") {
|
|
223
|
-
mql2?.addListener(handler);
|
|
224
|
-
} else {
|
|
225
|
-
mql2?.addEventListener("change", handler);
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
return () => {
|
|
229
|
-
mql.forEach((mql2) => {
|
|
230
|
-
if (typeof mql2?.removeListener === "function") {
|
|
231
|
-
mql2?.removeListener(handler);
|
|
232
|
-
} else {
|
|
233
|
-
mql2?.removeEventListener("change", handler);
|
|
234
|
-
}
|
|
235
|
-
});
|
|
236
|
-
};
|
|
237
|
-
}, [
|
|
238
|
-
document.defaultView
|
|
239
|
-
]);
|
|
240
|
-
return value.map((item) => !!item.matches);
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
// packages/ui/primitives/react-hooks/src/useMulticastObservable.ts
|
|
244
|
-
import { useMemo as useMemo4, useSyncExternalStore } from "react";
|
|
245
|
-
var useMulticastObservable = (observable) => {
|
|
246
|
-
const subscribeFn = useMemo4(() => (listener) => {
|
|
247
|
-
const subscription = observable.subscribe(listener);
|
|
248
|
-
return () => subscription.unsubscribe();
|
|
249
|
-
}, [
|
|
250
|
-
observable
|
|
251
|
-
]);
|
|
252
|
-
return useSyncExternalStore(subscribeFn, () => observable.get());
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
// packages/ui/primitives/react-hooks/src/useRefCallback.ts
|
|
256
|
-
import { useState as useState6 } from "react";
|
|
257
|
-
var useRefCallback = () => {
|
|
258
|
-
const [value, setValue] = useState6(null);
|
|
259
|
-
return {
|
|
260
|
-
refCallback: (value2) => setValue(value2),
|
|
261
|
-
value
|
|
262
|
-
};
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
// packages/ui/primitives/react-hooks/src/useTransitions.ts
|
|
266
|
-
import { useRef as useRef5, useEffect as useEffect10, useState as useState7 } from "react";
|
|
267
|
-
var isFunction = (functionToCheck) => {
|
|
268
|
-
return functionToCheck instanceof Function;
|
|
269
|
-
};
|
|
270
|
-
var useDidTransition = (currentValue, fromValue, toValue) => {
|
|
271
|
-
const [hasTransitioned, setHasTransitioned] = useState7(false);
|
|
272
|
-
const previousValue = useRef5(currentValue);
|
|
273
|
-
useEffect10(() => {
|
|
274
|
-
const toValueValid = isFunction(toValue) ? toValue(currentValue) : toValue === currentValue;
|
|
275
|
-
const fromValueValid = isFunction(fromValue) ? fromValue(previousValue.current) : fromValue === previousValue.current;
|
|
276
|
-
if (fromValueValid && toValueValid && !hasTransitioned) {
|
|
277
|
-
setHasTransitioned(true);
|
|
278
|
-
} else if ((!fromValueValid || !toValueValid) && hasTransitioned) {
|
|
279
|
-
setHasTransitioned(false);
|
|
280
|
-
}
|
|
281
|
-
previousValue.current = currentValue;
|
|
282
|
-
}, [
|
|
283
|
-
currentValue,
|
|
284
|
-
fromValue,
|
|
285
|
-
toValue,
|
|
286
|
-
hasTransitioned
|
|
287
|
-
]);
|
|
288
|
-
return hasTransitioned;
|
|
289
|
-
};
|
|
290
|
-
var useOnTransition = (currentValue, fromValue, toValue, callback) => {
|
|
291
|
-
const dirty = useRef5(false);
|
|
292
|
-
const hasTransitioned = useDidTransition(currentValue, fromValue, toValue);
|
|
293
|
-
useEffect10(() => {
|
|
294
|
-
dirty.current = false;
|
|
295
|
-
}, [
|
|
296
|
-
currentValue,
|
|
297
|
-
dirty
|
|
298
|
-
]);
|
|
299
|
-
useEffect10(() => {
|
|
300
|
-
if (hasTransitioned && !dirty.current) {
|
|
301
|
-
callback();
|
|
302
|
-
dirty.current = true;
|
|
303
|
-
}
|
|
304
|
-
}, [
|
|
305
|
-
hasTransitioned,
|
|
306
|
-
dirty,
|
|
307
|
-
callback
|
|
308
|
-
]);
|
|
309
|
-
};
|
|
310
|
-
export {
|
|
311
|
-
randomString,
|
|
312
|
-
useAsyncEffect,
|
|
313
|
-
useAsyncState,
|
|
314
|
-
useControlledValue,
|
|
315
|
-
useDebugReactDeps,
|
|
316
|
-
useDefaultValue,
|
|
317
|
-
useDidTransition,
|
|
318
|
-
useDynamicRef,
|
|
319
|
-
useFileDownload,
|
|
320
|
-
useForwardedRef,
|
|
321
|
-
useId,
|
|
322
|
-
useIsFocused,
|
|
323
|
-
useMediaQuery,
|
|
324
|
-
useMulticastObservable,
|
|
325
|
-
useOnTransition,
|
|
326
|
-
useRefCallback
|
|
327
|
-
};
|
|
328
|
-
//# sourceMappingURL=index.mjs.map
|