@reactuses/core 2.2.0 → 2.2.2

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 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",
@@ -116,38 +159,38 @@ const StorageSerializers = {
116
159
  }
117
160
  };
118
161
  function useStorage(key, defaults, getStorage, options = {}) {
119
- var _a;
162
+ const defaultOnError = React.useCallback((e) => {
163
+ console.error(e);
164
+ }, []);
120
165
  let storage;
121
- const {
122
- onError = (e) => {
123
- console.error(e);
124
- }
125
- } = options;
126
- const data = defaults;
166
+ const { onError = defaultOnError, ignoreDefaults = true } = options;
127
167
  try {
128
168
  storage = getStorage();
129
169
  } catch (err) {
130
- console.error(err);
170
+ onError(err);
131
171
  }
132
172
  const type = guessSerializerType(defaults);
133
- const serializer = (_a = options.serializer) != null ? _a : StorageSerializers[type];
134
- const getStoredValue = useEvent(() => {
135
- try {
136
- const raw = storage == null ? void 0 : storage.getItem(key);
137
- if (raw !== void 0 && raw !== null) {
138
- return serializer.read(raw);
139
- } else {
140
- storage == null ? void 0 : storage.setItem(key, serializer.write(data));
141
- return data;
142
- }
143
- } catch (e) {
144
- onError(e);
145
- }
146
- });
173
+ const serializer = React.useMemo(() => {
174
+ var _a;
175
+ return (_a = options.serializer) != null ? _a : StorageSerializers[type];
176
+ }, [options.serializer, type]);
147
177
  const [state, setState] = React.useState(defaults);
148
- React.useEffect(() => {
178
+ useDeepCompareEffect(() => {
179
+ const getStoredValue = () => {
180
+ try {
181
+ const raw = storage == null ? void 0 : storage.getItem(key);
182
+ if (raw !== void 0 && raw !== null && ignoreDefaults) {
183
+ return serializer.read(raw);
184
+ } else {
185
+ storage == null ? void 0 : storage.setItem(key, serializer.write(defaults));
186
+ return defaults;
187
+ }
188
+ } catch (e) {
189
+ onError(e);
190
+ }
191
+ };
149
192
  setState(getStoredValue());
150
- }, [getStoredValue, key]);
193
+ }, [key, ignoreDefaults, defaults, serializer, storage, onError]);
151
194
  const updateState = useEvent(
152
195
  (valOrFunc) => {
153
196
  const currentState = isFunction(valOrFunc) ? valOrFunc(state) : valOrFunc;
@@ -261,7 +304,9 @@ function useDarkMode(options = {}) {
261
304
  } = options;
262
305
  const prefersDarkMode = usePreferredDark(false);
263
306
  const value = initialValue ? initialValue : prefersDarkMode ? "dark" : "light";
264
- const [dark, setDark] = useStorage(storageKey, value, storage);
307
+ const [dark, setDark] = useStorage(storageKey, value, storage, {
308
+ ignoreDefaults: false
309
+ });
265
310
  const wrappedSetDark = React.useCallback(
266
311
  (latestDark) => {
267
312
  const element = window == null ? void 0 : window.document.querySelector(selector);
@@ -509,49 +554,6 @@ function useLatestElement(target, defaultElement) {
509
554
  return ref;
510
555
  }
511
556
 
512
- const isPrimitive$1 = (val) => val !== Object(val);
513
- function useCustomCompareEffect(effect, deps, depsEqual) {
514
- if (process.env.NODE_ENV !== "production") {
515
- if (!(deps instanceof Array) || !deps.length) {
516
- console.warn(
517
- "`useCustomCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
518
- );
519
- }
520
- if (deps.every(isPrimitive$1)) {
521
- console.warn(
522
- "`useCustomCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
523
- );
524
- }
525
- if (typeof depsEqual !== "function") {
526
- console.warn(
527
- "`useCustomCompareEffect` should be used with depsEqual callback for comparing deps list"
528
- );
529
- }
530
- }
531
- const ref = React.useRef(void 0);
532
- if (!ref.current || !depsEqual(deps, ref.current)) {
533
- ref.current = deps;
534
- }
535
- React.useEffect(effect, ref.current);
536
- }
537
-
538
- const isPrimitive = (val) => val !== Object(val);
539
- function useDeepCompareEffect(effect, deps) {
540
- if (process.env.NODE_ENV !== "production") {
541
- if (!(deps instanceof Array) || !deps.length) {
542
- console.warn(
543
- "`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
544
- );
545
- }
546
- if (deps.every(isPrimitive)) {
547
- console.warn(
548
- "`useDeepCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
549
- );
550
- }
551
- }
552
- useCustomCompareEffect(effect, deps, lodash.isEqual);
553
- }
554
-
555
557
  function useEventListener(eventName, handler, element, options) {
556
558
  const savedHandler = useLatest(handler);
557
559
  const targetElementRef = useLatestElement(element, defaultWindow);
package/dist/index.d.ts CHANGED
@@ -27,6 +27,10 @@ interface UseStorageOptions<T> {
27
27
  * Default log error to `console.error`
28
28
  */
29
29
  onError?: (error: unknown) => void;
30
+ /**
31
+ * ignore default value when storage has value
32
+ */
33
+ ignoreDefaults?: boolean;
30
34
  }
31
35
 
32
36
  declare function useLocalStorage(key: string, defaults: string, options?: UseStorageOptions<string>): readonly [string | null, Dispatch<SetStateAction<string | null>>];
@@ -74,8 +78,7 @@ interface UseDarkOptions<T> {
74
78
  */
75
79
  attribute?: string;
76
80
  /**
77
- * The initial value write the target element, defaultValue follow system prefer color
78
- * must be set in SSR
81
+ * The initial classname value write the target element, Otherwise, it will follow the system by default
79
82
  * @default 'light | dark'
80
83
  */
81
84
  initialValue?: T;
@@ -92,7 +95,7 @@ interface UseDarkOptions<T> {
92
95
  */
93
96
  storage?: () => Storage;
94
97
  }
95
- declare function useDarkMode<T extends string | "light" | "dark">(options?: UseDarkOptions<T>): readonly [T | null, (latestDark: T) => void];
98
+ declare function useDarkMode<T extends string>(options?: UseDarkOptions<T>): readonly [T | null, (latestDark: T) => void];
96
99
 
97
100
  declare function useMediaQuery(query: string, defaultState?: boolean): boolean;
98
101
 
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import React, { useRef, useEffect, useLayoutEffect, useCallback, useState, useReducer, useMemo } from 'react';
2
- import { throttle, debounce, isEqual } from 'lodash';
1
+ import React, { useRef, useEffect, useLayoutEffect, useCallback, useMemo, useState, useReducer } from 'react';
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",
@@ -108,38 +151,38 @@ const StorageSerializers = {
108
151
  }
109
152
  };
110
153
  function useStorage(key, defaults, getStorage, options = {}) {
111
- var _a;
154
+ const defaultOnError = useCallback((e) => {
155
+ console.error(e);
156
+ }, []);
112
157
  let storage;
113
- const {
114
- onError = (e) => {
115
- console.error(e);
116
- }
117
- } = options;
118
- const data = defaults;
158
+ const { onError = defaultOnError, ignoreDefaults = true } = options;
119
159
  try {
120
160
  storage = getStorage();
121
161
  } catch (err) {
122
- console.error(err);
162
+ onError(err);
123
163
  }
124
164
  const type = guessSerializerType(defaults);
125
- const serializer = (_a = options.serializer) != null ? _a : StorageSerializers[type];
126
- const getStoredValue = useEvent(() => {
127
- try {
128
- const raw = storage == null ? void 0 : storage.getItem(key);
129
- if (raw !== void 0 && raw !== null) {
130
- return serializer.read(raw);
131
- } else {
132
- storage == null ? void 0 : storage.setItem(key, serializer.write(data));
133
- return data;
134
- }
135
- } catch (e) {
136
- onError(e);
137
- }
138
- });
165
+ const serializer = useMemo(() => {
166
+ var _a;
167
+ return (_a = options.serializer) != null ? _a : StorageSerializers[type];
168
+ }, [options.serializer, type]);
139
169
  const [state, setState] = useState(defaults);
140
- useEffect(() => {
170
+ useDeepCompareEffect(() => {
171
+ const getStoredValue = () => {
172
+ try {
173
+ const raw = storage == null ? void 0 : storage.getItem(key);
174
+ if (raw !== void 0 && raw !== null && ignoreDefaults) {
175
+ return serializer.read(raw);
176
+ } else {
177
+ storage == null ? void 0 : storage.setItem(key, serializer.write(defaults));
178
+ return defaults;
179
+ }
180
+ } catch (e) {
181
+ onError(e);
182
+ }
183
+ };
141
184
  setState(getStoredValue());
142
- }, [getStoredValue, key]);
185
+ }, [key, ignoreDefaults, defaults, serializer, storage, onError]);
143
186
  const updateState = useEvent(
144
187
  (valOrFunc) => {
145
188
  const currentState = isFunction(valOrFunc) ? valOrFunc(state) : valOrFunc;
@@ -253,7 +296,9 @@ function useDarkMode(options = {}) {
253
296
  } = options;
254
297
  const prefersDarkMode = usePreferredDark(false);
255
298
  const value = initialValue ? initialValue : prefersDarkMode ? "dark" : "light";
256
- const [dark, setDark] = useStorage(storageKey, value, storage);
299
+ const [dark, setDark] = useStorage(storageKey, value, storage, {
300
+ ignoreDefaults: false
301
+ });
257
302
  const wrappedSetDark = useCallback(
258
303
  (latestDark) => {
259
304
  const element = window == null ? void 0 : window.document.querySelector(selector);
@@ -501,49 +546,6 @@ function useLatestElement(target, defaultElement) {
501
546
  return ref;
502
547
  }
503
548
 
504
- const isPrimitive$1 = (val) => val !== Object(val);
505
- function useCustomCompareEffect(effect, deps, depsEqual) {
506
- if (process.env.NODE_ENV !== "production") {
507
- if (!(deps instanceof Array) || !deps.length) {
508
- console.warn(
509
- "`useCustomCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
510
- );
511
- }
512
- if (deps.every(isPrimitive$1)) {
513
- console.warn(
514
- "`useCustomCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
515
- );
516
- }
517
- if (typeof depsEqual !== "function") {
518
- console.warn(
519
- "`useCustomCompareEffect` should be used with depsEqual callback for comparing deps list"
520
- );
521
- }
522
- }
523
- const ref = useRef(void 0);
524
- if (!ref.current || !depsEqual(deps, ref.current)) {
525
- ref.current = deps;
526
- }
527
- useEffect(effect, ref.current);
528
- }
529
-
530
- const isPrimitive = (val) => val !== Object(val);
531
- function useDeepCompareEffect(effect, deps) {
532
- if (process.env.NODE_ENV !== "production") {
533
- if (!(deps instanceof Array) || !deps.length) {
534
- console.warn(
535
- "`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
536
- );
537
- }
538
- if (deps.every(isPrimitive)) {
539
- console.warn(
540
- "`useDeepCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
541
- );
542
- }
543
- }
544
- useCustomCompareEffect(effect, deps, isEqual);
545
- }
546
-
547
549
  function useEventListener(eventName, handler, element, options) {
548
550
  const savedHandler = useLatest(handler);
549
551
  const targetElementRef = useLatestElement(element, defaultWindow);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reactuses/core",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "main": "./dist/index.cjs",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",