@fringeworks/react-utils 0.8.0
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/LICENSE +21 -0
- package/README.md +3 -0
- package/_helpers/_traverseContent.cjs +24 -0
- package/_helpers/_traverseContent.d.cts +15 -0
- package/_helpers/_traverseContent.d.mts +16 -0
- package/_helpers/_traverseContent.mjs +22 -0
- package/_helpers/_traverseElement.cjs +23 -0
- package/_helpers/_traverseElement.d.cts +15 -0
- package/_helpers/_traverseElement.d.mts +16 -0
- package/_helpers/_traverseElement.mjs +21 -0
- package/_virtual/_rolldown/runtime.cjs +43 -0
- package/_virtual/_rolldown/runtime.mjs +18 -0
- package/hooks/index.cjs +42 -0
- package/hooks/index.d.cts +14 -0
- package/hooks/index.d.mts +14 -0
- package/hooks/index.mjs +29 -0
- package/hooks/useCombinedRefs/index.cjs +3 -0
- package/hooks/useCombinedRefs/index.d.cts +2 -0
- package/hooks/useCombinedRefs/index.d.mts +2 -0
- package/hooks/useCombinedRefs/index.mjs +3 -0
- package/hooks/useCombinedRefs/useCombinedRefs.cjs +20 -0
- package/hooks/useCombinedRefs/useCombinedRefs.d.cts +10 -0
- package/hooks/useCombinedRefs/useCombinedRefs.d.mts +11 -0
- package/hooks/useCombinedRefs/useCombinedRefs.mjs +20 -0
- package/hooks/useConstant/index.cjs +3 -0
- package/hooks/useConstant/index.d.cts +2 -0
- package/hooks/useConstant/index.d.mts +2 -0
- package/hooks/useConstant/index.mjs +3 -0
- package/hooks/useConstant/useConstant.cjs +19 -0
- package/hooks/useConstant/useConstant.d.cts +12 -0
- package/hooks/useConstant/useConstant.d.mts +13 -0
- package/hooks/useConstant/useConstant.mjs +19 -0
- package/hooks/useExecutionAt/index.cjs +3 -0
- package/hooks/useExecutionAt/index.d.cts +3 -0
- package/hooks/useExecutionAt/index.d.mts +3 -0
- package/hooks/useExecutionAt/index.mjs +3 -0
- package/hooks/useExecutionAt/types.cjs +0 -0
- package/hooks/useExecutionAt/types.d.cts +35 -0
- package/hooks/useExecutionAt/types.d.mts +35 -0
- package/hooks/useExecutionAt/types.mjs +1 -0
- package/hooks/useExecutionAt/useExecutionAt.cjs +38 -0
- package/hooks/useExecutionAt/useExecutionAt.d.cts +24 -0
- package/hooks/useExecutionAt/useExecutionAt.d.mts +25 -0
- package/hooks/useExecutionAt/useExecutionAt.mjs +38 -0
- package/hooks/useIsMounted/index.cjs +3 -0
- package/hooks/useIsMounted/index.d.cts +2 -0
- package/hooks/useIsMounted/index.d.mts +2 -0
- package/hooks/useIsMounted/index.mjs +3 -0
- package/hooks/useIsMounted/useIsMounted.cjs +21 -0
- package/hooks/useIsMounted/useIsMounted.d.cts +7 -0
- package/hooks/useIsMounted/useIsMounted.d.mts +8 -0
- package/hooks/useIsMounted/useIsMounted.mjs +21 -0
- package/hooks/useLatestRef/index.cjs +3 -0
- package/hooks/useLatestRef/index.d.cts +3 -0
- package/hooks/useLatestRef/index.d.mts +3 -0
- package/hooks/useLatestRef/index.mjs +3 -0
- package/hooks/useLatestRef/types.cjs +0 -0
- package/hooks/useLatestRef/types.d.cts +12 -0
- package/hooks/useLatestRef/types.d.mts +12 -0
- package/hooks/useLatestRef/types.mjs +1 -0
- package/hooks/useLatestRef/useLatestRef.cjs +23 -0
- package/hooks/useLatestRef/useLatestRef.d.cts +15 -0
- package/hooks/useLatestRef/useLatestRef.d.mts +16 -0
- package/hooks/useLatestRef/useLatestRef.mjs +23 -0
- package/hooks/usePreviousValue/index.cjs +3 -0
- package/hooks/usePreviousValue/index.d.cts +3 -0
- package/hooks/usePreviousValue/index.d.mts +3 -0
- package/hooks/usePreviousValue/index.mjs +3 -0
- package/hooks/usePreviousValue/types.cjs +0 -0
- package/hooks/usePreviousValue/types.d.cts +18 -0
- package/hooks/usePreviousValue/types.d.mts +18 -0
- package/hooks/usePreviousValue/types.mjs +1 -0
- package/hooks/usePreviousValue/usePreviousValue.cjs +23 -0
- package/hooks/usePreviousValue/usePreviousValue.d.cts +11 -0
- package/hooks/usePreviousValue/usePreviousValue.d.mts +12 -0
- package/hooks/usePreviousValue/usePreviousValue.mjs +23 -0
- package/hooks/useStableLength/index.cjs +3 -0
- package/hooks/useStableLength/index.d.cts +2 -0
- package/hooks/useStableLength/index.d.mts +2 -0
- package/hooks/useStableLength/index.mjs +3 -0
- package/hooks/useStableLength/useStableLength.cjs +16 -0
- package/hooks/useStableLength/useStableLength.d.cts +9 -0
- package/hooks/useStableLength/useStableLength.d.mts +10 -0
- package/hooks/useStableLength/useStableLength.mjs +16 -0
- package/index.cjs +16 -0
- package/index.d.cts +4 -0
- package/index.d.mts +4 -0
- package/index.mjs +4 -0
- package/package.json +91 -0
- package/types.cjs +0 -0
- package/types.d.cts +11 -0
- package/types.d.mts +11 -0
- package/types.mjs +1 -0
- package/utils/applyDefaultProps/applyDefaultProps.cjs +41 -0
- package/utils/applyDefaultProps/applyDefaultProps.d.cts +33 -0
- package/utils/applyDefaultProps/applyDefaultProps.d.mts +34 -0
- package/utils/applyDefaultProps/applyDefaultProps.mjs +40 -0
- package/utils/applyDefaultProps/index.cjs +3 -0
- package/utils/applyDefaultProps/index.d.cts +3 -0
- package/utils/applyDefaultProps/index.d.mts +3 -0
- package/utils/applyDefaultProps/index.mjs +3 -0
- package/utils/applyDefaultProps/types.cjs +0 -0
- package/utils/applyDefaultProps/types.d.cts +27 -0
- package/utils/applyDefaultProps/types.d.mts +27 -0
- package/utils/applyDefaultProps/types.mjs +1 -0
- package/utils/ensureComponent/ensureComponent.cjs +28 -0
- package/utils/ensureComponent/ensureComponent.d.cts +10 -0
- package/utils/ensureComponent/ensureComponent.d.mts +11 -0
- package/utils/ensureComponent/ensureComponent.mjs +28 -0
- package/utils/ensureComponent/index.cjs +3 -0
- package/utils/ensureComponent/index.d.cts +2 -0
- package/utils/ensureComponent/index.d.mts +2 -0
- package/utils/ensureComponent/index.mjs +3 -0
- package/utils/extendElement/extendElement.cjs +30 -0
- package/utils/extendElement/extendElement.d.cts +12 -0
- package/utils/extendElement/extendElement.d.mts +13 -0
- package/utils/extendElement/extendElement.mjs +30 -0
- package/utils/extendElement/index.cjs +3 -0
- package/utils/extendElement/index.d.cts +2 -0
- package/utils/extendElement/index.d.mts +2 -0
- package/utils/extendElement/index.mjs +3 -0
- package/utils/forEachContent/forEachContent.cjs +14 -0
- package/utils/forEachContent/forEachContent.d.cts +10 -0
- package/utils/forEachContent/forEachContent.d.mts +11 -0
- package/utils/forEachContent/forEachContent.mjs +14 -0
- package/utils/forEachContent/index.cjs +3 -0
- package/utils/forEachContent/index.d.cts +2 -0
- package/utils/forEachContent/index.d.mts +2 -0
- package/utils/forEachContent/index.mjs +3 -0
- package/utils/forEachElement/forEachElement.cjs +14 -0
- package/utils/forEachElement/forEachElement.d.cts +10 -0
- package/utils/forEachElement/forEachElement.d.mts +11 -0
- package/utils/forEachElement/forEachElement.mjs +14 -0
- package/utils/forEachElement/index.cjs +3 -0
- package/utils/forEachElement/index.d.cts +2 -0
- package/utils/forEachElement/index.d.mts +2 -0
- package/utils/forEachElement/index.mjs +3 -0
- package/utils/index.cjs +47 -0
- package/utils/index.d.cts +14 -0
- package/utils/index.d.mts +14 -0
- package/utils/index.mjs +32 -0
- package/utils/markAt/index.cjs +3 -0
- package/utils/markAt/index.d.cts +3 -0
- package/utils/markAt/index.d.mts +3 -0
- package/utils/markAt/index.mjs +3 -0
- package/utils/markAt/markAt.cjs +14 -0
- package/utils/markAt/markAt.d.cts +9 -0
- package/utils/markAt/markAt.d.mts +10 -0
- package/utils/markAt/markAt.mjs +13 -0
- package/utils/markAt/types.cjs +0 -0
- package/utils/markAt/types.d.cts +4 -0
- package/utils/markAt/types.d.mts +4 -0
- package/utils/markAt/types.mjs +1 -0
- package/utils/setRefCurrent/index.cjs +3 -0
- package/utils/setRefCurrent/index.d.cts +2 -0
- package/utils/setRefCurrent/index.d.mts +2 -0
- package/utils/setRefCurrent/index.mjs +3 -0
- package/utils/setRefCurrent/setRefCurrent.cjs +29 -0
- package/utils/setRefCurrent/setRefCurrent.d.cts +11 -0
- package/utils/setRefCurrent/setRefCurrent.d.mts +12 -0
- package/utils/setRefCurrent/setRefCurrent.mjs +28 -0
- package/utils/transformContent/index.cjs +3 -0
- package/utils/transformContent/index.d.cts +2 -0
- package/utils/transformContent/index.d.mts +2 -0
- package/utils/transformContent/index.mjs +3 -0
- package/utils/transformContent/transformContent.cjs +16 -0
- package/utils/transformContent/transformContent.d.cts +10 -0
- package/utils/transformContent/transformContent.d.mts +11 -0
- package/utils/transformContent/transformContent.mjs +16 -0
- package/utils/transformElement/index.cjs +3 -0
- package/utils/transformElement/index.d.cts +2 -0
- package/utils/transformElement/index.d.mts +2 -0
- package/utils/transformElement/index.mjs +3 -0
- package/utils/transformElement/transformElement.cjs +16 -0
- package/utils/transformElement/transformElement.d.cts +10 -0
- package/utils/transformElement/transformElement.d.mts +11 -0
- package/utils/transformElement/transformElement.mjs +16 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const require_utils_markAt_markAt = require('../../utils/markAt/markAt.cjs');
|
|
2
|
+
let react = require("react");
|
|
3
|
+
|
|
4
|
+
//#region src/hooks/useExecutionAt/useExecutionAt.ts
|
|
5
|
+
/**
|
|
6
|
+
* Execution Requestプロパティ(xxxxxAt)へ渡す値と、\
|
|
7
|
+
* 処理実行用のトリガー関数を作るフック。\
|
|
8
|
+
* 命令的コンポーネントをラップしたreactコンポーネントで、\
|
|
9
|
+
* プロパティを通じて命令的コンポーネントのメソッドを実行する仕組みを提供する。
|
|
10
|
+
*
|
|
11
|
+
* @template P 実行時にExecution Requestプロパティに渡すペイロードの型
|
|
12
|
+
* @param options オプション
|
|
13
|
+
* @returns [request, execute]
|
|
14
|
+
* - `request`: 現在のリクエスト
|
|
15
|
+
* - `execute`: 新しいリクエストを発行する関数
|
|
16
|
+
* @example
|
|
17
|
+
* // 1. Hookの宣言
|
|
18
|
+
* const [undoAt, undo] = useExecutionAt<string>();
|
|
19
|
+
* // 2. 実行のトリガーになるコンポーネント
|
|
20
|
+
* <button onClick={() => undo('no-123')}>Undo</button>
|
|
21
|
+
* // 3. 処理を実行するコンポーネント
|
|
22
|
+
* <WrappedImperativeComponent undoAt={undoAt} />
|
|
23
|
+
*/
|
|
24
|
+
function useExecutionAt(options = {}) {
|
|
25
|
+
const [executionRequest, setExecutionRequest] = (0, react.useState)({
|
|
26
|
+
at: 0,
|
|
27
|
+
payload: options.initialPayload
|
|
28
|
+
});
|
|
29
|
+
return [executionRequest, (0, react.useCallback)((payload) => {
|
|
30
|
+
setExecutionRequest({
|
|
31
|
+
at: require_utils_markAt_markAt(),
|
|
32
|
+
payload
|
|
33
|
+
});
|
|
34
|
+
}, [])];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
//#endregion
|
|
38
|
+
module.exports = useExecutionAt;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { UseExecutionAtOptions, UseExecutionAtResult } from "./types.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/useExecutionAt/useExecutionAt.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Execution Requestプロパティ(xxxxxAt)へ渡す値と、\
|
|
6
|
+
* 処理実行用のトリガー関数を作るフック。\
|
|
7
|
+
* 命令的コンポーネントをラップしたreactコンポーネントで、\
|
|
8
|
+
* プロパティを通じて命令的コンポーネントのメソッドを実行する仕組みを提供する。
|
|
9
|
+
*
|
|
10
|
+
* @template P 実行時にExecution Requestプロパティに渡すペイロードの型
|
|
11
|
+
* @param options オプション
|
|
12
|
+
* @returns [request, execute]
|
|
13
|
+
* - `request`: 現在のリクエスト
|
|
14
|
+
* - `execute`: 新しいリクエストを発行する関数
|
|
15
|
+
* @example
|
|
16
|
+
* // 1. Hookの宣言
|
|
17
|
+
* const [undoAt, undo] = useExecutionAt<string>();
|
|
18
|
+
* // 2. 実行のトリガーになるコンポーネント
|
|
19
|
+
* <button onClick={() => undo('no-123')}>Undo</button>
|
|
20
|
+
* // 3. 処理を実行するコンポーネント
|
|
21
|
+
* <WrappedImperativeComponent undoAt={undoAt} />
|
|
22
|
+
*/
|
|
23
|
+
declare function useExecutionAt<P = void>(options?: UseExecutionAtOptions<P>): UseExecutionAtResult<P>;
|
|
24
|
+
export = useExecutionAt;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { UseExecutionAtOptions, UseExecutionAtResult } from "./types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/useExecutionAt/useExecutionAt.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Execution Requestプロパティ(xxxxxAt)へ渡す値と、\
|
|
6
|
+
* 処理実行用のトリガー関数を作るフック。\
|
|
7
|
+
* 命令的コンポーネントをラップしたreactコンポーネントで、\
|
|
8
|
+
* プロパティを通じて命令的コンポーネントのメソッドを実行する仕組みを提供する。
|
|
9
|
+
*
|
|
10
|
+
* @template P 実行時にExecution Requestプロパティに渡すペイロードの型
|
|
11
|
+
* @param options オプション
|
|
12
|
+
* @returns [request, execute]
|
|
13
|
+
* - `request`: 現在のリクエスト
|
|
14
|
+
* - `execute`: 新しいリクエストを発行する関数
|
|
15
|
+
* @example
|
|
16
|
+
* // 1. Hookの宣言
|
|
17
|
+
* const [undoAt, undo] = useExecutionAt<string>();
|
|
18
|
+
* // 2. 実行のトリガーになるコンポーネント
|
|
19
|
+
* <button onClick={() => undo('no-123')}>Undo</button>
|
|
20
|
+
* // 3. 処理を実行するコンポーネント
|
|
21
|
+
* <WrappedImperativeComponent undoAt={undoAt} />
|
|
22
|
+
*/
|
|
23
|
+
declare function useExecutionAt<P = void>(options?: UseExecutionAtOptions<P>): UseExecutionAtResult<P>;
|
|
24
|
+
//#endregion
|
|
25
|
+
export { useExecutionAt as default };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import markAt from "../../utils/markAt/markAt.mjs";
|
|
2
|
+
import { useCallback, useState } from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/hooks/useExecutionAt/useExecutionAt.ts
|
|
5
|
+
/**
|
|
6
|
+
* Execution Requestプロパティ(xxxxxAt)へ渡す値と、\
|
|
7
|
+
* 処理実行用のトリガー関数を作るフック。\
|
|
8
|
+
* 命令的コンポーネントをラップしたreactコンポーネントで、\
|
|
9
|
+
* プロパティを通じて命令的コンポーネントのメソッドを実行する仕組みを提供する。
|
|
10
|
+
*
|
|
11
|
+
* @template P 実行時にExecution Requestプロパティに渡すペイロードの型
|
|
12
|
+
* @param options オプション
|
|
13
|
+
* @returns [request, execute]
|
|
14
|
+
* - `request`: 現在のリクエスト
|
|
15
|
+
* - `execute`: 新しいリクエストを発行する関数
|
|
16
|
+
* @example
|
|
17
|
+
* // 1. Hookの宣言
|
|
18
|
+
* const [undoAt, undo] = useExecutionAt<string>();
|
|
19
|
+
* // 2. 実行のトリガーになるコンポーネント
|
|
20
|
+
* <button onClick={() => undo('no-123')}>Undo</button>
|
|
21
|
+
* // 3. 処理を実行するコンポーネント
|
|
22
|
+
* <WrappedImperativeComponent undoAt={undoAt} />
|
|
23
|
+
*/
|
|
24
|
+
function useExecutionAt(options = {}) {
|
|
25
|
+
const [executionRequest, setExecutionRequest] = useState({
|
|
26
|
+
at: 0,
|
|
27
|
+
payload: options.initialPayload
|
|
28
|
+
});
|
|
29
|
+
return [executionRequest, useCallback((payload) => {
|
|
30
|
+
setExecutionRequest({
|
|
31
|
+
at: markAt(),
|
|
32
|
+
payload
|
|
33
|
+
});
|
|
34
|
+
}, [])];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
//#endregion
|
|
38
|
+
export { useExecutionAt as default };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/useIsMounted/useIsMounted.ts
|
|
4
|
+
/**
|
|
5
|
+
* マウントされているかどうかを判定する関数を返すフック
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
function useIsMounted() {
|
|
9
|
+
const isMountedRef = (0, react.useRef)(false);
|
|
10
|
+
const isMounted = (0, react.useCallback)(() => isMountedRef.current, []);
|
|
11
|
+
(0, react.useEffect)(() => {
|
|
12
|
+
isMountedRef.current = true;
|
|
13
|
+
return () => {
|
|
14
|
+
isMountedRef.current = false;
|
|
15
|
+
};
|
|
16
|
+
}, []);
|
|
17
|
+
return isMounted;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
module.exports = useIsMounted;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/useIsMounted/useIsMounted.ts
|
|
4
|
+
/**
|
|
5
|
+
* マウントされているかどうかを判定する関数を返すフック
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
function useIsMounted() {
|
|
9
|
+
const isMountedRef = useRef(false);
|
|
10
|
+
const isMounted = useCallback(() => isMountedRef.current, []);
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
isMountedRef.current = true;
|
|
13
|
+
return () => {
|
|
14
|
+
isMountedRef.current = false;
|
|
15
|
+
};
|
|
16
|
+
}, []);
|
|
17
|
+
return isMounted;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
export { useIsMounted as default };
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/useLatestRef/useLatestRef.ts
|
|
4
|
+
/**
|
|
5
|
+
* 最新の値を保持する参照を返すhook
|
|
6
|
+
*
|
|
7
|
+
* 返却する参照は常に同じオブジェクトのため、依存配列に含めずに
|
|
8
|
+
* イベントハンドラーやエフェクトの中から最新の値を参照できる。
|
|
9
|
+
* 値はコミット時に更新されるため、レンダリング中には参照しないこと。
|
|
10
|
+
*
|
|
11
|
+
* @param value 値
|
|
12
|
+
* @returns 最新の値を保持する参照
|
|
13
|
+
*/
|
|
14
|
+
function useLatestRef(value) {
|
|
15
|
+
const ref = (0, react.useRef)(value);
|
|
16
|
+
(0, react.useInsertionEffect)(() => {
|
|
17
|
+
ref.current = value;
|
|
18
|
+
});
|
|
19
|
+
return ref;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
module.exports = useLatestRef;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { LatestRef } from "./types.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/useLatestRef/useLatestRef.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* 最新の値を保持する参照を返すhook
|
|
6
|
+
*
|
|
7
|
+
* 返却する参照は常に同じオブジェクトのため、依存配列に含めずに
|
|
8
|
+
* イベントハンドラーやエフェクトの中から最新の値を参照できる。
|
|
9
|
+
* 値はコミット時に更新されるため、レンダリング中には参照しないこと。
|
|
10
|
+
*
|
|
11
|
+
* @param value 値
|
|
12
|
+
* @returns 最新の値を保持する参照
|
|
13
|
+
*/
|
|
14
|
+
declare function useLatestRef<T>(value: T): LatestRef<T>;
|
|
15
|
+
export = useLatestRef;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { LatestRef } from "./types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/useLatestRef/useLatestRef.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* 最新の値を保持する参照を返すhook
|
|
6
|
+
*
|
|
7
|
+
* 返却する参照は常に同じオブジェクトのため、依存配列に含めずに
|
|
8
|
+
* イベントハンドラーやエフェクトの中から最新の値を参照できる。
|
|
9
|
+
* 値はコミット時に更新されるため、レンダリング中には参照しないこと。
|
|
10
|
+
*
|
|
11
|
+
* @param value 値
|
|
12
|
+
* @returns 最新の値を保持する参照
|
|
13
|
+
*/
|
|
14
|
+
declare function useLatestRef<T>(value: T): LatestRef<T>;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { useLatestRef as default };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useInsertionEffect, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/useLatestRef/useLatestRef.ts
|
|
4
|
+
/**
|
|
5
|
+
* 最新の値を保持する参照を返すhook
|
|
6
|
+
*
|
|
7
|
+
* 返却する参照は常に同じオブジェクトのため、依存配列に含めずに
|
|
8
|
+
* イベントハンドラーやエフェクトの中から最新の値を参照できる。
|
|
9
|
+
* 値はコミット時に更新されるため、レンダリング中には参照しないこと。
|
|
10
|
+
*
|
|
11
|
+
* @param value 値
|
|
12
|
+
* @returns 最新の値を保持する参照
|
|
13
|
+
*/
|
|
14
|
+
function useLatestRef(value) {
|
|
15
|
+
const ref = useRef(value);
|
|
16
|
+
useInsertionEffect(() => {
|
|
17
|
+
ref.current = value;
|
|
18
|
+
});
|
|
19
|
+
return ref;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
export { useLatestRef as default };
|
|
File without changes
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/hooks/usePreviousValue/types.d.ts
|
|
2
|
+
type UsePreviousValueOptions<T = any> = {
|
|
3
|
+
/**
|
|
4
|
+
* 初期値
|
|
5
|
+
*/
|
|
6
|
+
initialValue?: T;
|
|
7
|
+
/**
|
|
8
|
+
* 値の比較関数
|
|
9
|
+
* falseの場合に値を更新する
|
|
10
|
+
* 未指定の場合は`===`で比較
|
|
11
|
+
* @param oldValue
|
|
12
|
+
* @param newValue
|
|
13
|
+
* @returns
|
|
14
|
+
*/
|
|
15
|
+
isEqual?: (oldValue: T | undefined, newValue: T) => boolean;
|
|
16
|
+
};
|
|
17
|
+
//#endregion
|
|
18
|
+
export { UsePreviousValueOptions };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/hooks/usePreviousValue/types.d.ts
|
|
2
|
+
type UsePreviousValueOptions<T = any> = {
|
|
3
|
+
/**
|
|
4
|
+
* 初期値
|
|
5
|
+
*/
|
|
6
|
+
initialValue?: T;
|
|
7
|
+
/**
|
|
8
|
+
* 値の比較関数
|
|
9
|
+
* falseの場合に値を更新する
|
|
10
|
+
* 未指定の場合は`===`で比較
|
|
11
|
+
* @param oldValue
|
|
12
|
+
* @param newValue
|
|
13
|
+
* @returns
|
|
14
|
+
*/
|
|
15
|
+
isEqual?: (oldValue: T | undefined, newValue: T) => boolean;
|
|
16
|
+
};
|
|
17
|
+
//#endregion
|
|
18
|
+
export { UsePreviousValueOptions };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/usePreviousValue/usePreviousValue.ts
|
|
4
|
+
const defaultIsEqual = (oldValue, newValue) => oldValue === newValue;
|
|
5
|
+
/**
|
|
6
|
+
* 前回の値を返すhook
|
|
7
|
+
* @param value 現在の値
|
|
8
|
+
* @param options オプション
|
|
9
|
+
* @returns 前回の値
|
|
10
|
+
*/
|
|
11
|
+
function usePreviousValue(value, options = {}) {
|
|
12
|
+
const isEqual = options.isEqual ?? defaultIsEqual;
|
|
13
|
+
const [currentValue, setCurrentValue] = (0, react.useState)(options.initialValue);
|
|
14
|
+
const [prevValue, setPrevValue] = (0, react.useState)(options.initialValue);
|
|
15
|
+
if (!isEqual(currentValue, value)) {
|
|
16
|
+
setPrevValue(currentValue);
|
|
17
|
+
setCurrentValue(value);
|
|
18
|
+
}
|
|
19
|
+
return prevValue;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
module.exports = usePreviousValue;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { UsePreviousValueOptions } from "./types.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/usePreviousValue/usePreviousValue.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* 前回の値を返すhook
|
|
6
|
+
* @param value 現在の値
|
|
7
|
+
* @param options オプション
|
|
8
|
+
* @returns 前回の値
|
|
9
|
+
*/
|
|
10
|
+
declare function usePreviousValue<T = unknown>(value: T, options?: UsePreviousValueOptions<T>): T | undefined;
|
|
11
|
+
export = usePreviousValue;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { UsePreviousValueOptions } from "./types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/usePreviousValue/usePreviousValue.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* 前回の値を返すhook
|
|
6
|
+
* @param value 現在の値
|
|
7
|
+
* @param options オプション
|
|
8
|
+
* @returns 前回の値
|
|
9
|
+
*/
|
|
10
|
+
declare function usePreviousValue<T = unknown>(value: T, options?: UsePreviousValueOptions<T>): T | undefined;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { usePreviousValue as default };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/usePreviousValue/usePreviousValue.ts
|
|
4
|
+
const defaultIsEqual = (oldValue, newValue) => oldValue === newValue;
|
|
5
|
+
/**
|
|
6
|
+
* 前回の値を返すhook
|
|
7
|
+
* @param value 現在の値
|
|
8
|
+
* @param options オプション
|
|
9
|
+
* @returns 前回の値
|
|
10
|
+
*/
|
|
11
|
+
function usePreviousValue(value, options = {}) {
|
|
12
|
+
const isEqual = options.isEqual ?? defaultIsEqual;
|
|
13
|
+
const [currentValue, setCurrentValue] = useState(options.initialValue);
|
|
14
|
+
const [prevValue, setPrevValue] = useState(options.initialValue);
|
|
15
|
+
if (!isEqual(currentValue, value)) {
|
|
16
|
+
setPrevValue(currentValue);
|
|
17
|
+
setCurrentValue(value);
|
|
18
|
+
}
|
|
19
|
+
return prevValue;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
export { usePreviousValue as default };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/useStableLength/useStableLength.ts
|
|
4
|
+
/**
|
|
5
|
+
* 配列数が常に一定であることを保証する為のフック\
|
|
6
|
+
* 配列数が変わった際にはエラーをスローする
|
|
7
|
+
* @param arr 配列
|
|
8
|
+
* @throws Error
|
|
9
|
+
*/
|
|
10
|
+
function useStableLength(arr) {
|
|
11
|
+
const length = (0, react.useRef)(arr.length).current;
|
|
12
|
+
if (length !== arr.length) throw new Error(`useStableLength: Array length must remain stable across renders (${length} → ${arr.length})`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
module.exports = useStableLength;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/hooks/useStableLength/useStableLength.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 配列数が常に一定であることを保証する為のフック\
|
|
4
|
+
* 配列数が変わった際にはエラーをスローする
|
|
5
|
+
* @param arr 配列
|
|
6
|
+
* @throws Error
|
|
7
|
+
*/
|
|
8
|
+
declare function useStableLength(arr: unknown[]): void;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { useStableLength as default };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { useRef } from "react";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/useStableLength/useStableLength.ts
|
|
4
|
+
/**
|
|
5
|
+
* 配列数が常に一定であることを保証する為のフック\
|
|
6
|
+
* 配列数が変わった際にはエラーをスローする
|
|
7
|
+
* @param arr 配列
|
|
8
|
+
* @throws Error
|
|
9
|
+
*/
|
|
10
|
+
function useStableLength(arr) {
|
|
11
|
+
const length = useRef(arr.length).current;
|
|
12
|
+
if (length !== arr.length) throw new Error(`useStableLength: Array length must remain stable across renders (${length} → ${arr.length})`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
export { useStableLength as default };
|
package/index.cjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_hooks_index = require('./hooks/index.cjs');
|
|
3
|
+
const require_utils_index = require('./utils/index.cjs');
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, 'hooks', {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function () {
|
|
8
|
+
return require_hooks_index.hooks_exports;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
Object.defineProperty(exports, 'utils', {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function () {
|
|
14
|
+
return require_utils_index.utils_exports;
|
|
15
|
+
}
|
|
16
|
+
});
|
package/index.d.cts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { index_d_exports } from "./hooks/index.cjs";
|
|
2
|
+
import { CSSPropertiesWithVars } from "./types.cjs";
|
|
3
|
+
import { index_d_exports as index_d_exports$1 } from "./utils/index.cjs";
|
|
4
|
+
export { type CSSPropertiesWithVars, index_d_exports as hooks, index_d_exports$1 as utils };
|
package/index.d.mts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { index_d_exports } from "./hooks/index.mjs";
|
|
2
|
+
import { CSSPropertiesWithVars } from "./types.mjs";
|
|
3
|
+
import { index_d_exports as index_d_exports$1 } from "./utils/index.mjs";
|
|
4
|
+
export { type CSSPropertiesWithVars, index_d_exports as hooks, index_d_exports$1 as utils };
|
package/index.mjs
ADDED