@versini/ui-hooks 2.2.0 → 3.0.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/README.md +2 -26
- package/dist/hooks/useLocalStorage.js +82 -50
- package/dist/index.d.ts +2 -38
- package/dist/index.js +7 -10
- package/package.json +11 -8
- package/dist/hooks/useEventCallback.js +0 -12
- package/dist/hooks/useEventListener.js +0 -18
package/README.md
CHANGED
|
@@ -1,27 +1,3 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @versini/ui-hooks
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Currently, two official plugins are available:
|
|
6
|
-
|
|
7
|
-
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
|
|
8
|
-
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
|
|
9
|
-
|
|
10
|
-
## Expanding the ESLint configuration
|
|
11
|
-
|
|
12
|
-
If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:
|
|
13
|
-
|
|
14
|
-
- Configure the top-level `parserOptions` property like this:
|
|
15
|
-
|
|
16
|
-
```js
|
|
17
|
-
parserOptions: {
|
|
18
|
-
ecmaVersion: 'latest',
|
|
19
|
-
sourceType: 'module',
|
|
20
|
-
project: ['./tsconfig.json', './tsconfig.node.json'],
|
|
21
|
-
tsconfigRootDir: __dirname,
|
|
22
|
-
},
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked`
|
|
26
|
-
- Optionally add `plugin:@typescript-eslint/stylistic-type-checked`
|
|
27
|
-
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list
|
|
3
|
+
Hooks to be used in the UI-Components library.
|
|
@@ -1,59 +1,91 @@
|
|
|
1
|
-
import { useCallback as u, useState as
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
function
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
1
|
+
import { useCallback as u, useState as F, useEffect as g, useRef as h, useLayoutEffect as I } from "react";
|
|
2
|
+
var m = typeof window < "u" ? I : g;
|
|
3
|
+
function S(r, e, t, c) {
|
|
4
|
+
const d = h(e);
|
|
5
|
+
m(() => {
|
|
6
|
+
d.current = e;
|
|
7
|
+
}, [e]), g(() => {
|
|
8
|
+
const a = window;
|
|
9
|
+
if (!(a && a.addEventListener))
|
|
10
|
+
return;
|
|
11
|
+
const s = (f) => {
|
|
12
|
+
d.current(f);
|
|
13
|
+
};
|
|
14
|
+
return a.addEventListener(r, s, c), () => {
|
|
15
|
+
a.removeEventListener(r, s, c);
|
|
16
|
+
};
|
|
17
|
+
}, [r, t, c]);
|
|
18
|
+
}
|
|
19
|
+
function v(r) {
|
|
20
|
+
const e = h(() => {
|
|
21
|
+
throw new Error("Cannot call an event handler while rendering.");
|
|
22
|
+
});
|
|
23
|
+
return m(() => {
|
|
24
|
+
e.current = r;
|
|
25
|
+
}, [r]), u((...t) => {
|
|
26
|
+
var c;
|
|
27
|
+
return (c = e.current) == null ? void 0 : c.call(e, ...t);
|
|
28
|
+
}, [e]);
|
|
29
|
+
}
|
|
30
|
+
var l = typeof window > "u";
|
|
31
|
+
function $(r, e, t = {}) {
|
|
32
|
+
const { initializeWithValue: c = !0 } = t, d = u(
|
|
33
|
+
(n) => t.serializer ? t.serializer(n) : JSON.stringify(n),
|
|
34
|
+
[t]
|
|
35
|
+
), a = u(
|
|
36
|
+
(n) => {
|
|
37
|
+
if (t.deserializer)
|
|
38
|
+
return t.deserializer(n);
|
|
39
|
+
if (n === "undefined")
|
|
40
|
+
return;
|
|
41
|
+
const o = e instanceof Function ? e() : e;
|
|
42
|
+
let E;
|
|
43
|
+
try {
|
|
44
|
+
E = JSON.parse(n);
|
|
45
|
+
} catch (L) {
|
|
46
|
+
return console.error("Error parsing JSON:", L), o;
|
|
47
|
+
}
|
|
48
|
+
return E;
|
|
49
|
+
},
|
|
50
|
+
[t, e]
|
|
51
|
+
), s = u(() => {
|
|
52
|
+
const n = e instanceof Function ? e() : e;
|
|
53
|
+
if (l)
|
|
54
|
+
return n;
|
|
34
55
|
try {
|
|
35
|
-
const
|
|
36
|
-
return
|
|
37
|
-
} catch (
|
|
38
|
-
return console.warn(`Error reading localStorage key “${
|
|
56
|
+
const o = window.localStorage.getItem(r);
|
|
57
|
+
return o ? a(o) : n;
|
|
58
|
+
} catch (o) {
|
|
59
|
+
return console.warn(`Error reading localStorage key “${r}”:`, o), n;
|
|
39
60
|
}
|
|
40
|
-
}, [
|
|
61
|
+
}, [e, r, a]), [f, i] = F(() => c ? s() : e instanceof Function ? e() : e), C = v((n) => {
|
|
62
|
+
l && console.warn(
|
|
63
|
+
`Tried setting localStorage key “${r}” even though environment is not a client`
|
|
64
|
+
);
|
|
41
65
|
try {
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
} catch (e) {
|
|
47
|
-
console.warn(`Error setting localStorage key “${t}”:`, e);
|
|
66
|
+
const o = n instanceof Function ? n(s()) : n;
|
|
67
|
+
window.localStorage.setItem(r, d(o)), i(o), window.dispatchEvent(new StorageEvent("local-storage", { key: r }));
|
|
68
|
+
} catch (o) {
|
|
69
|
+
console.warn(`Error setting localStorage key “${r}”:`, o);
|
|
48
70
|
}
|
|
49
|
-
}),
|
|
50
|
-
|
|
51
|
-
|
|
71
|
+
}), z = v(() => {
|
|
72
|
+
l && console.warn(
|
|
73
|
+
`Tried removing localStorage key “${r}” even though environment is not a client`
|
|
74
|
+
);
|
|
75
|
+
const n = e instanceof Function ? e() : e;
|
|
76
|
+
window.localStorage.removeItem(r), i(n), window.dispatchEvent(new StorageEvent("local-storage", { key: r }));
|
|
77
|
+
});
|
|
78
|
+
g(() => {
|
|
79
|
+
i(s());
|
|
80
|
+
}, [r]);
|
|
81
|
+
const w = u(
|
|
82
|
+
(n) => {
|
|
83
|
+
n.key && n.key !== r || i(s());
|
|
52
84
|
},
|
|
53
|
-
[
|
|
85
|
+
[r, s]
|
|
54
86
|
);
|
|
55
|
-
return
|
|
87
|
+
return S("storage", w), S("local-storage", w), [f, C, z];
|
|
56
88
|
}
|
|
57
89
|
export {
|
|
58
|
-
|
|
90
|
+
$ as useLocalStorage
|
|
59
91
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,40 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
declare global {
|
|
3
|
-
interface WindowEventMap {
|
|
4
|
-
[CUSTOM_EVENT_NAME]: CustomEvent;
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
interface StorageProperties<T> {
|
|
8
|
-
/**
|
|
9
|
-
* Storage key.
|
|
10
|
-
*/
|
|
11
|
-
key: string;
|
|
12
|
-
/**
|
|
13
|
-
* Default value that will be set if value is not found in storage.
|
|
14
|
-
*/
|
|
15
|
-
defaultValue?: T;
|
|
16
|
-
/**
|
|
17
|
-
* Function to serialize value into string to be saved in storage.
|
|
18
|
-
*/
|
|
19
|
-
serialize?: (value: T) => string;
|
|
20
|
-
/**
|
|
21
|
-
* Function to deserialize string value from storage to value.
|
|
22
|
-
*/
|
|
23
|
-
deserialize?: (value: string | undefined) => T;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* import { useLocalStorage } from '@versini/ui-hooks';
|
|
29
|
-
* const [value, setValue] = useLocalStorage({
|
|
30
|
-
* key: 'gpt-model',
|
|
31
|
-
* defaultValue: 'gpt-3',
|
|
32
|
-
* });
|
|
33
|
-
*
|
|
34
|
-
* setValue('gpt-4');
|
|
35
|
-
* setValue((current) => (current === 'gpt-3' ? 'gpt-4' : 'gpt-3'));
|
|
36
|
-
*/
|
|
37
|
-
declare function useLocalStorage<T = string>({ key, defaultValue, deserialize, serialize, }: StorageProperties<T>): [T, (val: T | ((prevState: T) => T)) => void, () => void];
|
|
1
|
+
export { useLocalStorage } from 'usehooks-ts';
|
|
38
2
|
|
|
39
3
|
/**
|
|
40
4
|
* React utility to merge refs.
|
|
@@ -129,4 +93,4 @@ type UseUniqueIdOptions = string | number | {
|
|
|
129
93
|
};
|
|
130
94
|
declare function useUniqueId(options?: UseUniqueIdOptions): string | undefined;
|
|
131
95
|
|
|
132
|
-
export {
|
|
96
|
+
export { useMergeRefs, useUncontrolled, useUniqueId };
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
|
+
import { useMergeRefs as r } from "./hooks/useMergeRefs.js";
|
|
2
|
+
import { useUncontrolled as f } from "./hooks/useUncontrolled.js";
|
|
3
|
+
import { useUniqueId as u } from "./hooks/useUniqueId.js";
|
|
1
4
|
import { useLocalStorage as p } from "./hooks/useLocalStorage.js";
|
|
2
|
-
import { useMergeRefs as s } from "./hooks/useMergeRefs.js";
|
|
3
|
-
import { useUncontrolled as i } from "./hooks/useUncontrolled.js";
|
|
4
|
-
import { useUniqueId as l } from "./hooks/useUniqueId.js";
|
|
5
|
-
import "react";
|
|
6
|
-
import "./hooks/useEventCallback.js";
|
|
7
|
-
import "./hooks/useEventListener.js";
|
|
8
5
|
/*!
|
|
9
|
-
@versini/ui-hooks
|
|
6
|
+
@versini/ui-hooks v3.0.0
|
|
10
7
|
© 2024 gizmette.com
|
|
11
8
|
*/
|
|
12
9
|
export {
|
|
13
10
|
p as useLocalStorage,
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
r as useMergeRefs,
|
|
12
|
+
f as useUncontrolled,
|
|
13
|
+
u as useUniqueId
|
|
17
14
|
};
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@versini/ui-hooks",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Arno Versini",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
8
8
|
},
|
|
9
|
-
"homepage": "https://github.com/aversini/ui-components
|
|
9
|
+
"homepage": "https://github.com/aversini/ui-components",
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
12
|
"url": "git@github.com:aversini/ui-components.git"
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"dev:js": "vite build --watch --mode development",
|
|
27
27
|
"dev:types": "tsup --watch src",
|
|
28
28
|
"dev": "npm-run-all clean --parallel dev:js dev:types",
|
|
29
|
-
"lint": "
|
|
29
|
+
"lint": "biome lint src",
|
|
30
30
|
"start": "static-server dist --port 5173",
|
|
31
31
|
"test:coverage:ui": "vitest --coverage --ui",
|
|
32
32
|
"test:coverage": "vitest run --coverage",
|
|
@@ -34,12 +34,15 @@
|
|
|
34
34
|
"test": "vitest run"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"react": "^18.
|
|
38
|
-
"react-dom": "^18.
|
|
37
|
+
"react": "^18.3.1",
|
|
38
|
+
"react-dom": "^18.3.1"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"react": "18.
|
|
42
|
-
"react-dom": "18.
|
|
41
|
+
"react": "18.3.1",
|
|
42
|
+
"react-dom": "18.3.1"
|
|
43
43
|
},
|
|
44
|
-
"
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"usehooks-ts": "3.1.0"
|
|
46
|
+
},
|
|
47
|
+
"gitHead": "e3e5df0ce39be11620614a8f47de214155b589f5"
|
|
45
48
|
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { useRef as t, useEffect as a, useCallback as c } from "react";
|
|
2
|
-
function o(r) {
|
|
3
|
-
const e = t(() => {
|
|
4
|
-
throw new Error("Cannot call an event handler while rendering.");
|
|
5
|
-
});
|
|
6
|
-
return a(() => {
|
|
7
|
-
e.current = r;
|
|
8
|
-
}, [r]), c((...n) => e.current(...n), [e]);
|
|
9
|
-
}
|
|
10
|
-
export {
|
|
11
|
-
o as useEventCallback
|
|
12
|
-
};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { useRef as E, useEffect as i } from "react";
|
|
2
|
-
function o(n, c, r, e) {
|
|
3
|
-
const s = E(c);
|
|
4
|
-
i(() => {
|
|
5
|
-
s.current = c;
|
|
6
|
-
}, [c]), i(() => {
|
|
7
|
-
const t = (r == null ? void 0 : r.current) ?? window;
|
|
8
|
-
if (!(t && t.addEventListener))
|
|
9
|
-
return;
|
|
10
|
-
const u = (f) => s.current(f);
|
|
11
|
-
return t.addEventListener(n, u, e), () => {
|
|
12
|
-
t.removeEventListener(n, u, e);
|
|
13
|
-
};
|
|
14
|
-
}, [n, r, e]);
|
|
15
|
-
}
|
|
16
|
-
export {
|
|
17
|
-
o as useEventListener
|
|
18
|
-
};
|