@pawover/kit 0.0.0-alpha.2 → 0.0.0-alpha.20
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/enums.d.ts +24 -0
- package/dist/enums.js +24 -0
- package/dist/hooks-alova.d.ts +22 -0
- package/dist/hooks-alova.js +38 -0
- package/dist/hooks-react.d.ts +87 -0
- package/dist/hooks-react.js +295 -0
- package/dist/index.d.ts +382 -0
- package/dist/index.js +1123 -2
- package/dist/vite.d.ts +12 -0
- package/dist/vite.js +21 -0
- package/dist/zod.d.ts +104 -0
- package/dist/zod.js +137 -0
- package/metadata.json +139 -0
- package/package.json +46 -42
- package/dist/enums/index.js +0 -20
- package/dist/hooks/react/index.js +0 -5
- package/dist/hooks/react/useCreation.js +0 -11
- package/dist/hooks/react/useLatest.js +0 -6
- package/dist/hooks/react/useMount.js +0 -29
- package/dist/hooks/react/useResponsive.js +0 -59
- package/dist/hooks/react/useUnmount.js +0 -17
- package/dist/types/enums/index.d.ts +0 -20
- package/dist/types/hooks/react/index.d.ts +0 -5
- package/dist/types/hooks/react/useCreation.d.ts +0 -2
- package/dist/types/hooks/react/useLatest.d.ts +0 -1
- package/dist/types/hooks/react/useMount.d.ts +0 -11
- package/dist/types/hooks/react/useResponsive.d.ts +0 -16
- package/dist/types/hooks/react/useUnmount.d.ts +0 -6
- package/dist/types/index.d.ts +0 -2
- package/dist/types/utils/array.d.ts +0 -76
- package/dist/types/utils/index.d.ts +0 -6
- package/dist/types/utils/object.d.ts +0 -53
- package/dist/types/utils/string.d.ts +0 -13
- package/dist/types/utils/to.d.ts +0 -5
- package/dist/types/utils/tree/index.d.ts +0 -6
- package/dist/types/utils/tree/rowsToTree.d.ts +0 -10
- package/dist/types/utils/tree/treeFilter.d.ts +0 -6
- package/dist/types/utils/tree/treeFind.d.ts +0 -8
- package/dist/types/utils/tree/treeForEach.d.ts +0 -5
- package/dist/types/utils/tree/treeMap.d.ts +0 -6
- package/dist/types/utils/tree/treeToRows.d.ts +0 -9
- package/dist/types/utils/tree/types.d.ts +0 -24
- package/dist/types/utils/typeof.d.ts +0 -29
- package/dist/utils/array.js +0 -196
- package/dist/utils/index.js +0 -6
- package/dist/utils/object.js +0 -138
- package/dist/utils/string.js +0 -70
- package/dist/utils/to.js +0 -16
- package/dist/utils/tree/index.js +0 -6
- package/dist/utils/tree/rowsToTree.js +0 -35
- package/dist/utils/tree/treeFilter.js +0 -92
- package/dist/utils/tree/treeFind.js +0 -82
- package/dist/utils/tree/treeForEach.js +0 -60
- package/dist/utils/tree/treeMap.js +0 -79
- package/dist/utils/tree/treeToRows.js +0 -13
- package/dist/utils/tree/types.js +0 -10
- package/dist/utils/typeof.js +0 -114
package/dist/enums.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//#region src/enums/index.d.ts
|
|
2
|
+
interface BreakPointTokens {
|
|
3
|
+
XS: number;
|
|
4
|
+
XSMin: number;
|
|
5
|
+
XSMax: number;
|
|
6
|
+
SM: number;
|
|
7
|
+
SMMin: number;
|
|
8
|
+
SMMax: number;
|
|
9
|
+
MD: number;
|
|
10
|
+
MDMin: number;
|
|
11
|
+
MDMax: number;
|
|
12
|
+
LG: number;
|
|
13
|
+
LGMin: number;
|
|
14
|
+
LGMax: number;
|
|
15
|
+
XL: number;
|
|
16
|
+
XLMin: number;
|
|
17
|
+
XLMax: number;
|
|
18
|
+
XXL: number;
|
|
19
|
+
XXLMin: number;
|
|
20
|
+
}
|
|
21
|
+
/** 屏幕响应断点 token 配置 */
|
|
22
|
+
declare const BREAK_POINT_TOKENS: BreakPointTokens;
|
|
23
|
+
//#endregion
|
|
24
|
+
export { BREAK_POINT_TOKENS, BreakPointTokens };
|
package/dist/enums.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//#region src/enums/index.ts
|
|
2
|
+
/** 屏幕响应断点 token 配置 */
|
|
3
|
+
const BREAK_POINT_TOKENS = {
|
|
4
|
+
XS: 576,
|
|
5
|
+
XSMin: 576,
|
|
6
|
+
XSMax: 767,
|
|
7
|
+
SM: 768,
|
|
8
|
+
SMMin: 768,
|
|
9
|
+
SMMax: 991,
|
|
10
|
+
MD: 992,
|
|
11
|
+
MDMin: 992,
|
|
12
|
+
MDMax: 1199,
|
|
13
|
+
LG: 1200,
|
|
14
|
+
LGMin: 1200,
|
|
15
|
+
LGMax: 1599,
|
|
16
|
+
XL: 1600,
|
|
17
|
+
XLMin: 1600,
|
|
18
|
+
XLMax: 1919,
|
|
19
|
+
XXL: 1920,
|
|
20
|
+
XXLMin: 1920
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
export { BREAK_POINT_TOKENS };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import * as alova_client0 from "alova/client";
|
|
2
|
+
import { AlovaFrontMiddlewareContext, AlovaMethodHandler, CompleteHandler, ErrorHandler, RequestHookConfig, SuccessHandler, WatcherHookConfig } from "alova/client";
|
|
3
|
+
import { AlovaGenerics, Method } from "alova";
|
|
4
|
+
|
|
5
|
+
//#region src/hooks/alova/useAlovaRequest.d.ts
|
|
6
|
+
interface HookConfig$1<AG extends AlovaGenerics, Args extends any[]> extends RequestHookConfig<AG, Args> {
|
|
7
|
+
onBeforeRequest?: (context: AlovaFrontMiddlewareContext<AG, Args>) => void;
|
|
8
|
+
onSuccess?: SuccessHandler<AG, Args>;
|
|
9
|
+
onError?: ErrorHandler<AG, Args>;
|
|
10
|
+
onComplete?: CompleteHandler<AG, Args>;
|
|
11
|
+
}
|
|
12
|
+
declare function useAlovaRequest<AG extends AlovaGenerics, Args extends any[] = any[]>(methodHandler: Method<AG> | AlovaMethodHandler<AG, Args>, hookConfig?: HookConfig$1<AG, Args> | undefined): alova_client0.UseHookExposure<AG, Args, unknown>;
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/hooks/alova/useAlovaWatcher.d.ts
|
|
15
|
+
interface HookConfig<AG extends AlovaGenerics, Args extends any[]> extends WatcherHookConfig<AG, Args> {
|
|
16
|
+
onSuccess?: SuccessHandler<AG, Args>;
|
|
17
|
+
onError?: ErrorHandler<AG, Args>;
|
|
18
|
+
onComplete?: CompleteHandler<AG, Args>;
|
|
19
|
+
}
|
|
20
|
+
declare function useAlovaWatcher<AG extends AlovaGenerics, Args extends any[] = any[]>(methodHandler: Method<AG> | AlovaMethodHandler<AG, Args>, watchingStates: AG["StatesExport"]["Watched"][], hookConfig?: HookConfig<AG, Args>): alova_client0.UseHookExposure<AG, Args, unknown>;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { useAlovaRequest, useAlovaWatcher };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useRequest, useWatcher } from "alova/client";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/alova/useAlovaRequest.ts
|
|
4
|
+
function useAlovaRequest(methodHandler, hookConfig) {
|
|
5
|
+
const config = hookConfig || {};
|
|
6
|
+
config.immediate ?? (config.immediate = true);
|
|
7
|
+
if (config.onBeforeRequest) {
|
|
8
|
+
const middleware = config.middleware;
|
|
9
|
+
config.middleware = async (context, next) => {
|
|
10
|
+
config.onBeforeRequest?.(context);
|
|
11
|
+
if (middleware) {
|
|
12
|
+
async function run() {
|
|
13
|
+
await next();
|
|
14
|
+
}
|
|
15
|
+
await middleware?.(context, run);
|
|
16
|
+
} else await next();
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
const exposure = useRequest(methodHandler, config);
|
|
20
|
+
if (config.onSuccess) exposure.onSuccess(config.onSuccess);
|
|
21
|
+
if (config.onError) exposure.onError(config.onError);
|
|
22
|
+
if (config.onComplete) exposure.onComplete(config.onComplete);
|
|
23
|
+
return exposure;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/hooks/alova/useAlovaWatcher.ts
|
|
28
|
+
function useAlovaWatcher(methodHandler, watchingStates, hookConfig = {}) {
|
|
29
|
+
const config = hookConfig || {};
|
|
30
|
+
const exposure = useWatcher(methodHandler, watchingStates, config);
|
|
31
|
+
if (config.onSuccess) exposure.onSuccess(config.onSuccess);
|
|
32
|
+
if (config.onError) exposure.onError(config.onError);
|
|
33
|
+
if (config.onComplete) exposure.onComplete(config.onComplete);
|
|
34
|
+
return exposure;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
//#endregion
|
|
38
|
+
export { useAlovaRequest, useAlovaWatcher };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import * as react0 from "react";
|
|
2
|
+
import { DependencyList, EffectCallback } from "react";
|
|
3
|
+
import { Breakpoint } from "@pawover/types";
|
|
4
|
+
|
|
5
|
+
//#region src/hooks/react/useCreation.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* useCreation
|
|
9
|
+
* @reference https://ahooks.js.org/zh-CN/hooks/use-creation
|
|
10
|
+
*
|
|
11
|
+
* @template T
|
|
12
|
+
* @param {() => T} factory
|
|
13
|
+
* @param {DependencyList} deps
|
|
14
|
+
*/
|
|
15
|
+
declare function useCreation<T>(factory: () => T, deps: DependencyList): T;
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/hooks/react/useLatest.d.ts
|
|
18
|
+
/**
|
|
19
|
+
* 返回当前最新值的 Hook
|
|
20
|
+
* @reference https://ahooks.js.org/zh-CN/hooks/use-latest
|
|
21
|
+
*
|
|
22
|
+
* @template T
|
|
23
|
+
* @param {T} value
|
|
24
|
+
*/
|
|
25
|
+
declare function useLatest<T>(value: T): react0.RefObject<T>;
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/hooks/react/useMount.d.ts
|
|
28
|
+
type MountCallback = EffectCallback | AsyncFunc;
|
|
29
|
+
/**
|
|
30
|
+
* 在组件初始化时执行的 Hook
|
|
31
|
+
* - 即使在严格模式下也只执行一次
|
|
32
|
+
* @reference https://ahooks.js.org/hooks/use-mount
|
|
33
|
+
*
|
|
34
|
+
* @param {MountCallback} effect 副作用函数
|
|
35
|
+
*/
|
|
36
|
+
declare function useMount(effect: MountCallback): void;
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/enums/index.d.ts
|
|
39
|
+
interface BreakPointTokens {
|
|
40
|
+
XS: number;
|
|
41
|
+
XSMin: number;
|
|
42
|
+
XSMax: number;
|
|
43
|
+
SM: number;
|
|
44
|
+
SMMin: number;
|
|
45
|
+
SMMax: number;
|
|
46
|
+
MD: number;
|
|
47
|
+
MDMin: number;
|
|
48
|
+
MDMax: number;
|
|
49
|
+
LG: number;
|
|
50
|
+
LGMin: number;
|
|
51
|
+
LGMax: number;
|
|
52
|
+
XL: number;
|
|
53
|
+
XLMin: number;
|
|
54
|
+
XLMax: number;
|
|
55
|
+
XXL: number;
|
|
56
|
+
XXLMin: number;
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/hooks/react/useResponsive.d.ts
|
|
60
|
+
type ResponsiveValues = Record<Breakpoint, boolean>;
|
|
61
|
+
interface ResponsiveHookOptions {
|
|
62
|
+
/**
|
|
63
|
+
* 紧凑布局断点
|
|
64
|
+
* - 低于此断点时使用紧凑布局
|
|
65
|
+
* @default "xl"
|
|
66
|
+
*/
|
|
67
|
+
compactBreakPoint?: Breakpoint;
|
|
68
|
+
/** 屏幕响应断点 token 配置 */
|
|
69
|
+
breakPointTokens?: BreakPointTokens;
|
|
70
|
+
}
|
|
71
|
+
declare function useResponsive(options?: ResponsiveHookOptions): {
|
|
72
|
+
responsive: ResponsiveValues;
|
|
73
|
+
current: Breakpoint;
|
|
74
|
+
isCompact: boolean;
|
|
75
|
+
breakPointTokens: BreakPointTokens;
|
|
76
|
+
};
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/hooks/react/useUnmount.d.ts
|
|
79
|
+
/**
|
|
80
|
+
* 在组件卸载时执行的 Hook
|
|
81
|
+
* @reference https://ahooks.js.org/zh-CN/hooks/use-unmount
|
|
82
|
+
*
|
|
83
|
+
* @param {Func} effect 副作用函数
|
|
84
|
+
*/
|
|
85
|
+
declare function useUnmount(effect: Func): void;
|
|
86
|
+
//#endregion
|
|
87
|
+
export { useCreation, useLatest, useMount, useResponsive, useUnmount };
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from "react";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/typeof/types.ts
|
|
4
|
+
const prototypeStrings = {
|
|
5
|
+
string: "[object String]",
|
|
6
|
+
number: "[object Number]",
|
|
7
|
+
boolean: "[object Boolean]",
|
|
8
|
+
object: "[object Object]",
|
|
9
|
+
array: "[object Array]",
|
|
10
|
+
bigInt: "[object BigInt]",
|
|
11
|
+
symbol: "[object Symbol]",
|
|
12
|
+
function: "[object Function]",
|
|
13
|
+
generatorFunction: "[object GeneratorFunction]",
|
|
14
|
+
asyncFunction: "[object AsyncFunction]",
|
|
15
|
+
promise: "[object Promise]",
|
|
16
|
+
null: "[object Null]",
|
|
17
|
+
undefined: "[object Undefined]",
|
|
18
|
+
date: "[object Date]",
|
|
19
|
+
regExp: "[object RegExp]",
|
|
20
|
+
error: "[object Error]",
|
|
21
|
+
file: "[object File]",
|
|
22
|
+
map: "[object Map]",
|
|
23
|
+
weakMap: "[object WeakMap]",
|
|
24
|
+
set: "[object Set]",
|
|
25
|
+
weakSet: "[object WeakSet]",
|
|
26
|
+
window: "[object Window]",
|
|
27
|
+
webSocket: "[object WebSocket]",
|
|
28
|
+
URLSearchParams: "[object URLSearchParams]"
|
|
29
|
+
};
|
|
30
|
+
function resolvePrototypeString(value) {
|
|
31
|
+
return Object.prototype.toString.call(value);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/utils/typeof/isEqual.ts
|
|
36
|
+
/**
|
|
37
|
+
* 判断给定的值是否相等
|
|
38
|
+
* @reference https://github.com/radashi-org/radashi/blob/main/src/typed/isEqual.ts
|
|
39
|
+
*
|
|
40
|
+
* @param {T} x
|
|
41
|
+
* @param {T} y
|
|
42
|
+
*/
|
|
43
|
+
function isEqual(x, y) {
|
|
44
|
+
if (Object.is(x, y)) return true;
|
|
45
|
+
if (x instanceof Date && y instanceof Date) return x.getTime() === y.getTime();
|
|
46
|
+
if (x instanceof RegExp && y instanceof RegExp) return x.toString() === y.toString();
|
|
47
|
+
if (typeof x !== "object" || x === null || typeof y !== "object" || y === null) return false;
|
|
48
|
+
const keysX = Reflect.ownKeys(x);
|
|
49
|
+
const keysY = Reflect.ownKeys(y);
|
|
50
|
+
if (keysX.length !== keysY.length) return false;
|
|
51
|
+
for (const key of keysX) {
|
|
52
|
+
if (!Reflect.has(y, key)) return false;
|
|
53
|
+
if (!isEqual(x[key], y[key])) return false;
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/utils/typeof/isFunction.ts
|
|
60
|
+
function isFunction(value) {
|
|
61
|
+
return [
|
|
62
|
+
prototypeStrings.function,
|
|
63
|
+
prototypeStrings.generatorFunction,
|
|
64
|
+
prototypeStrings.asyncFunction
|
|
65
|
+
].includes(resolvePrototypeString(value));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region src/utils/typeof/isObject.ts
|
|
70
|
+
function isObject(value) {
|
|
71
|
+
return resolvePrototypeString(value) === prototypeStrings.object;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region src/utils/typeof/isPromise.ts
|
|
76
|
+
function isPromise(value) {
|
|
77
|
+
return resolvePrototypeString(value) === prototypeStrings.promise;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/utils/typeof/isPromiseLike.ts
|
|
82
|
+
function isPromiseLike(value) {
|
|
83
|
+
return isPromise(value) || isObject(value) && isFunction(value["then"]);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/utils/object/objectKeys.ts
|
|
88
|
+
/**
|
|
89
|
+
* 返回对象的可枚举属性和方法的名称
|
|
90
|
+
* - `Object.keys` 始终返回 `string[]` 类型,此函数可以返回具体类型
|
|
91
|
+
*
|
|
92
|
+
* @param obj 对象
|
|
93
|
+
*/
|
|
94
|
+
function objectKeys(obj) {
|
|
95
|
+
return Object.keys(obj);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region node_modules/.pnpm/radashi@12.7.1/node_modules/radashi/dist/radashi.js
|
|
100
|
+
function assign(initial, override) {
|
|
101
|
+
if (!initial || !override) return initial ?? override ?? {};
|
|
102
|
+
const proto = Object.getPrototypeOf(initial);
|
|
103
|
+
const merged = proto ? { ...initial } : Object.assign(Object.create(proto), initial);
|
|
104
|
+
for (const key of Object.keys(override)) merged[key] = isPlainObject(initial[key]) && isPlainObject(override[key]) ? assign(initial[key], override[key]) : override[key];
|
|
105
|
+
return merged;
|
|
106
|
+
}
|
|
107
|
+
function isPlainObject(value) {
|
|
108
|
+
if (typeof value !== "object" || value === null) return false;
|
|
109
|
+
const prototype = Object.getPrototypeOf(value);
|
|
110
|
+
return prototype === Object.prototype || prototype === null || Object.getPrototypeOf(prototype) === null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
//#endregion
|
|
114
|
+
//#region src/utils/object/objectAssign.ts
|
|
115
|
+
const objectAssign = assign;
|
|
116
|
+
|
|
117
|
+
//#endregion
|
|
118
|
+
//#region src/hooks/react/useCreation.ts
|
|
119
|
+
/**
|
|
120
|
+
* useCreation
|
|
121
|
+
* @reference https://ahooks.js.org/zh-CN/hooks/use-creation
|
|
122
|
+
*
|
|
123
|
+
* @template T
|
|
124
|
+
* @param {() => T} factory
|
|
125
|
+
* @param {DependencyList} deps
|
|
126
|
+
*/
|
|
127
|
+
function useCreation(factory, deps) {
|
|
128
|
+
const { current } = useRef({
|
|
129
|
+
deps,
|
|
130
|
+
result: void 0,
|
|
131
|
+
isInitialized: false
|
|
132
|
+
});
|
|
133
|
+
if (current.isInitialized === false || !isEqual(current.deps, deps)) {
|
|
134
|
+
current.deps = deps;
|
|
135
|
+
current.result = factory();
|
|
136
|
+
current.isInitialized = true;
|
|
137
|
+
}
|
|
138
|
+
return current.result;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
//#endregion
|
|
142
|
+
//#region src/hooks/react/useLatest.ts
|
|
143
|
+
/**
|
|
144
|
+
* 返回当前最新值的 Hook
|
|
145
|
+
* @reference https://ahooks.js.org/zh-CN/hooks/use-latest
|
|
146
|
+
*
|
|
147
|
+
* @template T
|
|
148
|
+
* @param {T} value
|
|
149
|
+
*/
|
|
150
|
+
function useLatest(value) {
|
|
151
|
+
const ref = useRef(value);
|
|
152
|
+
ref.current = value;
|
|
153
|
+
return ref;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
//#endregion
|
|
157
|
+
//#region src/hooks/react/useMount.ts
|
|
158
|
+
/**
|
|
159
|
+
* 在组件初始化时执行的 Hook
|
|
160
|
+
* - 即使在严格模式下也只执行一次
|
|
161
|
+
* @reference https://ahooks.js.org/hooks/use-mount
|
|
162
|
+
*
|
|
163
|
+
* @param {MountCallback} effect 副作用函数
|
|
164
|
+
*/
|
|
165
|
+
function useMount(effect) {
|
|
166
|
+
if (!isFunction(effect)) console.error(`useMount expected parameter is a function, but got ${typeof effect}`);
|
|
167
|
+
const isMountedRef = useRef(false);
|
|
168
|
+
const effectRef = useLatest(effect);
|
|
169
|
+
useEffect(() => {
|
|
170
|
+
if (isMountedRef.current) return;
|
|
171
|
+
isMountedRef.current = true;
|
|
172
|
+
const result = effectRef.current?.();
|
|
173
|
+
if (isPromiseLike(result)) return;
|
|
174
|
+
return result;
|
|
175
|
+
}, []);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
//#endregion
|
|
179
|
+
//#region src/enums/index.ts
|
|
180
|
+
/** 屏幕响应断点 token 配置 */
|
|
181
|
+
const BREAK_POINT_TOKENS = {
|
|
182
|
+
XS: 576,
|
|
183
|
+
XSMin: 576,
|
|
184
|
+
XSMax: 767,
|
|
185
|
+
SM: 768,
|
|
186
|
+
SMMin: 768,
|
|
187
|
+
SMMax: 991,
|
|
188
|
+
MD: 992,
|
|
189
|
+
MDMin: 992,
|
|
190
|
+
MDMax: 1199,
|
|
191
|
+
LG: 1200,
|
|
192
|
+
LGMin: 1200,
|
|
193
|
+
LGMax: 1599,
|
|
194
|
+
XL: 1600,
|
|
195
|
+
XLMin: 1600,
|
|
196
|
+
XLMax: 1919,
|
|
197
|
+
XXL: 1920,
|
|
198
|
+
XXLMin: 1920
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
//#endregion
|
|
202
|
+
//#region src/hooks/react/useResponsive.ts
|
|
203
|
+
const subscriberList = /* @__PURE__ */ new Set();
|
|
204
|
+
const { XS, SM, MD, LG, XL, XXL } = BREAK_POINT_TOKENS;
|
|
205
|
+
const defaultResponsiveValues = {
|
|
206
|
+
xxl: false,
|
|
207
|
+
xl: false,
|
|
208
|
+
lg: false,
|
|
209
|
+
md: false,
|
|
210
|
+
sm: false,
|
|
211
|
+
xs: false
|
|
212
|
+
};
|
|
213
|
+
let responsiveConfig = {
|
|
214
|
+
xxl: XXL,
|
|
215
|
+
xl: XL,
|
|
216
|
+
lg: LG,
|
|
217
|
+
md: MD,
|
|
218
|
+
sm: SM,
|
|
219
|
+
xs: XS
|
|
220
|
+
};
|
|
221
|
+
let responsiveValues = { ...defaultResponsiveValues };
|
|
222
|
+
function useResponsive(options) {
|
|
223
|
+
const { compactBreakPoint = "xl", breakPointTokens = {} } = options || {};
|
|
224
|
+
const tokens = objectAssign(BREAK_POINT_TOKENS, breakPointTokens);
|
|
225
|
+
responsiveConfig = {
|
|
226
|
+
xxl: tokens.XXL,
|
|
227
|
+
xl: tokens.XL,
|
|
228
|
+
lg: tokens.LG,
|
|
229
|
+
md: tokens.MD,
|
|
230
|
+
sm: tokens.SM,
|
|
231
|
+
xs: tokens.XS
|
|
232
|
+
};
|
|
233
|
+
calculate();
|
|
234
|
+
const [responsive, setResponsive] = useState(responsiveValues);
|
|
235
|
+
const isCompact = !responsive[compactBreakPoint];
|
|
236
|
+
const current = objectKeys(defaultResponsiveValues).find((key) => responsive[key] === true) || "xs";
|
|
237
|
+
useEffect(() => {
|
|
238
|
+
addListener();
|
|
239
|
+
const subscriber = () => {
|
|
240
|
+
setResponsive(responsiveValues);
|
|
241
|
+
};
|
|
242
|
+
subscriberList.add(subscriber);
|
|
243
|
+
return () => {
|
|
244
|
+
subscriberList.delete(subscriber);
|
|
245
|
+
if (subscriberList.size === 0) removeListener();
|
|
246
|
+
};
|
|
247
|
+
}, []);
|
|
248
|
+
return {
|
|
249
|
+
responsive,
|
|
250
|
+
current,
|
|
251
|
+
isCompact,
|
|
252
|
+
breakPointTokens: tokens
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
function resizeListener() {
|
|
256
|
+
const oldInfo = responsiveValues;
|
|
257
|
+
calculate();
|
|
258
|
+
if (oldInfo === responsiveValues) return;
|
|
259
|
+
for (const subscriber of subscriberList) subscriber();
|
|
260
|
+
}
|
|
261
|
+
function addListener() {
|
|
262
|
+
window.addEventListener("resize", resizeListener);
|
|
263
|
+
}
|
|
264
|
+
function removeListener() {
|
|
265
|
+
window.removeEventListener("resize", resizeListener);
|
|
266
|
+
}
|
|
267
|
+
function calculate() {
|
|
268
|
+
const width = window.innerWidth;
|
|
269
|
+
const newValues = { ...defaultResponsiveValues };
|
|
270
|
+
let shouldUpdate = false;
|
|
271
|
+
for (const key of objectKeys(responsiveConfig)) {
|
|
272
|
+
newValues[key] = width >= responsiveConfig[key];
|
|
273
|
+
if (newValues[key] !== responsiveValues[key]) shouldUpdate = true;
|
|
274
|
+
}
|
|
275
|
+
if (shouldUpdate) responsiveValues = newValues;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
//#endregion
|
|
279
|
+
//#region src/hooks/react/useUnmount.ts
|
|
280
|
+
/**
|
|
281
|
+
* 在组件卸载时执行的 Hook
|
|
282
|
+
* @reference https://ahooks.js.org/zh-CN/hooks/use-unmount
|
|
283
|
+
*
|
|
284
|
+
* @param {Func} effect 副作用函数
|
|
285
|
+
*/
|
|
286
|
+
function useUnmount(effect) {
|
|
287
|
+
if (!isFunction(effect)) console.error(`useUnmount expected parameter is a function, got ${typeof effect}`);
|
|
288
|
+
const effectRef = useLatest(effect);
|
|
289
|
+
useEffect(() => () => {
|
|
290
|
+
effectRef.current?.();
|
|
291
|
+
}, []);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
//#endregion
|
|
295
|
+
export { useCreation, useLatest, useMount, useResponsive, useUnmount };
|