@himanshu-sorathiya/react-kit 1.0.24 → 1.0.25

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/index.d.ts CHANGED
@@ -458,6 +458,44 @@ export interface UseSortReturn<T> {
458
458
  getSortIndex: (id: string) => number | undefined;
459
459
  }
460
460
  export declare function useSort<T>(data?: T[], initialSorts?: SortState): UseSortReturn<T>;
461
+ export interface StorageSerializer<T> {
462
+ serialize: (value: T) => string;
463
+ deserialize: (raw: string) => T;
464
+ }
465
+ export interface BaseStorageOptions<T> {
466
+ serializer?: StorageSerializer<T>;
467
+ initializeWithValue?: boolean;
468
+ sameInstanceSync?: boolean;
469
+ }
470
+ export interface StorageCustomEventDetail {
471
+ value: string | null;
472
+ instanceId: symbol;
473
+ }
474
+ export interface UseLocalStorageOptions<T> extends BaseStorageOptions<T> {
475
+ crossInstanceSync?: boolean;
476
+ }
477
+ export interface UseLocalStorageReturn<T> {
478
+ value: T | undefined;
479
+ setValue: (valueOrUpdater: T | ((prev: T | undefined) => T)) => void;
480
+ removeValue: () => void;
481
+ isHydrated: boolean;
482
+ error: Error | null;
483
+ }
484
+ export declare function useLocalStorage<T = unknown>(key: string, initialValue?: T, options?: UseLocalStorageOptions<T>): UseLocalStorageReturn<T>;
485
+ export type UseSessionStorageOptions<T> = BaseStorageOptions<T>;
486
+ export interface UseSessionStorageReturn<T> {
487
+ value: T | undefined;
488
+ setValue: (valueOrUpdater: T | ((prev: T | undefined) => T)) => void;
489
+ removeValue: () => void;
490
+ isHydrated: boolean;
491
+ error: Error | null;
492
+ }
493
+ export declare function useSessionStorage<T = unknown>(key: string, initialValue?: T, options?: UseSessionStorageOptions<T>): UseSessionStorageReturn<T>;
494
+ export declare const defaultSerializer: StorageSerializer<unknown>;
495
+ export declare function mapSerializer<K, V>(): StorageSerializer<Map<K, V>>;
496
+ export declare function setSerializer<V>(): StorageSerializer<Set<V>>;
497
+ export declare const dateSerializer: StorageSerializer<Date>;
498
+ export declare const bigIntSerializer: StorageSerializer<bigint>;
461
499
  export interface FuzzyHighlighterProps {
462
500
  text: string;
463
501
  query: string;
package/dist/index.js CHANGED
@@ -2,5 +2,6 @@ import { t as e } from "./useEventListener.js";
2
2
  import { n as t, t as n } from "./events2.js";
3
3
  import { a as r, c as i, d as a, f as o, i as s, l as c, n as l, o as u, r as d, s as f, t as p, u as m } from "./performance2.js";
4
4
  import { a as h, c as g, i as _, n as v, o as y, r as b, s as x, t as S } from "./state2.js";
5
- import { a as C, c as w, i as T, n as E, o as D, r as O, s as k, t as A } from "./ui2.js";
6
- export { w as FuzzyHighlighter, T as ModalLayout, t as useClickOutside, o as useDebounce, a as useDebouncedCallback, m as useDebouncedState, c as useDebouncedValue, e as useEventListener, O as useExpansion, g as useFilter, x as useFuzzySearch, y as useGrouping, n as useKey, C as useModal, D as useModalActions, k as useModalState, h as useMultipleSelection, _ as useOrder, b as usePagination, E as usePin, i as useRateLimit, f as useRateLimitedCallback, v as useSingleSelection, S as useSort, u as useThrottle, r as useThrottledCallback, s as useThrottledState, d as useThrottledValue, l as useVirtualGrid, p as useVirtualList, A as useVisibility };
5
+ import { a as C, i as w, n as T, o as E, r as D, s as O, t as k } from "./storage2.js";
6
+ import { a as A, c as j, i as M, n as N, o as P, r as F, s as I, t as L } from "./ui2.js";
7
+ export { j as FuzzyHighlighter, M as ModalLayout, D as bigIntSerializer, w as dateSerializer, C as defaultSerializer, E as mapSerializer, O as setSerializer, t as useClickOutside, o as useDebounce, a as useDebouncedCallback, m as useDebouncedState, c as useDebouncedValue, e as useEventListener, F as useExpansion, g as useFilter, x as useFuzzySearch, y as useGrouping, n as useKey, T as useLocalStorage, A as useModal, P as useModalActions, I as useModalState, h as useMultipleSelection, _ as useOrder, b as usePagination, N as usePin, i as useRateLimit, f as useRateLimitedCallback, k as useSessionStorage, v as useSingleSelection, S as useSort, u as useThrottle, r as useThrottledCallback, s as useThrottledState, d as useThrottledValue, l as useVirtualGrid, p as useVirtualList, L as useVisibility };
@@ -0,0 +1,42 @@
1
+ // Generated by dts-bundle-generator v9.5.1
2
+
3
+ export interface StorageSerializer<T> {
4
+ serialize: (value: T) => string;
5
+ deserialize: (raw: string) => T;
6
+ }
7
+ export interface BaseStorageOptions<T> {
8
+ serializer?: StorageSerializer<T>;
9
+ initializeWithValue?: boolean;
10
+ sameInstanceSync?: boolean;
11
+ }
12
+ export interface StorageCustomEventDetail {
13
+ value: string | null;
14
+ instanceId: symbol;
15
+ }
16
+ export interface UseLocalStorageOptions<T> extends BaseStorageOptions<T> {
17
+ crossInstanceSync?: boolean;
18
+ }
19
+ export interface UseLocalStorageReturn<T> {
20
+ value: T | undefined;
21
+ setValue: (valueOrUpdater: T | ((prev: T | undefined) => T)) => void;
22
+ removeValue: () => void;
23
+ isHydrated: boolean;
24
+ error: Error | null;
25
+ }
26
+ export declare function useLocalStorage<T = unknown>(key: string, initialValue?: T, options?: UseLocalStorageOptions<T>): UseLocalStorageReturn<T>;
27
+ export type UseSessionStorageOptions<T> = BaseStorageOptions<T>;
28
+ export interface UseSessionStorageReturn<T> {
29
+ value: T | undefined;
30
+ setValue: (valueOrUpdater: T | ((prev: T | undefined) => T)) => void;
31
+ removeValue: () => void;
32
+ isHydrated: boolean;
33
+ error: Error | null;
34
+ }
35
+ export declare function useSessionStorage<T = unknown>(key: string, initialValue?: T, options?: UseSessionStorageOptions<T>): UseSessionStorageReturn<T>;
36
+ export declare const defaultSerializer: StorageSerializer<unknown>;
37
+ export declare function mapSerializer<K, V>(): StorageSerializer<Map<K, V>>;
38
+ export declare function setSerializer<V>(): StorageSerializer<Set<V>>;
39
+ export declare const dateSerializer: StorageSerializer<Date>;
40
+ export declare const bigIntSerializer: StorageSerializer<bigint>;
41
+
42
+ export {};
@@ -0,0 +1,2 @@
1
+ import { a as e, i as t, n, o as r, r as i, s as a, t as o } from "./storage2.js";
2
+ export { i as bigIntSerializer, t as dateSerializer, e as defaultSerializer, r as mapSerializer, a as setSerializer, n as useLocalStorage, o as useSessionStorage };
@@ -0,0 +1,261 @@
1
+ import { t as e } from "./useEventListener.js";
2
+ import { useCallback as t, useEffect as n, useRef as r, useState as i } from "react";
3
+ //#region src/shared/storageShared/serializers.ts
4
+ var a = {
5
+ serialize: (e) => JSON.stringify(e),
6
+ deserialize: (e) => JSON.parse(e)
7
+ };
8
+ function o() {
9
+ return {
10
+ serialize: (e) => JSON.stringify(Array.from(e.entries())),
11
+ deserialize: (e) => new Map(JSON.parse(e))
12
+ };
13
+ }
14
+ function s() {
15
+ return {
16
+ serialize: (e) => JSON.stringify(Array.from(e.values())),
17
+ deserialize: (e) => new Set(JSON.parse(e))
18
+ };
19
+ }
20
+ var c = {
21
+ serialize: (e) => e.toISOString(),
22
+ deserialize: (e) => {
23
+ let t = new Date(e);
24
+ if (isNaN(t.getTime())) throw Error(`[react-kit] dateSerializer: invalid date string "${e}"`);
25
+ return t;
26
+ }
27
+ }, l = {
28
+ serialize: (e) => e.toString(),
29
+ deserialize: (e) => {
30
+ try {
31
+ return BigInt(e);
32
+ } catch {
33
+ throw Error(`[react-kit] bigIntSerializer: cannot convert "${e}" to bigint`);
34
+ }
35
+ }
36
+ }, u = "react-kit:use-local-storage";
37
+ //#endregion
38
+ //#region src/storage/useLocalStorage/utils.ts
39
+ function d() {
40
+ if (typeof window > "u") return null;
41
+ try {
42
+ return window.localStorage;
43
+ } catch {
44
+ return null;
45
+ }
46
+ }
47
+ function f(e, t, n, r) {
48
+ try {
49
+ let i = e.getItem(t);
50
+ return i === null ? {
51
+ value: n,
52
+ error: null
53
+ } : {
54
+ value: r.deserialize(i),
55
+ error: null
56
+ };
57
+ } catch (e) {
58
+ return {
59
+ value: n,
60
+ error: e instanceof Error ? e : Error(String(e))
61
+ };
62
+ }
63
+ }
64
+ function p(e, t, n) {
65
+ let r = {
66
+ value: t,
67
+ instanceId: n
68
+ }, i = new CustomEvent(`${u}:${e}`, { detail: r });
69
+ window.dispatchEvent(i);
70
+ }
71
+ //#endregion
72
+ //#region src/storage/useLocalStorage/useLocalStorage.ts
73
+ function m(o, s, c = {}) {
74
+ let { serializer: l = a, initializeWithValue: m = !0, sameInstanceSync: h = !0, crossInstanceSync: g = !0 } = c, [_] = i(() => o), v = r(_), y = o !== _, b = `${u}:${_}`, x = r(Symbol()), S = r(l);
75
+ n(() => {
76
+ S.current = l;
77
+ }, [l]);
78
+ let [C, w] = i(() => {
79
+ if (!m) return;
80
+ let e = d();
81
+ if (!e) return s;
82
+ let { value: t } = f(e, o, s, l);
83
+ return t;
84
+ }), T = r(C);
85
+ n(() => {
86
+ T.current = C;
87
+ }, [C]);
88
+ let [E, D] = i(m && typeof window < "u"), [O, k] = i(null);
89
+ n(() => {
90
+ if (!y) return;
91
+ let e = /* @__PURE__ */ Error(`[react-kit:use-local-storage] Changing the storage key at runtime is not supported. Still using original key: "${v.current}". Received new key: "${o}".`);
92
+ console.warn(e.message), k(e);
93
+ }, [o, y]), n(() => {
94
+ if (m) return;
95
+ let e = d(), { value: t, error: n } = e === null ? {
96
+ value: s,
97
+ error: null
98
+ } : f(e, v.current, s, S.current);
99
+ n && (console.warn(`[react-kit:use-local-storage] Failed to read key "${v.current}" from localStorage:`, n.message), k(n)), w(t), D(!0);
100
+ }, [m]);
101
+ let A = t((e) => {
102
+ let t = d();
103
+ if (!t) return;
104
+ let n = T.current, r = typeof e == "function" ? e(n) : e;
105
+ try {
106
+ let e = S.current.serialize(r);
107
+ t.setItem(v.current, e), w(r), k(null), h && p(v.current, e, x.current);
108
+ } catch (e) {
109
+ let t = e instanceof Error ? e : Error(String(e));
110
+ console.warn(`[react-kit:use-local-storage] Failed to write key "${v.current}" to localStorage:`, t.message), k(t);
111
+ }
112
+ }, [h]), j = t(() => {
113
+ let e = d();
114
+ e && (e.removeItem(v.current), w(s), k(null), h && p(v.current, null, x.current));
115
+ }, [h]);
116
+ return e("storage", (e) => {
117
+ if (g && e.storageArea === d() && e.key === v.current) {
118
+ if (e.newValue === null) {
119
+ w(s), k(null);
120
+ return;
121
+ }
122
+ try {
123
+ let t = S.current.deserialize(e.newValue);
124
+ w(t), k(null);
125
+ } catch (e) {
126
+ let t = e instanceof Error ? e : Error(String(e));
127
+ console.warn(`[react-kit:use-local-storage] Failed to deserialize cross-tab update for key "${v.current}":`, t.message), w(s), k(t);
128
+ }
129
+ }
130
+ }), e(b, (e) => {
131
+ if (!h) return;
132
+ let t = e.detail;
133
+ if (t.instanceId !== x.current) {
134
+ if (t.value === null) {
135
+ w(s), k(null);
136
+ return;
137
+ }
138
+ try {
139
+ let e = S.current.deserialize(t.value);
140
+ w(e), k(null);
141
+ } catch (e) {
142
+ let t = e instanceof Error ? e : Error(String(e));
143
+ console.warn(`[react-kit:use-local-storage] Failed to deserialize same-tab update for key "${v.current}":`, t.message), w(s), k(t);
144
+ }
145
+ }
146
+ }, { target: typeof window > "u" ? null : window }), {
147
+ value: C,
148
+ setValue: A,
149
+ removeValue: j,
150
+ isHydrated: E,
151
+ error: O
152
+ };
153
+ }
154
+ //#endregion
155
+ //#region src/storage/useSessionStorage/constants.ts
156
+ var h = "react-kit:use-session-storage";
157
+ //#endregion
158
+ //#region src/storage/useSessionStorage/utils.ts
159
+ function g() {
160
+ if (typeof window > "u") return null;
161
+ try {
162
+ return window.sessionStorage;
163
+ } catch {
164
+ return null;
165
+ }
166
+ }
167
+ function _(e, t, n, r) {
168
+ try {
169
+ let i = e.getItem(t);
170
+ return i === null ? {
171
+ value: n,
172
+ error: null
173
+ } : {
174
+ value: r.deserialize(i),
175
+ error: null
176
+ };
177
+ } catch (e) {
178
+ return {
179
+ value: n,
180
+ error: e instanceof Error ? e : Error(String(e))
181
+ };
182
+ }
183
+ }
184
+ function v(e, t, n) {
185
+ let r = {
186
+ value: t,
187
+ instanceId: n
188
+ }, i = new CustomEvent(`${h}:${e}`, { detail: r });
189
+ window.dispatchEvent(i);
190
+ }
191
+ //#endregion
192
+ //#region src/storage/useSessionStorage/useSessionStorage.ts
193
+ function y(o, s, c = {}) {
194
+ let { serializer: l = a, initializeWithValue: u = !0, sameInstanceSync: d = !0 } = c, [f] = i(() => o), p = r(f), m = o !== f, y = `${h}:${f}`, b = r(Symbol()), x = r(l);
195
+ n(() => {
196
+ x.current = l;
197
+ }, [l]);
198
+ let [S, C] = i(() => {
199
+ if (!u) return;
200
+ let e = g();
201
+ if (!e) return s;
202
+ let { value: t } = _(e, o, s, l);
203
+ return t;
204
+ }), w = r(S);
205
+ n(() => {
206
+ w.current = S;
207
+ }, [S]);
208
+ let [T, E] = i(u && typeof window < "u"), [D, O] = i(null);
209
+ n(() => {
210
+ if (!m) return;
211
+ let e = /* @__PURE__ */ Error(`[react-kit:use-session-storage] Changing the storage key at runtime is not supported. Still using original key: "${p.current}". Received new key: "${o}".`);
212
+ console.warn(e.message), O(e);
213
+ }, [o, m]), n(() => {
214
+ if (u) return;
215
+ let e = g(), { value: t, error: n } = e === null ? {
216
+ value: s,
217
+ error: null
218
+ } : _(e, p.current, s, x.current);
219
+ n && (console.warn(`[react-kit:use-session-storage] Failed to read key "${p.current}" from sessionStorage:`, n.message), O(n)), C(t), E(!0);
220
+ }, [u]);
221
+ let k = t((e) => {
222
+ let t = g();
223
+ if (!t) return;
224
+ let n = w.current, r = typeof e == "function" ? e(n) : e;
225
+ try {
226
+ let e = x.current.serialize(r);
227
+ t.setItem(p.current, e), C(r), O(null), d && v(p.current, e, b.current);
228
+ } catch (e) {
229
+ let t = e instanceof Error ? e : Error(String(e));
230
+ console.warn(`[react-kit:use-session-storage] Failed to write key "${p.current}" to sessionStorage:`, t.message), O(t);
231
+ }
232
+ }, [d]), A = t(() => {
233
+ let e = g();
234
+ e && (e.removeItem(p.current), C(s), O(null), d && v(p.current, null, b.current));
235
+ }, [d]);
236
+ return e(y, (e) => {
237
+ if (!d) return;
238
+ let t = e.detail;
239
+ if (t.instanceId !== b.current) {
240
+ if (t.value === null) {
241
+ C(s), O(null);
242
+ return;
243
+ }
244
+ try {
245
+ let e = x.current.deserialize(t.value);
246
+ C(e), O(null);
247
+ } catch (e) {
248
+ let t = e instanceof Error ? e : Error(String(e));
249
+ console.warn(`[react-kit:use-session-storage] Failed to deserialize same-tab update for key "${p.current}":`, t.message), C(s), O(t);
250
+ }
251
+ }
252
+ }, { target: typeof window > "u" ? null : window }), {
253
+ value: S,
254
+ setValue: k,
255
+ removeValue: A,
256
+ isHydrated: T,
257
+ error: D
258
+ };
259
+ }
260
+ //#endregion
261
+ export { a, c as i, m as n, o, l as r, s, y as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@himanshu-sorathiya/react-kit",
3
- "version": "1.0.24",
3
+ "version": "1.0.25",
4
4
  "description": "An opinionated collection of react hooks, and reusable UI components.",
5
5
  "type": "module",
6
6
  "module": "./dist/index.js",
@@ -29,11 +29,15 @@
29
29
  "types": "./dist/events.d.ts",
30
30
  "import": "./dist/events.js"
31
31
  },
32
+ "./storage": {
33
+ "types": "./dist/storage.d.ts",
34
+ "import": "./dist/storage.js"
35
+ },
32
36
  "./styles.css": "./dist/index.css"
33
37
  },
34
38
  "scripts": {
35
39
  "dev": "vite",
36
- "build": "vite build && dts-bundle-generator --project tsconfig.app.json -o dist/index.d.ts src/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/ui.d.ts src/ui/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/state.d.ts src/state/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/performance.d.ts src/performance/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/events.d.ts src/events/index.ts",
40
+ "build": "vite build && dts-bundle-generator --project tsconfig.app.json -o dist/index.d.ts src/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/ui.d.ts src/ui/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/state.d.ts src/state/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/performance.d.ts src/performance/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/events.d.ts src/events/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/storage.d.ts src/storage/index.ts",
37
41
  "lint": "eslint .",
38
42
  "preview": "vite preview",
39
43
  "format": "prettier . --write"