@mui/utils 5.15.11 → 5.15.13
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/CHANGELOG.md +290 -116
- package/index.js +1 -1
- package/legacy/index.js +1 -1
- package/legacy/useTimeout/useTimeout.js +4 -4
- package/legacy/visuallyHidden/visuallyHidden.js +1 -1
- package/modern/index.js +1 -1
- package/modern/useTimeout/useTimeout.js +4 -4
- package/modern/visuallyHidden/visuallyHidden.js +1 -1
- package/node/index.js +1 -1
- package/node/useTimeout/useTimeout.js +4 -4
- package/node/visuallyHidden/visuallyHidden.js +1 -1
- package/package.json +1 -1
- package/useTimeout/useTimeout.d.ts +1 -1
- package/useTimeout/useTimeout.js +4 -4
- package/visuallyHidden/visuallyHidden.js +1 -1
- package/legacy/useLocalStorageState/index.js +0 -1
- package/legacy/useLocalStorageState/useLocalStorageState.js +0 -112
- package/modern/useLocalStorageState/index.js +0 -1
- package/modern/useLocalStorageState/useLocalStorageState.js +0 -97
- package/node/useLocalStorageState/index.js +0 -13
- package/node/useLocalStorageState/useLocalStorageState.js +0 -107
- package/useLocalStorageState/index.d.ts +0 -1
- package/useLocalStorageState/index.js +0 -1
- package/useLocalStorageState/package.json +0 -6
- package/useLocalStorageState/useLocalStorageState.d.ts +0 -7
- package/useLocalStorageState/useLocalStorageState.js +0 -100
package/index.js
CHANGED
package/legacy/index.js
CHANGED
|
@@ -8,11 +8,11 @@ export var Timeout = /*#__PURE__*/function () {
|
|
|
8
8
|
function Timeout() {
|
|
9
9
|
var _this = this;
|
|
10
10
|
_classCallCheck(this, Timeout);
|
|
11
|
-
this.currentId =
|
|
11
|
+
this.currentId = null;
|
|
12
12
|
this.clear = function () {
|
|
13
|
-
if (_this.currentId !==
|
|
13
|
+
if (_this.currentId !== null) {
|
|
14
14
|
clearTimeout(_this.currentId);
|
|
15
|
-
_this.currentId =
|
|
15
|
+
_this.currentId = null;
|
|
16
16
|
}
|
|
17
17
|
};
|
|
18
18
|
this.disposeEffect = function () {
|
|
@@ -29,7 +29,7 @@ export var Timeout = /*#__PURE__*/function () {
|
|
|
29
29
|
var _this2 = this;
|
|
30
30
|
this.clear();
|
|
31
31
|
this.currentId = setTimeout(function () {
|
|
32
|
-
_this2.currentId =
|
|
32
|
+
_this2.currentId = null;
|
|
33
33
|
fn();
|
|
34
34
|
}, delay);
|
|
35
35
|
}
|
package/modern/index.js
CHANGED
|
@@ -4,11 +4,11 @@ import useLazyRef from '../useLazyRef/useLazyRef';
|
|
|
4
4
|
import useOnMount from '../useOnMount/useOnMount';
|
|
5
5
|
export class Timeout {
|
|
6
6
|
constructor() {
|
|
7
|
-
this.currentId =
|
|
7
|
+
this.currentId = null;
|
|
8
8
|
this.clear = () => {
|
|
9
|
-
if (this.currentId !==
|
|
9
|
+
if (this.currentId !== null) {
|
|
10
10
|
clearTimeout(this.currentId);
|
|
11
|
-
this.currentId =
|
|
11
|
+
this.currentId = null;
|
|
12
12
|
}
|
|
13
13
|
};
|
|
14
14
|
this.disposeEffect = () => {
|
|
@@ -24,7 +24,7 @@ export class Timeout {
|
|
|
24
24
|
start(delay, fn) {
|
|
25
25
|
this.clear();
|
|
26
26
|
this.currentId = setTimeout(() => {
|
|
27
|
-
this.currentId =
|
|
27
|
+
this.currentId = null;
|
|
28
28
|
fn();
|
|
29
29
|
}, delay);
|
|
30
30
|
}
|
package/node/index.js
CHANGED
|
@@ -11,11 +11,11 @@ var _useLazyRef = _interopRequireDefault(require("../useLazyRef/useLazyRef"));
|
|
|
11
11
|
var _useOnMount = _interopRequireDefault(require("../useOnMount/useOnMount"));
|
|
12
12
|
class Timeout {
|
|
13
13
|
constructor() {
|
|
14
|
-
this.currentId =
|
|
14
|
+
this.currentId = null;
|
|
15
15
|
this.clear = () => {
|
|
16
|
-
if (this.currentId !==
|
|
16
|
+
if (this.currentId !== null) {
|
|
17
17
|
clearTimeout(this.currentId);
|
|
18
|
-
this.currentId =
|
|
18
|
+
this.currentId = null;
|
|
19
19
|
}
|
|
20
20
|
};
|
|
21
21
|
this.disposeEffect = () => {
|
|
@@ -31,7 +31,7 @@ class Timeout {
|
|
|
31
31
|
start(delay, fn) {
|
|
32
32
|
this.clear();
|
|
33
33
|
this.currentId = setTimeout(() => {
|
|
34
|
-
this.currentId =
|
|
34
|
+
this.currentId = null;
|
|
35
35
|
fn();
|
|
36
36
|
}, delay);
|
|
37
37
|
}
|
package/package.json
CHANGED
package/useTimeout/useTimeout.js
CHANGED
|
@@ -4,11 +4,11 @@ import useLazyRef from '../useLazyRef/useLazyRef';
|
|
|
4
4
|
import useOnMount from '../useOnMount/useOnMount';
|
|
5
5
|
export class Timeout {
|
|
6
6
|
constructor() {
|
|
7
|
-
this.currentId =
|
|
7
|
+
this.currentId = null;
|
|
8
8
|
this.clear = () => {
|
|
9
|
-
if (this.currentId !==
|
|
9
|
+
if (this.currentId !== null) {
|
|
10
10
|
clearTimeout(this.currentId);
|
|
11
|
-
this.currentId =
|
|
11
|
+
this.currentId = null;
|
|
12
12
|
}
|
|
13
13
|
};
|
|
14
14
|
this.disposeEffect = () => {
|
|
@@ -24,7 +24,7 @@ export class Timeout {
|
|
|
24
24
|
start(delay, fn) {
|
|
25
25
|
this.clear();
|
|
26
26
|
this.currentId = setTimeout(() => {
|
|
27
|
-
this.currentId =
|
|
27
|
+
this.currentId = null;
|
|
28
28
|
fn();
|
|
29
29
|
}, delay);
|
|
30
30
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { default } from './useLocalStorageState';
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import * as React from 'react';
|
|
4
|
-
var NOOP = function NOOP() {};
|
|
5
|
-
|
|
6
|
-
// storage events only work across tabs, we'll use an event emitter to announce within the current tab
|
|
7
|
-
var currentTabChangeListeners = new Map();
|
|
8
|
-
function onCurrentTabStorageChange(key, handler) {
|
|
9
|
-
var listeners = currentTabChangeListeners.get(key);
|
|
10
|
-
if (!listeners) {
|
|
11
|
-
listeners = new Set();
|
|
12
|
-
currentTabChangeListeners.set(key, listeners);
|
|
13
|
-
}
|
|
14
|
-
listeners.add(handler);
|
|
15
|
-
}
|
|
16
|
-
function offCurrentTabStorageChange(key, handler) {
|
|
17
|
-
var listeners = currentTabChangeListeners.get(key);
|
|
18
|
-
if (!listeners) {
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
listeners.delete(handler);
|
|
22
|
-
if (listeners.size === 0) {
|
|
23
|
-
currentTabChangeListeners.delete(key);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
function emitCurrentTabStorageChange(key) {
|
|
27
|
-
var listeners = currentTabChangeListeners.get(key);
|
|
28
|
-
if (listeners) {
|
|
29
|
-
listeners.forEach(function (listener) {
|
|
30
|
-
return listener();
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
function subscribe(area, key, cb) {
|
|
35
|
-
var storageHandler = function storageHandler(event) {
|
|
36
|
-
if (event.storageArea === area && event.key === key) {
|
|
37
|
-
cb();
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
window.addEventListener('storage', storageHandler);
|
|
41
|
-
onCurrentTabStorageChange(key, cb);
|
|
42
|
-
return function () {
|
|
43
|
-
window.removeEventListener('storage', storageHandler);
|
|
44
|
-
offCurrentTabStorageChange(key, cb);
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
function getSnapshot(area, key) {
|
|
48
|
-
return area.getItem(key);
|
|
49
|
-
}
|
|
50
|
-
function setValue(area, key, value) {
|
|
51
|
-
if (typeof window !== 'undefined') {
|
|
52
|
-
if (value === null) {
|
|
53
|
-
area.removeItem(key);
|
|
54
|
-
} else {
|
|
55
|
-
area.setItem(key, String(value));
|
|
56
|
-
}
|
|
57
|
-
emitCurrentTabStorageChange(key);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
function useLocalStorageStateServer(key) {
|
|
61
|
-
var initializer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
62
|
-
var _React$useState = React.useState(initializer),
|
|
63
|
-
initialValue = _React$useState[0];
|
|
64
|
-
return [initialValue, function () {}];
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Sync state to local storage so that it persists through a page refresh. Usage is
|
|
69
|
-
* similar to useState except we pass in a storage key so that we can default
|
|
70
|
-
* to that value on page load instead of the specified initial value.
|
|
71
|
-
*
|
|
72
|
-
* Since the storage API isn't available in server-rendering environments, we
|
|
73
|
-
* return initialValue during SSR and hydration.
|
|
74
|
-
*
|
|
75
|
-
* Things this hook does different from existing solutions:
|
|
76
|
-
* - SSR-capable: it shows initial value during SSR and hydration, but immediately
|
|
77
|
-
* initializes when clientside mounted.
|
|
78
|
-
* - Sync state across tabs: When another tab changes the value in the storage area, the
|
|
79
|
-
* current tab follows suit.
|
|
80
|
-
*/
|
|
81
|
-
|
|
82
|
-
function useLocalStorageStateBrowser(key) {
|
|
83
|
-
var initializer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
84
|
-
var _React$useState2 = React.useState(initializer),
|
|
85
|
-
initialValue = _React$useState2[0];
|
|
86
|
-
var area = window.localStorage;
|
|
87
|
-
var subscribeKey = React.useCallback(function (cb) {
|
|
88
|
-
return key ? subscribe(area, key, cb) : NOOP;
|
|
89
|
-
}, [area, key]);
|
|
90
|
-
var getKeySnapshot = React.useCallback(function () {
|
|
91
|
-
var _ref;
|
|
92
|
-
return (_ref = key && getSnapshot(area, key)) != null ? _ref : initialValue;
|
|
93
|
-
}, [area, initialValue, key]);
|
|
94
|
-
var getKeyServerSnapshot = React.useCallback(function () {
|
|
95
|
-
return initialValue;
|
|
96
|
-
}, [initialValue]);
|
|
97
|
-
var storedValue = React.useSyncExternalStore(subscribeKey, getKeySnapshot, getKeyServerSnapshot);
|
|
98
|
-
var setStoredValue = React.useCallback(function (value) {
|
|
99
|
-
if (key) {
|
|
100
|
-
var valueToStore = value instanceof Function ? value(storedValue) : value;
|
|
101
|
-
setValue(area, key, valueToStore);
|
|
102
|
-
}
|
|
103
|
-
}, [area, key, storedValue]);
|
|
104
|
-
var _React$useState3 = React.useState(initialValue),
|
|
105
|
-
nonStoredValue = _React$useState3[0],
|
|
106
|
-
setNonStoredValue = _React$useState3[1];
|
|
107
|
-
if (!key) {
|
|
108
|
-
return [nonStoredValue, setNonStoredValue];
|
|
109
|
-
}
|
|
110
|
-
return [storedValue, setStoredValue];
|
|
111
|
-
}
|
|
112
|
-
export default typeof window === 'undefined' ? useLocalStorageStateServer : useLocalStorageStateBrowser;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { default } from './useLocalStorageState';
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import * as React from 'react';
|
|
4
|
-
const NOOP = () => {};
|
|
5
|
-
|
|
6
|
-
// storage events only work across tabs, we'll use an event emitter to announce within the current tab
|
|
7
|
-
const currentTabChangeListeners = new Map();
|
|
8
|
-
function onCurrentTabStorageChange(key, handler) {
|
|
9
|
-
let listeners = currentTabChangeListeners.get(key);
|
|
10
|
-
if (!listeners) {
|
|
11
|
-
listeners = new Set();
|
|
12
|
-
currentTabChangeListeners.set(key, listeners);
|
|
13
|
-
}
|
|
14
|
-
listeners.add(handler);
|
|
15
|
-
}
|
|
16
|
-
function offCurrentTabStorageChange(key, handler) {
|
|
17
|
-
const listeners = currentTabChangeListeners.get(key);
|
|
18
|
-
if (!listeners) {
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
listeners.delete(handler);
|
|
22
|
-
if (listeners.size === 0) {
|
|
23
|
-
currentTabChangeListeners.delete(key);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
function emitCurrentTabStorageChange(key) {
|
|
27
|
-
const listeners = currentTabChangeListeners.get(key);
|
|
28
|
-
if (listeners) {
|
|
29
|
-
listeners.forEach(listener => listener());
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
function subscribe(area, key, cb) {
|
|
33
|
-
const storageHandler = event => {
|
|
34
|
-
if (event.storageArea === area && event.key === key) {
|
|
35
|
-
cb();
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
window.addEventListener('storage', storageHandler);
|
|
39
|
-
onCurrentTabStorageChange(key, cb);
|
|
40
|
-
return () => {
|
|
41
|
-
window.removeEventListener('storage', storageHandler);
|
|
42
|
-
offCurrentTabStorageChange(key, cb);
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
function getSnapshot(area, key) {
|
|
46
|
-
return area.getItem(key);
|
|
47
|
-
}
|
|
48
|
-
function setValue(area, key, value) {
|
|
49
|
-
if (typeof window !== 'undefined') {
|
|
50
|
-
if (value === null) {
|
|
51
|
-
area.removeItem(key);
|
|
52
|
-
} else {
|
|
53
|
-
area.setItem(key, String(value));
|
|
54
|
-
}
|
|
55
|
-
emitCurrentTabStorageChange(key);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
function useLocalStorageStateServer(key, initializer = null) {
|
|
59
|
-
const [initialValue] = React.useState(initializer);
|
|
60
|
-
return [initialValue, () => {}];
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Sync state to local storage so that it persists through a page refresh. Usage is
|
|
65
|
-
* similar to useState except we pass in a storage key so that we can default
|
|
66
|
-
* to that value on page load instead of the specified initial value.
|
|
67
|
-
*
|
|
68
|
-
* Since the storage API isn't available in server-rendering environments, we
|
|
69
|
-
* return initialValue during SSR and hydration.
|
|
70
|
-
*
|
|
71
|
-
* Things this hook does different from existing solutions:
|
|
72
|
-
* - SSR-capable: it shows initial value during SSR and hydration, but immediately
|
|
73
|
-
* initializes when clientside mounted.
|
|
74
|
-
* - Sync state across tabs: When another tab changes the value in the storage area, the
|
|
75
|
-
* current tab follows suit.
|
|
76
|
-
*/
|
|
77
|
-
|
|
78
|
-
function useLocalStorageStateBrowser(key, initializer = null) {
|
|
79
|
-
const [initialValue] = React.useState(initializer);
|
|
80
|
-
const area = window.localStorage;
|
|
81
|
-
const subscribeKey = React.useCallback(cb => key ? subscribe(area, key, cb) : NOOP, [area, key]);
|
|
82
|
-
const getKeySnapshot = React.useCallback(() => (key && getSnapshot(area, key)) ?? initialValue, [area, initialValue, key]);
|
|
83
|
-
const getKeyServerSnapshot = React.useCallback(() => initialValue, [initialValue]);
|
|
84
|
-
const storedValue = React.useSyncExternalStore(subscribeKey, getKeySnapshot, getKeyServerSnapshot);
|
|
85
|
-
const setStoredValue = React.useCallback(value => {
|
|
86
|
-
if (key) {
|
|
87
|
-
const valueToStore = value instanceof Function ? value(storedValue) : value;
|
|
88
|
-
setValue(area, key, valueToStore);
|
|
89
|
-
}
|
|
90
|
-
}, [area, key, storedValue]);
|
|
91
|
-
const [nonStoredValue, setNonStoredValue] = React.useState(initialValue);
|
|
92
|
-
if (!key) {
|
|
93
|
-
return [nonStoredValue, setNonStoredValue];
|
|
94
|
-
}
|
|
95
|
-
return [storedValue, setStoredValue];
|
|
96
|
-
}
|
|
97
|
-
export default typeof window === 'undefined' ? useLocalStorageStateServer : useLocalStorageStateBrowser;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
Object.defineProperty(exports, "default", {
|
|
8
|
-
enumerable: true,
|
|
9
|
-
get: function () {
|
|
10
|
-
return _useLocalStorageState.default;
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
var _useLocalStorageState = _interopRequireDefault(require("./useLocalStorageState"));
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
'use client';
|
|
3
|
-
|
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
exports.default = void 0;
|
|
8
|
-
var React = _interopRequireWildcard(require("react"));
|
|
9
|
-
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
10
|
-
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
11
|
-
const NOOP = () => {};
|
|
12
|
-
|
|
13
|
-
// storage events only work across tabs, we'll use an event emitter to announce within the current tab
|
|
14
|
-
const currentTabChangeListeners = new Map();
|
|
15
|
-
function onCurrentTabStorageChange(key, handler) {
|
|
16
|
-
let listeners = currentTabChangeListeners.get(key);
|
|
17
|
-
if (!listeners) {
|
|
18
|
-
listeners = new Set();
|
|
19
|
-
currentTabChangeListeners.set(key, listeners);
|
|
20
|
-
}
|
|
21
|
-
listeners.add(handler);
|
|
22
|
-
}
|
|
23
|
-
function offCurrentTabStorageChange(key, handler) {
|
|
24
|
-
const listeners = currentTabChangeListeners.get(key);
|
|
25
|
-
if (!listeners) {
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
listeners.delete(handler);
|
|
29
|
-
if (listeners.size === 0) {
|
|
30
|
-
currentTabChangeListeners.delete(key);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
function emitCurrentTabStorageChange(key) {
|
|
34
|
-
const listeners = currentTabChangeListeners.get(key);
|
|
35
|
-
if (listeners) {
|
|
36
|
-
listeners.forEach(listener => listener());
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
function subscribe(area, key, cb) {
|
|
40
|
-
const storageHandler = event => {
|
|
41
|
-
if (event.storageArea === area && event.key === key) {
|
|
42
|
-
cb();
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
window.addEventListener('storage', storageHandler);
|
|
46
|
-
onCurrentTabStorageChange(key, cb);
|
|
47
|
-
return () => {
|
|
48
|
-
window.removeEventListener('storage', storageHandler);
|
|
49
|
-
offCurrentTabStorageChange(key, cb);
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
function getSnapshot(area, key) {
|
|
53
|
-
return area.getItem(key);
|
|
54
|
-
}
|
|
55
|
-
function setValue(area, key, value) {
|
|
56
|
-
if (typeof window !== 'undefined') {
|
|
57
|
-
if (value === null) {
|
|
58
|
-
area.removeItem(key);
|
|
59
|
-
} else {
|
|
60
|
-
area.setItem(key, String(value));
|
|
61
|
-
}
|
|
62
|
-
emitCurrentTabStorageChange(key);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
function useLocalStorageStateServer(key, initializer = null) {
|
|
66
|
-
const [initialValue] = React.useState(initializer);
|
|
67
|
-
return [initialValue, () => {}];
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Sync state to local storage so that it persists through a page refresh. Usage is
|
|
72
|
-
* similar to useState except we pass in a storage key so that we can default
|
|
73
|
-
* to that value on page load instead of the specified initial value.
|
|
74
|
-
*
|
|
75
|
-
* Since the storage API isn't available in server-rendering environments, we
|
|
76
|
-
* return initialValue during SSR and hydration.
|
|
77
|
-
*
|
|
78
|
-
* Things this hook does different from existing solutions:
|
|
79
|
-
* - SSR-capable: it shows initial value during SSR and hydration, but immediately
|
|
80
|
-
* initializes when clientside mounted.
|
|
81
|
-
* - Sync state across tabs: When another tab changes the value in the storage area, the
|
|
82
|
-
* current tab follows suit.
|
|
83
|
-
*/
|
|
84
|
-
|
|
85
|
-
function useLocalStorageStateBrowser(key, initializer = null) {
|
|
86
|
-
const [initialValue] = React.useState(initializer);
|
|
87
|
-
const area = window.localStorage;
|
|
88
|
-
const subscribeKey = React.useCallback(cb => key ? subscribe(area, key, cb) : NOOP, [area, key]);
|
|
89
|
-
const getKeySnapshot = React.useCallback(() => {
|
|
90
|
-
var _ref;
|
|
91
|
-
return (_ref = key && getSnapshot(area, key)) != null ? _ref : initialValue;
|
|
92
|
-
}, [area, initialValue, key]);
|
|
93
|
-
const getKeyServerSnapshot = React.useCallback(() => initialValue, [initialValue]);
|
|
94
|
-
const storedValue = React.useSyncExternalStore(subscribeKey, getKeySnapshot, getKeyServerSnapshot);
|
|
95
|
-
const setStoredValue = React.useCallback(value => {
|
|
96
|
-
if (key) {
|
|
97
|
-
const valueToStore = value instanceof Function ? value(storedValue) : value;
|
|
98
|
-
setValue(area, key, valueToStore);
|
|
99
|
-
}
|
|
100
|
-
}, [area, key, storedValue]);
|
|
101
|
-
const [nonStoredValue, setNonStoredValue] = React.useState(initialValue);
|
|
102
|
-
if (!key) {
|
|
103
|
-
return [nonStoredValue, setNonStoredValue];
|
|
104
|
-
}
|
|
105
|
-
return [storedValue, setStoredValue];
|
|
106
|
-
}
|
|
107
|
-
var _default = exports.default = typeof window === 'undefined' ? useLocalStorageStateServer : useLocalStorageStateBrowser;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { default } from './useLocalStorageState';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { default } from './useLocalStorageState';
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
type Initializer<T> = () => T;
|
|
3
|
-
type UseStorageStateHookResult<T> = [T, React.Dispatch<React.SetStateAction<T>>];
|
|
4
|
-
declare function useLocalStorageStateServer(key: string | null, initializer: string | Initializer<string>): UseStorageStateHookResult<string>;
|
|
5
|
-
declare function useLocalStorageStateServer(key: string | null, initializer?: string | null | Initializer<string | null>): UseStorageStateHookResult<string | null>;
|
|
6
|
-
declare const _default: typeof useLocalStorageStateServer;
|
|
7
|
-
export default _default;
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import * as React from 'react';
|
|
4
|
-
const NOOP = () => {};
|
|
5
|
-
|
|
6
|
-
// storage events only work across tabs, we'll use an event emitter to announce within the current tab
|
|
7
|
-
const currentTabChangeListeners = new Map();
|
|
8
|
-
function onCurrentTabStorageChange(key, handler) {
|
|
9
|
-
let listeners = currentTabChangeListeners.get(key);
|
|
10
|
-
if (!listeners) {
|
|
11
|
-
listeners = new Set();
|
|
12
|
-
currentTabChangeListeners.set(key, listeners);
|
|
13
|
-
}
|
|
14
|
-
listeners.add(handler);
|
|
15
|
-
}
|
|
16
|
-
function offCurrentTabStorageChange(key, handler) {
|
|
17
|
-
const listeners = currentTabChangeListeners.get(key);
|
|
18
|
-
if (!listeners) {
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
listeners.delete(handler);
|
|
22
|
-
if (listeners.size === 0) {
|
|
23
|
-
currentTabChangeListeners.delete(key);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
function emitCurrentTabStorageChange(key) {
|
|
27
|
-
const listeners = currentTabChangeListeners.get(key);
|
|
28
|
-
if (listeners) {
|
|
29
|
-
listeners.forEach(listener => listener());
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
function subscribe(area, key, cb) {
|
|
33
|
-
const storageHandler = event => {
|
|
34
|
-
if (event.storageArea === area && event.key === key) {
|
|
35
|
-
cb();
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
window.addEventListener('storage', storageHandler);
|
|
39
|
-
onCurrentTabStorageChange(key, cb);
|
|
40
|
-
return () => {
|
|
41
|
-
window.removeEventListener('storage', storageHandler);
|
|
42
|
-
offCurrentTabStorageChange(key, cb);
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
function getSnapshot(area, key) {
|
|
46
|
-
return area.getItem(key);
|
|
47
|
-
}
|
|
48
|
-
function setValue(area, key, value) {
|
|
49
|
-
if (typeof window !== 'undefined') {
|
|
50
|
-
if (value === null) {
|
|
51
|
-
area.removeItem(key);
|
|
52
|
-
} else {
|
|
53
|
-
area.setItem(key, String(value));
|
|
54
|
-
}
|
|
55
|
-
emitCurrentTabStorageChange(key);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
function useLocalStorageStateServer(key, initializer = null) {
|
|
59
|
-
const [initialValue] = React.useState(initializer);
|
|
60
|
-
return [initialValue, () => {}];
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Sync state to local storage so that it persists through a page refresh. Usage is
|
|
65
|
-
* similar to useState except we pass in a storage key so that we can default
|
|
66
|
-
* to that value on page load instead of the specified initial value.
|
|
67
|
-
*
|
|
68
|
-
* Since the storage API isn't available in server-rendering environments, we
|
|
69
|
-
* return initialValue during SSR and hydration.
|
|
70
|
-
*
|
|
71
|
-
* Things this hook does different from existing solutions:
|
|
72
|
-
* - SSR-capable: it shows initial value during SSR and hydration, but immediately
|
|
73
|
-
* initializes when clientside mounted.
|
|
74
|
-
* - Sync state across tabs: When another tab changes the value in the storage area, the
|
|
75
|
-
* current tab follows suit.
|
|
76
|
-
*/
|
|
77
|
-
|
|
78
|
-
function useLocalStorageStateBrowser(key, initializer = null) {
|
|
79
|
-
const [initialValue] = React.useState(initializer);
|
|
80
|
-
const area = window.localStorage;
|
|
81
|
-
const subscribeKey = React.useCallback(cb => key ? subscribe(area, key, cb) : NOOP, [area, key]);
|
|
82
|
-
const getKeySnapshot = React.useCallback(() => {
|
|
83
|
-
var _ref;
|
|
84
|
-
return (_ref = key && getSnapshot(area, key)) != null ? _ref : initialValue;
|
|
85
|
-
}, [area, initialValue, key]);
|
|
86
|
-
const getKeyServerSnapshot = React.useCallback(() => initialValue, [initialValue]);
|
|
87
|
-
const storedValue = React.useSyncExternalStore(subscribeKey, getKeySnapshot, getKeyServerSnapshot);
|
|
88
|
-
const setStoredValue = React.useCallback(value => {
|
|
89
|
-
if (key) {
|
|
90
|
-
const valueToStore = value instanceof Function ? value(storedValue) : value;
|
|
91
|
-
setValue(area, key, valueToStore);
|
|
92
|
-
}
|
|
93
|
-
}, [area, key, storedValue]);
|
|
94
|
-
const [nonStoredValue, setNonStoredValue] = React.useState(initialValue);
|
|
95
|
-
if (!key) {
|
|
96
|
-
return [nonStoredValue, setNonStoredValue];
|
|
97
|
-
}
|
|
98
|
-
return [storedValue, setStoredValue];
|
|
99
|
-
}
|
|
100
|
-
export default typeof window === 'undefined' ? useLocalStorageStateServer : useLocalStorageStateBrowser;
|