@kalink-ui/seedly 0.15.0 → 0.16.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @kalink-ui/seedly
2
2
 
3
+ ## 0.16.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 5af6427: [useLocalStorage] Move hook in the dibbly package
8
+
3
9
  ## 0.15.0
4
10
 
5
11
  ### Minor Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kalink-ui/seedly",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
4
4
  "description": "A set of components for building UIs with React and TypeScript",
5
5
  "sideEffects": false,
6
6
  "license": "MIT",
@@ -47,7 +47,7 @@
47
47
  "vite": "^6.2.1",
48
48
  "vite-tsconfig-paths": "^5.1.4",
49
49
  "vitest": "^3.0.8",
50
- "@kalink-ui/dibbly": "0.4.0",
50
+ "@kalink-ui/dibbly": "0.5.0",
51
51
  "@kalink-ui/eslint-config": "0.9.0",
52
52
  "@kalink-ui/typescript-config": "0.4.0"
53
53
  },
package/src/index.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  export * from './components';
2
2
  export * from './utils';
3
- export * from './hooks';
@@ -1 +0,0 @@
1
- export { useLocalStorage } from './use-local-storage';
@@ -1,99 +0,0 @@
1
- 'use client';
2
-
3
- import { useCallback, useEffect, useSyncExternalStore } from 'react';
4
-
5
- type Serializable =
6
- | string
7
- | number
8
- | boolean
9
- | null
10
- | Serializable[]
11
- | { [key: string]: Serializable };
12
-
13
- type StateUpdater<T> = (oldValue: T) => T;
14
-
15
- function dispatchStorageEvent(key: string, newValue: string | null) {
16
- window.dispatchEvent(new StorageEvent('storage', { key, newValue }));
17
- }
18
-
19
- function subscribeToStorageEvent(callback: () => void) {
20
- window.addEventListener('storage', callback);
21
-
22
- return () => window.removeEventListener('storage', callback);
23
- }
24
-
25
- function setLocalStorage<T>(key: string, value: T) {
26
- const stringifiedValue = JSON.stringify(value);
27
-
28
- window.localStorage.setItem(key, stringifiedValue);
29
-
30
- dispatchStorageEvent(key, stringifiedValue);
31
- }
32
-
33
- function removeLocalStorage(key: string) {
34
- window.localStorage.removeItem(key);
35
-
36
- dispatchStorageEvent(key, null);
37
- }
38
-
39
- function getLocalStorage(key: string) {
40
- return window.localStorage.getItem(key);
41
- }
42
-
43
- function getLocalStorageServerSnapshot<T>(initialValue: T) {
44
- const initialSnapshot = JSON.stringify(initialValue);
45
-
46
- return () => initialSnapshot;
47
- }
48
-
49
- const cachedStore = new Map<string, Serializable>();
50
-
51
- export function useLocalStorage<T extends Serializable>(
52
- key: string,
53
- initialValue: T,
54
- ): [T, (value: T | StateUpdater<T>) => void] {
55
- const getSnapshot = () => getLocalStorage(key);
56
-
57
- const store = useSyncExternalStore(
58
- subscribeToStorageEvent,
59
- getSnapshot,
60
- getLocalStorageServerSnapshot(initialValue),
61
- );
62
-
63
- const setValue = useCallback(
64
- (value: T | StateUpdater<T>) => {
65
- try {
66
- const newValue =
67
- typeof value === 'function'
68
- ? value(store ? JSON.parse(store) : null)
69
- : value;
70
-
71
- if (newValue === undefined || newValue === null) {
72
- removeLocalStorage(key);
73
- } else {
74
- setLocalStorage(key, newValue);
75
- }
76
- } catch (error) {
77
- console.error(error);
78
- }
79
- },
80
- [key, store],
81
- );
82
-
83
- useEffect(() => {
84
- if (getLocalStorage(key) === null && typeof initialValue !== 'undefined') {
85
- setLocalStorage(key, initialValue);
86
- }
87
- }, [key, initialValue]);
88
-
89
- if (!store) {
90
- return [initialValue, setValue];
91
- }
92
-
93
- if (!cachedStore.has(store)) {
94
- cachedStore.clear();
95
- cachedStore.set(store, JSON.parse(store));
96
- }
97
-
98
- return [cachedStore.get(store) as T, setValue];
99
- }