@tonar/react-kit 0.1.0-beta.0 → 0.1.0-beta.1

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.
@@ -1,2 +1,2 @@
1
- import { i as e, n as t, r as n, t as r } from "./js/components.CLeQ5flK.js";
1
+ import { i as e, n as t, r as n, t as r } from "./js/components.KKh9eRLH.js";
2
2
  export { t as AsyncCustomShow, e as Carousel, n as CustomShow, r as ErrorBoundary };
package/dist/hooks.esm.js CHANGED
@@ -115,53 +115,55 @@ var u = (t) => {
115
115
  unobserve: e((e) => e && a.current?.unobserve?.(e), []),
116
116
  disconnect: e(() => a.current?.disconnect?.(), [])
117
117
  };
118
- }, g = ({ key: n, initialValue: r, hasDiff: i, onChange: a, storage: o = localStorage, checkType: s = () => !0, beforeunload: c }) => {
119
- let [u, d] = l({
120
- initialValue: () => {
121
- try {
122
- let e = o.getItem(n), t = e ? JSON.parse(e) : r;
123
- return s(t) ? t : r;
124
- } catch (e) {
125
- return console.warn("初始化出错,已使用默认值", e), r;
126
- }
127
- },
128
- hasDiff: i,
129
- onChange: a
130
- }), f = e((e) => {
118
+ }, g = ({ key: n, defaultValue: i, hasDiff: a, onChange: o, storage: s = localStorage, checkType: u = () => !0, beforeunload: d }) => {
119
+ let f = c((e) => {
120
+ try {
121
+ let t = e ? JSON.parse(e) : i;
122
+ return u(t) ? t : i;
123
+ } catch (e) {
124
+ return console.warn("规范化出错,已使用默认值", e), i;
125
+ }
126
+ }), [p, m] = l({
127
+ initialValue: () => f(s.getItem(n) ?? ""),
128
+ hasDiff: a,
129
+ onChange: o
130
+ }), h = r(/* @__PURE__ */ new WeakSet()), g = e((e) => {
131
131
  try {
132
- d((t) => {
132
+ m((t) => {
133
133
  let r = e instanceof Function ? e(t) : e, i = JSON.stringify(r);
134
- return o.setItem(n, i), window.dispatchEvent(new StorageEvent("storage", {
134
+ s.setItem(n, i);
135
+ let a = new StorageEvent("storage", {
135
136
  key: n,
136
137
  newValue: i,
137
- storageArea: o
138
- })), r;
138
+ storageArea: s
139
+ });
140
+ return h.current.add(a), window.dispatchEvent(a), r;
139
141
  });
140
142
  } catch (e) {
141
143
  console.error("持久化储存错误", e);
142
144
  }
143
145
  }, [
144
146
  n,
145
- o,
146
- d
147
- ]);
147
+ s,
148
+ m
149
+ ]), _ = c((e) => {
150
+ if (!(h.current.has(e) || e.storageArea !== s || e.key !== n)) try {
151
+ let t = f(e.newValue ?? "");
152
+ m(t);
153
+ } catch (e) {
154
+ console.error("storage事件处理出错", e);
155
+ }
156
+ });
157
+ t(() => (window.addEventListener("storage", _), () => window.removeEventListener("storage", _)), [_]);
158
+ let v = c(() => d?.(n, p, s));
148
159
  return t(() => {
149
- let e = (e) => {
150
- if (!(e.storageArea !== o || e.key !== n)) try {
151
- d(e.newValue ? JSON.parse(e.newValue) : r);
152
- } catch (e) {
153
- console.error("storage事件处理出错", e);
154
- }
155
- };
156
- return window.addEventListener("storage", e), () => window.removeEventListener("storage", e);
157
- }, [
158
- n,
159
- o,
160
- r,
161
- d
162
- ]), t(() => {
163
- c && window.addEventListener("beforeunload", c);
164
- }, []), [u, f];
160
+ d && window.addEventListener("beforeunload", v);
161
+ }, []), [
162
+ p,
163
+ g,
164
+ () => window.removeEventListener("storage", _),
165
+ () => d && window.removeEventListener("beforeunload", v)
166
+ ];
165
167
  }, _ = (t, n, a) => {
166
168
  let [o, s] = i(!1), l = r(o), u = c(n), d = e((e, ...t) => {
167
169
  l.current = e, s(e), u?.(e, ...t);
package/dist/index.esm.js CHANGED
@@ -1,3 +1,3 @@
1
1
  import { useAsyncActionLock as e, useCreateSafeRef as t, useDistinctState as n, useIntersectionObserver as r, useInterval as i, useLatestCallback as a, useMutationObserver as o, useRAfInterval as s, useResizeObserver as c, useStaticState as l, useStorage as u } from "./hooks.esm.js";
2
- import { i as d, n as f, r as p, t as m } from "./js/components.CLeQ5flK.js";
2
+ import { i as d, n as f, r as p, t as m } from "./js/components.KKh9eRLH.js";
3
3
  export { f as AsyncCustomShow, d as Carousel, p as CustomShow, m as ErrorBoundary, e as useAsyncActionLock, t as useCreateSafeRef, n as useDistinctState, r as useIntersectionObserver, i as useInterval, a as useLatestCallback, o as useMutationObserver, s as useRAfInterval, c as useResizeObserver, l as useStaticState, u as useStorage };
@@ -174,11 +174,13 @@ function C({ when: e, fallback: t = void 0, children: n }) {
174
174
  function w({ when: e, fallback: t, children: n }) {
175
175
  let [r, i] = u(void 0);
176
176
  return o(() => {
177
- e && Promise.resolve(e).then(i);
177
+ if (!e) return;
178
+ let t = !0;
179
+ return Promise.resolve(e).then((e) => t && i(e)), () => void (t = !1);
178
180
  }, [e]), /* @__PURE__ */ f(d, { children: r ? n(r) : t });
179
181
  }
180
182
  //#endregion
181
- //#region \0@oxc-project+runtime@0.133.0/helpers/esm/typeof.js
183
+ //#region \0@oxc-project+runtime@0.138.0/helpers/esm/typeof.js
182
184
  function T(e) {
183
185
  "@babel/helpers - typeof";
184
186
  return T = typeof Symbol == "function" && typeof Symbol.iterator == "symbol" ? function(e) {
@@ -188,7 +190,7 @@ function T(e) {
188
190
  }, T(e);
189
191
  }
190
192
  //#endregion
191
- //#region \0@oxc-project+runtime@0.133.0/helpers/esm/toPrimitive.js
193
+ //#region \0@oxc-project+runtime@0.138.0/helpers/esm/toPrimitive.js
192
194
  function E(e, t) {
193
195
  if (T(e) != "object" || !e) return e;
194
196
  var n = e[Symbol.toPrimitive];
@@ -200,13 +202,13 @@ function E(e, t) {
200
202
  return (t === "string" ? String : Number)(e);
201
203
  }
202
204
  //#endregion
203
- //#region \0@oxc-project+runtime@0.133.0/helpers/esm/toPropertyKey.js
205
+ //#region \0@oxc-project+runtime@0.138.0/helpers/esm/toPropertyKey.js
204
206
  function D(e) {
205
207
  var t = E(e, "string");
206
208
  return T(t) == "symbol" ? t : t + "";
207
209
  }
208
210
  //#endregion
209
- //#region \0@oxc-project+runtime@0.133.0/helpers/esm/defineProperty.js
211
+ //#region \0@oxc-project+runtime@0.138.0/helpers/esm/defineProperty.js
210
212
  function O(e, t, n) {
211
213
  return (t = D(t)) in e ? Object.defineProperty(e, t, {
212
214
  value: n,
@@ -6,9 +6,10 @@ import { SetStateAction } from 'react';
6
6
  */
7
7
  export declare function useStaticState<T>(initialValue: T): [() => T, (t: T) => void, (t?: T) => T];
8
8
  export declare function useStaticState<T>(initialValue?: undefined): [() => T | undefined, (t: T) => void, (t?: T) => T | undefined];
9
+ type AnyFn = (...args: never[]) => unknown;
9
10
  interface UseLatestCallbackType {
10
- <T extends (...args: never[]) => unknown>(cb: T): T;
11
- <T extends (...args: never[]) => unknown>(cb?: T | undefined): T | undefined;
11
+ <T extends AnyFn>(cb: T): T;
12
+ <T extends AnyFn>(cb?: T): (...args: Parameters<T>) => ReturnType<T>;
12
13
  }
13
14
  /**
14
15
  * @author sonion
@@ -1,25 +1,28 @@
1
1
  import { useDistinctState } from '../state';
2
- type StorageParams<T> = Omit<Parameters<typeof useDistinctState<T>>[0], 'onlyEvent'> & {
2
+ type StorageType = typeof localStorage | typeof sessionStorage;
3
+ type StorageParams<T> = Omit<Parameters<typeof useDistinctState<T>>[0], 'onlyEvent' | 'initialValue'> & {
3
4
  /** 储存的key */
4
5
  key: string;
6
+ /** 默认值 */
7
+ defaultValue: T;
5
8
  /** 储存类型。 localStorage 或 sessionStorage */
6
- storage?: typeof localStorage | typeof sessionStorage;
9
+ storage?: StorageType;
7
10
  /** 初始化类型检查函数,检查不通过使用初始值。可避免类型不对引起的错误 */
8
11
  checkType?: (val: T) => boolean;
9
12
  /** tab关闭前的回调, 相同key的不同回调只有初始生效。 */
10
- beforeunload?: () => void;
13
+ beforeunload?: (key: string, value: T, storage: StorageType) => void;
11
14
  };
12
15
  /**
13
16
  * @author sonion
14
17
  * @description 本地储存
15
18
  * @param params - 参数对象
16
19
  * @param params.key - 储存的key
17
- * @param params.initialValue - 初始值
20
+ * @param params.defaultValue - 默认值
18
21
  * @param [params.hasDiff] - 对比函数,默认对比引用是否不相同。
19
22
  * @param params.onChange - 变化回调
20
23
  * @param [params.storage] - 储存类型。 localStorage 或 sessionStorage
21
24
  * @param [params.checkType] - 初始化类型检查函数,检查不通过使用初始值。可避免类型不对引起的错误
22
- * @param [params.beforeunload] - tab关闭前的回调, 相同key的不同回调只有初始生效。
25
+ * @param [params.beforeunload] - tab关闭前的回调。
23
26
  */
24
- export declare const useStorage: <T>({ key, initialValue, hasDiff, onChange, storage, checkType, beforeunload, }: StorageParams<T>) => readonly [T, (value: T | ((val: T) => T)) => void];
27
+ export declare const useStorage: <T>({ key, defaultValue, hasDiff, onChange, storage, checkType, beforeunload, }: StorageParams<T>) => readonly [T, (value: T | ((val: T) => T)) => void, () => void, () => void | undefined];
25
28
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tonar/react-kit",
3
- "version": "0.1.0-beta.0",
3
+ "version": "0.1.0-beta.1",
4
4
  "private": false,
5
5
  "description": "@tonar/react-kit: React 18 components and hooks for carousels, conditional rendering, error boundaries, state helpers, observers, storage, timers, and async action locking.",
6
6
  "keywords": [
@@ -34,27 +34,25 @@
34
34
  },
35
35
  "./hooks": {
36
36
  "import": "./dist/hooks.esm.js",
37
- "types": "./dist/types/hooks.d.ts"
37
+ "types": "./dist/types/hooks/index.d.ts"
38
38
  },
39
39
  "./components": {
40
40
  "import": "./dist/components.esm.js",
41
- "types": "./dist/types/components.d.ts"
41
+ "types": "./dist/types/components/index.d.ts"
42
42
  }
43
43
  },
44
44
  "dependencies": {
45
- "@tonar/utils": "0.1.0-beta.0"
45
+ "@tonar/utils": "0.1.0-beta.1"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@testing-library/react": "^16.3.2",
49
49
  "@types/react": "^18.3.31",
50
50
  "@types/react-dom": "^18.3.7",
51
- "@vitejs/plugin-react": "^6.0.2",
52
- "jsdom": "^29.1.1",
51
+ "@vitejs/plugin-react": "^6.0.3",
53
52
  "react": "^18.3.1",
54
53
  "react-dom": "^18.3.1",
55
54
  "sass-embedded": "^1.100.0",
56
- "vite-plugin-lib-inject-css": "^2.2.2",
57
- "vitest": "^4.1.8"
55
+ "vite-plugin-lib-inject-css": "^2.2.2"
58
56
  },
59
57
  "peerDependencies": {
60
58
  "react": "^18.0.0",
@@ -68,6 +66,7 @@
68
66
  },
69
67
  "scripts": {
70
68
  "build": "rimraf dist && vite build",
69
+ "typecheck": "pnpm exec tsc --noEmit",
71
70
  "test": "vitest --run"
72
71
  }
73
72
  }