@versini/ui-hooks 1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Arno Versini
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # React + TypeScript + Vite
2
+
3
+ This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
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
@@ -0,0 +1,12 @@
1
+ import { useMemo as t } from "react";
2
+ function o(r) {
3
+ return t(() => r.every((e) => e == null) ? () => {
4
+ } : (e) => {
5
+ r.forEach((n) => {
6
+ typeof n == "function" ? n(e) : n != null && (n.current = e);
7
+ });
8
+ }, r);
9
+ }
10
+ export {
11
+ o as useMergeRefs
12
+ };
@@ -0,0 +1,18 @@
1
+ import { useState as s } from "react";
2
+ function i({
3
+ value: r,
4
+ defaultValue: o,
5
+ finalValue: l,
6
+ onChange: t = () => {
7
+ }
8
+ }) {
9
+ const [c, d] = s(
10
+ o !== void 0 ? o : l
11
+ ), n = (e) => {
12
+ d(e), t == null || t(e);
13
+ };
14
+ return r !== void 0 ? [r, t, !0] : [c, n, !1];
15
+ }
16
+ export {
17
+ i as useUncontrolled
18
+ };
@@ -0,0 +1,15 @@
1
+ import { useId as u } from "react";
2
+ function i(e) {
3
+ const r = u();
4
+ if (!e)
5
+ return r;
6
+ if (typeof e == "number" || typeof e == "string")
7
+ return `${e}${r}`;
8
+ if (typeof e == "object") {
9
+ const { id: t, prefix: f = "" } = e;
10
+ return typeof t == "number" || typeof t == "string" ? `${f}${t}` : `${f}${r}`;
11
+ }
12
+ }
13
+ export {
14
+ i as useUniqueId
15
+ };
@@ -0,0 +1,94 @@
1
+ /**
2
+ * React utility to merge refs.
3
+ *
4
+ * When developing low level UI components, it is common to have to use a local
5
+ * ref but also support an external one using React.forwardRef. Natively, React
6
+ * does not offer a way to set two refs inside the ref property.
7
+ *
8
+ * @param Array of refs (object, function, etc.)
9
+ *
10
+ * @example
11
+ *
12
+ * const Example = React.forwardRef(function Example(props, ref) {
13
+ * const localRef = React.useRef();
14
+ * const mergedRefs = useMergeRefs([localRef, ref]);
15
+ *
16
+ * return <div ref={mergedRefs} />;
17
+ * });
18
+ *
19
+ */
20
+ declare function useMergeRefs<T = any>(refs: Array<React.MutableRefObject<T> | React.LegacyRef<T> | undefined | null>): React.RefCallback<T>;
21
+
22
+ /**
23
+ * MIT License
24
+ *
25
+ * Copyright (c) 2021 Vitaly Rtishchev
26
+ *
27
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
28
+ * of this software and associated documentation files (the "Software"), to deal
29
+ * in the Software without restriction, including without limitation the rights
30
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
31
+ * copies of the Software, and to permit persons to whom the Software is
32
+ * furnished to do so, subject to the following conditions:
33
+ *
34
+ * The above copyright notice and this permission notice shall be included in all
35
+ * copies or substantial portions of the Software.
36
+ *
37
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
43
+ * SOFTWARE.
44
+ */
45
+ interface UseUncontrolledInput<T> {
46
+ /** Value for controlled state */
47
+ value?: T;
48
+ /** Initial value for uncontrolled state */
49
+ defaultValue?: T;
50
+ /** Final value for uncontrolled state when value and defaultValue are not provided */
51
+ finalValue?: T;
52
+ /** Controlled state onChange handler */
53
+ onChange?: (value: T) => void;
54
+ }
55
+ declare function useUncontrolled<T>({ value, defaultValue, finalValue, onChange, }: UseUncontrolledInput<T>): [T, (value: T) => void, boolean];
56
+
57
+ /**
58
+ * Hook that generates a unique id that will retain its value
59
+ * during the lifecycle of the calling functional component.
60
+ *
61
+ * The parameters are either
62
+ * - nothing: a unique id will simply be generated with no prefix.
63
+ * - a string or a number: a prefix to prepend to the generated id.
64
+ * - an object with the following props:
65
+ * - id: if this is a number or a string, it will be returned as is
66
+ * - prefix: prefix to prepend to the generated id.
67
+ *
68
+ * @param {string | number | object} options
69
+ * @returns a unique id
70
+ *
71
+ * @examples
72
+ *
73
+ * const someId = useUniqueId();
74
+ * // -> someId = "1j3h4f5"
75
+ *
76
+ * const errorId = useUniqueId("av-text-input-error-");
77
+ * // -> errorId = "av-text-input-error-1j3h4f5"
78
+ *
79
+ * const inputId = useUniqueId({ id: 42, prefix: "av-text-input-" });
80
+ * // -> inputId = "av-text-input-42"
81
+ *
82
+ * const inputHintId = useUniqueId({
83
+ * prefix: "av-text-input-hint-",
84
+ * });
85
+ * // -> inputHintId = "av-text-input-hint-1j3h4f5"
86
+ *
87
+ */
88
+ type UseUniqueIdOptions = string | number | {
89
+ id?: string | number;
90
+ prefix?: string;
91
+ };
92
+ declare function useUniqueId(options?: UseUniqueIdOptions): string | undefined;
93
+
94
+ export { useMergeRefs, useUncontrolled, useUniqueId };
package/dist/index.js ADDED
@@ -0,0 +1,13 @@
1
+ import { useMergeRefs as t } from "./hooks/useMergeRefs.js";
2
+ import { useUncontrolled as m } from "./hooks/useUncontrolled.js";
3
+ import { useUniqueId as s } from "./hooks/useUniqueId.js";
4
+ import "react";
5
+ /*!
6
+ @versini/ui-hooks v1.0.0
7
+ © 2024 gizmette.com
8
+ */
9
+ export {
10
+ t as useMergeRefs,
11
+ m as useUncontrolled,
12
+ s as useUniqueId
13
+ };
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@versini/ui-hooks",
3
+ "version": "1.0.0",
4
+ "license": "MIT",
5
+ "author": "Arno Versini",
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "homepage": "https://github.com/aversini/ui-components/packages/ui-hooks",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git@github.com:aversini/ui-components.git"
13
+ },
14
+ "type": "module",
15
+ "main": "dist/index.js",
16
+ "types": "dist/index.d.ts",
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "scripts": {
21
+ "build:check": "tsc",
22
+ "build:js": "vite build",
23
+ "build:types": "tsup",
24
+ "build": "npm-run-all --serial clean build:check build:js build:types",
25
+ "clean": "rimraf dist",
26
+ "dev:js": "vite build --watch --mode development",
27
+ "dev:types": "tsup --watch src",
28
+ "dev": "npm-run-all clean --parallel dev:js dev:types",
29
+ "lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0 --fix --color",
30
+ "start": "static-server dist --port 5173",
31
+ "test:coverage:ui": "vitest --coverage --ui",
32
+ "test:coverage": "vitest run --coverage",
33
+ "test:watch": "vitest",
34
+ "test": "vitest run"
35
+ },
36
+ "peerDependencies": {
37
+ "react": "^18.2.0",
38
+ "react-dom": "^18.2.0"
39
+ },
40
+ "devDependencies": {
41
+ "react": "18.2.0",
42
+ "react-dom": "18.2.0"
43
+ },
44
+ "gitHead": "b3908b5958fb8dab384172191bab101229be48bd"
45
+ }