react-simplikit 0.0.2 → 0.0.3
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 +48 -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/useCallbackOnce/index.d.ts +41 -0
- package/dist/hooks/useCallbackOnce/index.js +59 -0
- package/dist/hooks/useDebounce/index.d.ts +47 -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 +48 -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/useCallbackOnce/index.d.mts +41 -0
- package/esm/hooks/useCallbackOnce/index.mjs +32 -0
- package/esm/hooks/useDebounce/index.d.mts +47 -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 +79 -7
package/dist/index.js
ADDED
|
@@ -0,0 +1,761 @@
|
|
|
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/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
ImpressionArea: () => ImpressionArea,
|
|
24
|
+
Separated: () => Separated,
|
|
25
|
+
SwitchCase: () => SwitchCase,
|
|
26
|
+
buildContext: () => buildContext,
|
|
27
|
+
mergeRefs: () => mergeRefs,
|
|
28
|
+
useAsyncEffect: () => useAsyncEffect,
|
|
29
|
+
useBooleanState: () => useBooleanState,
|
|
30
|
+
useCallbackOnce: () => useCallbackOnce,
|
|
31
|
+
useDebounce: () => useDebounce,
|
|
32
|
+
useImpressionRef: () => useImpressionRef,
|
|
33
|
+
useInputState: () => useInputState,
|
|
34
|
+
useIntersectionObserver: () => useIntersectionObserver,
|
|
35
|
+
useInterval: () => useInterval,
|
|
36
|
+
useLoading: () => useLoading,
|
|
37
|
+
useOutsideClickEffect: () => useOutsideClickEffect,
|
|
38
|
+
usePreservedCallback: () => usePreservedCallback,
|
|
39
|
+
usePreservedReference: () => usePreservedReference,
|
|
40
|
+
usePrevious: () => usePrevious,
|
|
41
|
+
useRefEffect: () => useRefEffect,
|
|
42
|
+
useStorageState: () => useStorageState,
|
|
43
|
+
useThrottle: () => useThrottle,
|
|
44
|
+
useTimeout: () => useTimeout,
|
|
45
|
+
useToggle: () => useToggle,
|
|
46
|
+
useVisibilityEvent: () => useVisibilityEvent
|
|
47
|
+
});
|
|
48
|
+
module.exports = __toCommonJS(index_exports);
|
|
49
|
+
|
|
50
|
+
// src/hooks/useImpressionRef/useImpressionRef.ts
|
|
51
|
+
var import_react6 = require("react");
|
|
52
|
+
|
|
53
|
+
// src/hooks/useIntersectionObserver/useIntersectionObserver.ts
|
|
54
|
+
var import_react3 = require("react");
|
|
55
|
+
|
|
56
|
+
// src/hooks/usePreservedCallback/usePreservedCallback.ts
|
|
57
|
+
var import_react = require("react");
|
|
58
|
+
function usePreservedCallback(callback) {
|
|
59
|
+
const callbackRef = (0, import_react.useRef)(callback);
|
|
60
|
+
(0, import_react.useEffect)(() => {
|
|
61
|
+
callbackRef.current = callback;
|
|
62
|
+
}, [callback]);
|
|
63
|
+
return (0, import_react.useCallback)((...args) => {
|
|
64
|
+
return callbackRef.current(...args);
|
|
65
|
+
}, []);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// src/hooks/useRefEffect/useRefEffect.ts
|
|
69
|
+
var import_react2 = require("react");
|
|
70
|
+
function useRefEffect(callback, deps) {
|
|
71
|
+
const preservedCallback = usePreservedCallback(callback);
|
|
72
|
+
const cleanupCallbackRef = (0, import_react2.useRef)(() => {
|
|
73
|
+
});
|
|
74
|
+
const effect = (0, import_react2.useCallback)(
|
|
75
|
+
(element) => {
|
|
76
|
+
cleanupCallbackRef.current();
|
|
77
|
+
cleanupCallbackRef.current = () => {
|
|
78
|
+
};
|
|
79
|
+
if (element == null) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const cleanup = preservedCallback(element);
|
|
83
|
+
if (typeof cleanup === "function") {
|
|
84
|
+
cleanupCallbackRef.current = cleanup;
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
88
|
+
[preservedCallback, ...deps]
|
|
89
|
+
);
|
|
90
|
+
return effect;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/hooks/useIntersectionObserver/useIntersectionObserver.ts
|
|
94
|
+
function useIntersectionObserver(callback, options) {
|
|
95
|
+
const preservedCallback = usePreservedCallback(callback);
|
|
96
|
+
const observer = (0, import_react3.useMemo)(() => {
|
|
97
|
+
if (typeof IntersectionObserver === "undefined") {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
return new IntersectionObserver(([entry]) => {
|
|
101
|
+
preservedCallback(entry);
|
|
102
|
+
}, options);
|
|
103
|
+
}, [preservedCallback, options]);
|
|
104
|
+
return useRefEffect(
|
|
105
|
+
(element) => {
|
|
106
|
+
observer?.observe(element);
|
|
107
|
+
return () => {
|
|
108
|
+
observer?.unobserve(element);
|
|
109
|
+
};
|
|
110
|
+
},
|
|
111
|
+
[preservedCallback, options]
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// src/hooks/useVisibilityEvent/useVisibilityEvent.ts
|
|
116
|
+
var import_react4 = require("react");
|
|
117
|
+
function useVisibilityEvent(callback, options = {}) {
|
|
118
|
+
const handleVisibilityChange = (0, import_react4.useCallback)(() => {
|
|
119
|
+
callback(document.visibilityState);
|
|
120
|
+
}, [callback]);
|
|
121
|
+
(0, import_react4.useEffect)(() => {
|
|
122
|
+
if (options?.immediate ?? false) {
|
|
123
|
+
handleVisibilityChange();
|
|
124
|
+
}
|
|
125
|
+
document.addEventListener("visibilitychange", handleVisibilityChange);
|
|
126
|
+
return () => {
|
|
127
|
+
document.removeEventListener("visibilitychange", handleVisibilityChange);
|
|
128
|
+
};
|
|
129
|
+
}, [handleVisibilityChange, options?.immediate]);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// src/hooks/useImpressionRef/useDebouncedCallback.ts
|
|
133
|
+
var import_react5 = require("react");
|
|
134
|
+
|
|
135
|
+
// src/hooks/useDebounce/debounce.ts
|
|
136
|
+
function debounce(func, debounceMs, { edges = ["leading", "trailing"] } = {}) {
|
|
137
|
+
let pendingThis = void 0;
|
|
138
|
+
let pendingArgs = null;
|
|
139
|
+
const leading = edges != null && edges.includes("leading");
|
|
140
|
+
const trailing = edges == null || edges.includes("trailing");
|
|
141
|
+
const invoke = () => {
|
|
142
|
+
if (pendingArgs !== null) {
|
|
143
|
+
func.apply(pendingThis, pendingArgs);
|
|
144
|
+
pendingThis = void 0;
|
|
145
|
+
pendingArgs = null;
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
const onTimerEnd = () => {
|
|
149
|
+
if (trailing) {
|
|
150
|
+
invoke();
|
|
151
|
+
}
|
|
152
|
+
cancel();
|
|
153
|
+
};
|
|
154
|
+
let timeoutId = null;
|
|
155
|
+
const schedule = () => {
|
|
156
|
+
if (timeoutId != null) {
|
|
157
|
+
clearTimeout(timeoutId);
|
|
158
|
+
}
|
|
159
|
+
timeoutId = setTimeout(() => {
|
|
160
|
+
timeoutId = null;
|
|
161
|
+
onTimerEnd();
|
|
162
|
+
}, debounceMs);
|
|
163
|
+
};
|
|
164
|
+
const cancelTimer = () => {
|
|
165
|
+
if (timeoutId !== null) {
|
|
166
|
+
clearTimeout(timeoutId);
|
|
167
|
+
timeoutId = null;
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
const cancel = () => {
|
|
171
|
+
cancelTimer();
|
|
172
|
+
pendingThis = void 0;
|
|
173
|
+
pendingArgs = null;
|
|
174
|
+
};
|
|
175
|
+
const debounced = function(...args) {
|
|
176
|
+
pendingThis = this;
|
|
177
|
+
pendingArgs = args;
|
|
178
|
+
const isFirstCall = timeoutId == null;
|
|
179
|
+
schedule();
|
|
180
|
+
if (leading && isFirstCall) {
|
|
181
|
+
invoke();
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
debounced.cancel = cancel;
|
|
185
|
+
return debounced;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// src/hooks/useImpressionRef/useDebouncedCallback.ts
|
|
189
|
+
function useDebouncedCallback({
|
|
190
|
+
onChange,
|
|
191
|
+
timeThreshold
|
|
192
|
+
}) {
|
|
193
|
+
const handleChange = usePreservedCallback(onChange);
|
|
194
|
+
const ref = (0, import_react5.useRef)({ value: false, clearPreviousDebounce: () => {
|
|
195
|
+
} });
|
|
196
|
+
(0, import_react5.useEffect)(() => {
|
|
197
|
+
const current = ref.current;
|
|
198
|
+
return () => {
|
|
199
|
+
current.clearPreviousDebounce();
|
|
200
|
+
};
|
|
201
|
+
}, []);
|
|
202
|
+
return (0, import_react5.useCallback)(
|
|
203
|
+
(nextValue) => {
|
|
204
|
+
if (nextValue === ref.current.value) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const debounced = debounce(() => {
|
|
208
|
+
handleChange(nextValue);
|
|
209
|
+
ref.current.value = nextValue;
|
|
210
|
+
}, timeThreshold);
|
|
211
|
+
ref.current.clearPreviousDebounce();
|
|
212
|
+
debounced();
|
|
213
|
+
ref.current.clearPreviousDebounce = debounced.cancel;
|
|
214
|
+
},
|
|
215
|
+
[handleChange, timeThreshold]
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// src/hooks/useImpressionRef/useImpressionRef.ts
|
|
220
|
+
function useImpressionRef({
|
|
221
|
+
onImpressionStart = () => {
|
|
222
|
+
},
|
|
223
|
+
onImpressionEnd = () => {
|
|
224
|
+
},
|
|
225
|
+
rootMargin,
|
|
226
|
+
areaThreshold = 0,
|
|
227
|
+
timeThreshold = 0
|
|
228
|
+
}) {
|
|
229
|
+
const impressionStartHandler = usePreservedCallback(onImpressionStart);
|
|
230
|
+
const impressionEndHandler = usePreservedCallback(onImpressionEnd);
|
|
231
|
+
const isIntersectingRef = (0, import_react6.useRef)(false);
|
|
232
|
+
const impressionEventHandler = useDebouncedCallback({
|
|
233
|
+
timeThreshold,
|
|
234
|
+
onChange: (impressed) => {
|
|
235
|
+
(impressed ? impressionStartHandler : impressionEndHandler)();
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
useVisibilityEvent((documentVisible) => {
|
|
239
|
+
if (!isIntersectingRef.current) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
impressionEventHandler(documentVisible === "visible");
|
|
243
|
+
});
|
|
244
|
+
return useIntersectionObserver(
|
|
245
|
+
(entry) => {
|
|
246
|
+
if (document.visibilityState === "hidden") {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
const currentRatio = entry.intersectionRatio;
|
|
250
|
+
const isIntersecting = areaThreshold === 0 ? entry.isIntersecting : currentRatio >= areaThreshold;
|
|
251
|
+
isIntersectingRef.current = isIntersecting;
|
|
252
|
+
impressionEventHandler(isIntersecting);
|
|
253
|
+
},
|
|
254
|
+
{ rootMargin, threshold: areaThreshold }
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// src/utils/mergeRefs/mergeRefs.ts
|
|
259
|
+
function mergeRefs(...refs) {
|
|
260
|
+
return (value) => {
|
|
261
|
+
for (const ref of refs) {
|
|
262
|
+
if (ref == null) {
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
if (typeof ref === "function") {
|
|
266
|
+
ref(value);
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
ref.current = value;
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// src/components/ImpressionArea/ImpressionArea.tsx
|
|
275
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
276
|
+
function ImpressionArea({
|
|
277
|
+
as,
|
|
278
|
+
rootMargin,
|
|
279
|
+
areaThreshold,
|
|
280
|
+
timeThreshold,
|
|
281
|
+
onImpressionStart,
|
|
282
|
+
onImpressionEnd,
|
|
283
|
+
ref,
|
|
284
|
+
...props
|
|
285
|
+
}) {
|
|
286
|
+
const Component = as ?? "div";
|
|
287
|
+
const impressionRef = useImpressionRef({
|
|
288
|
+
onImpressionStart,
|
|
289
|
+
onImpressionEnd,
|
|
290
|
+
areaThreshold,
|
|
291
|
+
timeThreshold,
|
|
292
|
+
rootMargin
|
|
293
|
+
});
|
|
294
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Component, { ref: mergeRefs(ref, impressionRef), ...props });
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// src/components/Separated/Separated.tsx
|
|
298
|
+
var import_react7 = require("react");
|
|
299
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
300
|
+
function Separated({ children, with: separator }) {
|
|
301
|
+
const childrenArray = import_react7.Children.toArray(children).filter(import_react7.isValidElement);
|
|
302
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: childrenArray.map((child, i, { length }) => /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_react7.Fragment, { children: [
|
|
303
|
+
child,
|
|
304
|
+
i + 1 !== length ? separator : null
|
|
305
|
+
] }, i)) });
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// src/components/SwitchCase/SwitchCase.tsx
|
|
309
|
+
function SwitchCase({ value, caseBy, defaultComponent = () => null }) {
|
|
310
|
+
const stringifiedValue = String(value);
|
|
311
|
+
return (caseBy[stringifiedValue] ?? defaultComponent)();
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// src/hooks/useAsyncEffect/useAsyncEffect.ts
|
|
315
|
+
var import_react8 = require("react");
|
|
316
|
+
function useAsyncEffect(effect, deps) {
|
|
317
|
+
(0, import_react8.useEffect)(() => {
|
|
318
|
+
let cleanup;
|
|
319
|
+
effect().then((result) => {
|
|
320
|
+
cleanup = result;
|
|
321
|
+
});
|
|
322
|
+
return () => {
|
|
323
|
+
cleanup?.();
|
|
324
|
+
};
|
|
325
|
+
}, deps);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// src/hooks/useBooleanState/useBooleanState.ts
|
|
329
|
+
var import_react9 = require("react");
|
|
330
|
+
var useBooleanState = (defaultValue = false) => {
|
|
331
|
+
const [bool, setBool] = (0, import_react9.useState)(defaultValue);
|
|
332
|
+
const setTrue = (0, import_react9.useCallback)(() => {
|
|
333
|
+
setBool(true);
|
|
334
|
+
}, []);
|
|
335
|
+
const setFalse = (0, import_react9.useCallback)(() => {
|
|
336
|
+
setBool(false);
|
|
337
|
+
}, []);
|
|
338
|
+
const toggle2 = (0, import_react9.useCallback)(() => {
|
|
339
|
+
setBool((b) => !b);
|
|
340
|
+
}, []);
|
|
341
|
+
return [bool, setTrue, setFalse, toggle2];
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
// src/hooks/useCallbackOnce/useCallbackOnce.ts
|
|
345
|
+
var import_react10 = require("react");
|
|
346
|
+
function useCallbackOnce(callback, deps) {
|
|
347
|
+
const hasFired = (0, import_react10.useRef)(false);
|
|
348
|
+
(0, import_react10.useEffect)(() => {
|
|
349
|
+
hasFired.current = false;
|
|
350
|
+
}, deps);
|
|
351
|
+
return usePreservedCallback((...args) => {
|
|
352
|
+
if (hasFired.current) {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
callback(...args);
|
|
356
|
+
hasFired.current = true;
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// src/hooks/useDebounce/useDebounce.ts
|
|
361
|
+
var import_react11 = require("react");
|
|
362
|
+
var import_react12 = require("react");
|
|
363
|
+
function useDebounce(callback, wait, options = {}) {
|
|
364
|
+
const preservedCallback = usePreservedCallback(callback);
|
|
365
|
+
const { leading = false, trailing = true } = options;
|
|
366
|
+
const edges = (0, import_react12.useMemo)(() => {
|
|
367
|
+
const _edges = [];
|
|
368
|
+
if (leading) {
|
|
369
|
+
_edges.push("leading");
|
|
370
|
+
}
|
|
371
|
+
if (trailing) {
|
|
372
|
+
_edges.push("trailing");
|
|
373
|
+
}
|
|
374
|
+
return _edges;
|
|
375
|
+
}, [leading, trailing]);
|
|
376
|
+
const debounced = (0, import_react12.useMemo)(() => {
|
|
377
|
+
return debounce(preservedCallback, wait, { edges });
|
|
378
|
+
}, [preservedCallback, wait, edges]);
|
|
379
|
+
(0, import_react11.useEffect)(() => {
|
|
380
|
+
return () => {
|
|
381
|
+
debounced.cancel();
|
|
382
|
+
};
|
|
383
|
+
}, [debounced]);
|
|
384
|
+
return debounced;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// src/hooks/useInputState/useInputState.ts
|
|
388
|
+
var import_react13 = require("react");
|
|
389
|
+
function useInputState(initialValue = "", transformValue = echo) {
|
|
390
|
+
const [value, setValue] = (0, import_react13.useState)(initialValue);
|
|
391
|
+
const handleValueChange = (0, import_react13.useCallback)(
|
|
392
|
+
({ target: { value: value2 } }) => {
|
|
393
|
+
setValue(transformValue(value2));
|
|
394
|
+
},
|
|
395
|
+
[transformValue]
|
|
396
|
+
);
|
|
397
|
+
return [value, handleValueChange];
|
|
398
|
+
}
|
|
399
|
+
function echo(v) {
|
|
400
|
+
return v;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// src/hooks/useInterval/useInterval.ts
|
|
404
|
+
var import_react14 = require("react");
|
|
405
|
+
function useInterval(callback, options) {
|
|
406
|
+
const delay = typeof options === "number" ? options : options.delay;
|
|
407
|
+
const immediate = typeof options === "number" ? false : options.immediate;
|
|
408
|
+
const enabled = typeof options === "number" ? true : options.enabled ?? true;
|
|
409
|
+
const preservedCallback = usePreservedCallback(callback);
|
|
410
|
+
(0, import_react14.useEffect)(() => {
|
|
411
|
+
if (immediate === true && enabled) {
|
|
412
|
+
preservedCallback();
|
|
413
|
+
}
|
|
414
|
+
}, [immediate, preservedCallback, enabled]);
|
|
415
|
+
(0, import_react14.useEffect)(() => {
|
|
416
|
+
if (!enabled) {
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
const id = window.setInterval(preservedCallback, delay);
|
|
420
|
+
return () => window.clearInterval(id);
|
|
421
|
+
}, [delay, preservedCallback, enabled]);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// src/hooks/useLoading/useLoading.ts
|
|
425
|
+
var import_react15 = require("react");
|
|
426
|
+
function useLoading() {
|
|
427
|
+
const [loading, setLoading] = (0, import_react15.useState)(false);
|
|
428
|
+
const ref = useIsMountedRef();
|
|
429
|
+
const startTransition = (0, import_react15.useCallback)(
|
|
430
|
+
async (promise) => {
|
|
431
|
+
try {
|
|
432
|
+
setLoading(true);
|
|
433
|
+
const data = await promise;
|
|
434
|
+
return data;
|
|
435
|
+
} finally {
|
|
436
|
+
if (ref.isMounted) {
|
|
437
|
+
setLoading(false);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
},
|
|
441
|
+
[ref.isMounted]
|
|
442
|
+
);
|
|
443
|
+
return (0, import_react15.useMemo)(() => [loading, startTransition], [loading, startTransition]);
|
|
444
|
+
}
|
|
445
|
+
function useIsMountedRef() {
|
|
446
|
+
const ref = (0, import_react15.useRef)({ isMounted: true }).current;
|
|
447
|
+
(0, import_react15.useEffect)(() => {
|
|
448
|
+
ref.isMounted = true;
|
|
449
|
+
return () => {
|
|
450
|
+
ref.isMounted = false;
|
|
451
|
+
};
|
|
452
|
+
}, [ref]);
|
|
453
|
+
return ref;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// src/hooks/useOutsideClickEffect/useOutsideClickEffect.ts
|
|
457
|
+
var import_react16 = require("react");
|
|
458
|
+
function useOutsideClickEffect(container, callback) {
|
|
459
|
+
const containers = (0, import_react16.useRef)([]);
|
|
460
|
+
const handleDocumentClick = usePreservedCallback(({ target }) => {
|
|
461
|
+
if (target === null) {
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
if (containers.current.length === 0) {
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
if (containers.current.some((x) => x.contains(target))) {
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
callback();
|
|
471
|
+
});
|
|
472
|
+
(0, import_react16.useEffect)(() => {
|
|
473
|
+
containers.current = [container].flat(1).filter((item) => item != null);
|
|
474
|
+
}, [container]);
|
|
475
|
+
(0, import_react16.useEffect)(() => {
|
|
476
|
+
document.addEventListener("click", handleDocumentClick);
|
|
477
|
+
return () => {
|
|
478
|
+
document.removeEventListener("click", handleDocumentClick);
|
|
479
|
+
};
|
|
480
|
+
}, [handleDocumentClick]);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// src/hooks/usePreservedReference/usePreservedReference.ts
|
|
484
|
+
var import_react17 = require("react");
|
|
485
|
+
function usePreservedReference(value, areValuesEqual = areDeeplyEqual) {
|
|
486
|
+
const ref = (0, import_react17.useRef)(value);
|
|
487
|
+
return (0, import_react17.useMemo)(() => {
|
|
488
|
+
if (!areValuesEqual(ref.current, value)) {
|
|
489
|
+
ref.current = value;
|
|
490
|
+
}
|
|
491
|
+
return ref.current;
|
|
492
|
+
}, [areValuesEqual, value]);
|
|
493
|
+
}
|
|
494
|
+
function areDeeplyEqual(x, y) {
|
|
495
|
+
return JSON.stringify(x) === JSON.stringify(y);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
// src/hooks/usePrevious/usePrevious.ts
|
|
499
|
+
var import_react18 = require("react");
|
|
500
|
+
var strictEquals = (prev, next) => prev === next;
|
|
501
|
+
function usePrevious(state, compare = strictEquals) {
|
|
502
|
+
const prevRef = (0, import_react18.useRef)(state);
|
|
503
|
+
const currentRef = (0, import_react18.useRef)(state);
|
|
504
|
+
const isFirstRender = (0, import_react18.useRef)(true);
|
|
505
|
+
(0, import_react18.useEffect)(() => {
|
|
506
|
+
isFirstRender.current = false;
|
|
507
|
+
}, []);
|
|
508
|
+
if (isFirstRender.current || compare(currentRef.current, state)) {
|
|
509
|
+
return prevRef.current;
|
|
510
|
+
}
|
|
511
|
+
prevRef.current = currentRef.current;
|
|
512
|
+
currentRef.current = state;
|
|
513
|
+
return prevRef.current;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// src/hooks/useStorageState/useStorageState.ts
|
|
517
|
+
var import_react19 = require("react");
|
|
518
|
+
|
|
519
|
+
// src/hooks/useStorageState/storage.ts
|
|
520
|
+
var MemoStorage = class {
|
|
521
|
+
storage = /* @__PURE__ */ new Map();
|
|
522
|
+
get(key) {
|
|
523
|
+
return this.storage.get(key) ?? null;
|
|
524
|
+
}
|
|
525
|
+
set(key, value) {
|
|
526
|
+
this.storage.set(key, value);
|
|
527
|
+
}
|
|
528
|
+
remove(key) {
|
|
529
|
+
this.storage.delete(key);
|
|
530
|
+
}
|
|
531
|
+
clear() {
|
|
532
|
+
this.storage.clear();
|
|
533
|
+
}
|
|
534
|
+
};
|
|
535
|
+
var LocalStorage = class {
|
|
536
|
+
static canUse() {
|
|
537
|
+
const TEST_KEY = generateTestKey();
|
|
538
|
+
try {
|
|
539
|
+
localStorage.setItem(TEST_KEY, "test");
|
|
540
|
+
localStorage.removeItem(TEST_KEY);
|
|
541
|
+
return true;
|
|
542
|
+
} catch {
|
|
543
|
+
return false;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
get(key) {
|
|
547
|
+
return localStorage.getItem(key);
|
|
548
|
+
}
|
|
549
|
+
set(key, value) {
|
|
550
|
+
localStorage.setItem(key, value);
|
|
551
|
+
}
|
|
552
|
+
remove(key) {
|
|
553
|
+
localStorage.removeItem(key);
|
|
554
|
+
}
|
|
555
|
+
clear() {
|
|
556
|
+
localStorage.clear();
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
var SessionStorage = class {
|
|
560
|
+
static canUse() {
|
|
561
|
+
const TEST_KEY = generateTestKey();
|
|
562
|
+
try {
|
|
563
|
+
sessionStorage.setItem(TEST_KEY, "test");
|
|
564
|
+
sessionStorage.removeItem(TEST_KEY);
|
|
565
|
+
return true;
|
|
566
|
+
} catch {
|
|
567
|
+
return false;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
get(key) {
|
|
571
|
+
return sessionStorage.getItem(key);
|
|
572
|
+
}
|
|
573
|
+
set(key, value) {
|
|
574
|
+
sessionStorage.setItem(key, value);
|
|
575
|
+
}
|
|
576
|
+
remove(key) {
|
|
577
|
+
sessionStorage.removeItem(key);
|
|
578
|
+
}
|
|
579
|
+
clear() {
|
|
580
|
+
sessionStorage.clear();
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
function generateTestKey() {
|
|
584
|
+
return new Array(4).fill(null).map(() => Math.random().toString(36).slice(2)).join("");
|
|
585
|
+
}
|
|
586
|
+
function generateStorage() {
|
|
587
|
+
if (LocalStorage.canUse()) {
|
|
588
|
+
return new LocalStorage();
|
|
589
|
+
}
|
|
590
|
+
return new MemoStorage();
|
|
591
|
+
}
|
|
592
|
+
function generateSessionStorage() {
|
|
593
|
+
if (SessionStorage.canUse()) {
|
|
594
|
+
return new SessionStorage();
|
|
595
|
+
}
|
|
596
|
+
return new MemoStorage();
|
|
597
|
+
}
|
|
598
|
+
var safeLocalStorage = generateStorage();
|
|
599
|
+
var safeSessionStorage = generateSessionStorage();
|
|
600
|
+
|
|
601
|
+
// src/hooks/useStorageState/useStorageState.ts
|
|
602
|
+
var listeners = /* @__PURE__ */ new Set();
|
|
603
|
+
var emitListeners = () => {
|
|
604
|
+
listeners.forEach((listener) => listener());
|
|
605
|
+
};
|
|
606
|
+
function useStorageState(key, { storage = safeLocalStorage, defaultValue } = {}) {
|
|
607
|
+
const cache = (0, import_react19.useRef)({
|
|
608
|
+
data: null,
|
|
609
|
+
parsed: defaultValue
|
|
610
|
+
});
|
|
611
|
+
const getSnapshot = (0, import_react19.useCallback)(() => {
|
|
612
|
+
const data = storage.get(key);
|
|
613
|
+
if (data !== cache.current.data) {
|
|
614
|
+
try {
|
|
615
|
+
cache.current.parsed = data != null ? JSON.parse(data) : defaultValue;
|
|
616
|
+
} catch {
|
|
617
|
+
cache.current.parsed = defaultValue;
|
|
618
|
+
}
|
|
619
|
+
cache.current.data = data;
|
|
620
|
+
}
|
|
621
|
+
return cache.current.parsed;
|
|
622
|
+
}, [defaultValue, key, storage]);
|
|
623
|
+
const storageState = (0, import_react19.useSyncExternalStore)(
|
|
624
|
+
(onStoreChange) => {
|
|
625
|
+
listeners.add(onStoreChange);
|
|
626
|
+
const handler = (event) => {
|
|
627
|
+
if (event.key === key) {
|
|
628
|
+
onStoreChange();
|
|
629
|
+
}
|
|
630
|
+
};
|
|
631
|
+
window.addEventListener("storage", handler);
|
|
632
|
+
return () => {
|
|
633
|
+
listeners.delete(onStoreChange);
|
|
634
|
+
window.removeEventListener("storage", handler);
|
|
635
|
+
};
|
|
636
|
+
},
|
|
637
|
+
() => getSnapshot(),
|
|
638
|
+
() => defaultValue
|
|
639
|
+
);
|
|
640
|
+
const setStorageState = (0, import_react19.useCallback)(
|
|
641
|
+
(value) => {
|
|
642
|
+
const nextValue = typeof value === "function" ? value(getSnapshot()) : value;
|
|
643
|
+
if (nextValue == null) {
|
|
644
|
+
storage.remove(key);
|
|
645
|
+
} else {
|
|
646
|
+
storage.set(key, JSON.stringify(nextValue));
|
|
647
|
+
}
|
|
648
|
+
emitListeners();
|
|
649
|
+
},
|
|
650
|
+
[getSnapshot, key, storage]
|
|
651
|
+
);
|
|
652
|
+
return [storageState, setStorageState];
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// src/hooks/useThrottle/useThrottle.ts
|
|
656
|
+
var import_react20 = require("react");
|
|
657
|
+
|
|
658
|
+
// src/hooks/useThrottle/throttle.ts
|
|
659
|
+
function throttle(func, throttleMs, { edges = ["leading", "trailing"] } = {}) {
|
|
660
|
+
let pendingAt = null;
|
|
661
|
+
const debounced = debounce(func, throttleMs, { edges });
|
|
662
|
+
const throttled = function(...args) {
|
|
663
|
+
if (pendingAt == null) {
|
|
664
|
+
pendingAt = Date.now();
|
|
665
|
+
} else {
|
|
666
|
+
if (Date.now() - pendingAt >= throttleMs) {
|
|
667
|
+
pendingAt = Date.now();
|
|
668
|
+
debounced.cancel();
|
|
669
|
+
debounced(...args);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
debounced(...args);
|
|
673
|
+
};
|
|
674
|
+
throttled.cancel = debounced.cancel;
|
|
675
|
+
return throttled;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
// src/hooks/useThrottle/useThrottle.ts
|
|
679
|
+
function useThrottle(callback, wait, options) {
|
|
680
|
+
const preservedCallback = usePreservedCallback(callback);
|
|
681
|
+
const preservedOptions = usePreservedReference(options ?? {});
|
|
682
|
+
const throttledCallback = (0, import_react20.useMemo)(() => {
|
|
683
|
+
return throttle(preservedCallback, wait, preservedOptions);
|
|
684
|
+
}, [preservedOptions, preservedCallback, wait]);
|
|
685
|
+
(0, import_react20.useEffect)(() => {
|
|
686
|
+
return () => {
|
|
687
|
+
throttledCallback.cancel();
|
|
688
|
+
};
|
|
689
|
+
}, [throttledCallback]);
|
|
690
|
+
return throttledCallback;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// src/hooks/useTimeout/useTimeout.ts
|
|
694
|
+
var import_react21 = require("react");
|
|
695
|
+
function useTimeout(callback, delay = 0) {
|
|
696
|
+
const preservedCallback = usePreservedCallback(callback);
|
|
697
|
+
(0, import_react21.useEffect)(() => {
|
|
698
|
+
const timeoutId = window.setTimeout(preservedCallback, delay);
|
|
699
|
+
return () => window.clearTimeout(timeoutId);
|
|
700
|
+
}, [delay, preservedCallback]);
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
// src/hooks/useToggle/useToggle.ts
|
|
704
|
+
var import_react22 = require("react");
|
|
705
|
+
function useToggle(initialValue = false) {
|
|
706
|
+
return (0, import_react22.useReducer)(toggle, initialValue);
|
|
707
|
+
}
|
|
708
|
+
var toggle = (state) => !state;
|
|
709
|
+
|
|
710
|
+
// src/utils/buildContext/buildContext.tsx
|
|
711
|
+
var import_react23 = require("react");
|
|
712
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
713
|
+
function buildContext(contextName, defaultContextValues) {
|
|
714
|
+
const Context = (0, import_react23.createContext)(defaultContextValues ?? void 0);
|
|
715
|
+
function Provider({ children, ...contextValues }) {
|
|
716
|
+
const value = (0, import_react23.useMemo)(
|
|
717
|
+
() => Object.keys(contextValues).length > 0 ? contextValues : null,
|
|
718
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
719
|
+
[...Object.values(contextValues)]
|
|
720
|
+
);
|
|
721
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Context.Provider, { value, children });
|
|
722
|
+
}
|
|
723
|
+
function useInnerContext() {
|
|
724
|
+
const context = (0, import_react23.useContext)(Context);
|
|
725
|
+
if (context != null) {
|
|
726
|
+
return context;
|
|
727
|
+
}
|
|
728
|
+
if (defaultContextValues != null) {
|
|
729
|
+
return defaultContextValues;
|
|
730
|
+
}
|
|
731
|
+
throw new Error(`\`${contextName}Context\` must be used within \`${contextName}Provider\``);
|
|
732
|
+
}
|
|
733
|
+
return [Provider, useInnerContext];
|
|
734
|
+
}
|
|
735
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
736
|
+
0 && (module.exports = {
|
|
737
|
+
ImpressionArea,
|
|
738
|
+
Separated,
|
|
739
|
+
SwitchCase,
|
|
740
|
+
buildContext,
|
|
741
|
+
mergeRefs,
|
|
742
|
+
useAsyncEffect,
|
|
743
|
+
useBooleanState,
|
|
744
|
+
useCallbackOnce,
|
|
745
|
+
useDebounce,
|
|
746
|
+
useImpressionRef,
|
|
747
|
+
useInputState,
|
|
748
|
+
useIntersectionObserver,
|
|
749
|
+
useInterval,
|
|
750
|
+
useLoading,
|
|
751
|
+
useOutsideClickEffect,
|
|
752
|
+
usePreservedCallback,
|
|
753
|
+
usePreservedReference,
|
|
754
|
+
usePrevious,
|
|
755
|
+
useRefEffect,
|
|
756
|
+
useStorageState,
|
|
757
|
+
useThrottle,
|
|
758
|
+
useTimeout,
|
|
759
|
+
useToggle,
|
|
760
|
+
useVisibilityEvent
|
|
761
|
+
});
|