@reactuses/core 2.2.1 → 2.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +109 -95
- package/dist/index.d.ts +15 -7
- package/dist/index.mjs +110 -96
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -81,6 +81,49 @@ function useEvent(fn) {
|
|
|
81
81
|
}, []);
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
+
const isPrimitive$1 = (val) => val !== Object(val);
|
|
85
|
+
function useCustomCompareEffect(effect, deps, depsEqual) {
|
|
86
|
+
if (process.env.NODE_ENV !== "production") {
|
|
87
|
+
if (!(deps instanceof Array) || !deps.length) {
|
|
88
|
+
console.warn(
|
|
89
|
+
"`useCustomCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
if (deps.every(isPrimitive$1)) {
|
|
93
|
+
console.warn(
|
|
94
|
+
"`useCustomCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
if (typeof depsEqual !== "function") {
|
|
98
|
+
console.warn(
|
|
99
|
+
"`useCustomCompareEffect` should be used with depsEqual callback for comparing deps list"
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const ref = React.useRef(void 0);
|
|
104
|
+
if (!ref.current || !depsEqual(deps, ref.current)) {
|
|
105
|
+
ref.current = deps;
|
|
106
|
+
}
|
|
107
|
+
React.useEffect(effect, ref.current);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const isPrimitive = (val) => val !== Object(val);
|
|
111
|
+
function useDeepCompareEffect(effect, deps) {
|
|
112
|
+
if (process.env.NODE_ENV !== "production") {
|
|
113
|
+
if (!(deps instanceof Array) || !deps.length) {
|
|
114
|
+
console.warn(
|
|
115
|
+
"`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
if (deps.every(isPrimitive)) {
|
|
119
|
+
console.warn(
|
|
120
|
+
"`useDeepCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
useCustomCompareEffect(effect, deps, lodash.isEqual);
|
|
125
|
+
}
|
|
126
|
+
|
|
84
127
|
const StorageSerializers = {
|
|
85
128
|
boolean: {
|
|
86
129
|
read: (v) => v === "true",
|
|
@@ -120,37 +163,35 @@ function useStorage(key, defaults, getStorage, options = {}) {
|
|
|
120
163
|
console.error(e);
|
|
121
164
|
}, []);
|
|
122
165
|
let storage;
|
|
123
|
-
const { onError = defaultOnError,
|
|
166
|
+
const { onError = defaultOnError, csrData } = options;
|
|
124
167
|
try {
|
|
125
168
|
storage = getStorage();
|
|
126
169
|
} catch (err) {
|
|
127
170
|
onError(err);
|
|
128
171
|
}
|
|
129
172
|
const type = guessSerializerType(defaults);
|
|
130
|
-
const serializer = React.useMemo(
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
},
|
|
135
|
-
[options.serializer, type]
|
|
136
|
-
);
|
|
173
|
+
const serializer = React.useMemo(() => {
|
|
174
|
+
var _a;
|
|
175
|
+
return (_a = options.serializer) != null ? _a : StorageSerializers[type];
|
|
176
|
+
}, [options.serializer, type]);
|
|
137
177
|
const [state, setState] = React.useState(defaults);
|
|
138
|
-
|
|
178
|
+
useDeepCompareEffect(() => {
|
|
179
|
+
const data = csrData ? isFunction(csrData) ? csrData() : csrData : defaults;
|
|
139
180
|
const getStoredValue = () => {
|
|
140
181
|
try {
|
|
141
182
|
const raw = storage == null ? void 0 : storage.getItem(key);
|
|
142
|
-
if (raw !== void 0 && raw !== null
|
|
183
|
+
if (raw !== void 0 && raw !== null) {
|
|
143
184
|
return serializer.read(raw);
|
|
144
185
|
} else {
|
|
145
|
-
storage == null ? void 0 : storage.setItem(key, serializer.write(
|
|
146
|
-
return
|
|
186
|
+
storage == null ? void 0 : storage.setItem(key, serializer.write(data));
|
|
187
|
+
return data;
|
|
147
188
|
}
|
|
148
189
|
} catch (e) {
|
|
149
190
|
onError(e);
|
|
150
191
|
}
|
|
151
192
|
};
|
|
152
193
|
setState(getStoredValue());
|
|
153
|
-
}, [key,
|
|
194
|
+
}, [key, defaults, serializer, storage, onError]);
|
|
154
195
|
const updateState = useEvent(
|
|
155
196
|
(valOrFunc) => {
|
|
156
197
|
const currentState = isFunction(valOrFunc) ? valOrFunc(state) : valOrFunc;
|
|
@@ -207,6 +248,55 @@ function useInterval(callback, delay, options) {
|
|
|
207
248
|
}, [delay, immediate]);
|
|
208
249
|
}
|
|
209
250
|
|
|
251
|
+
function useDarkMode(options) {
|
|
252
|
+
const {
|
|
253
|
+
selector = "html",
|
|
254
|
+
attribute = "class",
|
|
255
|
+
classNameDark = "",
|
|
256
|
+
classNameLight = "",
|
|
257
|
+
storageKey = "reactuses-color-scheme",
|
|
258
|
+
storage = () => isBrowser ? localStorage : void 0,
|
|
259
|
+
defaultValue = false
|
|
260
|
+
} = options;
|
|
261
|
+
const value = () => {
|
|
262
|
+
return window.matchMedia("(prefers-color-scheme: dark)").matches;
|
|
263
|
+
};
|
|
264
|
+
const [dark, setDark] = useStorage(
|
|
265
|
+
storageKey,
|
|
266
|
+
defaultValue,
|
|
267
|
+
storage,
|
|
268
|
+
{
|
|
269
|
+
csrData: value
|
|
270
|
+
}
|
|
271
|
+
);
|
|
272
|
+
React.useEffect(() => {
|
|
273
|
+
const element = window == null ? void 0 : window.document.querySelector(selector);
|
|
274
|
+
if (!element) {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
if (attribute === "class") {
|
|
278
|
+
dark && classNameDark && element.classList.add(classNameDark);
|
|
279
|
+
!dark && classNameLight && element.classList.add(classNameLight);
|
|
280
|
+
} else {
|
|
281
|
+
dark && classNameDark && element.setAttribute(attribute, classNameDark);
|
|
282
|
+
!dark && classNameLight && element.setAttribute(attribute, classNameLight);
|
|
283
|
+
}
|
|
284
|
+
return () => {
|
|
285
|
+
if (!element) {
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
if (attribute === "class") {
|
|
289
|
+
dark && classNameDark && element.classList.remove(classNameDark);
|
|
290
|
+
!dark && classNameLight && element.classList.remove(classNameLight);
|
|
291
|
+
} else {
|
|
292
|
+
dark && classNameDark && element.removeAttribute(attribute);
|
|
293
|
+
!dark && classNameLight && element.removeAttribute(attribute);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
}, [attribute, classNameDark, classNameLight, dark, selector]);
|
|
297
|
+
return [dark, () => setDark((dark2) => !dark2), setDark];
|
|
298
|
+
}
|
|
299
|
+
|
|
210
300
|
const getInitialState = (query, defaultState) => {
|
|
211
301
|
if (defaultState !== void 0) {
|
|
212
302
|
return defaultState;
|
|
@@ -254,39 +344,6 @@ function usePreferredDark(defaultState) {
|
|
|
254
344
|
return useMediaQuery("(prefers-color-scheme: dark)", defaultState);
|
|
255
345
|
}
|
|
256
346
|
|
|
257
|
-
function useDarkMode(options = {}) {
|
|
258
|
-
const {
|
|
259
|
-
selector = "html",
|
|
260
|
-
attribute = "class",
|
|
261
|
-
initialValue,
|
|
262
|
-
storageKey = "reactuses-color-scheme",
|
|
263
|
-
storage = () => isBrowser ? localStorage : void 0
|
|
264
|
-
} = options;
|
|
265
|
-
const prefersDarkMode = usePreferredDark(false);
|
|
266
|
-
const value = initialValue ? initialValue : prefersDarkMode ? "dark" : "light";
|
|
267
|
-
const [dark, setDark] = useStorage(storageKey, value, storage, {
|
|
268
|
-
ignoreDefaults: false
|
|
269
|
-
});
|
|
270
|
-
const wrappedSetDark = React.useCallback(
|
|
271
|
-
(latestDark) => {
|
|
272
|
-
const element = window == null ? void 0 : window.document.querySelector(selector);
|
|
273
|
-
if (!element) {
|
|
274
|
-
return;
|
|
275
|
-
}
|
|
276
|
-
if (attribute === "class") {
|
|
277
|
-
latestDark && element.classList.add(latestDark);
|
|
278
|
-
dark && element.classList.remove(dark);
|
|
279
|
-
} else {
|
|
280
|
-
latestDark && element.setAttribute(attribute, latestDark);
|
|
281
|
-
dark && element.removeAttribute(attribute);
|
|
282
|
-
}
|
|
283
|
-
setDark(latestDark);
|
|
284
|
-
},
|
|
285
|
-
[attribute, dark, selector, setDark]
|
|
286
|
-
);
|
|
287
|
-
return [dark, wrappedSetDark];
|
|
288
|
-
}
|
|
289
|
-
|
|
290
347
|
function useMount(fn) {
|
|
291
348
|
if (isDev) {
|
|
292
349
|
if (!isFunction(fn)) {
|
|
@@ -510,53 +567,10 @@ function useLatestElement(target, defaultElement) {
|
|
|
510
567
|
const ref = React.useRef(getTargetElement(target, defaultElement));
|
|
511
568
|
React.useEffect(() => {
|
|
512
569
|
ref.current = getTargetElement(target, defaultElement);
|
|
513
|
-
}
|
|
570
|
+
});
|
|
514
571
|
return ref;
|
|
515
572
|
}
|
|
516
573
|
|
|
517
|
-
const isPrimitive$1 = (val) => val !== Object(val);
|
|
518
|
-
function useCustomCompareEffect(effect, deps, depsEqual) {
|
|
519
|
-
if (process.env.NODE_ENV !== "production") {
|
|
520
|
-
if (!(deps instanceof Array) || !deps.length) {
|
|
521
|
-
console.warn(
|
|
522
|
-
"`useCustomCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
|
|
523
|
-
);
|
|
524
|
-
}
|
|
525
|
-
if (deps.every(isPrimitive$1)) {
|
|
526
|
-
console.warn(
|
|
527
|
-
"`useCustomCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
|
|
528
|
-
);
|
|
529
|
-
}
|
|
530
|
-
if (typeof depsEqual !== "function") {
|
|
531
|
-
console.warn(
|
|
532
|
-
"`useCustomCompareEffect` should be used with depsEqual callback for comparing deps list"
|
|
533
|
-
);
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
const ref = React.useRef(void 0);
|
|
537
|
-
if (!ref.current || !depsEqual(deps, ref.current)) {
|
|
538
|
-
ref.current = deps;
|
|
539
|
-
}
|
|
540
|
-
React.useEffect(effect, ref.current);
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
const isPrimitive = (val) => val !== Object(val);
|
|
544
|
-
function useDeepCompareEffect(effect, deps) {
|
|
545
|
-
if (process.env.NODE_ENV !== "production") {
|
|
546
|
-
if (!(deps instanceof Array) || !deps.length) {
|
|
547
|
-
console.warn(
|
|
548
|
-
"`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
|
|
549
|
-
);
|
|
550
|
-
}
|
|
551
|
-
if (deps.every(isPrimitive)) {
|
|
552
|
-
console.warn(
|
|
553
|
-
"`useDeepCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
|
|
554
|
-
);
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
useCustomCompareEffect(effect, deps, lodash.isEqual);
|
|
558
|
-
}
|
|
559
|
-
|
|
560
574
|
function useEventListener(eventName, handler, element, options) {
|
|
561
575
|
const savedHandler = useLatest(handler);
|
|
562
576
|
const targetElementRef = useLatestElement(element, defaultWindow);
|
|
@@ -2398,21 +2412,21 @@ function useClipBorad() {
|
|
|
2398
2412
|
|
|
2399
2413
|
function useClickOutSide(target, handler) {
|
|
2400
2414
|
const savedHandler = useLatest(handler);
|
|
2415
|
+
const element = useLatestElement(target);
|
|
2401
2416
|
const listener = (event) => {
|
|
2402
|
-
|
|
2403
|
-
if (!element) {
|
|
2417
|
+
if (!element.current) {
|
|
2404
2418
|
return;
|
|
2405
2419
|
}
|
|
2406
2420
|
const elements = event.composedPath();
|
|
2407
|
-
if (element === event.target || elements.includes(element)) {
|
|
2421
|
+
if (element.current === event.target || elements.includes(element.current)) {
|
|
2408
2422
|
return;
|
|
2409
2423
|
}
|
|
2410
2424
|
savedHandler.current(event);
|
|
2411
2425
|
};
|
|
2412
|
-
useEventListener("mousedown", listener,
|
|
2426
|
+
useEventListener("mousedown", listener, defaultWindow, {
|
|
2413
2427
|
passive: true
|
|
2414
2428
|
});
|
|
2415
|
-
useEventListener("touchstart", listener,
|
|
2429
|
+
useEventListener("touchstart", listener, defaultWindow, {
|
|
2416
2430
|
passive: true
|
|
2417
2431
|
});
|
|
2418
2432
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -28,9 +28,9 @@ interface UseStorageOptions<T> {
|
|
|
28
28
|
*/
|
|
29
29
|
onError?: (error: unknown) => void;
|
|
30
30
|
/**
|
|
31
|
-
*
|
|
31
|
+
* set to storage when nodata in effect, fallback to defaults
|
|
32
32
|
*/
|
|
33
|
-
|
|
33
|
+
csrData?: T | (() => T);
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
declare function useLocalStorage(key: string, defaults: string, options?: UseStorageOptions<string>): readonly [string | null, Dispatch<SetStateAction<string | null>>];
|
|
@@ -64,7 +64,7 @@ declare function useInterval(callback: () => void, delay?: number | null, option
|
|
|
64
64
|
immediate?: boolean;
|
|
65
65
|
}): void;
|
|
66
66
|
|
|
67
|
-
interface UseDarkOptions
|
|
67
|
+
interface UseDarkOptions {
|
|
68
68
|
/**
|
|
69
69
|
* CSS Selector for the target element applying to
|
|
70
70
|
*
|
|
@@ -78,10 +78,10 @@ interface UseDarkOptions<T> {
|
|
|
78
78
|
*/
|
|
79
79
|
attribute?: string;
|
|
80
80
|
/**
|
|
81
|
-
*
|
|
82
|
-
* @default
|
|
81
|
+
* isomorphic default value
|
|
82
|
+
* @default false
|
|
83
83
|
*/
|
|
84
|
-
|
|
84
|
+
defaultValue?: boolean;
|
|
85
85
|
/**
|
|
86
86
|
* Key to persist the data into localStorage/sessionStorage.
|
|
87
87
|
*
|
|
@@ -94,8 +94,16 @@ interface UseDarkOptions<T> {
|
|
|
94
94
|
* @default localStorage
|
|
95
95
|
*/
|
|
96
96
|
storage?: () => Storage;
|
|
97
|
+
/**
|
|
98
|
+
* name dark apply to element
|
|
99
|
+
*/
|
|
100
|
+
classNameDark: string;
|
|
101
|
+
/**
|
|
102
|
+
* name light apply to element
|
|
103
|
+
*/
|
|
104
|
+
classNameLight: string;
|
|
97
105
|
}
|
|
98
|
-
declare function useDarkMode
|
|
106
|
+
declare function useDarkMode(options: UseDarkOptions): readonly [boolean | null, () => void, react.Dispatch<react.SetStateAction<boolean | null>>];
|
|
99
107
|
|
|
100
108
|
declare function useMediaQuery(query: string, defaultState?: boolean): boolean;
|
|
101
109
|
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React, { useRef, useEffect, useLayoutEffect, useCallback, useMemo, useState, useReducer } from 'react';
|
|
2
|
-
import { throttle, debounce
|
|
2
|
+
import { isEqual, throttle, debounce } from 'lodash';
|
|
3
3
|
|
|
4
4
|
function usePrevious(state) {
|
|
5
5
|
const ref = useRef();
|
|
@@ -73,6 +73,49 @@ function useEvent(fn) {
|
|
|
73
73
|
}, []);
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
+
const isPrimitive$1 = (val) => val !== Object(val);
|
|
77
|
+
function useCustomCompareEffect(effect, deps, depsEqual) {
|
|
78
|
+
if (process.env.NODE_ENV !== "production") {
|
|
79
|
+
if (!(deps instanceof Array) || !deps.length) {
|
|
80
|
+
console.warn(
|
|
81
|
+
"`useCustomCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
if (deps.every(isPrimitive$1)) {
|
|
85
|
+
console.warn(
|
|
86
|
+
"`useCustomCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
if (typeof depsEqual !== "function") {
|
|
90
|
+
console.warn(
|
|
91
|
+
"`useCustomCompareEffect` should be used with depsEqual callback for comparing deps list"
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const ref = useRef(void 0);
|
|
96
|
+
if (!ref.current || !depsEqual(deps, ref.current)) {
|
|
97
|
+
ref.current = deps;
|
|
98
|
+
}
|
|
99
|
+
useEffect(effect, ref.current);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const isPrimitive = (val) => val !== Object(val);
|
|
103
|
+
function useDeepCompareEffect(effect, deps) {
|
|
104
|
+
if (process.env.NODE_ENV !== "production") {
|
|
105
|
+
if (!(deps instanceof Array) || !deps.length) {
|
|
106
|
+
console.warn(
|
|
107
|
+
"`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
if (deps.every(isPrimitive)) {
|
|
111
|
+
console.warn(
|
|
112
|
+
"`useDeepCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
useCustomCompareEffect(effect, deps, isEqual);
|
|
117
|
+
}
|
|
118
|
+
|
|
76
119
|
const StorageSerializers = {
|
|
77
120
|
boolean: {
|
|
78
121
|
read: (v) => v === "true",
|
|
@@ -112,37 +155,35 @@ function useStorage(key, defaults, getStorage, options = {}) {
|
|
|
112
155
|
console.error(e);
|
|
113
156
|
}, []);
|
|
114
157
|
let storage;
|
|
115
|
-
const { onError = defaultOnError,
|
|
158
|
+
const { onError = defaultOnError, csrData } = options;
|
|
116
159
|
try {
|
|
117
160
|
storage = getStorage();
|
|
118
161
|
} catch (err) {
|
|
119
162
|
onError(err);
|
|
120
163
|
}
|
|
121
164
|
const type = guessSerializerType(defaults);
|
|
122
|
-
const serializer = useMemo(
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
},
|
|
127
|
-
[options.serializer, type]
|
|
128
|
-
);
|
|
165
|
+
const serializer = useMemo(() => {
|
|
166
|
+
var _a;
|
|
167
|
+
return (_a = options.serializer) != null ? _a : StorageSerializers[type];
|
|
168
|
+
}, [options.serializer, type]);
|
|
129
169
|
const [state, setState] = useState(defaults);
|
|
130
|
-
|
|
170
|
+
useDeepCompareEffect(() => {
|
|
171
|
+
const data = csrData ? isFunction(csrData) ? csrData() : csrData : defaults;
|
|
131
172
|
const getStoredValue = () => {
|
|
132
173
|
try {
|
|
133
174
|
const raw = storage == null ? void 0 : storage.getItem(key);
|
|
134
|
-
if (raw !== void 0 && raw !== null
|
|
175
|
+
if (raw !== void 0 && raw !== null) {
|
|
135
176
|
return serializer.read(raw);
|
|
136
177
|
} else {
|
|
137
|
-
storage == null ? void 0 : storage.setItem(key, serializer.write(
|
|
138
|
-
return
|
|
178
|
+
storage == null ? void 0 : storage.setItem(key, serializer.write(data));
|
|
179
|
+
return data;
|
|
139
180
|
}
|
|
140
181
|
} catch (e) {
|
|
141
182
|
onError(e);
|
|
142
183
|
}
|
|
143
184
|
};
|
|
144
185
|
setState(getStoredValue());
|
|
145
|
-
}, [key,
|
|
186
|
+
}, [key, defaults, serializer, storage, onError]);
|
|
146
187
|
const updateState = useEvent(
|
|
147
188
|
(valOrFunc) => {
|
|
148
189
|
const currentState = isFunction(valOrFunc) ? valOrFunc(state) : valOrFunc;
|
|
@@ -199,6 +240,55 @@ function useInterval(callback, delay, options) {
|
|
|
199
240
|
}, [delay, immediate]);
|
|
200
241
|
}
|
|
201
242
|
|
|
243
|
+
function useDarkMode(options) {
|
|
244
|
+
const {
|
|
245
|
+
selector = "html",
|
|
246
|
+
attribute = "class",
|
|
247
|
+
classNameDark = "",
|
|
248
|
+
classNameLight = "",
|
|
249
|
+
storageKey = "reactuses-color-scheme",
|
|
250
|
+
storage = () => isBrowser ? localStorage : void 0,
|
|
251
|
+
defaultValue = false
|
|
252
|
+
} = options;
|
|
253
|
+
const value = () => {
|
|
254
|
+
return window.matchMedia("(prefers-color-scheme: dark)").matches;
|
|
255
|
+
};
|
|
256
|
+
const [dark, setDark] = useStorage(
|
|
257
|
+
storageKey,
|
|
258
|
+
defaultValue,
|
|
259
|
+
storage,
|
|
260
|
+
{
|
|
261
|
+
csrData: value
|
|
262
|
+
}
|
|
263
|
+
);
|
|
264
|
+
useEffect(() => {
|
|
265
|
+
const element = window == null ? void 0 : window.document.querySelector(selector);
|
|
266
|
+
if (!element) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
if (attribute === "class") {
|
|
270
|
+
dark && classNameDark && element.classList.add(classNameDark);
|
|
271
|
+
!dark && classNameLight && element.classList.add(classNameLight);
|
|
272
|
+
} else {
|
|
273
|
+
dark && classNameDark && element.setAttribute(attribute, classNameDark);
|
|
274
|
+
!dark && classNameLight && element.setAttribute(attribute, classNameLight);
|
|
275
|
+
}
|
|
276
|
+
return () => {
|
|
277
|
+
if (!element) {
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (attribute === "class") {
|
|
281
|
+
dark && classNameDark && element.classList.remove(classNameDark);
|
|
282
|
+
!dark && classNameLight && element.classList.remove(classNameLight);
|
|
283
|
+
} else {
|
|
284
|
+
dark && classNameDark && element.removeAttribute(attribute);
|
|
285
|
+
!dark && classNameLight && element.removeAttribute(attribute);
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
}, [attribute, classNameDark, classNameLight, dark, selector]);
|
|
289
|
+
return [dark, () => setDark((dark2) => !dark2), setDark];
|
|
290
|
+
}
|
|
291
|
+
|
|
202
292
|
const getInitialState = (query, defaultState) => {
|
|
203
293
|
if (defaultState !== void 0) {
|
|
204
294
|
return defaultState;
|
|
@@ -246,39 +336,6 @@ function usePreferredDark(defaultState) {
|
|
|
246
336
|
return useMediaQuery("(prefers-color-scheme: dark)", defaultState);
|
|
247
337
|
}
|
|
248
338
|
|
|
249
|
-
function useDarkMode(options = {}) {
|
|
250
|
-
const {
|
|
251
|
-
selector = "html",
|
|
252
|
-
attribute = "class",
|
|
253
|
-
initialValue,
|
|
254
|
-
storageKey = "reactuses-color-scheme",
|
|
255
|
-
storage = () => isBrowser ? localStorage : void 0
|
|
256
|
-
} = options;
|
|
257
|
-
const prefersDarkMode = usePreferredDark(false);
|
|
258
|
-
const value = initialValue ? initialValue : prefersDarkMode ? "dark" : "light";
|
|
259
|
-
const [dark, setDark] = useStorage(storageKey, value, storage, {
|
|
260
|
-
ignoreDefaults: false
|
|
261
|
-
});
|
|
262
|
-
const wrappedSetDark = useCallback(
|
|
263
|
-
(latestDark) => {
|
|
264
|
-
const element = window == null ? void 0 : window.document.querySelector(selector);
|
|
265
|
-
if (!element) {
|
|
266
|
-
return;
|
|
267
|
-
}
|
|
268
|
-
if (attribute === "class") {
|
|
269
|
-
latestDark && element.classList.add(latestDark);
|
|
270
|
-
dark && element.classList.remove(dark);
|
|
271
|
-
} else {
|
|
272
|
-
latestDark && element.setAttribute(attribute, latestDark);
|
|
273
|
-
dark && element.removeAttribute(attribute);
|
|
274
|
-
}
|
|
275
|
-
setDark(latestDark);
|
|
276
|
-
},
|
|
277
|
-
[attribute, dark, selector, setDark]
|
|
278
|
-
);
|
|
279
|
-
return [dark, wrappedSetDark];
|
|
280
|
-
}
|
|
281
|
-
|
|
282
339
|
function useMount(fn) {
|
|
283
340
|
if (isDev) {
|
|
284
341
|
if (!isFunction(fn)) {
|
|
@@ -502,53 +559,10 @@ function useLatestElement(target, defaultElement) {
|
|
|
502
559
|
const ref = useRef(getTargetElement(target, defaultElement));
|
|
503
560
|
useEffect(() => {
|
|
504
561
|
ref.current = getTargetElement(target, defaultElement);
|
|
505
|
-
}
|
|
562
|
+
});
|
|
506
563
|
return ref;
|
|
507
564
|
}
|
|
508
565
|
|
|
509
|
-
const isPrimitive$1 = (val) => val !== Object(val);
|
|
510
|
-
function useCustomCompareEffect(effect, deps, depsEqual) {
|
|
511
|
-
if (process.env.NODE_ENV !== "production") {
|
|
512
|
-
if (!(deps instanceof Array) || !deps.length) {
|
|
513
|
-
console.warn(
|
|
514
|
-
"`useCustomCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
|
|
515
|
-
);
|
|
516
|
-
}
|
|
517
|
-
if (deps.every(isPrimitive$1)) {
|
|
518
|
-
console.warn(
|
|
519
|
-
"`useCustomCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
|
|
520
|
-
);
|
|
521
|
-
}
|
|
522
|
-
if (typeof depsEqual !== "function") {
|
|
523
|
-
console.warn(
|
|
524
|
-
"`useCustomCompareEffect` should be used with depsEqual callback for comparing deps list"
|
|
525
|
-
);
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
const ref = useRef(void 0);
|
|
529
|
-
if (!ref.current || !depsEqual(deps, ref.current)) {
|
|
530
|
-
ref.current = deps;
|
|
531
|
-
}
|
|
532
|
-
useEffect(effect, ref.current);
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
const isPrimitive = (val) => val !== Object(val);
|
|
536
|
-
function useDeepCompareEffect(effect, deps) {
|
|
537
|
-
if (process.env.NODE_ENV !== "production") {
|
|
538
|
-
if (!(deps instanceof Array) || !deps.length) {
|
|
539
|
-
console.warn(
|
|
540
|
-
"`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
|
|
541
|
-
);
|
|
542
|
-
}
|
|
543
|
-
if (deps.every(isPrimitive)) {
|
|
544
|
-
console.warn(
|
|
545
|
-
"`useDeepCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
|
|
546
|
-
);
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
useCustomCompareEffect(effect, deps, isEqual);
|
|
550
|
-
}
|
|
551
|
-
|
|
552
566
|
function useEventListener(eventName, handler, element, options) {
|
|
553
567
|
const savedHandler = useLatest(handler);
|
|
554
568
|
const targetElementRef = useLatestElement(element, defaultWindow);
|
|
@@ -2390,21 +2404,21 @@ function useClipBorad() {
|
|
|
2390
2404
|
|
|
2391
2405
|
function useClickOutSide(target, handler) {
|
|
2392
2406
|
const savedHandler = useLatest(handler);
|
|
2407
|
+
const element = useLatestElement(target);
|
|
2393
2408
|
const listener = (event) => {
|
|
2394
|
-
|
|
2395
|
-
if (!element) {
|
|
2409
|
+
if (!element.current) {
|
|
2396
2410
|
return;
|
|
2397
2411
|
}
|
|
2398
2412
|
const elements = event.composedPath();
|
|
2399
|
-
if (element === event.target || elements.includes(element)) {
|
|
2413
|
+
if (element.current === event.target || elements.includes(element.current)) {
|
|
2400
2414
|
return;
|
|
2401
2415
|
}
|
|
2402
2416
|
savedHandler.current(event);
|
|
2403
2417
|
};
|
|
2404
|
-
useEventListener("mousedown", listener,
|
|
2418
|
+
useEventListener("mousedown", listener, defaultWindow, {
|
|
2405
2419
|
passive: true
|
|
2406
2420
|
});
|
|
2407
|
-
useEventListener("touchstart", listener,
|
|
2421
|
+
useEventListener("touchstart", listener, defaultWindow, {
|
|
2408
2422
|
passive: true
|
|
2409
2423
|
});
|
|
2410
2424
|
}
|