@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.cjs
CHANGED
|
@@ -58,7 +58,7 @@ const isIOS = isBrowser && ((_a = window == null ? void 0 : window.navigator) ==
|
|
|
58
58
|
!React__default["default"].useId;
|
|
59
59
|
|
|
60
60
|
function guessSerializerType(rawInit) {
|
|
61
|
-
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";
|
|
61
|
+
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";
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
const useIsomorphicLayoutEffect = isBrowser ? React.useLayoutEffect : React.useEffect;
|
|
@@ -81,6 +81,283 @@ function useEvent(fn) {
|
|
|
81
81
|
}, []);
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
+
const defaultOptions = {};
|
|
85
|
+
const defaultOnError = (e) => {
|
|
86
|
+
console.error(e);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const StorageSerializers = {
|
|
90
|
+
boolean: {
|
|
91
|
+
read: (v) => v === "true",
|
|
92
|
+
write: (v) => String(v)
|
|
93
|
+
},
|
|
94
|
+
object: {
|
|
95
|
+
read: (v) => JSON.parse(v),
|
|
96
|
+
write: (v) => JSON.stringify(v)
|
|
97
|
+
},
|
|
98
|
+
number: {
|
|
99
|
+
read: (v) => Number.parseFloat(v),
|
|
100
|
+
write: (v) => String(v)
|
|
101
|
+
},
|
|
102
|
+
any: {
|
|
103
|
+
read: (v) => v,
|
|
104
|
+
write: (v) => String(v)
|
|
105
|
+
},
|
|
106
|
+
string: {
|
|
107
|
+
read: (v) => v,
|
|
108
|
+
write: (v) => String(v)
|
|
109
|
+
},
|
|
110
|
+
map: {
|
|
111
|
+
read: (v) => new Map(JSON.parse(v)),
|
|
112
|
+
write: (v) => JSON.stringify(Array.from(v.entries()))
|
|
113
|
+
},
|
|
114
|
+
set: {
|
|
115
|
+
read: (v) => new Set(JSON.parse(v)),
|
|
116
|
+
write: (v) => JSON.stringify(Array.from(v))
|
|
117
|
+
},
|
|
118
|
+
date: {
|
|
119
|
+
read: (v) => new Date(v),
|
|
120
|
+
write: (v) => v.toISOString()
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
const getInitialState$3 = (key, defaultValue, storage, serializer, onError) => {
|
|
124
|
+
if (defaultValue !== void 0) {
|
|
125
|
+
return defaultValue;
|
|
126
|
+
}
|
|
127
|
+
if (isBrowser) {
|
|
128
|
+
try {
|
|
129
|
+
const raw = storage == null ? void 0 : storage.getItem(key);
|
|
130
|
+
if (raw !== void 0 && raw !== null) {
|
|
131
|
+
return serializer == null ? void 0 : serializer.read(raw);
|
|
132
|
+
}
|
|
133
|
+
} catch (error) {
|
|
134
|
+
onError == null ? void 0 : onError(error);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (process.env.NODE_ENV !== "production") {
|
|
138
|
+
console.warn(
|
|
139
|
+
"`createStorage` When server side rendering, defaultValue should be defined to prevent a hydration mismatches."
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
return null;
|
|
143
|
+
};
|
|
144
|
+
function useStorage(key, defaultValue, getStorage = () => isBrowser ? sessionStorage : void 0, options = defaultOptions) {
|
|
145
|
+
let storage;
|
|
146
|
+
const { onError = defaultOnError, csrData } = options;
|
|
147
|
+
try {
|
|
148
|
+
storage = getStorage();
|
|
149
|
+
} catch (err) {
|
|
150
|
+
onError(err);
|
|
151
|
+
}
|
|
152
|
+
const type = guessSerializerType(defaultValue);
|
|
153
|
+
const serializer = React.useMemo(() => {
|
|
154
|
+
var _a;
|
|
155
|
+
return (_a = options.serializer) != null ? _a : StorageSerializers[type];
|
|
156
|
+
}, [options.serializer, type]);
|
|
157
|
+
const [state, setState] = React.useState(
|
|
158
|
+
getInitialState$3(key, defaultValue, storage, serializer, onError)
|
|
159
|
+
);
|
|
160
|
+
React.useEffect(() => {
|
|
161
|
+
const data = csrData ? isFunction$1(csrData) ? csrData() : csrData : defaultValue;
|
|
162
|
+
const getStoredValue = () => {
|
|
163
|
+
try {
|
|
164
|
+
const raw = storage == null ? void 0 : storage.getItem(key);
|
|
165
|
+
if (raw !== void 0 && raw !== null) {
|
|
166
|
+
return serializer.read(raw);
|
|
167
|
+
} else {
|
|
168
|
+
storage == null ? void 0 : storage.setItem(key, serializer.write(data));
|
|
169
|
+
return data;
|
|
170
|
+
}
|
|
171
|
+
} catch (e) {
|
|
172
|
+
onError(e);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
setState(getStoredValue());
|
|
176
|
+
}, [key, defaultValue, serializer, storage, onError, csrData]);
|
|
177
|
+
const updateState = useEvent(
|
|
178
|
+
(valOrFunc) => {
|
|
179
|
+
const currentState = isFunction$1(valOrFunc) ? valOrFunc(state) : valOrFunc;
|
|
180
|
+
setState(currentState);
|
|
181
|
+
if (currentState === null) {
|
|
182
|
+
storage == null ? void 0 : storage.removeItem(key);
|
|
183
|
+
} else {
|
|
184
|
+
try {
|
|
185
|
+
storage == null ? void 0 : storage.setItem(key, serializer.write(currentState));
|
|
186
|
+
} catch (e) {
|
|
187
|
+
onError(e);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
);
|
|
192
|
+
return [state, updateState];
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function useLocalStorage(key, defaultValue, options = defaultOptions) {
|
|
196
|
+
return useStorage(
|
|
197
|
+
key,
|
|
198
|
+
defaultValue,
|
|
199
|
+
() => isBrowser ? localStorage : void 0,
|
|
200
|
+
options
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function useSessionStorage(key, defaultValue, options = defaultOptions) {
|
|
205
|
+
return useStorage(
|
|
206
|
+
key,
|
|
207
|
+
defaultValue,
|
|
208
|
+
() => isBrowser ? sessionStorage : void 0,
|
|
209
|
+
options
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const toggleReducer = (state, nextValue) => typeof nextValue === "boolean" ? nextValue : !state;
|
|
214
|
+
function useToggle(initialValue) {
|
|
215
|
+
return React.useReducer(toggleReducer, initialValue);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function useInterval(callback, delay, options = defaultOptions) {
|
|
219
|
+
const immediate = options.immediate;
|
|
220
|
+
const savedCallback = useLatest(callback);
|
|
221
|
+
React.useEffect(() => {
|
|
222
|
+
if (immediate) {
|
|
223
|
+
savedCallback.current();
|
|
224
|
+
}
|
|
225
|
+
if (delay !== null) {
|
|
226
|
+
const interval = setInterval(() => savedCallback.current(), delay || 0);
|
|
227
|
+
return () => clearInterval(interval);
|
|
228
|
+
}
|
|
229
|
+
return void 0;
|
|
230
|
+
}, [delay, immediate]);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function useDarkMode(options) {
|
|
234
|
+
const {
|
|
235
|
+
selector = "html",
|
|
236
|
+
attribute = "class",
|
|
237
|
+
classNameDark = "",
|
|
238
|
+
classNameLight = "",
|
|
239
|
+
storageKey = "reactuses-color-scheme",
|
|
240
|
+
storage = () => isBrowser ? localStorage : void 0,
|
|
241
|
+
defaultValue = false
|
|
242
|
+
} = options;
|
|
243
|
+
const value = () => {
|
|
244
|
+
return window.matchMedia("(prefers-color-scheme: dark)").matches;
|
|
245
|
+
};
|
|
246
|
+
const [dark, setDark] = useStorage(
|
|
247
|
+
storageKey,
|
|
248
|
+
defaultValue,
|
|
249
|
+
storage,
|
|
250
|
+
{
|
|
251
|
+
csrData: value
|
|
252
|
+
}
|
|
253
|
+
);
|
|
254
|
+
React.useEffect(() => {
|
|
255
|
+
const element = window == null ? void 0 : window.document.querySelector(selector);
|
|
256
|
+
if (!element) {
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (attribute === "class") {
|
|
260
|
+
dark && classNameDark && element.classList.add(classNameDark);
|
|
261
|
+
!dark && classNameLight && element.classList.add(classNameLight);
|
|
262
|
+
} else {
|
|
263
|
+
dark && classNameDark && element.setAttribute(attribute, classNameDark);
|
|
264
|
+
!dark && classNameLight && element.setAttribute(attribute, classNameLight);
|
|
265
|
+
}
|
|
266
|
+
return () => {
|
|
267
|
+
if (!element) {
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
if (attribute === "class") {
|
|
271
|
+
dark && classNameDark && element.classList.remove(classNameDark);
|
|
272
|
+
!dark && classNameLight && element.classList.remove(classNameLight);
|
|
273
|
+
} else {
|
|
274
|
+
dark && classNameDark && element.removeAttribute(attribute);
|
|
275
|
+
!dark && classNameLight && element.removeAttribute(attribute);
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
}, [attribute, classNameDark, classNameLight, dark, selector]);
|
|
279
|
+
return [dark, () => setDark((dark2) => !dark2), setDark];
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const getInitialState$2 = (query, defaultState) => {
|
|
283
|
+
if (defaultState !== void 0) {
|
|
284
|
+
return defaultState;
|
|
285
|
+
}
|
|
286
|
+
if (isBrowser) {
|
|
287
|
+
return window.matchMedia(query).matches;
|
|
288
|
+
}
|
|
289
|
+
if (process.env.NODE_ENV !== "production") {
|
|
290
|
+
console.warn(
|
|
291
|
+
"`useMediaQuery` When server side rendering, defaultState should be defined to prevent a hydration mismatches."
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
return false;
|
|
295
|
+
};
|
|
296
|
+
function useMediaQuery(query, defaultState) {
|
|
297
|
+
const [state, setState] = React.useState(getInitialState$2(query, defaultState));
|
|
298
|
+
React.useEffect(() => {
|
|
299
|
+
var _a;
|
|
300
|
+
let mounted = true;
|
|
301
|
+
const mql = window.matchMedia(query);
|
|
302
|
+
const onChange = () => {
|
|
303
|
+
if (!mounted) {
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
setState(!!mql.matches);
|
|
307
|
+
};
|
|
308
|
+
if ("addEventListener" in mql) {
|
|
309
|
+
mql.addEventListener("change", onChange);
|
|
310
|
+
} else {
|
|
311
|
+
(_a = mql.addListener) == null ? void 0 : _a.call(mql, onChange);
|
|
312
|
+
}
|
|
313
|
+
setState(mql.matches);
|
|
314
|
+
return () => {
|
|
315
|
+
var _a2;
|
|
316
|
+
mounted = false;
|
|
317
|
+
if ("removeEventListener" in mql) {
|
|
318
|
+
mql.removeEventListener("change", onChange);
|
|
319
|
+
} else {
|
|
320
|
+
(_a2 = mql.removeListener) == null ? void 0 : _a2.call(mql, onChange);
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
}, [query]);
|
|
324
|
+
return state;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function usePreferredDark(defaultState) {
|
|
328
|
+
return useMediaQuery("(prefers-color-scheme: dark)", defaultState);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function useMount(fn) {
|
|
332
|
+
if (isDev) {
|
|
333
|
+
if (!isFunction$1(fn)) {
|
|
334
|
+
console.error(
|
|
335
|
+
`useMount: parameter \`fn\` expected to be a function, but got "${typeof fn}".`
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
React.useEffect(() => {
|
|
340
|
+
fn == null ? void 0 : fn();
|
|
341
|
+
}, []);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function useUnmount(fn) {
|
|
345
|
+
if (isDev) {
|
|
346
|
+
if (!isFunction$1(fn)) {
|
|
347
|
+
console.error(
|
|
348
|
+
`useUnmount expected parameter is a function, got ${typeof fn}`
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
const fnRef = useLatest(fn);
|
|
353
|
+
React.useEffect(
|
|
354
|
+
() => () => {
|
|
355
|
+
fnRef.current();
|
|
356
|
+
},
|
|
357
|
+
[fnRef]
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
|
|
84
361
|
/** Detect free variable `global` from Node.js. */
|
|
85
362
|
var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;
|
|
86
363
|
|
|
@@ -2396,390 +2673,95 @@ function debounce(func, wait, options) {
|
|
|
2396
2673
|
*
|
|
2397
2674
|
* **Note:** This method supports comparing arrays, array buffers, booleans,
|
|
2398
2675
|
* date objects, error objects, maps, numbers, `Object` objects, regexes,
|
|
2399
|
-
* sets, strings, symbols, and typed arrays. `Object` objects are compared
|
|
2400
|
-
* by their own, not inherited, enumerable properties. Functions and DOM
|
|
2401
|
-
* nodes are compared by strict equality, i.e. `===`.
|
|
2402
|
-
*
|
|
2403
|
-
* @static
|
|
2404
|
-
* @memberOf _
|
|
2405
|
-
* @since 0.1.0
|
|
2406
|
-
* @category Lang
|
|
2407
|
-
* @param {*} value The value to compare.
|
|
2408
|
-
* @param {*} other The other value to compare.
|
|
2409
|
-
* @returns {boolean} Returns `true` if the values are equivalent, else `false`.
|
|
2410
|
-
* @example
|
|
2411
|
-
*
|
|
2412
|
-
* var object = { 'a': 1 };
|
|
2413
|
-
* var other = { 'a': 1 };
|
|
2414
|
-
*
|
|
2415
|
-
* _.isEqual(object, other);
|
|
2416
|
-
* // => true
|
|
2417
|
-
*
|
|
2418
|
-
* object === other;
|
|
2419
|
-
* // => false
|
|
2420
|
-
*/
|
|
2421
|
-
function isEqual(value, other) {
|
|
2422
|
-
return baseIsEqual(value, other);
|
|
2423
|
-
}
|
|
2424
|
-
|
|
2425
|
-
/** Error message constants. */
|
|
2426
|
-
var FUNC_ERROR_TEXT = 'Expected a function';
|
|
2427
|
-
|
|
2428
|
-
/**
|
|
2429
|
-
* Creates a throttled function that only invokes `func` at most once per
|
|
2430
|
-
* every `wait` milliseconds. The throttled function comes with a `cancel`
|
|
2431
|
-
* method to cancel delayed `func` invocations and a `flush` method to
|
|
2432
|
-
* immediately invoke them. Provide `options` to indicate whether `func`
|
|
2433
|
-
* should be invoked on the leading and/or trailing edge of the `wait`
|
|
2434
|
-
* timeout. The `func` is invoked with the last arguments provided to the
|
|
2435
|
-
* throttled function. Subsequent calls to the throttled function return the
|
|
2436
|
-
* result of the last `func` invocation.
|
|
2437
|
-
*
|
|
2438
|
-
* **Note:** If `leading` and `trailing` options are `true`, `func` is
|
|
2439
|
-
* invoked on the trailing edge of the timeout only if the throttled function
|
|
2440
|
-
* is invoked more than once during the `wait` timeout.
|
|
2441
|
-
*
|
|
2442
|
-
* If `wait` is `0` and `leading` is `false`, `func` invocation is deferred
|
|
2443
|
-
* until to the next tick, similar to `setTimeout` with a timeout of `0`.
|
|
2444
|
-
*
|
|
2445
|
-
* See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/)
|
|
2446
|
-
* for details over the differences between `_.throttle` and `_.debounce`.
|
|
2447
|
-
*
|
|
2448
|
-
* @static
|
|
2449
|
-
* @memberOf _
|
|
2450
|
-
* @since 0.1.0
|
|
2451
|
-
* @category Function
|
|
2452
|
-
* @param {Function} func The function to throttle.
|
|
2453
|
-
* @param {number} [wait=0] The number of milliseconds to throttle invocations to.
|
|
2454
|
-
* @param {Object} [options={}] The options object.
|
|
2455
|
-
* @param {boolean} [options.leading=true]
|
|
2456
|
-
* Specify invoking on the leading edge of the timeout.
|
|
2457
|
-
* @param {boolean} [options.trailing=true]
|
|
2458
|
-
* Specify invoking on the trailing edge of the timeout.
|
|
2459
|
-
* @returns {Function} Returns the new throttled function.
|
|
2460
|
-
* @example
|
|
2461
|
-
*
|
|
2462
|
-
* // Avoid excessively updating the position while scrolling.
|
|
2463
|
-
* jQuery(window).on('scroll', _.throttle(updatePosition, 100));
|
|
2464
|
-
*
|
|
2465
|
-
* // Invoke `renewToken` when the click event is fired, but not more than once every 5 minutes.
|
|
2466
|
-
* var throttled = _.throttle(renewToken, 300000, { 'trailing': false });
|
|
2467
|
-
* jQuery(element).on('click', throttled);
|
|
2468
|
-
*
|
|
2469
|
-
* // Cancel the trailing throttled invocation.
|
|
2470
|
-
* jQuery(window).on('popstate', throttled.cancel);
|
|
2471
|
-
*/
|
|
2472
|
-
function throttle(func, wait, options) {
|
|
2473
|
-
var leading = true,
|
|
2474
|
-
trailing = true;
|
|
2475
|
-
|
|
2476
|
-
if (typeof func != 'function') {
|
|
2477
|
-
throw new TypeError(FUNC_ERROR_TEXT);
|
|
2478
|
-
}
|
|
2479
|
-
if (isObject(options)) {
|
|
2480
|
-
leading = 'leading' in options ? !!options.leading : leading;
|
|
2481
|
-
trailing = 'trailing' in options ? !!options.trailing : trailing;
|
|
2482
|
-
}
|
|
2483
|
-
return debounce(func, wait, {
|
|
2484
|
-
'leading': leading,
|
|
2485
|
-
'maxWait': wait,
|
|
2486
|
-
'trailing': trailing
|
|
2487
|
-
});
|
|
2488
|
-
}
|
|
2489
|
-
|
|
2490
|
-
const isPrimitive$1 = (val) => val !== Object(val);
|
|
2491
|
-
function useCustomCompareEffect(effect, deps, depsEqual) {
|
|
2492
|
-
if (process.env.NODE_ENV !== "production") {
|
|
2493
|
-
if (!Array.isArray(deps) || !deps.length) {
|
|
2494
|
-
console.warn(
|
|
2495
|
-
"`useCustomCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
|
|
2496
|
-
);
|
|
2497
|
-
}
|
|
2498
|
-
if (deps.every(isPrimitive$1)) {
|
|
2499
|
-
console.warn(
|
|
2500
|
-
"`useCustomCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
|
|
2501
|
-
);
|
|
2502
|
-
}
|
|
2503
|
-
if (typeof depsEqual !== "function") {
|
|
2504
|
-
console.warn(
|
|
2505
|
-
"`useCustomCompareEffect` should be used with depsEqual callback for comparing deps list"
|
|
2506
|
-
);
|
|
2507
|
-
}
|
|
2508
|
-
}
|
|
2509
|
-
const ref = React.useRef(void 0);
|
|
2510
|
-
if (!ref.current || !depsEqual(deps, ref.current)) {
|
|
2511
|
-
ref.current = deps;
|
|
2512
|
-
}
|
|
2513
|
-
React.useEffect(effect, ref.current);
|
|
2514
|
-
}
|
|
2515
|
-
|
|
2516
|
-
const isPrimitive = (val) => val !== Object(val);
|
|
2517
|
-
function useDeepCompareEffect(effect, deps) {
|
|
2518
|
-
if (process.env.NODE_ENV !== "production") {
|
|
2519
|
-
if (!Array.isArray(deps) || !deps.length) {
|
|
2520
|
-
console.warn(
|
|
2521
|
-
"`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
|
|
2522
|
-
);
|
|
2523
|
-
}
|
|
2524
|
-
if (deps.every(isPrimitive)) {
|
|
2525
|
-
console.warn(
|
|
2526
|
-
"`useDeepCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
|
|
2527
|
-
);
|
|
2528
|
-
}
|
|
2529
|
-
}
|
|
2530
|
-
useCustomCompareEffect(effect, deps, isEqual);
|
|
2531
|
-
}
|
|
2532
|
-
|
|
2533
|
-
const StorageSerializers = {
|
|
2534
|
-
boolean: {
|
|
2535
|
-
read: (v) => v === "true",
|
|
2536
|
-
write: (v) => String(v)
|
|
2537
|
-
},
|
|
2538
|
-
object: {
|
|
2539
|
-
read: (v) => JSON.parse(v),
|
|
2540
|
-
write: (v) => JSON.stringify(v)
|
|
2541
|
-
},
|
|
2542
|
-
number: {
|
|
2543
|
-
read: (v) => Number.parseFloat(v),
|
|
2544
|
-
write: (v) => String(v)
|
|
2545
|
-
},
|
|
2546
|
-
any: {
|
|
2547
|
-
read: (v) => v,
|
|
2548
|
-
write: (v) => String(v)
|
|
2549
|
-
},
|
|
2550
|
-
string: {
|
|
2551
|
-
read: (v) => v,
|
|
2552
|
-
write: (v) => String(v)
|
|
2553
|
-
},
|
|
2554
|
-
map: {
|
|
2555
|
-
read: (v) => new Map(JSON.parse(v)),
|
|
2556
|
-
write: (v) => JSON.stringify(Array.from(v.entries()))
|
|
2557
|
-
},
|
|
2558
|
-
set: {
|
|
2559
|
-
read: (v) => new Set(JSON.parse(v)),
|
|
2560
|
-
write: (v) => JSON.stringify(Array.from(v))
|
|
2561
|
-
},
|
|
2562
|
-
date: {
|
|
2563
|
-
read: (v) => new Date(v),
|
|
2564
|
-
write: (v) => v.toISOString()
|
|
2565
|
-
}
|
|
2566
|
-
};
|
|
2567
|
-
function useStorage(key, defaults, getStorage, options = {}) {
|
|
2568
|
-
const defaultOnError = React.useCallback((e) => {
|
|
2569
|
-
console.error(e);
|
|
2570
|
-
}, []);
|
|
2571
|
-
let storage;
|
|
2572
|
-
const { onError = defaultOnError, csrData } = options;
|
|
2573
|
-
try {
|
|
2574
|
-
storage = getStorage();
|
|
2575
|
-
} catch (err) {
|
|
2576
|
-
onError(err);
|
|
2577
|
-
}
|
|
2578
|
-
const type = guessSerializerType(defaults);
|
|
2579
|
-
const serializer = React.useMemo(() => {
|
|
2580
|
-
var _a;
|
|
2581
|
-
return (_a = options.serializer) != null ? _a : StorageSerializers[type];
|
|
2582
|
-
}, [options.serializer, type]);
|
|
2583
|
-
const [state, setState] = React.useState(defaults);
|
|
2584
|
-
useDeepCompareEffect(() => {
|
|
2585
|
-
const data = csrData ? isFunction$1(csrData) ? csrData() : csrData : defaults;
|
|
2586
|
-
const getStoredValue = () => {
|
|
2587
|
-
try {
|
|
2588
|
-
const raw = storage == null ? void 0 : storage.getItem(key);
|
|
2589
|
-
if (raw !== void 0 && raw !== null) {
|
|
2590
|
-
return serializer.read(raw);
|
|
2591
|
-
} else {
|
|
2592
|
-
storage == null ? void 0 : storage.setItem(key, serializer.write(data));
|
|
2593
|
-
return data;
|
|
2594
|
-
}
|
|
2595
|
-
} catch (e) {
|
|
2596
|
-
onError(e);
|
|
2597
|
-
}
|
|
2598
|
-
};
|
|
2599
|
-
setState(getStoredValue());
|
|
2600
|
-
}, [key, defaults, serializer, storage, onError]);
|
|
2601
|
-
const updateState = useEvent(
|
|
2602
|
-
(valOrFunc) => {
|
|
2603
|
-
const currentState = isFunction$1(valOrFunc) ? valOrFunc(state) : valOrFunc;
|
|
2604
|
-
setState(currentState);
|
|
2605
|
-
if (currentState === null) {
|
|
2606
|
-
storage == null ? void 0 : storage.removeItem(key);
|
|
2607
|
-
} else {
|
|
2608
|
-
try {
|
|
2609
|
-
storage == null ? void 0 : storage.setItem(key, serializer.write(currentState));
|
|
2610
|
-
} catch (e) {
|
|
2611
|
-
onError(e);
|
|
2612
|
-
}
|
|
2613
|
-
}
|
|
2614
|
-
}
|
|
2615
|
-
);
|
|
2616
|
-
return [state, updateState];
|
|
2617
|
-
}
|
|
2618
|
-
|
|
2619
|
-
function useLocalStorage(key, defaults, options = {}) {
|
|
2620
|
-
return useStorage(
|
|
2621
|
-
key,
|
|
2622
|
-
defaults,
|
|
2623
|
-
() => isBrowser ? localStorage : void 0,
|
|
2624
|
-
options
|
|
2625
|
-
);
|
|
2626
|
-
}
|
|
2627
|
-
|
|
2628
|
-
function useSessionStorage(key, defaults, options = {}) {
|
|
2629
|
-
return useStorage(
|
|
2630
|
-
key,
|
|
2631
|
-
defaults,
|
|
2632
|
-
() => isBrowser ? sessionStorage : void 0,
|
|
2633
|
-
options
|
|
2634
|
-
);
|
|
2635
|
-
}
|
|
2636
|
-
|
|
2637
|
-
const toggleReducer = (state, nextValue) => typeof nextValue === "boolean" ? nextValue : !state;
|
|
2638
|
-
function useToggle(initialValue) {
|
|
2639
|
-
return React.useReducer(toggleReducer, initialValue);
|
|
2640
|
-
}
|
|
2641
|
-
|
|
2642
|
-
function useInterval(callback, delay, options) {
|
|
2643
|
-
const immediate = options == null ? void 0 : options.immediate;
|
|
2644
|
-
const savedCallback = useLatest(callback);
|
|
2645
|
-
React.useEffect(() => {
|
|
2646
|
-
if (immediate) {
|
|
2647
|
-
savedCallback.current();
|
|
2648
|
-
}
|
|
2649
|
-
if (delay !== null) {
|
|
2650
|
-
const interval = setInterval(() => savedCallback.current(), delay || 0);
|
|
2651
|
-
return () => clearInterval(interval);
|
|
2652
|
-
}
|
|
2653
|
-
return void 0;
|
|
2654
|
-
}, [delay, immediate]);
|
|
2655
|
-
}
|
|
2656
|
-
|
|
2657
|
-
function useDarkMode(options) {
|
|
2658
|
-
const {
|
|
2659
|
-
selector = "html",
|
|
2660
|
-
attribute = "class",
|
|
2661
|
-
classNameDark = "",
|
|
2662
|
-
classNameLight = "",
|
|
2663
|
-
storageKey = "reactuses-color-scheme",
|
|
2664
|
-
storage = () => isBrowser ? localStorage : void 0,
|
|
2665
|
-
defaultValue = false
|
|
2666
|
-
} = options;
|
|
2667
|
-
const value = () => {
|
|
2668
|
-
return window.matchMedia("(prefers-color-scheme: dark)").matches;
|
|
2669
|
-
};
|
|
2670
|
-
const [dark, setDark] = useStorage(
|
|
2671
|
-
storageKey,
|
|
2672
|
-
defaultValue,
|
|
2673
|
-
storage,
|
|
2674
|
-
{
|
|
2675
|
-
csrData: value
|
|
2676
|
-
}
|
|
2677
|
-
);
|
|
2678
|
-
React.useEffect(() => {
|
|
2679
|
-
const element = window == null ? void 0 : window.document.querySelector(selector);
|
|
2680
|
-
if (!element) {
|
|
2681
|
-
return;
|
|
2682
|
-
}
|
|
2683
|
-
if (attribute === "class") {
|
|
2684
|
-
dark && classNameDark && element.classList.add(classNameDark);
|
|
2685
|
-
!dark && classNameLight && element.classList.add(classNameLight);
|
|
2686
|
-
} else {
|
|
2687
|
-
dark && classNameDark && element.setAttribute(attribute, classNameDark);
|
|
2688
|
-
!dark && classNameLight && element.setAttribute(attribute, classNameLight);
|
|
2689
|
-
}
|
|
2690
|
-
return () => {
|
|
2691
|
-
if (!element) {
|
|
2692
|
-
return;
|
|
2693
|
-
}
|
|
2694
|
-
if (attribute === "class") {
|
|
2695
|
-
dark && classNameDark && element.classList.remove(classNameDark);
|
|
2696
|
-
!dark && classNameLight && element.classList.remove(classNameLight);
|
|
2697
|
-
} else {
|
|
2698
|
-
dark && classNameDark && element.removeAttribute(attribute);
|
|
2699
|
-
!dark && classNameLight && element.removeAttribute(attribute);
|
|
2700
|
-
}
|
|
2701
|
-
};
|
|
2702
|
-
}, [attribute, classNameDark, classNameLight, dark, selector]);
|
|
2703
|
-
return [dark, () => setDark((dark2) => !dark2), setDark];
|
|
2704
|
-
}
|
|
2705
|
-
|
|
2706
|
-
const getInitialState = (query, defaultState) => {
|
|
2707
|
-
if (defaultState !== void 0) {
|
|
2708
|
-
return defaultState;
|
|
2709
|
-
}
|
|
2710
|
-
if (isBrowser) {
|
|
2711
|
-
return window.matchMedia(query).matches;
|
|
2712
|
-
}
|
|
2713
|
-
if (process.env.NODE_ENV !== "production") {
|
|
2714
|
-
console.warn(
|
|
2715
|
-
"`useMediaQuery` When server side rendering, defaultState should be defined to prevent a hydration mismatches."
|
|
2716
|
-
);
|
|
2717
|
-
}
|
|
2718
|
-
return false;
|
|
2719
|
-
};
|
|
2720
|
-
function useMediaQuery(query, defaultState) {
|
|
2721
|
-
const [state, setState] = React.useState(getInitialState(query, defaultState));
|
|
2722
|
-
React.useEffect(() => {
|
|
2723
|
-
var _a;
|
|
2724
|
-
let mounted = true;
|
|
2725
|
-
const mql = window.matchMedia(query);
|
|
2726
|
-
const onChange = () => {
|
|
2727
|
-
if (!mounted) {
|
|
2728
|
-
return;
|
|
2729
|
-
}
|
|
2730
|
-
setState(!!mql.matches);
|
|
2731
|
-
};
|
|
2732
|
-
if ("addEventListener" in mql) {
|
|
2733
|
-
mql.addEventListener("change", onChange);
|
|
2734
|
-
} else {
|
|
2735
|
-
(_a = mql.addListener) == null ? void 0 : _a.call(mql, onChange);
|
|
2736
|
-
}
|
|
2737
|
-
setState(mql.matches);
|
|
2738
|
-
return () => {
|
|
2739
|
-
var _a2;
|
|
2740
|
-
mounted = false;
|
|
2741
|
-
if ("removeEventListener" in mql) {
|
|
2742
|
-
mql.removeEventListener("change", onChange);
|
|
2743
|
-
} else {
|
|
2744
|
-
(_a2 = mql.removeListener) == null ? void 0 : _a2.call(mql, onChange);
|
|
2745
|
-
}
|
|
2746
|
-
};
|
|
2747
|
-
}, [query]);
|
|
2748
|
-
return state;
|
|
2676
|
+
* sets, strings, symbols, and typed arrays. `Object` objects are compared
|
|
2677
|
+
* by their own, not inherited, enumerable properties. Functions and DOM
|
|
2678
|
+
* nodes are compared by strict equality, i.e. `===`.
|
|
2679
|
+
*
|
|
2680
|
+
* @static
|
|
2681
|
+
* @memberOf _
|
|
2682
|
+
* @since 0.1.0
|
|
2683
|
+
* @category Lang
|
|
2684
|
+
* @param {*} value The value to compare.
|
|
2685
|
+
* @param {*} other The other value to compare.
|
|
2686
|
+
* @returns {boolean} Returns `true` if the values are equivalent, else `false`.
|
|
2687
|
+
* @example
|
|
2688
|
+
*
|
|
2689
|
+
* var object = { 'a': 1 };
|
|
2690
|
+
* var other = { 'a': 1 };
|
|
2691
|
+
*
|
|
2692
|
+
* _.isEqual(object, other);
|
|
2693
|
+
* // => true
|
|
2694
|
+
*
|
|
2695
|
+
* object === other;
|
|
2696
|
+
* // => false
|
|
2697
|
+
*/
|
|
2698
|
+
function isEqual(value, other) {
|
|
2699
|
+
return baseIsEqual(value, other);
|
|
2749
2700
|
}
|
|
2750
2701
|
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
}
|
|
2702
|
+
/** Error message constants. */
|
|
2703
|
+
var FUNC_ERROR_TEXT = 'Expected a function';
|
|
2754
2704
|
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2705
|
+
/**
|
|
2706
|
+
* Creates a throttled function that only invokes `func` at most once per
|
|
2707
|
+
* every `wait` milliseconds. The throttled function comes with a `cancel`
|
|
2708
|
+
* method to cancel delayed `func` invocations and a `flush` method to
|
|
2709
|
+
* immediately invoke them. Provide `options` to indicate whether `func`
|
|
2710
|
+
* should be invoked on the leading and/or trailing edge of the `wait`
|
|
2711
|
+
* timeout. The `func` is invoked with the last arguments provided to the
|
|
2712
|
+
* throttled function. Subsequent calls to the throttled function return the
|
|
2713
|
+
* result of the last `func` invocation.
|
|
2714
|
+
*
|
|
2715
|
+
* **Note:** If `leading` and `trailing` options are `true`, `func` is
|
|
2716
|
+
* invoked on the trailing edge of the timeout only if the throttled function
|
|
2717
|
+
* is invoked more than once during the `wait` timeout.
|
|
2718
|
+
*
|
|
2719
|
+
* If `wait` is `0` and `leading` is `false`, `func` invocation is deferred
|
|
2720
|
+
* until to the next tick, similar to `setTimeout` with a timeout of `0`.
|
|
2721
|
+
*
|
|
2722
|
+
* See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/)
|
|
2723
|
+
* for details over the differences between `_.throttle` and `_.debounce`.
|
|
2724
|
+
*
|
|
2725
|
+
* @static
|
|
2726
|
+
* @memberOf _
|
|
2727
|
+
* @since 0.1.0
|
|
2728
|
+
* @category Function
|
|
2729
|
+
* @param {Function} func The function to throttle.
|
|
2730
|
+
* @param {number} [wait=0] The number of milliseconds to throttle invocations to.
|
|
2731
|
+
* @param {Object} [options={}] The options object.
|
|
2732
|
+
* @param {boolean} [options.leading=true]
|
|
2733
|
+
* Specify invoking on the leading edge of the timeout.
|
|
2734
|
+
* @param {boolean} [options.trailing=true]
|
|
2735
|
+
* Specify invoking on the trailing edge of the timeout.
|
|
2736
|
+
* @returns {Function} Returns the new throttled function.
|
|
2737
|
+
* @example
|
|
2738
|
+
*
|
|
2739
|
+
* // Avoid excessively updating the position while scrolling.
|
|
2740
|
+
* jQuery(window).on('scroll', _.throttle(updatePosition, 100));
|
|
2741
|
+
*
|
|
2742
|
+
* // Invoke `renewToken` when the click event is fired, but not more than once every 5 minutes.
|
|
2743
|
+
* var throttled = _.throttle(renewToken, 300000, { 'trailing': false });
|
|
2744
|
+
* jQuery(element).on('click', throttled);
|
|
2745
|
+
*
|
|
2746
|
+
* // Cancel the trailing throttled invocation.
|
|
2747
|
+
* jQuery(window).on('popstate', throttled.cancel);
|
|
2748
|
+
*/
|
|
2749
|
+
function throttle(func, wait, options) {
|
|
2750
|
+
var leading = true,
|
|
2751
|
+
trailing = true;
|
|
2767
2752
|
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
if (!isFunction$1(fn)) {
|
|
2771
|
-
console.error(
|
|
2772
|
-
`useUnmount expected parameter is a function, got ${typeof fn}`
|
|
2773
|
-
);
|
|
2774
|
-
}
|
|
2753
|
+
if (typeof func != 'function') {
|
|
2754
|
+
throw new TypeError(FUNC_ERROR_TEXT);
|
|
2775
2755
|
}
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
|
|
2756
|
+
if (isObject(options)) {
|
|
2757
|
+
leading = 'leading' in options ? !!options.leading : leading;
|
|
2758
|
+
trailing = 'trailing' in options ? !!options.trailing : trailing;
|
|
2759
|
+
}
|
|
2760
|
+
return debounce(func, wait, {
|
|
2761
|
+
'leading': leading,
|
|
2762
|
+
'maxWait': wait,
|
|
2763
|
+
'trailing': trailing
|
|
2764
|
+
});
|
|
2783
2765
|
}
|
|
2784
2766
|
|
|
2785
2767
|
function useThrottleFn(fn, wait, options) {
|
|
@@ -2799,8 +2781,7 @@ function useThrottleFn(fn, wait, options) {
|
|
|
2799
2781
|
wait,
|
|
2800
2782
|
options
|
|
2801
2783
|
),
|
|
2802
|
-
|
|
2803
|
-
[]
|
|
2784
|
+
[wait, options]
|
|
2804
2785
|
);
|
|
2805
2786
|
useUnmount(() => {
|
|
2806
2787
|
throttled.cancel();
|
|
@@ -2844,8 +2825,7 @@ function useDebounceFn(fn, wait, options) {
|
|
|
2844
2825
|
wait,
|
|
2845
2826
|
options
|
|
2846
2827
|
),
|
|
2847
|
-
|
|
2848
|
-
[]
|
|
2828
|
+
[options, wait]
|
|
2849
2829
|
);
|
|
2850
2830
|
useUnmount(() => {
|
|
2851
2831
|
debounced.cancel();
|
|
@@ -2893,7 +2873,7 @@ function useUpdate() {
|
|
|
2893
2873
|
return update;
|
|
2894
2874
|
}
|
|
2895
2875
|
|
|
2896
|
-
function useTimeoutFn(cb, interval, options =
|
|
2876
|
+
function useTimeoutFn(cb, interval, options = defaultOptions) {
|
|
2897
2877
|
const { immediate = true } = options;
|
|
2898
2878
|
const [pending, setPending] = React.useState(false);
|
|
2899
2879
|
const savedCallback = useLatest(cb);
|
|
@@ -2983,10 +2963,10 @@ function useLatestElement(target, defaultElement) {
|
|
|
2983
2963
|
return latestElement;
|
|
2984
2964
|
}
|
|
2985
2965
|
|
|
2986
|
-
function useEventListener(eventName, handler, element, options) {
|
|
2966
|
+
function useEventListener(eventName, handler, element, options = defaultOptions) {
|
|
2987
2967
|
const savedHandler = useLatest(handler);
|
|
2988
2968
|
const targetElement = useLatestElement(element, defaultWindow);
|
|
2989
|
-
|
|
2969
|
+
React.useEffect(() => {
|
|
2990
2970
|
if (!(targetElement && targetElement.addEventListener)) {
|
|
2991
2971
|
return;
|
|
2992
2972
|
}
|
|
@@ -3140,7 +3120,7 @@ function useFavicon(href, baseUrl = "", rel = "icon") {
|
|
|
3140
3120
|
}, [baseUrl, href, rel]);
|
|
3141
3121
|
}
|
|
3142
3122
|
|
|
3143
|
-
function useMutationObserver(callback, target, options =
|
|
3123
|
+
function useMutationObserver(callback, target, options = defaultOptions) {
|
|
3144
3124
|
const callbackRef = useLatest(callback);
|
|
3145
3125
|
const observerRef = React.useRef();
|
|
3146
3126
|
const element = useLatestElement(target);
|
|
@@ -3149,7 +3129,7 @@ function useMutationObserver(callback, target, options = {}) {
|
|
|
3149
3129
|
observerRef.current.disconnect();
|
|
3150
3130
|
}
|
|
3151
3131
|
}, []);
|
|
3152
|
-
|
|
3132
|
+
React.useEffect(() => {
|
|
3153
3133
|
if (!element) {
|
|
3154
3134
|
return;
|
|
3155
3135
|
}
|
|
@@ -3160,13 +3140,56 @@ function useMutationObserver(callback, target, options = {}) {
|
|
|
3160
3140
|
return stop;
|
|
3161
3141
|
}
|
|
3162
3142
|
|
|
3143
|
+
const isPrimitive$1 = (val) => val !== Object(val);
|
|
3144
|
+
function useCustomCompareEffect(effect, deps, depsEqual) {
|
|
3145
|
+
if (process.env.NODE_ENV !== "production") {
|
|
3146
|
+
if (!Array.isArray(deps) || !deps.length) {
|
|
3147
|
+
console.warn(
|
|
3148
|
+
"`useCustomCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
|
|
3149
|
+
);
|
|
3150
|
+
}
|
|
3151
|
+
if (deps.every(isPrimitive$1)) {
|
|
3152
|
+
console.warn(
|
|
3153
|
+
"`useCustomCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
|
|
3154
|
+
);
|
|
3155
|
+
}
|
|
3156
|
+
if (typeof depsEqual !== "function") {
|
|
3157
|
+
console.warn(
|
|
3158
|
+
"`useCustomCompareEffect` should be used with depsEqual callback for comparing deps list"
|
|
3159
|
+
);
|
|
3160
|
+
}
|
|
3161
|
+
}
|
|
3162
|
+
const ref = React.useRef(void 0);
|
|
3163
|
+
if (!ref.current || !depsEqual(deps, ref.current)) {
|
|
3164
|
+
ref.current = deps;
|
|
3165
|
+
}
|
|
3166
|
+
React.useEffect(effect, ref.current);
|
|
3167
|
+
}
|
|
3168
|
+
|
|
3169
|
+
const isPrimitive = (val) => val !== Object(val);
|
|
3170
|
+
function useDeepCompareEffect(effect, deps) {
|
|
3171
|
+
if (process.env.NODE_ENV !== "production") {
|
|
3172
|
+
if (!Array.isArray(deps) || !deps.length) {
|
|
3173
|
+
console.warn(
|
|
3174
|
+
"`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
|
|
3175
|
+
);
|
|
3176
|
+
}
|
|
3177
|
+
if (deps.every(isPrimitive)) {
|
|
3178
|
+
console.warn(
|
|
3179
|
+
"`useDeepCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
|
|
3180
|
+
);
|
|
3181
|
+
}
|
|
3182
|
+
}
|
|
3183
|
+
useCustomCompareEffect(effect, deps, isEqual);
|
|
3184
|
+
}
|
|
3185
|
+
|
|
3163
3186
|
function useTitle(title) {
|
|
3164
3187
|
React.useEffect(() => {
|
|
3165
3188
|
document.title = title;
|
|
3166
3189
|
}, [title]);
|
|
3167
3190
|
}
|
|
3168
3191
|
|
|
3169
|
-
function useScriptTag(src, onLoaded = noop, options =
|
|
3192
|
+
function useScriptTag(src, onLoaded = noop, options = defaultOptions) {
|
|
3170
3193
|
const {
|
|
3171
3194
|
immediate = true,
|
|
3172
3195
|
manual = false,
|
|
@@ -3320,7 +3343,7 @@ const preventDefault$1 = (ev) => {
|
|
|
3320
3343
|
ev.preventDefault();
|
|
3321
3344
|
}
|
|
3322
3345
|
};
|
|
3323
|
-
function useLongPress(callback, { isPreventDefault = true, delay = 300 } =
|
|
3346
|
+
function useLongPress(callback, { isPreventDefault = true, delay = 300 } = defaultOptions) {
|
|
3324
3347
|
const timeout = React.useRef();
|
|
3325
3348
|
const target = React.useRef();
|
|
3326
3349
|
const start = React.useCallback(
|
|
@@ -3506,7 +3529,7 @@ function useMediaDevices(options = {}) {
|
|
|
3506
3529
|
return [state, ensurePermissions];
|
|
3507
3530
|
}
|
|
3508
3531
|
|
|
3509
|
-
function useTextDirection(options =
|
|
3532
|
+
function useTextDirection(options = defaultOptions) {
|
|
3510
3533
|
const { selector = "html", initialValue = "ltr" } = options;
|
|
3511
3534
|
const getValue = () => {
|
|
3512
3535
|
var _a, _b;
|
|
@@ -3583,10 +3606,10 @@ function useMouse(target) {
|
|
|
3583
3606
|
return state;
|
|
3584
3607
|
}
|
|
3585
3608
|
|
|
3586
|
-
function useFps(options) {
|
|
3609
|
+
function useFps(options = defaultOptions) {
|
|
3587
3610
|
var _a;
|
|
3588
3611
|
const [fps, setFps] = React.useState(0);
|
|
3589
|
-
const every = (_a = options
|
|
3612
|
+
const every = (_a = options.every) != null ? _a : 10;
|
|
3590
3613
|
const last = React.useRef(performance.now());
|
|
3591
3614
|
const ticks = React.useRef(0);
|
|
3592
3615
|
useRafFn(() => {
|
|
@@ -3613,7 +3636,7 @@ const initCoord = {
|
|
|
3613
3636
|
heading: null,
|
|
3614
3637
|
speed: null
|
|
3615
3638
|
};
|
|
3616
|
-
function useGeolocation(options =
|
|
3639
|
+
function useGeolocation(options = defaultOptions) {
|
|
3617
3640
|
const {
|
|
3618
3641
|
enableHighAccuracy = true,
|
|
3619
3642
|
maximumAge = 3e4,
|
|
@@ -3839,20 +3862,17 @@ var screenfull$1 = {exports: {}};
|
|
|
3839
3862
|
|
|
3840
3863
|
var screenfull = screenfull$1.exports;
|
|
3841
3864
|
|
|
3842
|
-
function useFullscreen(target, options) {
|
|
3843
|
-
const { onExit, onEnter } = options
|
|
3844
|
-
const onExitRef = useLatest(onExit);
|
|
3845
|
-
const onEnterRef = useLatest(onEnter);
|
|
3865
|
+
function useFullscreen(target, options = defaultOptions) {
|
|
3866
|
+
const { onExit, onEnter } = options;
|
|
3846
3867
|
const [state, setState] = React.useState(false);
|
|
3847
3868
|
const onChange = () => {
|
|
3848
|
-
var _a, _b;
|
|
3849
3869
|
if (screenfull.isEnabled) {
|
|
3850
3870
|
const { isFullscreen } = screenfull;
|
|
3851
3871
|
if (isFullscreen) {
|
|
3852
|
-
|
|
3872
|
+
onEnter == null ? void 0 : onEnter();
|
|
3853
3873
|
} else {
|
|
3854
3874
|
screenfull.off("change", onChange);
|
|
3855
|
-
|
|
3875
|
+
onExit == null ? void 0 : onExit();
|
|
3856
3876
|
}
|
|
3857
3877
|
setState(isFullscreen);
|
|
3858
3878
|
}
|
|
@@ -3994,7 +4014,7 @@ function useOrientation(initialState = defaultState$1) {
|
|
|
3994
4014
|
return [state, lockOrientation, unlockOrientation];
|
|
3995
4015
|
}
|
|
3996
4016
|
|
|
3997
|
-
function useIntersectionObserver(target, callback, options =
|
|
4017
|
+
function useIntersectionObserver(target, callback, options = defaultOptions) {
|
|
3998
4018
|
const savedCallback = useLatest(callback);
|
|
3999
4019
|
const observerRef = React.useRef();
|
|
4000
4020
|
const element = useLatestElement(target);
|
|
@@ -4003,7 +4023,7 @@ function useIntersectionObserver(target, callback, options = {}) {
|
|
|
4003
4023
|
observerRef.current.disconnect();
|
|
4004
4024
|
}
|
|
4005
4025
|
}, []);
|
|
4006
|
-
|
|
4026
|
+
React.useEffect(() => {
|
|
4007
4027
|
if (!element) {
|
|
4008
4028
|
return;
|
|
4009
4029
|
}
|
|
@@ -4032,14 +4052,24 @@ function usePageLeave() {
|
|
|
4032
4052
|
return isLeft;
|
|
4033
4053
|
}
|
|
4034
4054
|
|
|
4035
|
-
|
|
4036
|
-
|
|
4037
|
-
|
|
4038
|
-
|
|
4039
|
-
|
|
4040
|
-
|
|
4041
|
-
|
|
4042
|
-
|
|
4055
|
+
const getInitialState$1 = (defaultValue) => {
|
|
4056
|
+
if (defaultValue !== void 0) {
|
|
4057
|
+
return defaultValue;
|
|
4058
|
+
}
|
|
4059
|
+
if (isBrowser) {
|
|
4060
|
+
return document.visibilityState;
|
|
4061
|
+
}
|
|
4062
|
+
if (process.env.NODE_ENV !== "production") {
|
|
4063
|
+
console.warn(
|
|
4064
|
+
"`useDocumentVisibility` When server side rendering, defaultValue should be defined to prevent a hydration mismatches."
|
|
4065
|
+
);
|
|
4066
|
+
}
|
|
4067
|
+
return "visible";
|
|
4068
|
+
};
|
|
4069
|
+
function useDocumentVisibility(defaultValue) {
|
|
4070
|
+
const [visible, setVisible] = React.useState(
|
|
4071
|
+
getInitialState$1(defaultValue)
|
|
4072
|
+
);
|
|
4043
4073
|
useEventListener(
|
|
4044
4074
|
"visibilitychange",
|
|
4045
4075
|
() => {
|
|
@@ -4050,7 +4080,7 @@ function useDocumentVisibility() {
|
|
|
4050
4080
|
return visible;
|
|
4051
4081
|
}
|
|
4052
4082
|
|
|
4053
|
-
function useResizeObserver(target, callback, options =
|
|
4083
|
+
function useResizeObserver(target, callback, options = defaultOptions) {
|
|
4054
4084
|
const savedCallback = useLatest(callback);
|
|
4055
4085
|
const observerRef = React.useRef();
|
|
4056
4086
|
const element = useLatestElement(target);
|
|
@@ -4059,7 +4089,7 @@ function useResizeObserver(target, callback, options = {}) {
|
|
|
4059
4089
|
observerRef.current.disconnect();
|
|
4060
4090
|
}
|
|
4061
4091
|
}, []);
|
|
4062
|
-
|
|
4092
|
+
React.useEffect(() => {
|
|
4063
4093
|
if (!element) {
|
|
4064
4094
|
return;
|
|
4065
4095
|
}
|
|
@@ -4116,17 +4146,17 @@ function useDropZone(target, onDrop) {
|
|
|
4116
4146
|
}
|
|
4117
4147
|
|
|
4118
4148
|
var __defProp$2 = Object.defineProperty;
|
|
4119
|
-
var __getOwnPropSymbols$
|
|
4120
|
-
var __hasOwnProp$
|
|
4121
|
-
var __propIsEnum$
|
|
4149
|
+
var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
|
|
4150
|
+
var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
|
|
4151
|
+
var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
|
|
4122
4152
|
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4123
4153
|
var __spreadValues$2 = (a, b) => {
|
|
4124
4154
|
for (var prop in b || (b = {}))
|
|
4125
|
-
if (__hasOwnProp$
|
|
4155
|
+
if (__hasOwnProp$2.call(b, prop))
|
|
4126
4156
|
__defNormalProp$2(a, prop, b[prop]);
|
|
4127
|
-
if (__getOwnPropSymbols$
|
|
4128
|
-
for (var prop of __getOwnPropSymbols$
|
|
4129
|
-
if (__propIsEnum$
|
|
4157
|
+
if (__getOwnPropSymbols$2)
|
|
4158
|
+
for (var prop of __getOwnPropSymbols$2(b)) {
|
|
4159
|
+
if (__propIsEnum$2.call(b, prop))
|
|
4130
4160
|
__defNormalProp$2(a, prop, b[prop]);
|
|
4131
4161
|
}
|
|
4132
4162
|
return a;
|
|
@@ -4135,7 +4165,7 @@ const DEFAULT_OPTIONS = {
|
|
|
4135
4165
|
multiple: true,
|
|
4136
4166
|
accept: "*"
|
|
4137
4167
|
};
|
|
4138
|
-
function useFileDialog(options =
|
|
4168
|
+
function useFileDialog(options = defaultOptions) {
|
|
4139
4169
|
const [files, setFiles] = React.useState(null);
|
|
4140
4170
|
const inputRef = React.useRef();
|
|
4141
4171
|
const initFn = React.useCallback(() => {
|
|
@@ -4171,7 +4201,11 @@ function useFileDialog(options = {}) {
|
|
|
4171
4201
|
}
|
|
4172
4202
|
|
|
4173
4203
|
const ARRIVED_STATE_THRESHOLD_PIXELS = 1;
|
|
4174
|
-
|
|
4204
|
+
const defaultListerOptions = {
|
|
4205
|
+
capture: false,
|
|
4206
|
+
passive: true
|
|
4207
|
+
};
|
|
4208
|
+
function useScroll(target, options = defaultOptions) {
|
|
4175
4209
|
const {
|
|
4176
4210
|
throttle = 0,
|
|
4177
4211
|
idle = 200,
|
|
@@ -4183,10 +4217,7 @@ function useScroll(target, options = {}) {
|
|
|
4183
4217
|
top: 0,
|
|
4184
4218
|
bottom: 0
|
|
4185
4219
|
},
|
|
4186
|
-
eventListenerOptions =
|
|
4187
|
-
capture: false,
|
|
4188
|
-
passive: true
|
|
4189
|
-
}
|
|
4220
|
+
eventListenerOptions = defaultListerOptions
|
|
4190
4221
|
} = options;
|
|
4191
4222
|
const [x, setX] = React.useState(0);
|
|
4192
4223
|
const [y, setY] = React.useState(0);
|
|
@@ -4245,17 +4276,17 @@ function useScroll(target, options = {}) {
|
|
|
4245
4276
|
var __defProp$1 = Object.defineProperty;
|
|
4246
4277
|
var __defProps = Object.defineProperties;
|
|
4247
4278
|
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4248
|
-
var __getOwnPropSymbols$
|
|
4249
|
-
var __hasOwnProp$
|
|
4250
|
-
var __propIsEnum$
|
|
4279
|
+
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
|
|
4280
|
+
var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
|
|
4281
|
+
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
|
|
4251
4282
|
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4252
4283
|
var __spreadValues$1 = (a, b) => {
|
|
4253
4284
|
for (var prop in b || (b = {}))
|
|
4254
|
-
if (__hasOwnProp$
|
|
4285
|
+
if (__hasOwnProp$1.call(b, prop))
|
|
4255
4286
|
__defNormalProp$1(a, prop, b[prop]);
|
|
4256
|
-
if (__getOwnPropSymbols$
|
|
4257
|
-
for (var prop of __getOwnPropSymbols$
|
|
4258
|
-
if (__propIsEnum$
|
|
4287
|
+
if (__getOwnPropSymbols$1)
|
|
4288
|
+
for (var prop of __getOwnPropSymbols$1(b)) {
|
|
4289
|
+
if (__propIsEnum$1.call(b, prop))
|
|
4259
4290
|
__defNormalProp$1(a, prop, b[prop]);
|
|
4260
4291
|
}
|
|
4261
4292
|
return a;
|
|
@@ -4281,7 +4312,7 @@ var __async$3 = (__this, __arguments, generator) => {
|
|
|
4281
4312
|
step((generator = generator.apply(__this, __arguments)).next());
|
|
4282
4313
|
});
|
|
4283
4314
|
};
|
|
4284
|
-
function useInfiniteScroll(target, onLoadMore, options =
|
|
4315
|
+
function useInfiniteScroll(target, onLoadMore, options = defaultOptions) {
|
|
4285
4316
|
var _a, _b;
|
|
4286
4317
|
const savedLoadMore = useLatest(onLoadMore);
|
|
4287
4318
|
const direction = (_a = options.direction) != null ? _a : "bottom";
|
|
@@ -4291,10 +4322,8 @@ function useInfiniteScroll(target, onLoadMore, options = {}) {
|
|
|
4291
4322
|
}, options.offset)
|
|
4292
4323
|
}));
|
|
4293
4324
|
const element = useLatestElement(target);
|
|
4294
|
-
const latestOptions = useLatest(options);
|
|
4295
4325
|
const di = state[3][direction];
|
|
4296
4326
|
useUpdateEffect(() => {
|
|
4297
|
-
const opts = latestOptions.current;
|
|
4298
4327
|
const fn = () => __async$3(this, null, function* () {
|
|
4299
4328
|
var _a2, _b2;
|
|
4300
4329
|
const previous = {
|
|
@@ -4302,7 +4331,7 @@ function useInfiniteScroll(target, onLoadMore, options = {}) {
|
|
|
4302
4331
|
width: (_b2 = element == null ? void 0 : element.scrollWidth) != null ? _b2 : 0
|
|
4303
4332
|
};
|
|
4304
4333
|
yield savedLoadMore.current(state);
|
|
4305
|
-
if (
|
|
4334
|
+
if (options.preserveScrollPosition && element) {
|
|
4306
4335
|
element.scrollTo({
|
|
4307
4336
|
top: element.scrollHeight - previous.height,
|
|
4308
4337
|
left: element.scrollWidth - previous.width
|
|
@@ -4310,7 +4339,7 @@ function useInfiniteScroll(target, onLoadMore, options = {}) {
|
|
|
4310
4339
|
}
|
|
4311
4340
|
});
|
|
4312
4341
|
fn();
|
|
4313
|
-
}, [di]);
|
|
4342
|
+
}, [di, options.preserveScrollPosition]);
|
|
4314
4343
|
}
|
|
4315
4344
|
|
|
4316
4345
|
const defaultEvents = [
|
|
@@ -4319,7 +4348,7 @@ const defaultEvents = [
|
|
|
4319
4348
|
"keydown",
|
|
4320
4349
|
"keyup"
|
|
4321
4350
|
];
|
|
4322
|
-
function useKeyModifier(modifier, options =
|
|
4351
|
+
function useKeyModifier(modifier, options = defaultOptions) {
|
|
4323
4352
|
const { events = defaultEvents, initial = false } = options;
|
|
4324
4353
|
const [state, setState] = React.useState(initial);
|
|
4325
4354
|
useMount(() => {
|
|
@@ -4343,7 +4372,8 @@ function useKeyModifier(modifier, options = {}) {
|
|
|
4343
4372
|
return state;
|
|
4344
4373
|
}
|
|
4345
4374
|
|
|
4346
|
-
|
|
4375
|
+
const listenerOptions$2 = { passive: true };
|
|
4376
|
+
function useMousePressed(target, options = defaultOptions) {
|
|
4347
4377
|
const { touch = true, drag = true, initialValue = false } = options;
|
|
4348
4378
|
const [pressed, setPressed] = React.useState(initialValue);
|
|
4349
4379
|
const [sourceType, setSourceType] = React.useState(null);
|
|
@@ -4359,31 +4389,27 @@ function useMousePressed(target, options = {}) {
|
|
|
4359
4389
|
setPressed(false);
|
|
4360
4390
|
setSourceType(null);
|
|
4361
4391
|
}, []);
|
|
4362
|
-
useEventListener("mousedown", onPressed("mouse"), target,
|
|
4363
|
-
useEventListener("mouseleave", onReleased, () => window,
|
|
4364
|
-
useEventListener("mouseup", onReleased, () => window,
|
|
4392
|
+
useEventListener("mousedown", onPressed("mouse"), target, listenerOptions$2);
|
|
4393
|
+
useEventListener("mouseleave", onReleased, () => window, listenerOptions$2);
|
|
4394
|
+
useEventListener("mouseup", onReleased, () => window, listenerOptions$2);
|
|
4365
4395
|
React.useEffect(() => {
|
|
4366
4396
|
if (drag) {
|
|
4367
|
-
element == null ? void 0 : element.addEventListener(
|
|
4368
|
-
|
|
4369
|
-
|
|
4370
|
-
|
|
4371
|
-
|
|
4372
|
-
|
|
4373
|
-
element == null ? void 0 : element.addEventListener("dragend", onReleased,
|
|
4374
|
-
passive: true
|
|
4375
|
-
});
|
|
4397
|
+
element == null ? void 0 : element.addEventListener(
|
|
4398
|
+
"dragstart",
|
|
4399
|
+
onPressed("mouse"),
|
|
4400
|
+
listenerOptions$2
|
|
4401
|
+
);
|
|
4402
|
+
element == null ? void 0 : element.addEventListener("drop", onReleased, listenerOptions$2);
|
|
4403
|
+
element == null ? void 0 : element.addEventListener("dragend", onReleased, listenerOptions$2);
|
|
4376
4404
|
}
|
|
4377
4405
|
if (touch) {
|
|
4378
|
-
element == null ? void 0 : element.addEventListener(
|
|
4379
|
-
|
|
4380
|
-
|
|
4381
|
-
|
|
4382
|
-
|
|
4383
|
-
|
|
4384
|
-
element == null ? void 0 : element.addEventListener("touchcancel", onReleased,
|
|
4385
|
-
passive: true
|
|
4386
|
-
});
|
|
4406
|
+
element == null ? void 0 : element.addEventListener(
|
|
4407
|
+
"touchstart",
|
|
4408
|
+
onPressed("touch"),
|
|
4409
|
+
listenerOptions$2
|
|
4410
|
+
);
|
|
4411
|
+
element == null ? void 0 : element.addEventListener("touchend", onReleased, listenerOptions$2);
|
|
4412
|
+
element == null ? void 0 : element.addEventListener("touchcancel", onReleased, listenerOptions$2);
|
|
4387
4413
|
}
|
|
4388
4414
|
return () => {
|
|
4389
4415
|
if (drag) {
|
|
@@ -4454,7 +4480,7 @@ function useScrollLock(target, initialState = false) {
|
|
|
4454
4480
|
return [locked, set];
|
|
4455
4481
|
}
|
|
4456
4482
|
|
|
4457
|
-
function useElementSize(target, options =
|
|
4483
|
+
function useElementSize(target, options = defaultOptions) {
|
|
4458
4484
|
const { box = "content-box" } = options;
|
|
4459
4485
|
const [width, setWidth] = React.useState(0);
|
|
4460
4486
|
const [height, setHeight] = React.useState(0);
|
|
@@ -4481,44 +4507,38 @@ function useVirtualList(list = [], options) {
|
|
|
4481
4507
|
const [currentList, setCurrentList] = React.useState([]);
|
|
4482
4508
|
const { itemHeight, overscan = 5, containerHeight = 300 } = options;
|
|
4483
4509
|
const state = React.useRef({ start: 0, end: 10 });
|
|
4484
|
-
const getViewCapacity =
|
|
4485
|
-
(
|
|
4486
|
-
|
|
4487
|
-
|
|
4488
|
-
|
|
4489
|
-
|
|
4490
|
-
|
|
4491
|
-
|
|
4492
|
-
|
|
4493
|
-
|
|
4494
|
-
|
|
4495
|
-
|
|
4496
|
-
|
|
4497
|
-
break;
|
|
4498
|
-
}
|
|
4510
|
+
const getViewCapacity = (containerHeight2) => {
|
|
4511
|
+
if (typeof itemHeight === "number") {
|
|
4512
|
+
return Math.ceil(containerHeight2 / itemHeight);
|
|
4513
|
+
}
|
|
4514
|
+
const { start = 0 } = state.current;
|
|
4515
|
+
let sum = 0;
|
|
4516
|
+
let capacity = 0;
|
|
4517
|
+
for (let i = start; i < list.length; i++) {
|
|
4518
|
+
const height2 = itemHeight(i);
|
|
4519
|
+
sum += height2;
|
|
4520
|
+
if (sum >= containerHeight2) {
|
|
4521
|
+
capacity = i;
|
|
4522
|
+
break;
|
|
4499
4523
|
}
|
|
4500
|
-
|
|
4501
|
-
|
|
4502
|
-
|
|
4503
|
-
)
|
|
4504
|
-
|
|
4505
|
-
|
|
4506
|
-
|
|
4507
|
-
|
|
4508
|
-
|
|
4509
|
-
|
|
4510
|
-
|
|
4511
|
-
|
|
4512
|
-
|
|
4513
|
-
|
|
4514
|
-
offset = i;
|
|
4515
|
-
break;
|
|
4516
|
-
}
|
|
4524
|
+
}
|
|
4525
|
+
return capacity - start;
|
|
4526
|
+
};
|
|
4527
|
+
const getOffset = (scrollTop) => {
|
|
4528
|
+
if (typeof itemHeight === "number")
|
|
4529
|
+
return Math.floor(scrollTop / itemHeight) + 1;
|
|
4530
|
+
let sum = 0;
|
|
4531
|
+
let offset = 0;
|
|
4532
|
+
for (let i = 0; i < list.length; i++) {
|
|
4533
|
+
const height2 = itemHeight(i);
|
|
4534
|
+
sum += height2;
|
|
4535
|
+
if (sum >= scrollTop) {
|
|
4536
|
+
offset = i;
|
|
4537
|
+
break;
|
|
4517
4538
|
}
|
|
4518
|
-
|
|
4519
|
-
|
|
4520
|
-
|
|
4521
|
-
);
|
|
4539
|
+
}
|
|
4540
|
+
return offset + 1;
|
|
4541
|
+
};
|
|
4522
4542
|
const calculateRange = useEvent(() => {
|
|
4523
4543
|
const element = containerRef.current;
|
|
4524
4544
|
if (element != null) {
|
|
@@ -4530,17 +4550,18 @@ function useVirtualList(list = [], options) {
|
|
|
4530
4550
|
start: from < 0 ? 0 : from,
|
|
4531
4551
|
end: to > list.length ? list.length : to
|
|
4532
4552
|
};
|
|
4553
|
+
const { start, end } = state.current;
|
|
4533
4554
|
setCurrentList(
|
|
4534
|
-
list.slice(
|
|
4555
|
+
list.slice(start, end).map((ele, index) => ({
|
|
4535
4556
|
data: ele,
|
|
4536
|
-
index: index +
|
|
4557
|
+
index: index + start
|
|
4537
4558
|
}))
|
|
4538
4559
|
);
|
|
4539
4560
|
}
|
|
4540
4561
|
});
|
|
4541
4562
|
React.useEffect(() => {
|
|
4542
4563
|
calculateRange();
|
|
4543
|
-
}, [width, height, list
|
|
4564
|
+
}, [width, height, list]);
|
|
4544
4565
|
const totalHeight = React.useMemo(() => {
|
|
4545
4566
|
if (typeof itemHeight === "number") {
|
|
4546
4567
|
return list.length * itemHeight;
|
|
@@ -4558,16 +4579,7 @@ function useVirtualList(list = [], options) {
|
|
|
4558
4579
|
},
|
|
4559
4580
|
[itemHeight, list]
|
|
4560
4581
|
);
|
|
4561
|
-
const
|
|
4562
|
-
if (containerRef.current) {
|
|
4563
|
-
containerRef.current.scrollTop = getDistanceTop(index);
|
|
4564
|
-
calculateRange();
|
|
4565
|
-
}
|
|
4566
|
-
});
|
|
4567
|
-
const offsetTop = React.useMemo(
|
|
4568
|
-
() => getDistanceTop(state.current.start),
|
|
4569
|
-
[getDistanceTop]
|
|
4570
|
-
);
|
|
4582
|
+
const offsetTop = getDistanceTop(state.current.start);
|
|
4571
4583
|
const wrapperProps = React.useMemo(() => {
|
|
4572
4584
|
return {
|
|
4573
4585
|
style: {
|
|
@@ -4576,7 +4588,13 @@ function useVirtualList(list = [], options) {
|
|
|
4576
4588
|
marginTop: `${offsetTop}px`
|
|
4577
4589
|
}
|
|
4578
4590
|
};
|
|
4579
|
-
}, [
|
|
4591
|
+
}, [totalHeight, offsetTop]);
|
|
4592
|
+
const scrollTo = (index) => {
|
|
4593
|
+
if (containerRef.current) {
|
|
4594
|
+
containerRef.current.scrollTop = getDistanceTop(index);
|
|
4595
|
+
calculateRange();
|
|
4596
|
+
}
|
|
4597
|
+
};
|
|
4580
4598
|
const containerStyle = React.useMemo(() => {
|
|
4581
4599
|
return { overflowY: "auto", height: containerHeight };
|
|
4582
4600
|
}, [containerHeight]);
|
|
@@ -4702,7 +4720,7 @@ function useDraggable(target, options = {}) {
|
|
|
4702
4720
|
return [position.x, position.y, !!pressedDelta];
|
|
4703
4721
|
}
|
|
4704
4722
|
|
|
4705
|
-
function useElementBounding(target, options =
|
|
4723
|
+
function useElementBounding(target, options = defaultOptions) {
|
|
4706
4724
|
const {
|
|
4707
4725
|
reset = true,
|
|
4708
4726
|
windowResize = true,
|
|
@@ -4777,7 +4795,7 @@ function useElementBounding(target, options = {}) {
|
|
|
4777
4795
|
};
|
|
4778
4796
|
}
|
|
4779
4797
|
|
|
4780
|
-
function useElementVisibility(target, options =
|
|
4798
|
+
function useElementVisibility(target, options = defaultOptions) {
|
|
4781
4799
|
const [visible, setVisible] = React.useState(false);
|
|
4782
4800
|
const callback = React.useCallback((entries) => {
|
|
4783
4801
|
const rect = entries[0].boundingClientRect;
|
|
@@ -4789,13 +4807,11 @@ function useElementVisibility(target, options = {}) {
|
|
|
4789
4807
|
return [visible, stop];
|
|
4790
4808
|
}
|
|
4791
4809
|
|
|
4792
|
-
function useWindowsFocus() {
|
|
4793
|
-
const [focused, setFocused] = React.useState(
|
|
4794
|
-
|
|
4795
|
-
|
|
4796
|
-
|
|
4797
|
-
return window.document.hasFocus();
|
|
4798
|
-
});
|
|
4810
|
+
function useWindowsFocus(defauleValue = false) {
|
|
4811
|
+
const [focused, setFocused] = React.useState(defauleValue);
|
|
4812
|
+
React.useEffect(() => {
|
|
4813
|
+
setFocused(window.document.hasFocus());
|
|
4814
|
+
}, []);
|
|
4799
4815
|
useEventListener("blur", () => {
|
|
4800
4816
|
setFocused(false);
|
|
4801
4817
|
});
|
|
@@ -4823,6 +4839,10 @@ function useWindowSize() {
|
|
|
4823
4839
|
return windowSize;
|
|
4824
4840
|
}
|
|
4825
4841
|
|
|
4842
|
+
const listenerOptions$1 = {
|
|
4843
|
+
capture: false,
|
|
4844
|
+
passive: true
|
|
4845
|
+
};
|
|
4826
4846
|
function useWindowScroll() {
|
|
4827
4847
|
const [state, setState] = useRafState(() => ({
|
|
4828
4848
|
x: 0,
|
|
@@ -4831,10 +4851,7 @@ function useWindowScroll() {
|
|
|
4831
4851
|
const handleScroll = () => {
|
|
4832
4852
|
setState({ x: window.scrollX, y: window.scrollY });
|
|
4833
4853
|
};
|
|
4834
|
-
useEventListener("scroll", handleScroll, window,
|
|
4835
|
-
capture: false,
|
|
4836
|
-
passive: true
|
|
4837
|
-
});
|
|
4854
|
+
useEventListener("scroll", handleScroll, window, listenerOptions$1);
|
|
4838
4855
|
useIsomorphicLayoutEffect(() => {
|
|
4839
4856
|
handleScroll();
|
|
4840
4857
|
}, []);
|
|
@@ -5106,17 +5123,16 @@ const getRelativePosition = ({
|
|
|
5106
5123
|
return 0;
|
|
5107
5124
|
};
|
|
5108
5125
|
|
|
5109
|
-
|
|
5126
|
+
const listenerOptions = { passive: true };
|
|
5127
|
+
function useScrollIntoView(targetElement, {
|
|
5110
5128
|
duration = 1250,
|
|
5111
5129
|
axis = "y",
|
|
5112
5130
|
onScrollFinish,
|
|
5113
5131
|
easing = easeInOutQuad,
|
|
5114
5132
|
offset = 0,
|
|
5115
5133
|
cancelable = true,
|
|
5116
|
-
isList = false
|
|
5117
|
-
|
|
5118
|
-
scrollElement
|
|
5119
|
-
}) {
|
|
5134
|
+
isList = false
|
|
5135
|
+
} = defaultOptions, scrollElement) {
|
|
5120
5136
|
const frameID = React.useRef(0);
|
|
5121
5137
|
const startTime = React.useRef(0);
|
|
5122
5138
|
const shouldStop = React.useRef(false);
|
|
@@ -5174,8 +5190,8 @@ function useScrollIntoView({
|
|
|
5174
5190
|
shouldStop.current = true;
|
|
5175
5191
|
}
|
|
5176
5192
|
};
|
|
5177
|
-
useEventListener("wheel", handleStop, null,
|
|
5178
|
-
useEventListener("touchmove", handleStop, null,
|
|
5193
|
+
useEventListener("wheel", handleStop, null, listenerOptions);
|
|
5194
|
+
useEventListener("touchmove", handleStop, null, listenerOptions);
|
|
5179
5195
|
React.useEffect(() => cancel, []);
|
|
5180
5196
|
return {
|
|
5181
5197
|
scrollIntoView,
|
|
@@ -5183,12 +5199,7 @@ function useScrollIntoView({
|
|
|
5183
5199
|
};
|
|
5184
5200
|
}
|
|
5185
5201
|
|
|
5186
|
-
const useSticky = ({
|
|
5187
|
-
targetElement,
|
|
5188
|
-
scrollElement,
|
|
5189
|
-
axis = "y",
|
|
5190
|
-
nav = 0
|
|
5191
|
-
}) => {
|
|
5202
|
+
const useSticky = (targetElement, { axis = "y", nav = 0 }, scrollElement) => {
|
|
5192
5203
|
const [isSticky, setSticky] = React.useState(false);
|
|
5193
5204
|
const element = useLatestElement(targetElement);
|
|
5194
5205
|
const { run: scrollHandler } = useThrottleFn(() => {
|
|
@@ -5472,54 +5483,51 @@ function init (converter, defaultAttributes) {
|
|
|
5472
5483
|
|
|
5473
5484
|
var api = init(defaultConverter, { path: '/' });
|
|
5474
5485
|
|
|
5475
|
-
|
|
5476
|
-
|
|
5477
|
-
|
|
5478
|
-
|
|
5479
|
-
|
|
5480
|
-
|
|
5481
|
-
|
|
5482
|
-
|
|
5483
|
-
|
|
5484
|
-
|
|
5485
|
-
|
|
5486
|
-
|
|
5487
|
-
|
|
5488
|
-
return target;
|
|
5486
|
+
const getInitialState = (key, defaultValue) => {
|
|
5487
|
+
if (defaultValue !== void 0) {
|
|
5488
|
+
return defaultValue;
|
|
5489
|
+
}
|
|
5490
|
+
if (isBrowser) {
|
|
5491
|
+
return api.get(key);
|
|
5492
|
+
}
|
|
5493
|
+
if (process.env.NODE_ENV !== "production") {
|
|
5494
|
+
console.warn(
|
|
5495
|
+
"`useCookie` When server side rendering, defaultValue should be defined to prevent a hydration mismatches."
|
|
5496
|
+
);
|
|
5497
|
+
}
|
|
5498
|
+
return "";
|
|
5489
5499
|
};
|
|
5490
|
-
function useCookie(key, options = {
|
|
5491
|
-
|
|
5492
|
-
|
|
5493
|
-
|
|
5494
|
-
|
|
5495
|
-
useDeepCompareEffect(() => {
|
|
5496
|
-
const data = csrData ? isFunction$1(csrData) ? csrData() : csrData : isFunction$1(defaultValue) ? defaultValue() : defaultValue;
|
|
5500
|
+
function useCookie(key, options = defaultOptions, defaultValue) {
|
|
5501
|
+
const [cookieValue, setCookieValue] = React.useState(
|
|
5502
|
+
getInitialState(key, defaultValue)
|
|
5503
|
+
);
|
|
5504
|
+
React.useEffect(() => {
|
|
5497
5505
|
const getStoredValue = () => {
|
|
5498
5506
|
const raw = api.get(key);
|
|
5499
5507
|
if (raw !== void 0 && raw !== null) {
|
|
5500
5508
|
return raw;
|
|
5501
5509
|
} else {
|
|
5502
|
-
if (
|
|
5510
|
+
if (defaultValue === void 0) {
|
|
5503
5511
|
api.remove(key);
|
|
5504
5512
|
} else {
|
|
5505
|
-
api.set(key,
|
|
5513
|
+
api.set(key, defaultValue, options);
|
|
5506
5514
|
}
|
|
5507
|
-
return
|
|
5515
|
+
return defaultValue;
|
|
5508
5516
|
}
|
|
5509
5517
|
};
|
|
5510
5518
|
setCookieValue(getStoredValue());
|
|
5511
|
-
}, [
|
|
5519
|
+
}, [defaultValue, key, options]);
|
|
5512
5520
|
const updateCookie = React.useCallback(
|
|
5513
5521
|
(newValue) => {
|
|
5514
5522
|
const value = isFunction$1(newValue) ? newValue(cookieValue) : newValue;
|
|
5515
5523
|
if (value === void 0) {
|
|
5516
5524
|
api.remove(key);
|
|
5517
5525
|
} else {
|
|
5518
|
-
api.set(key, value,
|
|
5526
|
+
api.set(key, value, options);
|
|
5519
5527
|
}
|
|
5520
5528
|
setCookieValue(value);
|
|
5521
5529
|
},
|
|
5522
|
-
[key, cookieValue]
|
|
5530
|
+
[key, cookieValue, options]
|
|
5523
5531
|
);
|
|
5524
5532
|
const refreshCookie = React.useCallback(() => {
|
|
5525
5533
|
const cookieValue2 = api.get(key);
|
|
@@ -5527,7 +5535,7 @@ function useCookie(key, options = {
|
|
|
5527
5535
|
setCookieValue(cookieValue2);
|
|
5528
5536
|
}
|
|
5529
5537
|
}, [key]);
|
|
5530
|
-
return
|
|
5538
|
+
return [cookieValue, updateCookie, refreshCookie];
|
|
5531
5539
|
}
|
|
5532
5540
|
|
|
5533
5541
|
function useDoubleClick({
|
|
@@ -5601,7 +5609,7 @@ const defaultState = {
|
|
|
5601
5609
|
bottom: 0,
|
|
5602
5610
|
right: 0
|
|
5603
5611
|
};
|
|
5604
|
-
function useMeasure(target, options =
|
|
5612
|
+
function useMeasure(target, options = defaultOptions) {
|
|
5605
5613
|
const [rect, setRect] = React.useState(defaultState);
|
|
5606
5614
|
const stop = useResizeObserver(
|
|
5607
5615
|
target,
|