react-simplikit 0.0.2 → 0.0.4
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 +21 -0
- package/README.md +40 -0
- package/dist/components/ImpressionArea/index.d.ts +46 -0
- package/dist/components/ImpressionArea/index.js +276 -0
- package/dist/components/Separated/index.d.ts +38 -0
- package/dist/components/Separated/index.js +40 -0
- package/dist/components/SwitchCase/index.d.ts +46 -0
- package/dist/components/SwitchCase/index.js +35 -0
- package/dist/hooks/useAsyncEffect/index.d.ts +25 -0
- package/dist/hooks/useAsyncEffect/index.js +43 -0
- package/dist/hooks/useBooleanState/index.d.ts +19 -0
- package/dist/hooks/useBooleanState/index.js +45 -0
- package/dist/hooks/useCallbackOncePerRender/index.d.ts +41 -0
- package/dist/hooks/useCallbackOncePerRender/index.js +59 -0
- package/dist/hooks/useDebounce/index.d.ts +48 -0
- package/dist/hooks/useDebounce/index.js +123 -0
- package/dist/hooks/useImpressionRef/index.d.ts +38 -0
- package/dist/hooks/useImpressionRef/index.js +237 -0
- package/dist/hooks/useInputState/index.d.ts +27 -0
- package/dist/hooks/useInputState/index.js +45 -0
- package/dist/hooks/useIntersectionObserver/index.d.ts +33 -0
- package/dist/hooks/useIntersectionObserver/index.js +91 -0
- package/dist/hooks/useInterval/index.d.ts +37 -0
- package/dist/hooks/useInterval/index.js +64 -0
- package/dist/hooks/useLoading/index.d.ts +37 -0
- package/dist/hooks/useLoading/index.js +61 -0
- package/dist/hooks/useOutsideClickEffect/index.d.ts +27 -0
- package/dist/hooks/useOutsideClickEffect/index.js +70 -0
- package/dist/hooks/usePreservedCallback/index.d.ts +30 -0
- package/dist/hooks/usePreservedCallback/index.js +41 -0
- package/dist/hooks/usePreservedReference/index.d.ts +42 -0
- package/dist/hooks/usePreservedReference/index.js +44 -0
- package/dist/hooks/usePrevious/index.d.ts +21 -0
- package/dist/hooks/usePrevious/index.js +47 -0
- package/dist/hooks/useRefEffect/index.d.ts +31 -0
- package/dist/hooks/useRefEffect/index.js +68 -0
- package/dist/hooks/useStorageState/index.d.ts +50 -0
- package/dist/hooks/useStorageState/index.js +168 -0
- package/dist/hooks/useThrottle/index.d.ts +47 -0
- package/dist/hooks/useThrottle/index.js +147 -0
- package/dist/hooks/useTimeout/index.d.ts +30 -0
- package/dist/hooks/useTimeout/index.js +53 -0
- package/dist/hooks/useToggle/index.d.ts +30 -0
- package/dist/hooks/useToggle/index.js +36 -0
- package/dist/hooks/useVisibilityEvent/index.d.ts +27 -0
- package/dist/hooks/useVisibilityEvent/index.js +46 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +761 -0
- package/dist/utils/buildContext/index.d.ts +38 -0
- package/dist/utils/buildContext/index.js +55 -0
- package/dist/utils/mergeRefs/index.d.ts +37 -0
- package/dist/utils/mergeRefs/index.js +45 -0
- package/esm/components/ImpressionArea/index.d.mts +46 -0
- package/esm/components/ImpressionArea/index.mjs +249 -0
- package/esm/components/Separated/index.d.mts +38 -0
- package/esm/components/Separated/index.mjs +13 -0
- package/esm/components/SwitchCase/index.d.mts +46 -0
- package/esm/components/SwitchCase/index.mjs +8 -0
- package/esm/hooks/useAsyncEffect/index.d.mts +25 -0
- package/esm/hooks/useAsyncEffect/index.mjs +16 -0
- package/esm/hooks/useBooleanState/index.d.mts +19 -0
- package/esm/hooks/useBooleanState/index.mjs +18 -0
- package/esm/hooks/useCallbackOncePerRender/index.d.mts +41 -0
- package/esm/hooks/useCallbackOncePerRender/index.mjs +32 -0
- package/esm/hooks/useDebounce/index.d.mts +48 -0
- package/esm/hooks/useDebounce/index.mjs +96 -0
- package/esm/hooks/useImpressionRef/index.d.mts +38 -0
- package/esm/hooks/useImpressionRef/index.mjs +210 -0
- package/esm/hooks/useInputState/index.d.mts +27 -0
- package/esm/hooks/useInputState/index.mjs +18 -0
- package/esm/hooks/useIntersectionObserver/index.d.mts +33 -0
- package/esm/hooks/useIntersectionObserver/index.mjs +64 -0
- package/esm/hooks/useInterval/index.d.mts +37 -0
- package/esm/hooks/useInterval/index.mjs +37 -0
- package/esm/hooks/useLoading/index.d.mts +37 -0
- package/esm/hooks/useLoading/index.mjs +34 -0
- package/esm/hooks/useOutsideClickEffect/index.d.mts +27 -0
- package/esm/hooks/useOutsideClickEffect/index.mjs +43 -0
- package/esm/hooks/usePreservedCallback/index.d.mts +30 -0
- package/esm/hooks/usePreservedCallback/index.mjs +14 -0
- package/esm/hooks/usePreservedReference/index.d.mts +42 -0
- package/esm/hooks/usePreservedReference/index.mjs +17 -0
- package/esm/hooks/usePrevious/index.d.mts +21 -0
- package/esm/hooks/usePrevious/index.mjs +20 -0
- package/esm/hooks/useRefEffect/index.d.mts +31 -0
- package/esm/hooks/useRefEffect/index.mjs +41 -0
- package/esm/hooks/useStorageState/index.d.mts +50 -0
- package/esm/hooks/useStorageState/index.mjs +141 -0
- package/esm/hooks/useThrottle/index.d.mts +47 -0
- package/esm/hooks/useThrottle/index.mjs +120 -0
- package/esm/hooks/useTimeout/index.d.mts +30 -0
- package/esm/hooks/useTimeout/index.mjs +26 -0
- package/esm/hooks/useToggle/index.d.mts +30 -0
- package/esm/hooks/useToggle/index.mjs +9 -0
- package/esm/hooks/useVisibilityEvent/index.d.mts +27 -0
- package/esm/hooks/useVisibilityEvent/index.mjs +19 -0
- package/esm/index.d.mts +26 -0
- package/esm/index.mjs +711 -0
- package/esm/utils/buildContext/index.d.mts +38 -0
- package/esm/utils/buildContext/index.mjs +28 -0
- package/esm/utils/mergeRefs/index.d.mts +37 -0
- package/esm/utils/mergeRefs/index.mjs +18 -0
- package/package.json +87 -7
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/hooks/useRefEffect/index.ts
|
|
21
|
+
var useRefEffect_exports = {};
|
|
22
|
+
__export(useRefEffect_exports, {
|
|
23
|
+
useRefEffect: () => useRefEffect
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(useRefEffect_exports);
|
|
26
|
+
|
|
27
|
+
// src/hooks/useRefEffect/useRefEffect.ts
|
|
28
|
+
var import_react2 = require("react");
|
|
29
|
+
|
|
30
|
+
// src/hooks/usePreservedCallback/usePreservedCallback.ts
|
|
31
|
+
var import_react = require("react");
|
|
32
|
+
function usePreservedCallback(callback) {
|
|
33
|
+
const callbackRef = (0, import_react.useRef)(callback);
|
|
34
|
+
(0, import_react.useEffect)(() => {
|
|
35
|
+
callbackRef.current = callback;
|
|
36
|
+
}, [callback]);
|
|
37
|
+
return (0, import_react.useCallback)((...args) => {
|
|
38
|
+
return callbackRef.current(...args);
|
|
39
|
+
}, []);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/hooks/useRefEffect/useRefEffect.ts
|
|
43
|
+
function useRefEffect(callback, deps) {
|
|
44
|
+
const preservedCallback = usePreservedCallback(callback);
|
|
45
|
+
const cleanupCallbackRef = (0, import_react2.useRef)(() => {
|
|
46
|
+
});
|
|
47
|
+
const effect = (0, import_react2.useCallback)(
|
|
48
|
+
(element) => {
|
|
49
|
+
cleanupCallbackRef.current();
|
|
50
|
+
cleanupCallbackRef.current = () => {
|
|
51
|
+
};
|
|
52
|
+
if (element == null) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const cleanup = preservedCallback(element);
|
|
56
|
+
if (typeof cleanup === "function") {
|
|
57
|
+
cleanupCallbackRef.current = cleanup;
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
61
|
+
[preservedCallback, ...deps]
|
|
62
|
+
);
|
|
63
|
+
return effect;
|
|
64
|
+
}
|
|
65
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
66
|
+
0 && (module.exports = {
|
|
67
|
+
useRefEffect
|
|
68
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { SetStateAction } from 'react';
|
|
2
|
+
|
|
3
|
+
type Storage = {
|
|
4
|
+
get(key: string): string | null;
|
|
5
|
+
set(key: string, value: string): void;
|
|
6
|
+
remove(key: string): void;
|
|
7
|
+
clear(): void;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
type ToPrimitive<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : never;
|
|
11
|
+
type ToObject<T> = T extends unknown[] | Record<string, unknown> ? T : never;
|
|
12
|
+
type Serializable<T> = T extends string | number | boolean ? ToPrimitive<T> : ToObject<T>;
|
|
13
|
+
type StorageStateOptions<T> = {
|
|
14
|
+
storage?: Storage;
|
|
15
|
+
defaultValue?: Serializable<T>;
|
|
16
|
+
};
|
|
17
|
+
type StorageStateOptionsWithDefaultValue<T> = StorageStateOptions<T> & {
|
|
18
|
+
defaultValue: Serializable<T>;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* @description
|
|
22
|
+
* A React hook that functions like `useState` but persists the state value in browser storage.
|
|
23
|
+
* The value is retained across page reloads and can be shared between tabs when using `localStorage`.
|
|
24
|
+
*
|
|
25
|
+
* @param {string} key - The key used to store the value in storage.
|
|
26
|
+
* @param {object} [options] - Configuration options for storage behavior.
|
|
27
|
+
* @param {Storage} [options.storage=localStorage] - The storage type (`localStorage` or `sessionStorage`). Defaults to `localStorage`.
|
|
28
|
+
* @param {T} [options.defaultValue] - The initial value if no existing value is found.
|
|
29
|
+
*
|
|
30
|
+
* @returns {[Serializable<T> | undefined, (value: SetStateAction<Serializable<T> | undefined>) => void]} A tuple:
|
|
31
|
+
* - `state`: The current state value retrieved from storage.
|
|
32
|
+
* - `setState`: A function to update and persist the state.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* // Counter with persistent state
|
|
36
|
+
* import { useStorageState } from 'react-simplikit';
|
|
37
|
+
*
|
|
38
|
+
* function Counter() {
|
|
39
|
+
* const [count, setCount] = useStorageState<number>('counter', {
|
|
40
|
+
* defaultValue: 0,
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* return <button onClick={() => setCount(prev => prev + 1)}>Count: {count}</button>;
|
|
44
|
+
* }
|
|
45
|
+
*/
|
|
46
|
+
declare function useStorageState<T>(key: string): readonly [Serializable<T> | undefined, (value: SetStateAction<Serializable<T> | undefined>) => void];
|
|
47
|
+
declare function useStorageState<T>(key: string, { storage, defaultValue }: StorageStateOptionsWithDefaultValue<T>): readonly [Serializable<T>, (value: SetStateAction<Serializable<T>>) => void];
|
|
48
|
+
declare function useStorageState<T>(key: string, { storage, defaultValue }: StorageStateOptions<T>): readonly [Serializable<T> | undefined, (value: SetStateAction<Serializable<T> | undefined>) => void];
|
|
49
|
+
|
|
50
|
+
export { type Serializable, useStorageState };
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/hooks/useStorageState/index.ts
|
|
21
|
+
var useStorageState_exports = {};
|
|
22
|
+
__export(useStorageState_exports, {
|
|
23
|
+
useStorageState: () => useStorageState
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(useStorageState_exports);
|
|
26
|
+
|
|
27
|
+
// src/hooks/useStorageState/useStorageState.ts
|
|
28
|
+
var import_react = require("react");
|
|
29
|
+
|
|
30
|
+
// src/hooks/useStorageState/storage.ts
|
|
31
|
+
var MemoStorage = class {
|
|
32
|
+
storage = /* @__PURE__ */ new Map();
|
|
33
|
+
get(key) {
|
|
34
|
+
return this.storage.get(key) ?? null;
|
|
35
|
+
}
|
|
36
|
+
set(key, value) {
|
|
37
|
+
this.storage.set(key, value);
|
|
38
|
+
}
|
|
39
|
+
remove(key) {
|
|
40
|
+
this.storage.delete(key);
|
|
41
|
+
}
|
|
42
|
+
clear() {
|
|
43
|
+
this.storage.clear();
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
var LocalStorage = class {
|
|
47
|
+
static canUse() {
|
|
48
|
+
const TEST_KEY = generateTestKey();
|
|
49
|
+
try {
|
|
50
|
+
localStorage.setItem(TEST_KEY, "test");
|
|
51
|
+
localStorage.removeItem(TEST_KEY);
|
|
52
|
+
return true;
|
|
53
|
+
} catch {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
get(key) {
|
|
58
|
+
return localStorage.getItem(key);
|
|
59
|
+
}
|
|
60
|
+
set(key, value) {
|
|
61
|
+
localStorage.setItem(key, value);
|
|
62
|
+
}
|
|
63
|
+
remove(key) {
|
|
64
|
+
localStorage.removeItem(key);
|
|
65
|
+
}
|
|
66
|
+
clear() {
|
|
67
|
+
localStorage.clear();
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
var SessionStorage = class {
|
|
71
|
+
static canUse() {
|
|
72
|
+
const TEST_KEY = generateTestKey();
|
|
73
|
+
try {
|
|
74
|
+
sessionStorage.setItem(TEST_KEY, "test");
|
|
75
|
+
sessionStorage.removeItem(TEST_KEY);
|
|
76
|
+
return true;
|
|
77
|
+
} catch {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
get(key) {
|
|
82
|
+
return sessionStorage.getItem(key);
|
|
83
|
+
}
|
|
84
|
+
set(key, value) {
|
|
85
|
+
sessionStorage.setItem(key, value);
|
|
86
|
+
}
|
|
87
|
+
remove(key) {
|
|
88
|
+
sessionStorage.removeItem(key);
|
|
89
|
+
}
|
|
90
|
+
clear() {
|
|
91
|
+
sessionStorage.clear();
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
function generateTestKey() {
|
|
95
|
+
return new Array(4).fill(null).map(() => Math.random().toString(36).slice(2)).join("");
|
|
96
|
+
}
|
|
97
|
+
function generateStorage() {
|
|
98
|
+
if (LocalStorage.canUse()) {
|
|
99
|
+
return new LocalStorage();
|
|
100
|
+
}
|
|
101
|
+
return new MemoStorage();
|
|
102
|
+
}
|
|
103
|
+
function generateSessionStorage() {
|
|
104
|
+
if (SessionStorage.canUse()) {
|
|
105
|
+
return new SessionStorage();
|
|
106
|
+
}
|
|
107
|
+
return new MemoStorage();
|
|
108
|
+
}
|
|
109
|
+
var safeLocalStorage = generateStorage();
|
|
110
|
+
var safeSessionStorage = generateSessionStorage();
|
|
111
|
+
|
|
112
|
+
// src/hooks/useStorageState/useStorageState.ts
|
|
113
|
+
var listeners = /* @__PURE__ */ new Set();
|
|
114
|
+
var emitListeners = () => {
|
|
115
|
+
listeners.forEach((listener) => listener());
|
|
116
|
+
};
|
|
117
|
+
function useStorageState(key, { storage = safeLocalStorage, defaultValue } = {}) {
|
|
118
|
+
const cache = (0, import_react.useRef)({
|
|
119
|
+
data: null,
|
|
120
|
+
parsed: defaultValue
|
|
121
|
+
});
|
|
122
|
+
const getSnapshot = (0, import_react.useCallback)(() => {
|
|
123
|
+
const data = storage.get(key);
|
|
124
|
+
if (data !== cache.current.data) {
|
|
125
|
+
try {
|
|
126
|
+
cache.current.parsed = data != null ? JSON.parse(data) : defaultValue;
|
|
127
|
+
} catch {
|
|
128
|
+
cache.current.parsed = defaultValue;
|
|
129
|
+
}
|
|
130
|
+
cache.current.data = data;
|
|
131
|
+
}
|
|
132
|
+
return cache.current.parsed;
|
|
133
|
+
}, [defaultValue, key, storage]);
|
|
134
|
+
const storageState = (0, import_react.useSyncExternalStore)(
|
|
135
|
+
(onStoreChange) => {
|
|
136
|
+
listeners.add(onStoreChange);
|
|
137
|
+
const handler = (event) => {
|
|
138
|
+
if (event.key === key) {
|
|
139
|
+
onStoreChange();
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
window.addEventListener("storage", handler);
|
|
143
|
+
return () => {
|
|
144
|
+
listeners.delete(onStoreChange);
|
|
145
|
+
window.removeEventListener("storage", handler);
|
|
146
|
+
};
|
|
147
|
+
},
|
|
148
|
+
() => getSnapshot(),
|
|
149
|
+
() => defaultValue
|
|
150
|
+
);
|
|
151
|
+
const setStorageState = (0, import_react.useCallback)(
|
|
152
|
+
(value) => {
|
|
153
|
+
const nextValue = typeof value === "function" ? value(getSnapshot()) : value;
|
|
154
|
+
if (nextValue == null) {
|
|
155
|
+
storage.remove(key);
|
|
156
|
+
} else {
|
|
157
|
+
storage.set(key, JSON.stringify(nextValue));
|
|
158
|
+
}
|
|
159
|
+
emitListeners();
|
|
160
|
+
},
|
|
161
|
+
[getSnapshot, key, storage]
|
|
162
|
+
);
|
|
163
|
+
return [storageState, setStorageState];
|
|
164
|
+
}
|
|
165
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
166
|
+
0 && (module.exports = {
|
|
167
|
+
useStorageState
|
|
168
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
type ThrottleOptions = {
|
|
2
|
+
/**
|
|
3
|
+
* An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both.
|
|
4
|
+
* If `edges` includes "leading", the function will be invoked at the start of the delay period.
|
|
5
|
+
* If `edges` includes "trailing", the function will be invoked at the end of the delay period.
|
|
6
|
+
* If both "leading" and "trailing" are included, the function will be invoked at both the start and end of the delay period.
|
|
7
|
+
* @default ["leading", "trailing"]
|
|
8
|
+
*/
|
|
9
|
+
edges?: Array<'leading' | 'trailing'>;
|
|
10
|
+
};
|
|
11
|
+
type ThrottledFunction<F extends (...args: any[]) => void> = {
|
|
12
|
+
(...args: Parameters<F>): void;
|
|
13
|
+
cancel: () => void;
|
|
14
|
+
};
|
|
15
|
+
declare function throttle<F extends (...args: any[]) => void>(func: F, throttleMs: number, { edges }?: ThrottleOptions): ThrottledFunction<F>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @description
|
|
19
|
+
* A React hook that creates a throttled version of a callback function.
|
|
20
|
+
* This is useful for limiting the rate at which a function can be called,
|
|
21
|
+
* such as when handling scroll or resize events.
|
|
22
|
+
*
|
|
23
|
+
* @template F - The type of the callback function.
|
|
24
|
+
* @param {F} callback - The function to be throttled.
|
|
25
|
+
* @param {number} wait - The number of milliseconds to throttle invocations to.
|
|
26
|
+
* @param {{ edges?: Array<'leading' | 'trailing'> }} [options] - Options to control the behavior of the throttle.
|
|
27
|
+
* @returns {F & { cancel: () => void }} - Returns the throttled function with a `cancel` method to cancel pending executions.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* const throttledScroll = useThrottle(() => {
|
|
31
|
+
* console.log('Scroll event');
|
|
32
|
+
* }, 200, { edges: ['leading', 'trailing'] });
|
|
33
|
+
*
|
|
34
|
+
* useEffect(() => {
|
|
35
|
+
* window.addEventListener('scroll', throttledScroll);
|
|
36
|
+
* return () => {
|
|
37
|
+
* window.removeEventListener('scroll', throttledScroll);
|
|
38
|
+
* throttledScroll.cancel();
|
|
39
|
+
* };
|
|
40
|
+
* }, [throttledScroll]);
|
|
41
|
+
*/
|
|
42
|
+
declare function useThrottle<F extends (...args: any[]) => any>(callback: F, wait: number, options?: Parameters<typeof throttle>[2]): {
|
|
43
|
+
(...args: any[]): void;
|
|
44
|
+
cancel: () => void;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export { useThrottle };
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/hooks/useThrottle/index.ts
|
|
21
|
+
var useThrottle_exports = {};
|
|
22
|
+
__export(useThrottle_exports, {
|
|
23
|
+
useThrottle: () => useThrottle
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(useThrottle_exports);
|
|
26
|
+
|
|
27
|
+
// src/hooks/useThrottle/useThrottle.ts
|
|
28
|
+
var import_react3 = require("react");
|
|
29
|
+
|
|
30
|
+
// src/hooks/usePreservedCallback/usePreservedCallback.ts
|
|
31
|
+
var import_react = require("react");
|
|
32
|
+
function usePreservedCallback(callback) {
|
|
33
|
+
const callbackRef = (0, import_react.useRef)(callback);
|
|
34
|
+
(0, import_react.useEffect)(() => {
|
|
35
|
+
callbackRef.current = callback;
|
|
36
|
+
}, [callback]);
|
|
37
|
+
return (0, import_react.useCallback)((...args) => {
|
|
38
|
+
return callbackRef.current(...args);
|
|
39
|
+
}, []);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/hooks/usePreservedReference/usePreservedReference.ts
|
|
43
|
+
var import_react2 = require("react");
|
|
44
|
+
function usePreservedReference(value, areValuesEqual = areDeeplyEqual) {
|
|
45
|
+
const ref = (0, import_react2.useRef)(value);
|
|
46
|
+
return (0, import_react2.useMemo)(() => {
|
|
47
|
+
if (!areValuesEqual(ref.current, value)) {
|
|
48
|
+
ref.current = value;
|
|
49
|
+
}
|
|
50
|
+
return ref.current;
|
|
51
|
+
}, [areValuesEqual, value]);
|
|
52
|
+
}
|
|
53
|
+
function areDeeplyEqual(x, y) {
|
|
54
|
+
return JSON.stringify(x) === JSON.stringify(y);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/hooks/useDebounce/debounce.ts
|
|
58
|
+
function debounce(func, debounceMs, { edges = ["leading", "trailing"] } = {}) {
|
|
59
|
+
let pendingThis = void 0;
|
|
60
|
+
let pendingArgs = null;
|
|
61
|
+
const leading = edges != null && edges.includes("leading");
|
|
62
|
+
const trailing = edges == null || edges.includes("trailing");
|
|
63
|
+
const invoke = () => {
|
|
64
|
+
if (pendingArgs !== null) {
|
|
65
|
+
func.apply(pendingThis, pendingArgs);
|
|
66
|
+
pendingThis = void 0;
|
|
67
|
+
pendingArgs = null;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const onTimerEnd = () => {
|
|
71
|
+
if (trailing) {
|
|
72
|
+
invoke();
|
|
73
|
+
}
|
|
74
|
+
cancel();
|
|
75
|
+
};
|
|
76
|
+
let timeoutId = null;
|
|
77
|
+
const schedule = () => {
|
|
78
|
+
if (timeoutId != null) {
|
|
79
|
+
clearTimeout(timeoutId);
|
|
80
|
+
}
|
|
81
|
+
timeoutId = setTimeout(() => {
|
|
82
|
+
timeoutId = null;
|
|
83
|
+
onTimerEnd();
|
|
84
|
+
}, debounceMs);
|
|
85
|
+
};
|
|
86
|
+
const cancelTimer = () => {
|
|
87
|
+
if (timeoutId !== null) {
|
|
88
|
+
clearTimeout(timeoutId);
|
|
89
|
+
timeoutId = null;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
const cancel = () => {
|
|
93
|
+
cancelTimer();
|
|
94
|
+
pendingThis = void 0;
|
|
95
|
+
pendingArgs = null;
|
|
96
|
+
};
|
|
97
|
+
const debounced = function(...args) {
|
|
98
|
+
pendingThis = this;
|
|
99
|
+
pendingArgs = args;
|
|
100
|
+
const isFirstCall = timeoutId == null;
|
|
101
|
+
schedule();
|
|
102
|
+
if (leading && isFirstCall) {
|
|
103
|
+
invoke();
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
debounced.cancel = cancel;
|
|
107
|
+
return debounced;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// src/hooks/useThrottle/throttle.ts
|
|
111
|
+
function throttle(func, throttleMs, { edges = ["leading", "trailing"] } = {}) {
|
|
112
|
+
let pendingAt = null;
|
|
113
|
+
const debounced = debounce(func, throttleMs, { edges });
|
|
114
|
+
const throttled = function(...args) {
|
|
115
|
+
if (pendingAt == null) {
|
|
116
|
+
pendingAt = Date.now();
|
|
117
|
+
} else {
|
|
118
|
+
if (Date.now() - pendingAt >= throttleMs) {
|
|
119
|
+
pendingAt = Date.now();
|
|
120
|
+
debounced.cancel();
|
|
121
|
+
debounced(...args);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
debounced(...args);
|
|
125
|
+
};
|
|
126
|
+
throttled.cancel = debounced.cancel;
|
|
127
|
+
return throttled;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// src/hooks/useThrottle/useThrottle.ts
|
|
131
|
+
function useThrottle(callback, wait, options) {
|
|
132
|
+
const preservedCallback = usePreservedCallback(callback);
|
|
133
|
+
const preservedOptions = usePreservedReference(options ?? {});
|
|
134
|
+
const throttledCallback = (0, import_react3.useMemo)(() => {
|
|
135
|
+
return throttle(preservedCallback, wait, preservedOptions);
|
|
136
|
+
}, [preservedOptions, preservedCallback, wait]);
|
|
137
|
+
(0, import_react3.useEffect)(() => {
|
|
138
|
+
return () => {
|
|
139
|
+
throttledCallback.cancel();
|
|
140
|
+
};
|
|
141
|
+
}, [throttledCallback]);
|
|
142
|
+
return throttledCallback;
|
|
143
|
+
}
|
|
144
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
145
|
+
0 && (module.exports = {
|
|
146
|
+
useThrottle
|
|
147
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description
|
|
3
|
+
* `useTimeout` is a React hook that executes a callback function after a specified delay.
|
|
4
|
+
* It manages `window.setTimeout` in accordance with the React lifecycle, ensuring cleanup on unmount or when dependencies change.
|
|
5
|
+
*
|
|
6
|
+
* @param {() => void} callback - The function to be executed after the delay.
|
|
7
|
+
* @param {number} [delay=0] - The time in milliseconds to wait before executing the callback.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // Updating a title after a delay
|
|
11
|
+
* import { useTimeout } from 'react-simplikit';
|
|
12
|
+
* import { useState } from 'react';
|
|
13
|
+
*
|
|
14
|
+
* function Example() {
|
|
15
|
+
* const [title, setTitle] = useState('');
|
|
16
|
+
*
|
|
17
|
+
* useTimeout(() => {
|
|
18
|
+
* setTitle('Searching for products...');
|
|
19
|
+
* }, 2000);
|
|
20
|
+
*
|
|
21
|
+
* useTimeout(() => {
|
|
22
|
+
* setTitle('Almost done...');
|
|
23
|
+
* }, 4000);
|
|
24
|
+
*
|
|
25
|
+
* return <div>{title}</div>;
|
|
26
|
+
* }
|
|
27
|
+
*/
|
|
28
|
+
declare function useTimeout(callback: () => void, delay?: number): void;
|
|
29
|
+
|
|
30
|
+
export { useTimeout };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/hooks/useTimeout/index.ts
|
|
21
|
+
var useTimeout_exports = {};
|
|
22
|
+
__export(useTimeout_exports, {
|
|
23
|
+
useTimeout: () => useTimeout
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(useTimeout_exports);
|
|
26
|
+
|
|
27
|
+
// src/hooks/useTimeout/useTimeout.ts
|
|
28
|
+
var import_react2 = require("react");
|
|
29
|
+
|
|
30
|
+
// src/hooks/usePreservedCallback/usePreservedCallback.ts
|
|
31
|
+
var import_react = require("react");
|
|
32
|
+
function usePreservedCallback(callback) {
|
|
33
|
+
const callbackRef = (0, import_react.useRef)(callback);
|
|
34
|
+
(0, import_react.useEffect)(() => {
|
|
35
|
+
callbackRef.current = callback;
|
|
36
|
+
}, [callback]);
|
|
37
|
+
return (0, import_react.useCallback)((...args) => {
|
|
38
|
+
return callbackRef.current(...args);
|
|
39
|
+
}, []);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/hooks/useTimeout/useTimeout.ts
|
|
43
|
+
function useTimeout(callback, delay = 0) {
|
|
44
|
+
const preservedCallback = usePreservedCallback(callback);
|
|
45
|
+
(0, import_react2.useEffect)(() => {
|
|
46
|
+
const timeoutId = window.setTimeout(preservedCallback, delay);
|
|
47
|
+
return () => window.clearTimeout(timeoutId);
|
|
48
|
+
}, [delay, preservedCallback]);
|
|
49
|
+
}
|
|
50
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
51
|
+
0 && (module.exports = {
|
|
52
|
+
useTimeout
|
|
53
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @description
|
|
5
|
+
* `useToggle` is a React hook that simplifies managing a boolean state.
|
|
6
|
+
* It provides a function to toggle the state between `true` and `false`.
|
|
7
|
+
*
|
|
8
|
+
* @param {boolean} [initialValue=false] - The initial state value. Defaults to `false`.
|
|
9
|
+
*
|
|
10
|
+
* @returns {[boolean, () => void]} A tuple:
|
|
11
|
+
* - `state`: The current state value.
|
|
12
|
+
* - `toggle`: A function to toggle the state.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* import { useToggle } from 'react-simplikit';
|
|
16
|
+
*
|
|
17
|
+
* function Component() {
|
|
18
|
+
* const [open, toggle] = useToggle(false);
|
|
19
|
+
*
|
|
20
|
+
* return (
|
|
21
|
+
* <div>
|
|
22
|
+
* <p>Bottom Sheet state: {open ? 'opened' : 'closed'}</p>
|
|
23
|
+
* <button onClick={toggle}>Toggle</button>
|
|
24
|
+
* </div>
|
|
25
|
+
* );
|
|
26
|
+
* }
|
|
27
|
+
*/
|
|
28
|
+
declare function useToggle(initialValue?: boolean): [boolean, React.ActionDispatch<[]>];
|
|
29
|
+
|
|
30
|
+
export { useToggle };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/hooks/useToggle/index.ts
|
|
21
|
+
var useToggle_exports = {};
|
|
22
|
+
__export(useToggle_exports, {
|
|
23
|
+
useToggle: () => useToggle
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(useToggle_exports);
|
|
26
|
+
|
|
27
|
+
// src/hooks/useToggle/useToggle.ts
|
|
28
|
+
var import_react = require("react");
|
|
29
|
+
function useToggle(initialValue = false) {
|
|
30
|
+
return (0, import_react.useReducer)(toggle, initialValue);
|
|
31
|
+
}
|
|
32
|
+
var toggle = (state) => !state;
|
|
33
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
34
|
+
0 && (module.exports = {
|
|
35
|
+
useToggle
|
|
36
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
type Options = {
|
|
2
|
+
immediate?: boolean;
|
|
3
|
+
};
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* A React hook that listens to changes in the document's visibility state and triggers a callback.
|
|
7
|
+
*
|
|
8
|
+
* @param {(visibilityState: 'visible' | 'hidden') => void} callback - A function to be called
|
|
9
|
+
* when the visibility state changes. It receives the current visibility state ('visible' or 'hidden') as an argument.
|
|
10
|
+
* @param {object} [options] - Optional configuration for the hook.
|
|
11
|
+
* @param {boolean} [options.immediate=false] - If true, the callback is invoked immediately upon mounting
|
|
12
|
+
* with the current visibility state.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* import { useVisibilityEvent } from 'react-simplikit';
|
|
16
|
+
*
|
|
17
|
+
* function Component() {
|
|
18
|
+
* useVisibilityEvent(visibilityState => {
|
|
19
|
+
* console.log(`Document is now ${visibilityState}`);
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* return <p>Check the console for visibility changes.</p>;
|
|
23
|
+
* }
|
|
24
|
+
*/
|
|
25
|
+
declare function useVisibilityEvent(callback: (visibilityState: 'visible' | 'hidden') => void, options?: Options): void;
|
|
26
|
+
|
|
27
|
+
export { useVisibilityEvent };
|