@adara-cs/hooks 1.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Adara Cloud
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,3 @@
1
+ # @adara-cs/hooks
2
+
3
+ Hooks for Adara UI Kit Web
@@ -0,0 +1,4 @@
1
+ export * from './useResize';
2
+ export * from './useIsomophicLayoutEffect';
3
+ export * from './useLatest';
4
+ export * from './useControlledState';
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ import { useResize as t } from "./useResize.js";
2
+ import { useIsomorphicLayoutEffect as s } from "./useIsomophicLayoutEffect.js";
3
+ import { useLatest as m } from "./useLatest.js";
4
+ import { useControlledState as u } from "./useControlledState.js";
5
+ export {
6
+ u as useControlledState,
7
+ s as useIsomorphicLayoutEffect,
8
+ m as useLatest,
9
+ t as useResize
10
+ };
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
@@ -0,0 +1,2 @@
1
+ export type UseControlled = <T>(value: T | undefined, defaultValue: T, onChange?: ((v: T, ...args: never[]) => void) | undefined) => [T, (value: T) => void];
2
+ export declare const useControlledState: UseControlled;
@@ -0,0 +1,18 @@
1
+ import { useState as a, useCallback as l } from "react";
2
+ import { useLatest as f } from "./useLatest.js";
3
+ const S = (t, n, c) => {
4
+ const [u, d] = a(
5
+ t !== void 0 ? t : n
6
+ ), s = t !== void 0, e = f(c), i = l(
7
+ (o) => {
8
+ var r;
9
+ s || d(o), (r = e.current) == null || r.call(e, o);
10
+ },
11
+ [s, e]
12
+ );
13
+ return [s ? t : u, i];
14
+ };
15
+ export {
16
+ S as useControlledState
17
+ };
18
+ //# sourceMappingURL=useControlledState.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useControlledState.js","sources":["../src/useControlledState.ts"],"sourcesContent":["import { useCallback, useState } from 'react';\r\nimport { useLatest } from './useLatest';\r\n\r\nexport type UseControlled = <T>(\r\n value: T | undefined,\r\n defaultValue: T,\r\n onChange?: ((v: T, ...args: never[]) => void) | undefined\r\n) => [T, (value: T) => void]\r\n\r\nexport const useControlledState: UseControlled = <T>(\r\n value: T | undefined,\r\n defaultValue: T,\r\n onChange?: (v: T, ...args: never[]) => void,\r\n): [T, (value: T) => void] => {\r\n const [stateValue, setStateValue] = useState(\r\n value !== undefined ? value : defaultValue,\r\n )\r\n const isControlled = value !== undefined\r\n\r\n const onChangeRef = useLatest(onChange)\r\n const setValue = useCallback(\r\n (newValue: T) => {\r\n if (!isControlled) {\r\n setStateValue(newValue)\r\n }\r\n onChangeRef.current?.(newValue)\r\n },\r\n [isControlled, onChangeRef],\r\n )\r\n\r\n return [isControlled ? value : stateValue, setValue]\r\n}\r\n"],"names":["useControlledState","value","defaultValue","onChange","stateValue","setStateValue","useState","isControlled","onChangeRef","useLatest","setValue","useCallback","newValue","_a"],"mappings":";;AASO,MAAMA,IAAoC,CAC/CC,GACAC,GACAC,MAC4B;AACtB,QAAA,CAACC,GAAYC,CAAa,IAAIC;AAAA,IAClCL,MAAU,SAAYA,IAAQC;AAAA,EAChC,GACMK,IAAeN,MAAU,QAEzBO,IAAcC,EAAUN,CAAQ,GAChCO,IAAWC;AAAA,IACf,CAACC,MAAgB;;AACf,MAAKL,KACHF,EAAcO,CAAQ,IAExBC,IAAAL,EAAY,YAAZ,QAAAK,EAAA,KAAAL,GAAsBI;AAAA,IACxB;AAAA,IACA,CAACL,GAAcC,CAAW;AAAA,EAC5B;AAEA,SAAO,CAACD,IAAeN,IAAQG,GAAYM,CAAQ;AACrD;"}
@@ -0,0 +1,2 @@
1
+ import { useEffect } from 'react';
2
+ export declare const useIsomorphicLayoutEffect: typeof useEffect;
@@ -0,0 +1,6 @@
1
+ import { useLayoutEffect as e, useEffect as o } from "react";
2
+ const t = typeof window > "u" ? e : o;
3
+ export {
4
+ t as useIsomorphicLayoutEffect
5
+ };
6
+ //# sourceMappingURL=useIsomophicLayoutEffect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useIsomophicLayoutEffect.js","sources":["../src/useIsomophicLayoutEffect.ts"],"sourcesContent":["import { useEffect, useLayoutEffect } from 'react';\r\n\r\nexport const useIsomorphicLayoutEffect = typeof window === 'undefined' ? useLayoutEffect : useEffect"],"names":["useIsomorphicLayoutEffect","useLayoutEffect","useEffect"],"mappings":";AAEO,MAAMA,IAA4B,OAAO,SAAW,MAAcC,IAAkBC;"}
@@ -0,0 +1,2 @@
1
+ import { MutableRefObject } from 'react';
2
+ export declare const useLatest: <T>(value: T) => MutableRefObject<T>;
@@ -0,0 +1,12 @@
1
+ import { useRef as o } from "react";
2
+ import { useIsomorphicLayoutEffect as e } from "./useIsomophicLayoutEffect.js";
3
+ const c = (r) => {
4
+ const t = o(r);
5
+ return e(() => {
6
+ t.current = r;
7
+ }, [r]), t;
8
+ };
9
+ export {
10
+ c as useLatest
11
+ };
12
+ //# sourceMappingURL=useLatest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useLatest.js","sources":["../src/useLatest.ts"],"sourcesContent":["import type { MutableRefObject } from 'react'\r\nimport { useRef } from 'react'\r\nimport { useIsomorphicLayoutEffect } from './useIsomophicLayoutEffect.ts'\r\n\r\nexport const useLatest = <T>(value: T): MutableRefObject<T> => {\r\n const ref = useRef(value)\r\n\r\n useIsomorphicLayoutEffect(() => {\r\n ref.current = value\r\n }, [value])\r\n\r\n return ref\r\n}"],"names":["useLatest","value","ref","useRef","useIsomorphicLayoutEffect"],"mappings":";;AAIa,MAAAA,IAAY,CAAIC,MAAkC;AACvD,QAAAC,IAAMC,EAAOF,CAAK;AAExB,SAAAG,EAA0B,MAAM;AAC9B,IAAAF,EAAI,UAAUD;AAAA,EAAA,GACb,CAACA,CAAK,CAAC,GAEHC;AACT;"}
@@ -0,0 +1,4 @@
1
+ export declare const useResize: () => {
2
+ height: number;
3
+ width: number;
4
+ };
@@ -0,0 +1,16 @@
1
+ import { useState as o, useEffect as w } from "react";
2
+ const h = () => {
3
+ const [e, r] = o(typeof window < "u" ? window.innerHeight : 0), [n, d] = o(typeof window < "u" ? window.innerWidth : 0);
4
+ return w(() => {
5
+ const t = new AbortController(), i = () => {
6
+ d(window.innerWidth), r(window.innerHeight);
7
+ };
8
+ return (e === 0 || n === 0) && i(), window.addEventListener("resize", i, { signal: t.signal }), () => {
9
+ t.abort();
10
+ };
11
+ }, []), { height: e, width: n };
12
+ };
13
+ export {
14
+ h as useResize
15
+ };
16
+ //# sourceMappingURL=useResize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useResize.js","sources":["../src/useResize.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\r\n\r\nexport const useResize = () => {\r\n const [height, setHeight] = useState<number>(typeof window !== 'undefined' ? window.innerHeight : 0);\r\n const [width, setWidth] = useState<number>(typeof window !== 'undefined' ? window.innerWidth : 0)\r\n\r\n useEffect(() => {\r\n const controller = new AbortController()\r\n\r\n const onChange = () => {\r\n setWidth(window.innerWidth)\r\n setHeight(window.innerHeight)\r\n }\r\n\r\n if (height === 0 || width === 0) onChange()\r\n\r\n window.addEventListener('resize', onChange, { signal: controller.signal })\r\n\r\n return () => {\r\n controller.abort()\r\n }\r\n }, [])\r\n\r\n return { height, width }\r\n}"],"names":["useResize","height","setHeight","useState","width","setWidth","useEffect","controller","onChange"],"mappings":";AAEO,MAAMA,IAAY,MAAM;AACvB,QAAA,CAACC,GAAQC,CAAS,IAAIC,EAAiB,OAAO,SAAW,MAAc,OAAO,cAAc,CAAC,GAC7F,CAACC,GAAOC,CAAQ,IAAIF,EAAiB,OAAO,SAAW,MAAc,OAAO,aAAa,CAAC;AAEhG,SAAAG,EAAU,MAAM;AACR,UAAAC,IAAa,IAAI,gBAAgB,GAEjCC,IAAW,MAAM;AACrB,MAAAH,EAAS,OAAO,UAAU,GAC1BH,EAAU,OAAO,WAAW;AAAA,IAC9B;AAEA,YAAID,MAAW,KAAKG,MAAU,MAAYI,EAAA,GAE1C,OAAO,iBAAiB,UAAUA,GAAU,EAAE,QAAQD,EAAW,QAAQ,GAElE,MAAM;AACX,MAAAA,EAAW,MAAM;AAAA,IACnB;AAAA,EACF,GAAG,EAAE,GAEE,EAAE,QAAAN,GAAQ,OAAAG,EAAM;AACzB;"}
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@adara-cs/hooks",
3
+ "version": "1.1.1",
4
+ "description": "AdaraCloud UI Kit for Web",
5
+ "license": "MIT",
6
+ "keywords": [
7
+ "ui-kit",
8
+ "ui",
9
+ "ts",
10
+ "typescript",
11
+ "js"
12
+ ],
13
+ "author": "adara-cs",
14
+ "bugs": {
15
+ "url": "https://github.com/adara-cs/ui-kit-web/issues"
16
+ },
17
+ "homepage": "https://github.com/adara-cs/ui-kit-web#readme",
18
+ "type": "module",
19
+ "main": "./src/index.ts",
20
+ "types": "./src/index.d.ts",
21
+ "exports": {
22
+ ".": {
23
+ "source": "./src/index.ts",
24
+ "import": {
25
+ "types": "./dist/index.d.ts",
26
+ "default": "./dist/index.js"
27
+ }
28
+ }
29
+ },
30
+ "files": [
31
+ "dist/*"
32
+ ],
33
+ "peerDependencies": {
34
+ "react": "^18.3.1",
35
+ "react-dom": "^18.3.1"
36
+ },
37
+ "dependencies": {
38
+ "react": "^18.3.1"
39
+ },
40
+ "devDependencies": {
41
+ "@testing-library/react": "^16.1.0",
42
+ "@types/react": "^18.3.12",
43
+ "@types/react-dom": "^18.3.1",
44
+ "fast-glob": "^3.3.3",
45
+ "rollup-preserve-directives": "^1.1.3",
46
+ "vite": "^6.0.1",
47
+ "vite-plugin-dts": "^4.3.0",
48
+ "vite-plugin-svgr": "^4.3.0",
49
+ "@vitejs/plugin-react": "^4.3.4"
50
+ },
51
+ "config": {
52
+ "commitizen": {
53
+ "path": "git-cz"
54
+ }
55
+ },
56
+ "eslintConfig": {
57
+ "extends": [
58
+ "plugin:storybook/recommended"
59
+ ]
60
+ },
61
+ "publishConfig": {
62
+ "access": "public"
63
+ },
64
+ "scripts": {
65
+ "build": "vite build",
66
+ "check-types": "tsc --noEmit"
67
+ }
68
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './useResize'
2
+ export * from './useIsomophicLayoutEffect'
3
+ export * from './useLatest'
4
+ export * from './useControlledState'