@reactuses/core 3.0.3 → 4.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/dist/index.cjs +576 -568
- package/dist/index.d.ts +9 -23
- package/dist/index.mjs +576 -568
- package/package.json +2 -1
package/dist/index.mjs
CHANGED
|
@@ -50,7 +50,7 @@ const isIOS = isBrowser && ((_a = window == null ? void 0 : window.navigator) ==
|
|
|
50
50
|
!React.useId;
|
|
51
51
|
|
|
52
52
|
function guessSerializerType(rawInit) {
|
|
53
|
-
return rawInit == null ? "any" : rawInit instanceof Set ? "set" : rawInit instanceof Map ? "map" : rawInit instanceof Date ? "date" : typeof rawInit === "boolean" ? "boolean" : typeof rawInit === "string" ? "string" : typeof rawInit === "object" ? "object" : Array.isArray(rawInit) ? "object" : !Number.isNaN(rawInit) ? "number" : "any";
|
|
53
|
+
return rawInit == null || rawInit === void 0 ? "any" : rawInit instanceof Set ? "set" : rawInit instanceof Map ? "map" : rawInit instanceof Date ? "date" : typeof rawInit === "boolean" ? "boolean" : typeof rawInit === "string" ? "string" : typeof rawInit === "object" ? "object" : Array.isArray(rawInit) ? "object" : !Number.isNaN(rawInit) ? "number" : "any";
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
const useIsomorphicLayoutEffect = isBrowser ? useLayoutEffect : useEffect;
|
|
@@ -73,6 +73,283 @@ function useEvent(fn) {
|
|
|
73
73
|
}, []);
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
+
const defaultOptions = {};
|
|
77
|
+
const defaultOnError = (e) => {
|
|
78
|
+
console.error(e);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const StorageSerializers = {
|
|
82
|
+
boolean: {
|
|
83
|
+
read: (v) => v === "true",
|
|
84
|
+
write: (v) => String(v)
|
|
85
|
+
},
|
|
86
|
+
object: {
|
|
87
|
+
read: (v) => JSON.parse(v),
|
|
88
|
+
write: (v) => JSON.stringify(v)
|
|
89
|
+
},
|
|
90
|
+
number: {
|
|
91
|
+
read: (v) => Number.parseFloat(v),
|
|
92
|
+
write: (v) => String(v)
|
|
93
|
+
},
|
|
94
|
+
any: {
|
|
95
|
+
read: (v) => v,
|
|
96
|
+
write: (v) => String(v)
|
|
97
|
+
},
|
|
98
|
+
string: {
|
|
99
|
+
read: (v) => v,
|
|
100
|
+
write: (v) => String(v)
|
|
101
|
+
},
|
|
102
|
+
map: {
|
|
103
|
+
read: (v) => new Map(JSON.parse(v)),
|
|
104
|
+
write: (v) => JSON.stringify(Array.from(v.entries()))
|
|
105
|
+
},
|
|
106
|
+
set: {
|
|
107
|
+
read: (v) => new Set(JSON.parse(v)),
|
|
108
|
+
write: (v) => JSON.stringify(Array.from(v))
|
|
109
|
+
},
|
|
110
|
+
date: {
|
|
111
|
+
read: (v) => new Date(v),
|
|
112
|
+
write: (v) => v.toISOString()
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
const getInitialState$3 = (key, defaultValue, storage, serializer, onError) => {
|
|
116
|
+
if (defaultValue !== void 0) {
|
|
117
|
+
return defaultValue;
|
|
118
|
+
}
|
|
119
|
+
if (isBrowser) {
|
|
120
|
+
try {
|
|
121
|
+
const raw = storage == null ? void 0 : storage.getItem(key);
|
|
122
|
+
if (raw !== void 0 && raw !== null) {
|
|
123
|
+
return serializer == null ? void 0 : serializer.read(raw);
|
|
124
|
+
}
|
|
125
|
+
} catch (error) {
|
|
126
|
+
onError == null ? void 0 : onError(error);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (process.env.NODE_ENV !== "production") {
|
|
130
|
+
console.warn(
|
|
131
|
+
"`createStorage` When server side rendering, defaultValue should be defined to prevent a hydration mismatches."
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
return null;
|
|
135
|
+
};
|
|
136
|
+
function useStorage(key, defaultValue, getStorage = () => isBrowser ? sessionStorage : void 0, options = defaultOptions) {
|
|
137
|
+
let storage;
|
|
138
|
+
const { onError = defaultOnError, csrData } = options;
|
|
139
|
+
try {
|
|
140
|
+
storage = getStorage();
|
|
141
|
+
} catch (err) {
|
|
142
|
+
onError(err);
|
|
143
|
+
}
|
|
144
|
+
const type = guessSerializerType(defaultValue);
|
|
145
|
+
const serializer = useMemo(() => {
|
|
146
|
+
var _a;
|
|
147
|
+
return (_a = options.serializer) != null ? _a : StorageSerializers[type];
|
|
148
|
+
}, [options.serializer, type]);
|
|
149
|
+
const [state, setState] = useState(
|
|
150
|
+
getInitialState$3(key, defaultValue, storage, serializer, onError)
|
|
151
|
+
);
|
|
152
|
+
useEffect(() => {
|
|
153
|
+
const data = csrData ? isFunction$1(csrData) ? csrData() : csrData : defaultValue;
|
|
154
|
+
const getStoredValue = () => {
|
|
155
|
+
try {
|
|
156
|
+
const raw = storage == null ? void 0 : storage.getItem(key);
|
|
157
|
+
if (raw !== void 0 && raw !== null) {
|
|
158
|
+
return serializer.read(raw);
|
|
159
|
+
} else {
|
|
160
|
+
storage == null ? void 0 : storage.setItem(key, serializer.write(data));
|
|
161
|
+
return data;
|
|
162
|
+
}
|
|
163
|
+
} catch (e) {
|
|
164
|
+
onError(e);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
setState(getStoredValue());
|
|
168
|
+
}, [key, defaultValue, serializer, storage, onError, csrData]);
|
|
169
|
+
const updateState = useEvent(
|
|
170
|
+
(valOrFunc) => {
|
|
171
|
+
const currentState = isFunction$1(valOrFunc) ? valOrFunc(state) : valOrFunc;
|
|
172
|
+
setState(currentState);
|
|
173
|
+
if (currentState === null) {
|
|
174
|
+
storage == null ? void 0 : storage.removeItem(key);
|
|
175
|
+
} else {
|
|
176
|
+
try {
|
|
177
|
+
storage == null ? void 0 : storage.setItem(key, serializer.write(currentState));
|
|
178
|
+
} catch (e) {
|
|
179
|
+
onError(e);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
);
|
|
184
|
+
return [state, updateState];
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function useLocalStorage(key, defaultValue, options = defaultOptions) {
|
|
188
|
+
return useStorage(
|
|
189
|
+
key,
|
|
190
|
+
defaultValue,
|
|
191
|
+
() => isBrowser ? localStorage : void 0,
|
|
192
|
+
options
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function useSessionStorage(key, defaultValue, options = defaultOptions) {
|
|
197
|
+
return useStorage(
|
|
198
|
+
key,
|
|
199
|
+
defaultValue,
|
|
200
|
+
() => isBrowser ? sessionStorage : void 0,
|
|
201
|
+
options
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const toggleReducer = (state, nextValue) => typeof nextValue === "boolean" ? nextValue : !state;
|
|
206
|
+
function useToggle(initialValue) {
|
|
207
|
+
return useReducer(toggleReducer, initialValue);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function useInterval(callback, delay, options = defaultOptions) {
|
|
211
|
+
const immediate = options.immediate;
|
|
212
|
+
const savedCallback = useLatest(callback);
|
|
213
|
+
useEffect(() => {
|
|
214
|
+
if (immediate) {
|
|
215
|
+
savedCallback.current();
|
|
216
|
+
}
|
|
217
|
+
if (delay !== null) {
|
|
218
|
+
const interval = setInterval(() => savedCallback.current(), delay || 0);
|
|
219
|
+
return () => clearInterval(interval);
|
|
220
|
+
}
|
|
221
|
+
return void 0;
|
|
222
|
+
}, [delay, immediate]);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function useDarkMode(options) {
|
|
226
|
+
const {
|
|
227
|
+
selector = "html",
|
|
228
|
+
attribute = "class",
|
|
229
|
+
classNameDark = "",
|
|
230
|
+
classNameLight = "",
|
|
231
|
+
storageKey = "reactuses-color-scheme",
|
|
232
|
+
storage = () => isBrowser ? localStorage : void 0,
|
|
233
|
+
defaultValue = false
|
|
234
|
+
} = options;
|
|
235
|
+
const value = () => {
|
|
236
|
+
return window.matchMedia("(prefers-color-scheme: dark)").matches;
|
|
237
|
+
};
|
|
238
|
+
const [dark, setDark] = useStorage(
|
|
239
|
+
storageKey,
|
|
240
|
+
defaultValue,
|
|
241
|
+
storage,
|
|
242
|
+
{
|
|
243
|
+
csrData: value
|
|
244
|
+
}
|
|
245
|
+
);
|
|
246
|
+
useEffect(() => {
|
|
247
|
+
const element = window == null ? void 0 : window.document.querySelector(selector);
|
|
248
|
+
if (!element) {
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
if (attribute === "class") {
|
|
252
|
+
dark && classNameDark && element.classList.add(classNameDark);
|
|
253
|
+
!dark && classNameLight && element.classList.add(classNameLight);
|
|
254
|
+
} else {
|
|
255
|
+
dark && classNameDark && element.setAttribute(attribute, classNameDark);
|
|
256
|
+
!dark && classNameLight && element.setAttribute(attribute, classNameLight);
|
|
257
|
+
}
|
|
258
|
+
return () => {
|
|
259
|
+
if (!element) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (attribute === "class") {
|
|
263
|
+
dark && classNameDark && element.classList.remove(classNameDark);
|
|
264
|
+
!dark && classNameLight && element.classList.remove(classNameLight);
|
|
265
|
+
} else {
|
|
266
|
+
dark && classNameDark && element.removeAttribute(attribute);
|
|
267
|
+
!dark && classNameLight && element.removeAttribute(attribute);
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
}, [attribute, classNameDark, classNameLight, dark, selector]);
|
|
271
|
+
return [dark, () => setDark((dark2) => !dark2), setDark];
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const getInitialState$2 = (query, defaultState) => {
|
|
275
|
+
if (defaultState !== void 0) {
|
|
276
|
+
return defaultState;
|
|
277
|
+
}
|
|
278
|
+
if (isBrowser) {
|
|
279
|
+
return window.matchMedia(query).matches;
|
|
280
|
+
}
|
|
281
|
+
if (process.env.NODE_ENV !== "production") {
|
|
282
|
+
console.warn(
|
|
283
|
+
"`useMediaQuery` When server side rendering, defaultState should be defined to prevent a hydration mismatches."
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
return false;
|
|
287
|
+
};
|
|
288
|
+
function useMediaQuery(query, defaultState) {
|
|
289
|
+
const [state, setState] = useState(getInitialState$2(query, defaultState));
|
|
290
|
+
useEffect(() => {
|
|
291
|
+
var _a;
|
|
292
|
+
let mounted = true;
|
|
293
|
+
const mql = window.matchMedia(query);
|
|
294
|
+
const onChange = () => {
|
|
295
|
+
if (!mounted) {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
setState(!!mql.matches);
|
|
299
|
+
};
|
|
300
|
+
if ("addEventListener" in mql) {
|
|
301
|
+
mql.addEventListener("change", onChange);
|
|
302
|
+
} else {
|
|
303
|
+
(_a = mql.addListener) == null ? void 0 : _a.call(mql, onChange);
|
|
304
|
+
}
|
|
305
|
+
setState(mql.matches);
|
|
306
|
+
return () => {
|
|
307
|
+
var _a2;
|
|
308
|
+
mounted = false;
|
|
309
|
+
if ("removeEventListener" in mql) {
|
|
310
|
+
mql.removeEventListener("change", onChange);
|
|
311
|
+
} else {
|
|
312
|
+
(_a2 = mql.removeListener) == null ? void 0 : _a2.call(mql, onChange);
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
}, [query]);
|
|
316
|
+
return state;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function usePreferredDark(defaultState) {
|
|
320
|
+
return useMediaQuery("(prefers-color-scheme: dark)", defaultState);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function useMount(fn) {
|
|
324
|
+
if (isDev) {
|
|
325
|
+
if (!isFunction$1(fn)) {
|
|
326
|
+
console.error(
|
|
327
|
+
`useMount: parameter \`fn\` expected to be a function, but got "${typeof fn}".`
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
useEffect(() => {
|
|
332
|
+
fn == null ? void 0 : fn();
|
|
333
|
+
}, []);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function useUnmount(fn) {
|
|
337
|
+
if (isDev) {
|
|
338
|
+
if (!isFunction$1(fn)) {
|
|
339
|
+
console.error(
|
|
340
|
+
`useUnmount expected parameter is a function, got ${typeof fn}`
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
const fnRef = useLatest(fn);
|
|
345
|
+
useEffect(
|
|
346
|
+
() => () => {
|
|
347
|
+
fnRef.current();
|
|
348
|
+
},
|
|
349
|
+
[fnRef]
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
|
|
76
353
|
/** Detect free variable `global` from Node.js. */
|
|
77
354
|
var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;
|
|
78
355
|
|
|
@@ -2388,390 +2665,95 @@ function debounce(func, wait, options) {
|
|
|
2388
2665
|
*
|
|
2389
2666
|
* **Note:** This method supports comparing arrays, array buffers, booleans,
|
|
2390
2667
|
* date objects, error objects, maps, numbers, `Object` objects, regexes,
|
|
2391
|
-
* sets, strings, symbols, and typed arrays. `Object` objects are compared
|
|
2392
|
-
* by their own, not inherited, enumerable properties. Functions and DOM
|
|
2393
|
-
* nodes are compared by strict equality, i.e. `===`.
|
|
2394
|
-
*
|
|
2395
|
-
* @static
|
|
2396
|
-
* @memberOf _
|
|
2397
|
-
* @since 0.1.0
|
|
2398
|
-
* @category Lang
|
|
2399
|
-
* @param {*} value The value to compare.
|
|
2400
|
-
* @param {*} other The other value to compare.
|
|
2401
|
-
* @returns {boolean} Returns `true` if the values are equivalent, else `false`.
|
|
2402
|
-
* @example
|
|
2403
|
-
*
|
|
2404
|
-
* var object = { 'a': 1 };
|
|
2405
|
-
* var other = { 'a': 1 };
|
|
2406
|
-
*
|
|
2407
|
-
* _.isEqual(object, other);
|
|
2408
|
-
* // => true
|
|
2409
|
-
*
|
|
2410
|
-
* object === other;
|
|
2411
|
-
* // => false
|
|
2412
|
-
*/
|
|
2413
|
-
function isEqual(value, other) {
|
|
2414
|
-
return baseIsEqual(value, other);
|
|
2415
|
-
}
|
|
2416
|
-
|
|
2417
|
-
/** Error message constants. */
|
|
2418
|
-
var FUNC_ERROR_TEXT = 'Expected a function';
|
|
2419
|
-
|
|
2420
|
-
/**
|
|
2421
|
-
* Creates a throttled function that only invokes `func` at most once per
|
|
2422
|
-
* every `wait` milliseconds. The throttled function comes with a `cancel`
|
|
2423
|
-
* method to cancel delayed `func` invocations and a `flush` method to
|
|
2424
|
-
* immediately invoke them. Provide `options` to indicate whether `func`
|
|
2425
|
-
* should be invoked on the leading and/or trailing edge of the `wait`
|
|
2426
|
-
* timeout. The `func` is invoked with the last arguments provided to the
|
|
2427
|
-
* throttled function. Subsequent calls to the throttled function return the
|
|
2428
|
-
* result of the last `func` invocation.
|
|
2429
|
-
*
|
|
2430
|
-
* **Note:** If `leading` and `trailing` options are `true`, `func` is
|
|
2431
|
-
* invoked on the trailing edge of the timeout only if the throttled function
|
|
2432
|
-
* is invoked more than once during the `wait` timeout.
|
|
2433
|
-
*
|
|
2434
|
-
* If `wait` is `0` and `leading` is `false`, `func` invocation is deferred
|
|
2435
|
-
* until to the next tick, similar to `setTimeout` with a timeout of `0`.
|
|
2436
|
-
*
|
|
2437
|
-
* See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/)
|
|
2438
|
-
* for details over the differences between `_.throttle` and `_.debounce`.
|
|
2439
|
-
*
|
|
2440
|
-
* @static
|
|
2441
|
-
* @memberOf _
|
|
2442
|
-
* @since 0.1.0
|
|
2443
|
-
* @category Function
|
|
2444
|
-
* @param {Function} func The function to throttle.
|
|
2445
|
-
* @param {number} [wait=0] The number of milliseconds to throttle invocations to.
|
|
2446
|
-
* @param {Object} [options={}] The options object.
|
|
2447
|
-
* @param {boolean} [options.leading=true]
|
|
2448
|
-
* Specify invoking on the leading edge of the timeout.
|
|
2449
|
-
* @param {boolean} [options.trailing=true]
|
|
2450
|
-
* Specify invoking on the trailing edge of the timeout.
|
|
2451
|
-
* @returns {Function} Returns the new throttled function.
|
|
2452
|
-
* @example
|
|
2453
|
-
*
|
|
2454
|
-
* // Avoid excessively updating the position while scrolling.
|
|
2455
|
-
* jQuery(window).on('scroll', _.throttle(updatePosition, 100));
|
|
2456
|
-
*
|
|
2457
|
-
* // Invoke `renewToken` when the click event is fired, but not more than once every 5 minutes.
|
|
2458
|
-
* var throttled = _.throttle(renewToken, 300000, { 'trailing': false });
|
|
2459
|
-
* jQuery(element).on('click', throttled);
|
|
2460
|
-
*
|
|
2461
|
-
* // Cancel the trailing throttled invocation.
|
|
2462
|
-
* jQuery(window).on('popstate', throttled.cancel);
|
|
2463
|
-
*/
|
|
2464
|
-
function throttle(func, wait, options) {
|
|
2465
|
-
var leading = true,
|
|
2466
|
-
trailing = true;
|
|
2467
|
-
|
|
2468
|
-
if (typeof func != 'function') {
|
|
2469
|
-
throw new TypeError(FUNC_ERROR_TEXT);
|
|
2470
|
-
}
|
|
2471
|
-
if (isObject(options)) {
|
|
2472
|
-
leading = 'leading' in options ? !!options.leading : leading;
|
|
2473
|
-
trailing = 'trailing' in options ? !!options.trailing : trailing;
|
|
2474
|
-
}
|
|
2475
|
-
return debounce(func, wait, {
|
|
2476
|
-
'leading': leading,
|
|
2477
|
-
'maxWait': wait,
|
|
2478
|
-
'trailing': trailing
|
|
2479
|
-
});
|
|
2480
|
-
}
|
|
2481
|
-
|
|
2482
|
-
const isPrimitive$1 = (val) => val !== Object(val);
|
|
2483
|
-
function useCustomCompareEffect(effect, deps, depsEqual) {
|
|
2484
|
-
if (process.env.NODE_ENV !== "production") {
|
|
2485
|
-
if (!Array.isArray(deps) || !deps.length) {
|
|
2486
|
-
console.warn(
|
|
2487
|
-
"`useCustomCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
|
|
2488
|
-
);
|
|
2489
|
-
}
|
|
2490
|
-
if (deps.every(isPrimitive$1)) {
|
|
2491
|
-
console.warn(
|
|
2492
|
-
"`useCustomCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
|
|
2493
|
-
);
|
|
2494
|
-
}
|
|
2495
|
-
if (typeof depsEqual !== "function") {
|
|
2496
|
-
console.warn(
|
|
2497
|
-
"`useCustomCompareEffect` should be used with depsEqual callback for comparing deps list"
|
|
2498
|
-
);
|
|
2499
|
-
}
|
|
2500
|
-
}
|
|
2501
|
-
const ref = useRef(void 0);
|
|
2502
|
-
if (!ref.current || !depsEqual(deps, ref.current)) {
|
|
2503
|
-
ref.current = deps;
|
|
2504
|
-
}
|
|
2505
|
-
useEffect(effect, ref.current);
|
|
2506
|
-
}
|
|
2507
|
-
|
|
2508
|
-
const isPrimitive = (val) => val !== Object(val);
|
|
2509
|
-
function useDeepCompareEffect(effect, deps) {
|
|
2510
|
-
if (process.env.NODE_ENV !== "production") {
|
|
2511
|
-
if (!Array.isArray(deps) || !deps.length) {
|
|
2512
|
-
console.warn(
|
|
2513
|
-
"`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
|
|
2514
|
-
);
|
|
2515
|
-
}
|
|
2516
|
-
if (deps.every(isPrimitive)) {
|
|
2517
|
-
console.warn(
|
|
2518
|
-
"`useDeepCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
|
|
2519
|
-
);
|
|
2520
|
-
}
|
|
2521
|
-
}
|
|
2522
|
-
useCustomCompareEffect(effect, deps, isEqual);
|
|
2523
|
-
}
|
|
2524
|
-
|
|
2525
|
-
const StorageSerializers = {
|
|
2526
|
-
boolean: {
|
|
2527
|
-
read: (v) => v === "true",
|
|
2528
|
-
write: (v) => String(v)
|
|
2529
|
-
},
|
|
2530
|
-
object: {
|
|
2531
|
-
read: (v) => JSON.parse(v),
|
|
2532
|
-
write: (v) => JSON.stringify(v)
|
|
2533
|
-
},
|
|
2534
|
-
number: {
|
|
2535
|
-
read: (v) => Number.parseFloat(v),
|
|
2536
|
-
write: (v) => String(v)
|
|
2537
|
-
},
|
|
2538
|
-
any: {
|
|
2539
|
-
read: (v) => v,
|
|
2540
|
-
write: (v) => String(v)
|
|
2541
|
-
},
|
|
2542
|
-
string: {
|
|
2543
|
-
read: (v) => v,
|
|
2544
|
-
write: (v) => String(v)
|
|
2545
|
-
},
|
|
2546
|
-
map: {
|
|
2547
|
-
read: (v) => new Map(JSON.parse(v)),
|
|
2548
|
-
write: (v) => JSON.stringify(Array.from(v.entries()))
|
|
2549
|
-
},
|
|
2550
|
-
set: {
|
|
2551
|
-
read: (v) => new Set(JSON.parse(v)),
|
|
2552
|
-
write: (v) => JSON.stringify(Array.from(v))
|
|
2553
|
-
},
|
|
2554
|
-
date: {
|
|
2555
|
-
read: (v) => new Date(v),
|
|
2556
|
-
write: (v) => v.toISOString()
|
|
2557
|
-
}
|
|
2558
|
-
};
|
|
2559
|
-
function useStorage(key, defaults, getStorage, options = {}) {
|
|
2560
|
-
const defaultOnError = useCallback((e) => {
|
|
2561
|
-
console.error(e);
|
|
2562
|
-
}, []);
|
|
2563
|
-
let storage;
|
|
2564
|
-
const { onError = defaultOnError, csrData } = options;
|
|
2565
|
-
try {
|
|
2566
|
-
storage = getStorage();
|
|
2567
|
-
} catch (err) {
|
|
2568
|
-
onError(err);
|
|
2569
|
-
}
|
|
2570
|
-
const type = guessSerializerType(defaults);
|
|
2571
|
-
const serializer = useMemo(() => {
|
|
2572
|
-
var _a;
|
|
2573
|
-
return (_a = options.serializer) != null ? _a : StorageSerializers[type];
|
|
2574
|
-
}, [options.serializer, type]);
|
|
2575
|
-
const [state, setState] = useState(defaults);
|
|
2576
|
-
useDeepCompareEffect(() => {
|
|
2577
|
-
const data = csrData ? isFunction$1(csrData) ? csrData() : csrData : defaults;
|
|
2578
|
-
const getStoredValue = () => {
|
|
2579
|
-
try {
|
|
2580
|
-
const raw = storage == null ? void 0 : storage.getItem(key);
|
|
2581
|
-
if (raw !== void 0 && raw !== null) {
|
|
2582
|
-
return serializer.read(raw);
|
|
2583
|
-
} else {
|
|
2584
|
-
storage == null ? void 0 : storage.setItem(key, serializer.write(data));
|
|
2585
|
-
return data;
|
|
2586
|
-
}
|
|
2587
|
-
} catch (e) {
|
|
2588
|
-
onError(e);
|
|
2589
|
-
}
|
|
2590
|
-
};
|
|
2591
|
-
setState(getStoredValue());
|
|
2592
|
-
}, [key, defaults, serializer, storage, onError]);
|
|
2593
|
-
const updateState = useEvent(
|
|
2594
|
-
(valOrFunc) => {
|
|
2595
|
-
const currentState = isFunction$1(valOrFunc) ? valOrFunc(state) : valOrFunc;
|
|
2596
|
-
setState(currentState);
|
|
2597
|
-
if (currentState === null) {
|
|
2598
|
-
storage == null ? void 0 : storage.removeItem(key);
|
|
2599
|
-
} else {
|
|
2600
|
-
try {
|
|
2601
|
-
storage == null ? void 0 : storage.setItem(key, serializer.write(currentState));
|
|
2602
|
-
} catch (e) {
|
|
2603
|
-
onError(e);
|
|
2604
|
-
}
|
|
2605
|
-
}
|
|
2606
|
-
}
|
|
2607
|
-
);
|
|
2608
|
-
return [state, updateState];
|
|
2609
|
-
}
|
|
2610
|
-
|
|
2611
|
-
function useLocalStorage(key, defaults, options = {}) {
|
|
2612
|
-
return useStorage(
|
|
2613
|
-
key,
|
|
2614
|
-
defaults,
|
|
2615
|
-
() => isBrowser ? localStorage : void 0,
|
|
2616
|
-
options
|
|
2617
|
-
);
|
|
2618
|
-
}
|
|
2619
|
-
|
|
2620
|
-
function useSessionStorage(key, defaults, options = {}) {
|
|
2621
|
-
return useStorage(
|
|
2622
|
-
key,
|
|
2623
|
-
defaults,
|
|
2624
|
-
() => isBrowser ? sessionStorage : void 0,
|
|
2625
|
-
options
|
|
2626
|
-
);
|
|
2627
|
-
}
|
|
2628
|
-
|
|
2629
|
-
const toggleReducer = (state, nextValue) => typeof nextValue === "boolean" ? nextValue : !state;
|
|
2630
|
-
function useToggle(initialValue) {
|
|
2631
|
-
return useReducer(toggleReducer, initialValue);
|
|
2632
|
-
}
|
|
2633
|
-
|
|
2634
|
-
function useInterval(callback, delay, options) {
|
|
2635
|
-
const immediate = options == null ? void 0 : options.immediate;
|
|
2636
|
-
const savedCallback = useLatest(callback);
|
|
2637
|
-
useEffect(() => {
|
|
2638
|
-
if (immediate) {
|
|
2639
|
-
savedCallback.current();
|
|
2640
|
-
}
|
|
2641
|
-
if (delay !== null) {
|
|
2642
|
-
const interval = setInterval(() => savedCallback.current(), delay || 0);
|
|
2643
|
-
return () => clearInterval(interval);
|
|
2644
|
-
}
|
|
2645
|
-
return void 0;
|
|
2646
|
-
}, [delay, immediate]);
|
|
2647
|
-
}
|
|
2648
|
-
|
|
2649
|
-
function useDarkMode(options) {
|
|
2650
|
-
const {
|
|
2651
|
-
selector = "html",
|
|
2652
|
-
attribute = "class",
|
|
2653
|
-
classNameDark = "",
|
|
2654
|
-
classNameLight = "",
|
|
2655
|
-
storageKey = "reactuses-color-scheme",
|
|
2656
|
-
storage = () => isBrowser ? localStorage : void 0,
|
|
2657
|
-
defaultValue = false
|
|
2658
|
-
} = options;
|
|
2659
|
-
const value = () => {
|
|
2660
|
-
return window.matchMedia("(prefers-color-scheme: dark)").matches;
|
|
2661
|
-
};
|
|
2662
|
-
const [dark, setDark] = useStorage(
|
|
2663
|
-
storageKey,
|
|
2664
|
-
defaultValue,
|
|
2665
|
-
storage,
|
|
2666
|
-
{
|
|
2667
|
-
csrData: value
|
|
2668
|
-
}
|
|
2669
|
-
);
|
|
2670
|
-
useEffect(() => {
|
|
2671
|
-
const element = window == null ? void 0 : window.document.querySelector(selector);
|
|
2672
|
-
if (!element) {
|
|
2673
|
-
return;
|
|
2674
|
-
}
|
|
2675
|
-
if (attribute === "class") {
|
|
2676
|
-
dark && classNameDark && element.classList.add(classNameDark);
|
|
2677
|
-
!dark && classNameLight && element.classList.add(classNameLight);
|
|
2678
|
-
} else {
|
|
2679
|
-
dark && classNameDark && element.setAttribute(attribute, classNameDark);
|
|
2680
|
-
!dark && classNameLight && element.setAttribute(attribute, classNameLight);
|
|
2681
|
-
}
|
|
2682
|
-
return () => {
|
|
2683
|
-
if (!element) {
|
|
2684
|
-
return;
|
|
2685
|
-
}
|
|
2686
|
-
if (attribute === "class") {
|
|
2687
|
-
dark && classNameDark && element.classList.remove(classNameDark);
|
|
2688
|
-
!dark && classNameLight && element.classList.remove(classNameLight);
|
|
2689
|
-
} else {
|
|
2690
|
-
dark && classNameDark && element.removeAttribute(attribute);
|
|
2691
|
-
!dark && classNameLight && element.removeAttribute(attribute);
|
|
2692
|
-
}
|
|
2693
|
-
};
|
|
2694
|
-
}, [attribute, classNameDark, classNameLight, dark, selector]);
|
|
2695
|
-
return [dark, () => setDark((dark2) => !dark2), setDark];
|
|
2696
|
-
}
|
|
2697
|
-
|
|
2698
|
-
const getInitialState = (query, defaultState) => {
|
|
2699
|
-
if (defaultState !== void 0) {
|
|
2700
|
-
return defaultState;
|
|
2701
|
-
}
|
|
2702
|
-
if (isBrowser) {
|
|
2703
|
-
return window.matchMedia(query).matches;
|
|
2704
|
-
}
|
|
2705
|
-
if (process.env.NODE_ENV !== "production") {
|
|
2706
|
-
console.warn(
|
|
2707
|
-
"`useMediaQuery` When server side rendering, defaultState should be defined to prevent a hydration mismatches."
|
|
2708
|
-
);
|
|
2709
|
-
}
|
|
2710
|
-
return false;
|
|
2711
|
-
};
|
|
2712
|
-
function useMediaQuery(query, defaultState) {
|
|
2713
|
-
const [state, setState] = useState(getInitialState(query, defaultState));
|
|
2714
|
-
useEffect(() => {
|
|
2715
|
-
var _a;
|
|
2716
|
-
let mounted = true;
|
|
2717
|
-
const mql = window.matchMedia(query);
|
|
2718
|
-
const onChange = () => {
|
|
2719
|
-
if (!mounted) {
|
|
2720
|
-
return;
|
|
2721
|
-
}
|
|
2722
|
-
setState(!!mql.matches);
|
|
2723
|
-
};
|
|
2724
|
-
if ("addEventListener" in mql) {
|
|
2725
|
-
mql.addEventListener("change", onChange);
|
|
2726
|
-
} else {
|
|
2727
|
-
(_a = mql.addListener) == null ? void 0 : _a.call(mql, onChange);
|
|
2728
|
-
}
|
|
2729
|
-
setState(mql.matches);
|
|
2730
|
-
return () => {
|
|
2731
|
-
var _a2;
|
|
2732
|
-
mounted = false;
|
|
2733
|
-
if ("removeEventListener" in mql) {
|
|
2734
|
-
mql.removeEventListener("change", onChange);
|
|
2735
|
-
} else {
|
|
2736
|
-
(_a2 = mql.removeListener) == null ? void 0 : _a2.call(mql, onChange);
|
|
2737
|
-
}
|
|
2738
|
-
};
|
|
2739
|
-
}, [query]);
|
|
2740
|
-
return state;
|
|
2668
|
+
* sets, strings, symbols, and typed arrays. `Object` objects are compared
|
|
2669
|
+
* by their own, not inherited, enumerable properties. Functions and DOM
|
|
2670
|
+
* nodes are compared by strict equality, i.e. `===`.
|
|
2671
|
+
*
|
|
2672
|
+
* @static
|
|
2673
|
+
* @memberOf _
|
|
2674
|
+
* @since 0.1.0
|
|
2675
|
+
* @category Lang
|
|
2676
|
+
* @param {*} value The value to compare.
|
|
2677
|
+
* @param {*} other The other value to compare.
|
|
2678
|
+
* @returns {boolean} Returns `true` if the values are equivalent, else `false`.
|
|
2679
|
+
* @example
|
|
2680
|
+
*
|
|
2681
|
+
* var object = { 'a': 1 };
|
|
2682
|
+
* var other = { 'a': 1 };
|
|
2683
|
+
*
|
|
2684
|
+
* _.isEqual(object, other);
|
|
2685
|
+
* // => true
|
|
2686
|
+
*
|
|
2687
|
+
* object === other;
|
|
2688
|
+
* // => false
|
|
2689
|
+
*/
|
|
2690
|
+
function isEqual(value, other) {
|
|
2691
|
+
return baseIsEqual(value, other);
|
|
2741
2692
|
}
|
|
2742
2693
|
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
}
|
|
2694
|
+
/** Error message constants. */
|
|
2695
|
+
var FUNC_ERROR_TEXT = 'Expected a function';
|
|
2746
2696
|
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2697
|
+
/**
|
|
2698
|
+
* Creates a throttled function that only invokes `func` at most once per
|
|
2699
|
+
* every `wait` milliseconds. The throttled function comes with a `cancel`
|
|
2700
|
+
* method to cancel delayed `func` invocations and a `flush` method to
|
|
2701
|
+
* immediately invoke them. Provide `options` to indicate whether `func`
|
|
2702
|
+
* should be invoked on the leading and/or trailing edge of the `wait`
|
|
2703
|
+
* timeout. The `func` is invoked with the last arguments provided to the
|
|
2704
|
+
* throttled function. Subsequent calls to the throttled function return the
|
|
2705
|
+
* result of the last `func` invocation.
|
|
2706
|
+
*
|
|
2707
|
+
* **Note:** If `leading` and `trailing` options are `true`, `func` is
|
|
2708
|
+
* invoked on the trailing edge of the timeout only if the throttled function
|
|
2709
|
+
* is invoked more than once during the `wait` timeout.
|
|
2710
|
+
*
|
|
2711
|
+
* If `wait` is `0` and `leading` is `false`, `func` invocation is deferred
|
|
2712
|
+
* until to the next tick, similar to `setTimeout` with a timeout of `0`.
|
|
2713
|
+
*
|
|
2714
|
+
* See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/)
|
|
2715
|
+
* for details over the differences between `_.throttle` and `_.debounce`.
|
|
2716
|
+
*
|
|
2717
|
+
* @static
|
|
2718
|
+
* @memberOf _
|
|
2719
|
+
* @since 0.1.0
|
|
2720
|
+
* @category Function
|
|
2721
|
+
* @param {Function} func The function to throttle.
|
|
2722
|
+
* @param {number} [wait=0] The number of milliseconds to throttle invocations to.
|
|
2723
|
+
* @param {Object} [options={}] The options object.
|
|
2724
|
+
* @param {boolean} [options.leading=true]
|
|
2725
|
+
* Specify invoking on the leading edge of the timeout.
|
|
2726
|
+
* @param {boolean} [options.trailing=true]
|
|
2727
|
+
* Specify invoking on the trailing edge of the timeout.
|
|
2728
|
+
* @returns {Function} Returns the new throttled function.
|
|
2729
|
+
* @example
|
|
2730
|
+
*
|
|
2731
|
+
* // Avoid excessively updating the position while scrolling.
|
|
2732
|
+
* jQuery(window).on('scroll', _.throttle(updatePosition, 100));
|
|
2733
|
+
*
|
|
2734
|
+
* // Invoke `renewToken` when the click event is fired, but not more than once every 5 minutes.
|
|
2735
|
+
* var throttled = _.throttle(renewToken, 300000, { 'trailing': false });
|
|
2736
|
+
* jQuery(element).on('click', throttled);
|
|
2737
|
+
*
|
|
2738
|
+
* // Cancel the trailing throttled invocation.
|
|
2739
|
+
* jQuery(window).on('popstate', throttled.cancel);
|
|
2740
|
+
*/
|
|
2741
|
+
function throttle(func, wait, options) {
|
|
2742
|
+
var leading = true,
|
|
2743
|
+
trailing = true;
|
|
2759
2744
|
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
if (!isFunction$1(fn)) {
|
|
2763
|
-
console.error(
|
|
2764
|
-
`useUnmount expected parameter is a function, got ${typeof fn}`
|
|
2765
|
-
);
|
|
2766
|
-
}
|
|
2745
|
+
if (typeof func != 'function') {
|
|
2746
|
+
throw new TypeError(FUNC_ERROR_TEXT);
|
|
2767
2747
|
}
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2748
|
+
if (isObject(options)) {
|
|
2749
|
+
leading = 'leading' in options ? !!options.leading : leading;
|
|
2750
|
+
trailing = 'trailing' in options ? !!options.trailing : trailing;
|
|
2751
|
+
}
|
|
2752
|
+
return debounce(func, wait, {
|
|
2753
|
+
'leading': leading,
|
|
2754
|
+
'maxWait': wait,
|
|
2755
|
+
'trailing': trailing
|
|
2756
|
+
});
|
|
2775
2757
|
}
|
|
2776
2758
|
|
|
2777
2759
|
function useThrottleFn(fn, wait, options) {
|
|
@@ -2791,8 +2773,7 @@ function useThrottleFn(fn, wait, options) {
|
|
|
2791
2773
|
wait,
|
|
2792
2774
|
options
|
|
2793
2775
|
),
|
|
2794
|
-
|
|
2795
|
-
[]
|
|
2776
|
+
[wait, options]
|
|
2796
2777
|
);
|
|
2797
2778
|
useUnmount(() => {
|
|
2798
2779
|
throttled.cancel();
|
|
@@ -2836,8 +2817,7 @@ function useDebounceFn(fn, wait, options) {
|
|
|
2836
2817
|
wait,
|
|
2837
2818
|
options
|
|
2838
2819
|
),
|
|
2839
|
-
|
|
2840
|
-
[]
|
|
2820
|
+
[options, wait]
|
|
2841
2821
|
);
|
|
2842
2822
|
useUnmount(() => {
|
|
2843
2823
|
debounced.cancel();
|
|
@@ -2885,7 +2865,7 @@ function useUpdate() {
|
|
|
2885
2865
|
return update;
|
|
2886
2866
|
}
|
|
2887
2867
|
|
|
2888
|
-
function useTimeoutFn(cb, interval, options =
|
|
2868
|
+
function useTimeoutFn(cb, interval, options = defaultOptions) {
|
|
2889
2869
|
const { immediate = true } = options;
|
|
2890
2870
|
const [pending, setPending] = useState(false);
|
|
2891
2871
|
const savedCallback = useLatest(cb);
|
|
@@ -2975,10 +2955,10 @@ function useLatestElement(target, defaultElement) {
|
|
|
2975
2955
|
return latestElement;
|
|
2976
2956
|
}
|
|
2977
2957
|
|
|
2978
|
-
function useEventListener(eventName, handler, element, options) {
|
|
2958
|
+
function useEventListener(eventName, handler, element, options = defaultOptions) {
|
|
2979
2959
|
const savedHandler = useLatest(handler);
|
|
2980
2960
|
const targetElement = useLatestElement(element, defaultWindow);
|
|
2981
|
-
|
|
2961
|
+
useEffect(() => {
|
|
2982
2962
|
if (!(targetElement && targetElement.addEventListener)) {
|
|
2983
2963
|
return;
|
|
2984
2964
|
}
|
|
@@ -3132,7 +3112,7 @@ function useFavicon(href, baseUrl = "", rel = "icon") {
|
|
|
3132
3112
|
}, [baseUrl, href, rel]);
|
|
3133
3113
|
}
|
|
3134
3114
|
|
|
3135
|
-
function useMutationObserver(callback, target, options =
|
|
3115
|
+
function useMutationObserver(callback, target, options = defaultOptions) {
|
|
3136
3116
|
const callbackRef = useLatest(callback);
|
|
3137
3117
|
const observerRef = useRef();
|
|
3138
3118
|
const element = useLatestElement(target);
|
|
@@ -3141,7 +3121,7 @@ function useMutationObserver(callback, target, options = {}) {
|
|
|
3141
3121
|
observerRef.current.disconnect();
|
|
3142
3122
|
}
|
|
3143
3123
|
}, []);
|
|
3144
|
-
|
|
3124
|
+
useEffect(() => {
|
|
3145
3125
|
if (!element) {
|
|
3146
3126
|
return;
|
|
3147
3127
|
}
|
|
@@ -3152,13 +3132,56 @@ function useMutationObserver(callback, target, options = {}) {
|
|
|
3152
3132
|
return stop;
|
|
3153
3133
|
}
|
|
3154
3134
|
|
|
3135
|
+
const isPrimitive$1 = (val) => val !== Object(val);
|
|
3136
|
+
function useCustomCompareEffect(effect, deps, depsEqual) {
|
|
3137
|
+
if (process.env.NODE_ENV !== "production") {
|
|
3138
|
+
if (!Array.isArray(deps) || !deps.length) {
|
|
3139
|
+
console.warn(
|
|
3140
|
+
"`useCustomCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
|
|
3141
|
+
);
|
|
3142
|
+
}
|
|
3143
|
+
if (deps.every(isPrimitive$1)) {
|
|
3144
|
+
console.warn(
|
|
3145
|
+
"`useCustomCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
|
|
3146
|
+
);
|
|
3147
|
+
}
|
|
3148
|
+
if (typeof depsEqual !== "function") {
|
|
3149
|
+
console.warn(
|
|
3150
|
+
"`useCustomCompareEffect` should be used with depsEqual callback for comparing deps list"
|
|
3151
|
+
);
|
|
3152
|
+
}
|
|
3153
|
+
}
|
|
3154
|
+
const ref = useRef(void 0);
|
|
3155
|
+
if (!ref.current || !depsEqual(deps, ref.current)) {
|
|
3156
|
+
ref.current = deps;
|
|
3157
|
+
}
|
|
3158
|
+
useEffect(effect, ref.current);
|
|
3159
|
+
}
|
|
3160
|
+
|
|
3161
|
+
const isPrimitive = (val) => val !== Object(val);
|
|
3162
|
+
function useDeepCompareEffect(effect, deps) {
|
|
3163
|
+
if (process.env.NODE_ENV !== "production") {
|
|
3164
|
+
if (!Array.isArray(deps) || !deps.length) {
|
|
3165
|
+
console.warn(
|
|
3166
|
+
"`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
|
|
3167
|
+
);
|
|
3168
|
+
}
|
|
3169
|
+
if (deps.every(isPrimitive)) {
|
|
3170
|
+
console.warn(
|
|
3171
|
+
"`useDeepCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
|
|
3172
|
+
);
|
|
3173
|
+
}
|
|
3174
|
+
}
|
|
3175
|
+
useCustomCompareEffect(effect, deps, isEqual);
|
|
3176
|
+
}
|
|
3177
|
+
|
|
3155
3178
|
function useTitle(title) {
|
|
3156
3179
|
useEffect(() => {
|
|
3157
3180
|
document.title = title;
|
|
3158
3181
|
}, [title]);
|
|
3159
3182
|
}
|
|
3160
3183
|
|
|
3161
|
-
function useScriptTag(src, onLoaded = noop, options =
|
|
3184
|
+
function useScriptTag(src, onLoaded = noop, options = defaultOptions) {
|
|
3162
3185
|
const {
|
|
3163
3186
|
immediate = true,
|
|
3164
3187
|
manual = false,
|
|
@@ -3312,7 +3335,7 @@ const preventDefault$1 = (ev) => {
|
|
|
3312
3335
|
ev.preventDefault();
|
|
3313
3336
|
}
|
|
3314
3337
|
};
|
|
3315
|
-
function useLongPress(callback, { isPreventDefault = true, delay = 300 } =
|
|
3338
|
+
function useLongPress(callback, { isPreventDefault = true, delay = 300 } = defaultOptions) {
|
|
3316
3339
|
const timeout = useRef();
|
|
3317
3340
|
const target = useRef();
|
|
3318
3341
|
const start = useCallback(
|
|
@@ -3498,7 +3521,7 @@ function useMediaDevices(options = {}) {
|
|
|
3498
3521
|
return [state, ensurePermissions];
|
|
3499
3522
|
}
|
|
3500
3523
|
|
|
3501
|
-
function useTextDirection(options =
|
|
3524
|
+
function useTextDirection(options = defaultOptions) {
|
|
3502
3525
|
const { selector = "html", initialValue = "ltr" } = options;
|
|
3503
3526
|
const getValue = () => {
|
|
3504
3527
|
var _a, _b;
|
|
@@ -3575,10 +3598,10 @@ function useMouse(target) {
|
|
|
3575
3598
|
return state;
|
|
3576
3599
|
}
|
|
3577
3600
|
|
|
3578
|
-
function useFps(options) {
|
|
3601
|
+
function useFps(options = defaultOptions) {
|
|
3579
3602
|
var _a;
|
|
3580
3603
|
const [fps, setFps] = useState(0);
|
|
3581
|
-
const every = (_a = options
|
|
3604
|
+
const every = (_a = options.every) != null ? _a : 10;
|
|
3582
3605
|
const last = useRef(performance.now());
|
|
3583
3606
|
const ticks = useRef(0);
|
|
3584
3607
|
useRafFn(() => {
|
|
@@ -3605,7 +3628,7 @@ const initCoord = {
|
|
|
3605
3628
|
heading: null,
|
|
3606
3629
|
speed: null
|
|
3607
3630
|
};
|
|
3608
|
-
function useGeolocation(options =
|
|
3631
|
+
function useGeolocation(options = defaultOptions) {
|
|
3609
3632
|
const {
|
|
3610
3633
|
enableHighAccuracy = true,
|
|
3611
3634
|
maximumAge = 3e4,
|
|
@@ -3831,20 +3854,17 @@ var screenfull$1 = {exports: {}};
|
|
|
3831
3854
|
|
|
3832
3855
|
var screenfull = screenfull$1.exports;
|
|
3833
3856
|
|
|
3834
|
-
function useFullscreen(target, options) {
|
|
3835
|
-
const { onExit, onEnter } = options
|
|
3836
|
-
const onExitRef = useLatest(onExit);
|
|
3837
|
-
const onEnterRef = useLatest(onEnter);
|
|
3857
|
+
function useFullscreen(target, options = defaultOptions) {
|
|
3858
|
+
const { onExit, onEnter } = options;
|
|
3838
3859
|
const [state, setState] = useState(false);
|
|
3839
3860
|
const onChange = () => {
|
|
3840
|
-
var _a, _b;
|
|
3841
3861
|
if (screenfull.isEnabled) {
|
|
3842
3862
|
const { isFullscreen } = screenfull;
|
|
3843
3863
|
if (isFullscreen) {
|
|
3844
|
-
|
|
3864
|
+
onEnter == null ? void 0 : onEnter();
|
|
3845
3865
|
} else {
|
|
3846
3866
|
screenfull.off("change", onChange);
|
|
3847
|
-
|
|
3867
|
+
onExit == null ? void 0 : onExit();
|
|
3848
3868
|
}
|
|
3849
3869
|
setState(isFullscreen);
|
|
3850
3870
|
}
|
|
@@ -3986,7 +4006,7 @@ function useOrientation(initialState = defaultState$1) {
|
|
|
3986
4006
|
return [state, lockOrientation, unlockOrientation];
|
|
3987
4007
|
}
|
|
3988
4008
|
|
|
3989
|
-
function useIntersectionObserver(target, callback, options =
|
|
4009
|
+
function useIntersectionObserver(target, callback, options = defaultOptions) {
|
|
3990
4010
|
const savedCallback = useLatest(callback);
|
|
3991
4011
|
const observerRef = useRef();
|
|
3992
4012
|
const element = useLatestElement(target);
|
|
@@ -3995,7 +4015,7 @@ function useIntersectionObserver(target, callback, options = {}) {
|
|
|
3995
4015
|
observerRef.current.disconnect();
|
|
3996
4016
|
}
|
|
3997
4017
|
}, []);
|
|
3998
|
-
|
|
4018
|
+
useEffect(() => {
|
|
3999
4019
|
if (!element) {
|
|
4000
4020
|
return;
|
|
4001
4021
|
}
|
|
@@ -4024,14 +4044,24 @@ function usePageLeave() {
|
|
|
4024
4044
|
return isLeft;
|
|
4025
4045
|
}
|
|
4026
4046
|
|
|
4027
|
-
|
|
4028
|
-
|
|
4029
|
-
|
|
4030
|
-
|
|
4031
|
-
|
|
4032
|
-
|
|
4033
|
-
|
|
4034
|
-
|
|
4047
|
+
const getInitialState$1 = (defaultValue) => {
|
|
4048
|
+
if (defaultValue !== void 0) {
|
|
4049
|
+
return defaultValue;
|
|
4050
|
+
}
|
|
4051
|
+
if (isBrowser) {
|
|
4052
|
+
return document.visibilityState;
|
|
4053
|
+
}
|
|
4054
|
+
if (process.env.NODE_ENV !== "production") {
|
|
4055
|
+
console.warn(
|
|
4056
|
+
"`useDocumentVisibility` When server side rendering, defaultValue should be defined to prevent a hydration mismatches."
|
|
4057
|
+
);
|
|
4058
|
+
}
|
|
4059
|
+
return "visible";
|
|
4060
|
+
};
|
|
4061
|
+
function useDocumentVisibility(defaultValue) {
|
|
4062
|
+
const [visible, setVisible] = useState(
|
|
4063
|
+
getInitialState$1(defaultValue)
|
|
4064
|
+
);
|
|
4035
4065
|
useEventListener(
|
|
4036
4066
|
"visibilitychange",
|
|
4037
4067
|
() => {
|
|
@@ -4042,7 +4072,7 @@ function useDocumentVisibility() {
|
|
|
4042
4072
|
return visible;
|
|
4043
4073
|
}
|
|
4044
4074
|
|
|
4045
|
-
function useResizeObserver(target, callback, options =
|
|
4075
|
+
function useResizeObserver(target, callback, options = defaultOptions) {
|
|
4046
4076
|
const savedCallback = useLatest(callback);
|
|
4047
4077
|
const observerRef = useRef();
|
|
4048
4078
|
const element = useLatestElement(target);
|
|
@@ -4051,7 +4081,7 @@ function useResizeObserver(target, callback, options = {}) {
|
|
|
4051
4081
|
observerRef.current.disconnect();
|
|
4052
4082
|
}
|
|
4053
4083
|
}, []);
|
|
4054
|
-
|
|
4084
|
+
useEffect(() => {
|
|
4055
4085
|
if (!element) {
|
|
4056
4086
|
return;
|
|
4057
4087
|
}
|
|
@@ -4108,17 +4138,17 @@ function useDropZone(target, onDrop) {
|
|
|
4108
4138
|
}
|
|
4109
4139
|
|
|
4110
4140
|
var __defProp$2 = Object.defineProperty;
|
|
4111
|
-
var __getOwnPropSymbols$
|
|
4112
|
-
var __hasOwnProp$
|
|
4113
|
-
var __propIsEnum$
|
|
4141
|
+
var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
|
|
4142
|
+
var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
|
|
4143
|
+
var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
|
|
4114
4144
|
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4115
4145
|
var __spreadValues$2 = (a, b) => {
|
|
4116
4146
|
for (var prop in b || (b = {}))
|
|
4117
|
-
if (__hasOwnProp$
|
|
4147
|
+
if (__hasOwnProp$2.call(b, prop))
|
|
4118
4148
|
__defNormalProp$2(a, prop, b[prop]);
|
|
4119
|
-
if (__getOwnPropSymbols$
|
|
4120
|
-
for (var prop of __getOwnPropSymbols$
|
|
4121
|
-
if (__propIsEnum$
|
|
4149
|
+
if (__getOwnPropSymbols$2)
|
|
4150
|
+
for (var prop of __getOwnPropSymbols$2(b)) {
|
|
4151
|
+
if (__propIsEnum$2.call(b, prop))
|
|
4122
4152
|
__defNormalProp$2(a, prop, b[prop]);
|
|
4123
4153
|
}
|
|
4124
4154
|
return a;
|
|
@@ -4127,7 +4157,7 @@ const DEFAULT_OPTIONS = {
|
|
|
4127
4157
|
multiple: true,
|
|
4128
4158
|
accept: "*"
|
|
4129
4159
|
};
|
|
4130
|
-
function useFileDialog(options =
|
|
4160
|
+
function useFileDialog(options = defaultOptions) {
|
|
4131
4161
|
const [files, setFiles] = useState(null);
|
|
4132
4162
|
const inputRef = useRef();
|
|
4133
4163
|
const initFn = useCallback(() => {
|
|
@@ -4163,7 +4193,11 @@ function useFileDialog(options = {}) {
|
|
|
4163
4193
|
}
|
|
4164
4194
|
|
|
4165
4195
|
const ARRIVED_STATE_THRESHOLD_PIXELS = 1;
|
|
4166
|
-
|
|
4196
|
+
const defaultListerOptions = {
|
|
4197
|
+
capture: false,
|
|
4198
|
+
passive: true
|
|
4199
|
+
};
|
|
4200
|
+
function useScroll(target, options = defaultOptions) {
|
|
4167
4201
|
const {
|
|
4168
4202
|
throttle = 0,
|
|
4169
4203
|
idle = 200,
|
|
@@ -4175,10 +4209,7 @@ function useScroll(target, options = {}) {
|
|
|
4175
4209
|
top: 0,
|
|
4176
4210
|
bottom: 0
|
|
4177
4211
|
},
|
|
4178
|
-
eventListenerOptions =
|
|
4179
|
-
capture: false,
|
|
4180
|
-
passive: true
|
|
4181
|
-
}
|
|
4212
|
+
eventListenerOptions = defaultListerOptions
|
|
4182
4213
|
} = options;
|
|
4183
4214
|
const [x, setX] = useState(0);
|
|
4184
4215
|
const [y, setY] = useState(0);
|
|
@@ -4237,17 +4268,17 @@ function useScroll(target, options = {}) {
|
|
|
4237
4268
|
var __defProp$1 = Object.defineProperty;
|
|
4238
4269
|
var __defProps = Object.defineProperties;
|
|
4239
4270
|
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4240
|
-
var __getOwnPropSymbols$
|
|
4241
|
-
var __hasOwnProp$
|
|
4242
|
-
var __propIsEnum$
|
|
4271
|
+
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
|
|
4272
|
+
var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
|
|
4273
|
+
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
|
|
4243
4274
|
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4244
4275
|
var __spreadValues$1 = (a, b) => {
|
|
4245
4276
|
for (var prop in b || (b = {}))
|
|
4246
|
-
if (__hasOwnProp$
|
|
4277
|
+
if (__hasOwnProp$1.call(b, prop))
|
|
4247
4278
|
__defNormalProp$1(a, prop, b[prop]);
|
|
4248
|
-
if (__getOwnPropSymbols$
|
|
4249
|
-
for (var prop of __getOwnPropSymbols$
|
|
4250
|
-
if (__propIsEnum$
|
|
4279
|
+
if (__getOwnPropSymbols$1)
|
|
4280
|
+
for (var prop of __getOwnPropSymbols$1(b)) {
|
|
4281
|
+
if (__propIsEnum$1.call(b, prop))
|
|
4251
4282
|
__defNormalProp$1(a, prop, b[prop]);
|
|
4252
4283
|
}
|
|
4253
4284
|
return a;
|
|
@@ -4273,7 +4304,7 @@ var __async$3 = (__this, __arguments, generator) => {
|
|
|
4273
4304
|
step((generator = generator.apply(__this, __arguments)).next());
|
|
4274
4305
|
});
|
|
4275
4306
|
};
|
|
4276
|
-
function useInfiniteScroll(target, onLoadMore, options =
|
|
4307
|
+
function useInfiniteScroll(target, onLoadMore, options = defaultOptions) {
|
|
4277
4308
|
var _a, _b;
|
|
4278
4309
|
const savedLoadMore = useLatest(onLoadMore);
|
|
4279
4310
|
const direction = (_a = options.direction) != null ? _a : "bottom";
|
|
@@ -4283,10 +4314,8 @@ function useInfiniteScroll(target, onLoadMore, options = {}) {
|
|
|
4283
4314
|
}, options.offset)
|
|
4284
4315
|
}));
|
|
4285
4316
|
const element = useLatestElement(target);
|
|
4286
|
-
const latestOptions = useLatest(options);
|
|
4287
4317
|
const di = state[3][direction];
|
|
4288
4318
|
useUpdateEffect(() => {
|
|
4289
|
-
const opts = latestOptions.current;
|
|
4290
4319
|
const fn = () => __async$3(this, null, function* () {
|
|
4291
4320
|
var _a2, _b2;
|
|
4292
4321
|
const previous = {
|
|
@@ -4294,7 +4323,7 @@ function useInfiniteScroll(target, onLoadMore, options = {}) {
|
|
|
4294
4323
|
width: (_b2 = element == null ? void 0 : element.scrollWidth) != null ? _b2 : 0
|
|
4295
4324
|
};
|
|
4296
4325
|
yield savedLoadMore.current(state);
|
|
4297
|
-
if (
|
|
4326
|
+
if (options.preserveScrollPosition && element) {
|
|
4298
4327
|
element.scrollTo({
|
|
4299
4328
|
top: element.scrollHeight - previous.height,
|
|
4300
4329
|
left: element.scrollWidth - previous.width
|
|
@@ -4302,7 +4331,7 @@ function useInfiniteScroll(target, onLoadMore, options = {}) {
|
|
|
4302
4331
|
}
|
|
4303
4332
|
});
|
|
4304
4333
|
fn();
|
|
4305
|
-
}, [di]);
|
|
4334
|
+
}, [di, options.preserveScrollPosition]);
|
|
4306
4335
|
}
|
|
4307
4336
|
|
|
4308
4337
|
const defaultEvents = [
|
|
@@ -4311,7 +4340,7 @@ const defaultEvents = [
|
|
|
4311
4340
|
"keydown",
|
|
4312
4341
|
"keyup"
|
|
4313
4342
|
];
|
|
4314
|
-
function useKeyModifier(modifier, options =
|
|
4343
|
+
function useKeyModifier(modifier, options = defaultOptions) {
|
|
4315
4344
|
const { events = defaultEvents, initial = false } = options;
|
|
4316
4345
|
const [state, setState] = useState(initial);
|
|
4317
4346
|
useMount(() => {
|
|
@@ -4335,7 +4364,8 @@ function useKeyModifier(modifier, options = {}) {
|
|
|
4335
4364
|
return state;
|
|
4336
4365
|
}
|
|
4337
4366
|
|
|
4338
|
-
|
|
4367
|
+
const listenerOptions$2 = { passive: true };
|
|
4368
|
+
function useMousePressed(target, options = defaultOptions) {
|
|
4339
4369
|
const { touch = true, drag = true, initialValue = false } = options;
|
|
4340
4370
|
const [pressed, setPressed] = useState(initialValue);
|
|
4341
4371
|
const [sourceType, setSourceType] = useState(null);
|
|
@@ -4351,31 +4381,27 @@ function useMousePressed(target, options = {}) {
|
|
|
4351
4381
|
setPressed(false);
|
|
4352
4382
|
setSourceType(null);
|
|
4353
4383
|
}, []);
|
|
4354
|
-
useEventListener("mousedown", onPressed("mouse"), target,
|
|
4355
|
-
useEventListener("mouseleave", onReleased, () => window,
|
|
4356
|
-
useEventListener("mouseup", onReleased, () => window,
|
|
4384
|
+
useEventListener("mousedown", onPressed("mouse"), target, listenerOptions$2);
|
|
4385
|
+
useEventListener("mouseleave", onReleased, () => window, listenerOptions$2);
|
|
4386
|
+
useEventListener("mouseup", onReleased, () => window, listenerOptions$2);
|
|
4357
4387
|
useEffect(() => {
|
|
4358
4388
|
if (drag) {
|
|
4359
|
-
element == null ? void 0 : element.addEventListener(
|
|
4360
|
-
|
|
4361
|
-
|
|
4362
|
-
|
|
4363
|
-
|
|
4364
|
-
|
|
4365
|
-
element == null ? void 0 : element.addEventListener("dragend", onReleased,
|
|
4366
|
-
passive: true
|
|
4367
|
-
});
|
|
4389
|
+
element == null ? void 0 : element.addEventListener(
|
|
4390
|
+
"dragstart",
|
|
4391
|
+
onPressed("mouse"),
|
|
4392
|
+
listenerOptions$2
|
|
4393
|
+
);
|
|
4394
|
+
element == null ? void 0 : element.addEventListener("drop", onReleased, listenerOptions$2);
|
|
4395
|
+
element == null ? void 0 : element.addEventListener("dragend", onReleased, listenerOptions$2);
|
|
4368
4396
|
}
|
|
4369
4397
|
if (touch) {
|
|
4370
|
-
element == null ? void 0 : element.addEventListener(
|
|
4371
|
-
|
|
4372
|
-
|
|
4373
|
-
|
|
4374
|
-
|
|
4375
|
-
|
|
4376
|
-
element == null ? void 0 : element.addEventListener("touchcancel", onReleased,
|
|
4377
|
-
passive: true
|
|
4378
|
-
});
|
|
4398
|
+
element == null ? void 0 : element.addEventListener(
|
|
4399
|
+
"touchstart",
|
|
4400
|
+
onPressed("touch"),
|
|
4401
|
+
listenerOptions$2
|
|
4402
|
+
);
|
|
4403
|
+
element == null ? void 0 : element.addEventListener("touchend", onReleased, listenerOptions$2);
|
|
4404
|
+
element == null ? void 0 : element.addEventListener("touchcancel", onReleased, listenerOptions$2);
|
|
4379
4405
|
}
|
|
4380
4406
|
return () => {
|
|
4381
4407
|
if (drag) {
|
|
@@ -4446,7 +4472,7 @@ function useScrollLock(target, initialState = false) {
|
|
|
4446
4472
|
return [locked, set];
|
|
4447
4473
|
}
|
|
4448
4474
|
|
|
4449
|
-
function useElementSize(target, options =
|
|
4475
|
+
function useElementSize(target, options = defaultOptions) {
|
|
4450
4476
|
const { box = "content-box" } = options;
|
|
4451
4477
|
const [width, setWidth] = useState(0);
|
|
4452
4478
|
const [height, setHeight] = useState(0);
|
|
@@ -4473,44 +4499,38 @@ function useVirtualList(list = [], options) {
|
|
|
4473
4499
|
const [currentList, setCurrentList] = useState([]);
|
|
4474
4500
|
const { itemHeight, overscan = 5, containerHeight = 300 } = options;
|
|
4475
4501
|
const state = useRef({ start: 0, end: 10 });
|
|
4476
|
-
const getViewCapacity =
|
|
4477
|
-
(
|
|
4478
|
-
|
|
4479
|
-
|
|
4480
|
-
|
|
4481
|
-
|
|
4482
|
-
|
|
4483
|
-
|
|
4484
|
-
|
|
4485
|
-
|
|
4486
|
-
|
|
4487
|
-
|
|
4488
|
-
|
|
4489
|
-
break;
|
|
4490
|
-
}
|
|
4502
|
+
const getViewCapacity = (containerHeight2) => {
|
|
4503
|
+
if (typeof itemHeight === "number") {
|
|
4504
|
+
return Math.ceil(containerHeight2 / itemHeight);
|
|
4505
|
+
}
|
|
4506
|
+
const { start = 0 } = state.current;
|
|
4507
|
+
let sum = 0;
|
|
4508
|
+
let capacity = 0;
|
|
4509
|
+
for (let i = start; i < list.length; i++) {
|
|
4510
|
+
const height2 = itemHeight(i);
|
|
4511
|
+
sum += height2;
|
|
4512
|
+
if (sum >= containerHeight2) {
|
|
4513
|
+
capacity = i;
|
|
4514
|
+
break;
|
|
4491
4515
|
}
|
|
4492
|
-
|
|
4493
|
-
|
|
4494
|
-
|
|
4495
|
-
)
|
|
4496
|
-
|
|
4497
|
-
|
|
4498
|
-
|
|
4499
|
-
|
|
4500
|
-
|
|
4501
|
-
|
|
4502
|
-
|
|
4503
|
-
|
|
4504
|
-
|
|
4505
|
-
|
|
4506
|
-
offset = i;
|
|
4507
|
-
break;
|
|
4508
|
-
}
|
|
4516
|
+
}
|
|
4517
|
+
return capacity - start;
|
|
4518
|
+
};
|
|
4519
|
+
const getOffset = (scrollTop) => {
|
|
4520
|
+
if (typeof itemHeight === "number")
|
|
4521
|
+
return Math.floor(scrollTop / itemHeight) + 1;
|
|
4522
|
+
let sum = 0;
|
|
4523
|
+
let offset = 0;
|
|
4524
|
+
for (let i = 0; i < list.length; i++) {
|
|
4525
|
+
const height2 = itemHeight(i);
|
|
4526
|
+
sum += height2;
|
|
4527
|
+
if (sum >= scrollTop) {
|
|
4528
|
+
offset = i;
|
|
4529
|
+
break;
|
|
4509
4530
|
}
|
|
4510
|
-
|
|
4511
|
-
|
|
4512
|
-
|
|
4513
|
-
);
|
|
4531
|
+
}
|
|
4532
|
+
return offset + 1;
|
|
4533
|
+
};
|
|
4514
4534
|
const calculateRange = useEvent(() => {
|
|
4515
4535
|
const element = containerRef.current;
|
|
4516
4536
|
if (element != null) {
|
|
@@ -4522,17 +4542,18 @@ function useVirtualList(list = [], options) {
|
|
|
4522
4542
|
start: from < 0 ? 0 : from,
|
|
4523
4543
|
end: to > list.length ? list.length : to
|
|
4524
4544
|
};
|
|
4545
|
+
const { start, end } = state.current;
|
|
4525
4546
|
setCurrentList(
|
|
4526
|
-
list.slice(
|
|
4547
|
+
list.slice(start, end).map((ele, index) => ({
|
|
4527
4548
|
data: ele,
|
|
4528
|
-
index: index +
|
|
4549
|
+
index: index + start
|
|
4529
4550
|
}))
|
|
4530
4551
|
);
|
|
4531
4552
|
}
|
|
4532
4553
|
});
|
|
4533
4554
|
useEffect(() => {
|
|
4534
4555
|
calculateRange();
|
|
4535
|
-
}, [width, height, list
|
|
4556
|
+
}, [width, height, list]);
|
|
4536
4557
|
const totalHeight = useMemo(() => {
|
|
4537
4558
|
if (typeof itemHeight === "number") {
|
|
4538
4559
|
return list.length * itemHeight;
|
|
@@ -4550,16 +4571,7 @@ function useVirtualList(list = [], options) {
|
|
|
4550
4571
|
},
|
|
4551
4572
|
[itemHeight, list]
|
|
4552
4573
|
);
|
|
4553
|
-
const
|
|
4554
|
-
if (containerRef.current) {
|
|
4555
|
-
containerRef.current.scrollTop = getDistanceTop(index);
|
|
4556
|
-
calculateRange();
|
|
4557
|
-
}
|
|
4558
|
-
});
|
|
4559
|
-
const offsetTop = useMemo(
|
|
4560
|
-
() => getDistanceTop(state.current.start),
|
|
4561
|
-
[getDistanceTop]
|
|
4562
|
-
);
|
|
4574
|
+
const offsetTop = getDistanceTop(state.current.start);
|
|
4563
4575
|
const wrapperProps = useMemo(() => {
|
|
4564
4576
|
return {
|
|
4565
4577
|
style: {
|
|
@@ -4568,7 +4580,13 @@ function useVirtualList(list = [], options) {
|
|
|
4568
4580
|
marginTop: `${offsetTop}px`
|
|
4569
4581
|
}
|
|
4570
4582
|
};
|
|
4571
|
-
}, [
|
|
4583
|
+
}, [totalHeight, offsetTop]);
|
|
4584
|
+
const scrollTo = (index) => {
|
|
4585
|
+
if (containerRef.current) {
|
|
4586
|
+
containerRef.current.scrollTop = getDistanceTop(index);
|
|
4587
|
+
calculateRange();
|
|
4588
|
+
}
|
|
4589
|
+
};
|
|
4572
4590
|
const containerStyle = useMemo(() => {
|
|
4573
4591
|
return { overflowY: "auto", height: containerHeight };
|
|
4574
4592
|
}, [containerHeight]);
|
|
@@ -4694,7 +4712,7 @@ function useDraggable(target, options = {}) {
|
|
|
4694
4712
|
return [position.x, position.y, !!pressedDelta];
|
|
4695
4713
|
}
|
|
4696
4714
|
|
|
4697
|
-
function useElementBounding(target, options =
|
|
4715
|
+
function useElementBounding(target, options = defaultOptions) {
|
|
4698
4716
|
const {
|
|
4699
4717
|
reset = true,
|
|
4700
4718
|
windowResize = true,
|
|
@@ -4769,7 +4787,7 @@ function useElementBounding(target, options = {}) {
|
|
|
4769
4787
|
};
|
|
4770
4788
|
}
|
|
4771
4789
|
|
|
4772
|
-
function useElementVisibility(target, options =
|
|
4790
|
+
function useElementVisibility(target, options = defaultOptions) {
|
|
4773
4791
|
const [visible, setVisible] = useState(false);
|
|
4774
4792
|
const callback = useCallback((entries) => {
|
|
4775
4793
|
const rect = entries[0].boundingClientRect;
|
|
@@ -4781,13 +4799,11 @@ function useElementVisibility(target, options = {}) {
|
|
|
4781
4799
|
return [visible, stop];
|
|
4782
4800
|
}
|
|
4783
4801
|
|
|
4784
|
-
function useWindowsFocus() {
|
|
4785
|
-
const [focused, setFocused] = useState(
|
|
4786
|
-
|
|
4787
|
-
|
|
4788
|
-
|
|
4789
|
-
return window.document.hasFocus();
|
|
4790
|
-
});
|
|
4802
|
+
function useWindowsFocus(defauleValue = false) {
|
|
4803
|
+
const [focused, setFocused] = useState(defauleValue);
|
|
4804
|
+
useEffect(() => {
|
|
4805
|
+
setFocused(window.document.hasFocus());
|
|
4806
|
+
}, []);
|
|
4791
4807
|
useEventListener("blur", () => {
|
|
4792
4808
|
setFocused(false);
|
|
4793
4809
|
});
|
|
@@ -4815,6 +4831,10 @@ function useWindowSize() {
|
|
|
4815
4831
|
return windowSize;
|
|
4816
4832
|
}
|
|
4817
4833
|
|
|
4834
|
+
const listenerOptions$1 = {
|
|
4835
|
+
capture: false,
|
|
4836
|
+
passive: true
|
|
4837
|
+
};
|
|
4818
4838
|
function useWindowScroll() {
|
|
4819
4839
|
const [state, setState] = useRafState(() => ({
|
|
4820
4840
|
x: 0,
|
|
@@ -4823,10 +4843,7 @@ function useWindowScroll() {
|
|
|
4823
4843
|
const handleScroll = () => {
|
|
4824
4844
|
setState({ x: window.scrollX, y: window.scrollY });
|
|
4825
4845
|
};
|
|
4826
|
-
useEventListener("scroll", handleScroll, window,
|
|
4827
|
-
capture: false,
|
|
4828
|
-
passive: true
|
|
4829
|
-
});
|
|
4846
|
+
useEventListener("scroll", handleScroll, window, listenerOptions$1);
|
|
4830
4847
|
useIsomorphicLayoutEffect(() => {
|
|
4831
4848
|
handleScroll();
|
|
4832
4849
|
}, []);
|
|
@@ -5098,17 +5115,16 @@ const getRelativePosition = ({
|
|
|
5098
5115
|
return 0;
|
|
5099
5116
|
};
|
|
5100
5117
|
|
|
5101
|
-
|
|
5118
|
+
const listenerOptions = { passive: true };
|
|
5119
|
+
function useScrollIntoView(targetElement, {
|
|
5102
5120
|
duration = 1250,
|
|
5103
5121
|
axis = "y",
|
|
5104
5122
|
onScrollFinish,
|
|
5105
5123
|
easing = easeInOutQuad,
|
|
5106
5124
|
offset = 0,
|
|
5107
5125
|
cancelable = true,
|
|
5108
|
-
isList = false
|
|
5109
|
-
|
|
5110
|
-
scrollElement
|
|
5111
|
-
}) {
|
|
5126
|
+
isList = false
|
|
5127
|
+
} = defaultOptions, scrollElement) {
|
|
5112
5128
|
const frameID = useRef(0);
|
|
5113
5129
|
const startTime = useRef(0);
|
|
5114
5130
|
const shouldStop = useRef(false);
|
|
@@ -5166,8 +5182,8 @@ function useScrollIntoView({
|
|
|
5166
5182
|
shouldStop.current = true;
|
|
5167
5183
|
}
|
|
5168
5184
|
};
|
|
5169
|
-
useEventListener("wheel", handleStop, null,
|
|
5170
|
-
useEventListener("touchmove", handleStop, null,
|
|
5185
|
+
useEventListener("wheel", handleStop, null, listenerOptions);
|
|
5186
|
+
useEventListener("touchmove", handleStop, null, listenerOptions);
|
|
5171
5187
|
useEffect(() => cancel, []);
|
|
5172
5188
|
return {
|
|
5173
5189
|
scrollIntoView,
|
|
@@ -5175,12 +5191,7 @@ function useScrollIntoView({
|
|
|
5175
5191
|
};
|
|
5176
5192
|
}
|
|
5177
5193
|
|
|
5178
|
-
const useSticky = ({
|
|
5179
|
-
targetElement,
|
|
5180
|
-
scrollElement,
|
|
5181
|
-
axis = "y",
|
|
5182
|
-
nav = 0
|
|
5183
|
-
}) => {
|
|
5194
|
+
const useSticky = (targetElement, { axis = "y", nav = 0 }, scrollElement) => {
|
|
5184
5195
|
const [isSticky, setSticky] = useState(false);
|
|
5185
5196
|
const element = useLatestElement(targetElement);
|
|
5186
5197
|
const { run: scrollHandler } = useThrottleFn(() => {
|
|
@@ -5464,54 +5475,51 @@ function init (converter, defaultAttributes) {
|
|
|
5464
5475
|
|
|
5465
5476
|
var api = init(defaultConverter, { path: '/' });
|
|
5466
5477
|
|
|
5467
|
-
|
|
5468
|
-
|
|
5469
|
-
|
|
5470
|
-
|
|
5471
|
-
|
|
5472
|
-
|
|
5473
|
-
|
|
5474
|
-
|
|
5475
|
-
|
|
5476
|
-
|
|
5477
|
-
|
|
5478
|
-
|
|
5479
|
-
|
|
5480
|
-
return target;
|
|
5478
|
+
const getInitialState = (key, defaultValue) => {
|
|
5479
|
+
if (defaultValue !== void 0) {
|
|
5480
|
+
return defaultValue;
|
|
5481
|
+
}
|
|
5482
|
+
if (isBrowser) {
|
|
5483
|
+
return api.get(key);
|
|
5484
|
+
}
|
|
5485
|
+
if (process.env.NODE_ENV !== "production") {
|
|
5486
|
+
console.warn(
|
|
5487
|
+
"`useCookie` When server side rendering, defaultValue should be defined to prevent a hydration mismatches."
|
|
5488
|
+
);
|
|
5489
|
+
}
|
|
5490
|
+
return "";
|
|
5481
5491
|
};
|
|
5482
|
-
function useCookie(key, options = {
|
|
5483
|
-
|
|
5484
|
-
|
|
5485
|
-
|
|
5486
|
-
|
|
5487
|
-
useDeepCompareEffect(() => {
|
|
5488
|
-
const data = csrData ? isFunction$1(csrData) ? csrData() : csrData : isFunction$1(defaultValue) ? defaultValue() : defaultValue;
|
|
5492
|
+
function useCookie(key, options = defaultOptions, defaultValue) {
|
|
5493
|
+
const [cookieValue, setCookieValue] = useState(
|
|
5494
|
+
getInitialState(key, defaultValue)
|
|
5495
|
+
);
|
|
5496
|
+
useEffect(() => {
|
|
5489
5497
|
const getStoredValue = () => {
|
|
5490
5498
|
const raw = api.get(key);
|
|
5491
5499
|
if (raw !== void 0 && raw !== null) {
|
|
5492
5500
|
return raw;
|
|
5493
5501
|
} else {
|
|
5494
|
-
if (
|
|
5502
|
+
if (defaultValue === void 0) {
|
|
5495
5503
|
api.remove(key);
|
|
5496
5504
|
} else {
|
|
5497
|
-
api.set(key,
|
|
5505
|
+
api.set(key, defaultValue, options);
|
|
5498
5506
|
}
|
|
5499
|
-
return
|
|
5507
|
+
return defaultValue;
|
|
5500
5508
|
}
|
|
5501
5509
|
};
|
|
5502
5510
|
setCookieValue(getStoredValue());
|
|
5503
|
-
}, [
|
|
5511
|
+
}, [defaultValue, key, options]);
|
|
5504
5512
|
const updateCookie = useCallback(
|
|
5505
5513
|
(newValue) => {
|
|
5506
5514
|
const value = isFunction$1(newValue) ? newValue(cookieValue) : newValue;
|
|
5507
5515
|
if (value === void 0) {
|
|
5508
5516
|
api.remove(key);
|
|
5509
5517
|
} else {
|
|
5510
|
-
api.set(key, value,
|
|
5518
|
+
api.set(key, value, options);
|
|
5511
5519
|
}
|
|
5512
5520
|
setCookieValue(value);
|
|
5513
5521
|
},
|
|
5514
|
-
[key, cookieValue]
|
|
5522
|
+
[key, cookieValue, options]
|
|
5515
5523
|
);
|
|
5516
5524
|
const refreshCookie = useCallback(() => {
|
|
5517
5525
|
const cookieValue2 = api.get(key);
|
|
@@ -5519,7 +5527,7 @@ function useCookie(key, options = {
|
|
|
5519
5527
|
setCookieValue(cookieValue2);
|
|
5520
5528
|
}
|
|
5521
5529
|
}, [key]);
|
|
5522
|
-
return
|
|
5530
|
+
return [cookieValue, updateCookie, refreshCookie];
|
|
5523
5531
|
}
|
|
5524
5532
|
|
|
5525
5533
|
function useDoubleClick({
|
|
@@ -5593,7 +5601,7 @@ const defaultState = {
|
|
|
5593
5601
|
bottom: 0,
|
|
5594
5602
|
right: 0
|
|
5595
5603
|
};
|
|
5596
|
-
function useMeasure(target, options =
|
|
5604
|
+
function useMeasure(target, options = defaultOptions) {
|
|
5597
5605
|
const [rect, setRect] = useState(defaultState);
|
|
5598
5606
|
const stop = useResizeObserver(
|
|
5599
5607
|
target,
|