@ox0/hooks 0.1.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/dist/index.js +92 -0
- package/package.json +26 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
|
|
2
|
+
function useControllableState(options) {
|
|
3
|
+
const { defaultValue, onChange, value } = options;
|
|
4
|
+
const [uncontrolledValue, setUncontrolledValue] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(defaultValue);
|
|
5
|
+
const isControlled = void 0 !== value;
|
|
6
|
+
const currentValue = void 0 !== value ? value : uncontrolledValue;
|
|
7
|
+
(0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
|
|
8
|
+
if (!isControlled) setUncontrolledValue(defaultValue);
|
|
9
|
+
}, [
|
|
10
|
+
defaultValue,
|
|
11
|
+
isControlled
|
|
12
|
+
]);
|
|
13
|
+
function setValue(nextValue) {
|
|
14
|
+
if (!isControlled) setUncontrolledValue(nextValue);
|
|
15
|
+
onChange?.(nextValue);
|
|
16
|
+
}
|
|
17
|
+
return [
|
|
18
|
+
currentValue,
|
|
19
|
+
setValue
|
|
20
|
+
];
|
|
21
|
+
}
|
|
22
|
+
function useDebouncedValue(value, delay = 200) {
|
|
23
|
+
const [debouncedValue, setDebouncedValue] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(value);
|
|
24
|
+
(0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
|
|
25
|
+
const timeoutId = window.setTimeout(()=>{
|
|
26
|
+
setDebouncedValue(value);
|
|
27
|
+
}, delay);
|
|
28
|
+
return ()=>{
|
|
29
|
+
window.clearTimeout(timeoutId);
|
|
30
|
+
};
|
|
31
|
+
}, [
|
|
32
|
+
delay,
|
|
33
|
+
value
|
|
34
|
+
]);
|
|
35
|
+
return debouncedValue;
|
|
36
|
+
}
|
|
37
|
+
function useDisclosure(initialOpen = false) {
|
|
38
|
+
const [isOpen, setIsOpen] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(initialOpen);
|
|
39
|
+
const onClose = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>{
|
|
40
|
+
setIsOpen(false);
|
|
41
|
+
}, []);
|
|
42
|
+
const onOpen = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>{
|
|
43
|
+
setIsOpen(true);
|
|
44
|
+
}, []);
|
|
45
|
+
const onToggle = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>{
|
|
46
|
+
setIsOpen((value)=>!value);
|
|
47
|
+
}, []);
|
|
48
|
+
return {
|
|
49
|
+
isOpen,
|
|
50
|
+
onClose,
|
|
51
|
+
onOpen,
|
|
52
|
+
onToggle
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function useLocalStorage(key, initialValue) {
|
|
56
|
+
const [value, setValue] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(()=>{
|
|
57
|
+
if ("undefined" == typeof window) return initialValue;
|
|
58
|
+
const storedValue = window.localStorage.getItem(key);
|
|
59
|
+
return storedValue ? JSON.parse(storedValue) : initialValue;
|
|
60
|
+
});
|
|
61
|
+
(0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
|
|
62
|
+
if ("undefined" == typeof window) return;
|
|
63
|
+
window.localStorage.setItem(key, JSON.stringify(value));
|
|
64
|
+
}, [
|
|
65
|
+
key,
|
|
66
|
+
value
|
|
67
|
+
]);
|
|
68
|
+
return [
|
|
69
|
+
value,
|
|
70
|
+
setValue
|
|
71
|
+
];
|
|
72
|
+
}
|
|
73
|
+
function useMediaQuery(query) {
|
|
74
|
+
const getMatches = ()=>"undefined" != typeof window && window.matchMedia(query).matches;
|
|
75
|
+
const [matches, setMatches] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(getMatches);
|
|
76
|
+
(0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
|
|
77
|
+
if ("undefined" == typeof window) return;
|
|
78
|
+
const mediaQuery = window.matchMedia(query);
|
|
79
|
+
const update = ()=>{
|
|
80
|
+
setMatches(mediaQuery.matches);
|
|
81
|
+
};
|
|
82
|
+
update();
|
|
83
|
+
mediaQuery.addEventListener("change", update);
|
|
84
|
+
return ()=>{
|
|
85
|
+
mediaQuery.removeEventListener("change", update);
|
|
86
|
+
};
|
|
87
|
+
}, [
|
|
88
|
+
query
|
|
89
|
+
]);
|
|
90
|
+
return matches;
|
|
91
|
+
}
|
|
92
|
+
export { useControllableState, useDebouncedValue, useDisclosure, useLocalStorage, useMediaQuery };
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ox0/hooks",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"files": [
|
|
5
|
+
"dist"
|
|
6
|
+
],
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"react": "^19.0.0"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "rslib build -c ../../rslib.config.ts",
|
|
22
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
23
|
+
"lint": "oxlint .",
|
|
24
|
+
"test": "vitest run"
|
|
25
|
+
}
|
|
26
|
+
}
|