@mui/utils 5.15.13 → 6.0.0-alpha.1
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 +324 -62
- package/index.js +1 -1
- package/isMuiElement/isMuiElement.js +3 -3
- package/legacy/index.js +1 -1
- package/legacy/useLocalStorageState/index.js +1 -0
- package/legacy/useLocalStorageState/useLocalStorageState.js +121 -0
- package/legacy/useTimeout/useTimeout.js +1 -2
- package/modern/index.js +1 -1
- package/modern/useLocalStorageState/index.js +1 -0
- package/modern/useLocalStorageState/useLocalStorageState.js +108 -0
- package/node/debounce/index.js +1 -1
- package/node/deepmerge/index.js +1 -1
- package/node/generateUtilityClass/index.js +1 -1
- package/node/getDisplayName/index.js +1 -1
- package/node/getValidReactChildren/getValidReactChildren.js +1 -1
- package/node/index.js +2 -2
- package/node/integerPropType/index.js +1 -1
- package/node/isMuiElement/isMuiElement.js +1 -1
- package/node/useControlled/useControlled.js +1 -1
- package/node/useEnhancedEffect/useEnhancedEffect.js +1 -1
- package/node/useEventCallback/useEventCallback.js +1 -1
- package/node/useForkRef/useForkRef.js +1 -1
- package/node/useId/useId.js +1 -1
- package/node/useIsFocusVisible/index.js +1 -1
- package/node/useIsFocusVisible/useIsFocusVisible.js +1 -1
- package/node/useLazyRef/useLazyRef.js +1 -1
- package/node/useLocalStorageState/index.js +13 -0
- package/node/useLocalStorageState/useLocalStorageState.js +117 -0
- package/node/useOnMount/useOnMount.js +1 -1
- package/node/usePreviousProps/usePreviousProps.js +1 -1
- package/node/useTimeout/index.js +1 -1
- package/package.json +3 -3
- package/requirePropFactory/requirePropFactory.js +1 -1
- package/useId/useId.js +1 -1
- package/useLocalStorageState/index.d.ts +1 -0
- package/useLocalStorageState/index.js +1 -0
- package/useLocalStorageState/package.json +6 -0
- package/useLocalStorageState/useLocalStorageState.d.ts +17 -0
- package/useLocalStorageState/useLocalStorageState.js +108 -0
package/node/useTimeout/index.js
CHANGED
|
@@ -17,4 +17,4 @@ Object.defineProperty(exports, "default", {
|
|
|
17
17
|
});
|
|
18
18
|
var _useTimeout = _interopRequireWildcard(require("./useTimeout"));
|
|
19
19
|
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); }
|
|
20
|
-
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 &&
|
|
20
|
+
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 && {}.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; }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mui/utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0-alpha.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"author": "MUI Team",
|
|
6
6
|
"description": "Utility functions for React components.",
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
"url": "https://opencollective.com/mui-org"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@babel/runtime": "^7.
|
|
30
|
-
"@types/prop-types": "^15.7.
|
|
29
|
+
"@babel/runtime": "^7.24.4",
|
|
30
|
+
"@types/prop-types": "^15.7.12",
|
|
31
31
|
"prop-types": "^15.8.1",
|
|
32
32
|
"react-is": "^18.2.0"
|
|
33
33
|
},
|
|
@@ -8,7 +8,7 @@ export default function requirePropFactory(componentNameInError, Component) {
|
|
|
8
8
|
const prevPropTypes = Component ? _extends({}, Component.propTypes) : null;
|
|
9
9
|
const requireProp = requiredProp => (props, propName, componentName, location, propFullName, ...args) => {
|
|
10
10
|
const propFullNameSafe = propFullName || propName;
|
|
11
|
-
const defaultTypeChecker = prevPropTypes
|
|
11
|
+
const defaultTypeChecker = prevPropTypes?.[propFullNameSafe];
|
|
12
12
|
if (defaultTypeChecker) {
|
|
13
13
|
const typeCheckerResult = defaultTypeChecker(props, propName, componentName, location, propFullName, ...args);
|
|
14
14
|
if (typeCheckerResult) {
|
package/useId/useId.js
CHANGED
|
@@ -29,7 +29,7 @@ const maybeReactUseId = React['useId'.toString()];
|
|
|
29
29
|
export default function useId(idOverride) {
|
|
30
30
|
if (maybeReactUseId !== undefined) {
|
|
31
31
|
const reactId = maybeReactUseId();
|
|
32
|
-
return idOverride
|
|
32
|
+
return idOverride ?? reactId;
|
|
33
33
|
}
|
|
34
34
|
// eslint-disable-next-line react-hooks/rules-of-hooks -- `React.useId` is invariant at runtime.
|
|
35
35
|
return useGlobalId(idOverride);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from './useLocalStorageState';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from './useLocalStorageState';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
type Initializer = () => string | null;
|
|
3
|
+
type UseStorageStateHookResult = [
|
|
4
|
+
string | null,
|
|
5
|
+
React.Dispatch<React.SetStateAction<string | null>>
|
|
6
|
+
];
|
|
7
|
+
/**
|
|
8
|
+
* Sync state to local storage so that it persists through a page refresh. Usage is
|
|
9
|
+
* similar to useState except we pass in a storage key so that we can default
|
|
10
|
+
* to that value on page load instead of the specified initial value.
|
|
11
|
+
*
|
|
12
|
+
* Since the storage API isn't available in server-rendering environments, we
|
|
13
|
+
* return null during SSR and hydration.
|
|
14
|
+
*/
|
|
15
|
+
declare function useLocalStorageStateBrowser(key: string | null, initializer?: string | null | Initializer): UseStorageStateHookResult;
|
|
16
|
+
declare const _default: typeof useLocalStorageStateBrowser;
|
|
17
|
+
export default _default;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
|
|
5
|
+
// storage events only work across tabs, we'll use an event emitter to announce within the current tab
|
|
6
|
+
const currentTabChangeListeners = new Map();
|
|
7
|
+
function onCurrentTabStorageChange(key, handler) {
|
|
8
|
+
let listeners = currentTabChangeListeners.get(key);
|
|
9
|
+
if (!listeners) {
|
|
10
|
+
listeners = new Set();
|
|
11
|
+
currentTabChangeListeners.set(key, listeners);
|
|
12
|
+
}
|
|
13
|
+
listeners.add(handler);
|
|
14
|
+
}
|
|
15
|
+
function offCurrentTabStorageChange(key, handler) {
|
|
16
|
+
const listeners = currentTabChangeListeners.get(key);
|
|
17
|
+
if (!listeners) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
listeners.delete(handler);
|
|
21
|
+
if (listeners.size === 0) {
|
|
22
|
+
currentTabChangeListeners.delete(key);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function emitCurrentTabStorageChange(key) {
|
|
26
|
+
const listeners = currentTabChangeListeners.get(key);
|
|
27
|
+
if (listeners) {
|
|
28
|
+
listeners.forEach(listener => listener());
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function subscribe(area, key, callbark) {
|
|
32
|
+
if (!key) {
|
|
33
|
+
return () => {};
|
|
34
|
+
}
|
|
35
|
+
const storageHandler = event => {
|
|
36
|
+
if (event.storageArea === area && event.key === key) {
|
|
37
|
+
callbark();
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
window.addEventListener('storage', storageHandler);
|
|
41
|
+
onCurrentTabStorageChange(key, callbark);
|
|
42
|
+
return () => {
|
|
43
|
+
window.removeEventListener('storage', storageHandler);
|
|
44
|
+
offCurrentTabStorageChange(key, callbark);
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function getSnapshot(area, key) {
|
|
48
|
+
if (!key) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
return area.getItem(key);
|
|
53
|
+
} catch {
|
|
54
|
+
// ignore
|
|
55
|
+
// See https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#feature-detecting_localstorage
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function setValue(area, key, value) {
|
|
60
|
+
if (!key) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
if (value === null) {
|
|
65
|
+
area.removeItem(key);
|
|
66
|
+
} else {
|
|
67
|
+
area.setItem(key, String(value));
|
|
68
|
+
}
|
|
69
|
+
} catch {
|
|
70
|
+
// ignore
|
|
71
|
+
// See https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#feature-detecting_localstorage
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
emitCurrentTabStorageChange(key);
|
|
75
|
+
}
|
|
76
|
+
const serverValue = [null, () => {}];
|
|
77
|
+
function useLocalStorageStateServer() {
|
|
78
|
+
return serverValue;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Sync state to local storage so that it persists through a page refresh. Usage is
|
|
83
|
+
* similar to useState except we pass in a storage key so that we can default
|
|
84
|
+
* to that value on page load instead of the specified initial value.
|
|
85
|
+
*
|
|
86
|
+
* Since the storage API isn't available in server-rendering environments, we
|
|
87
|
+
* return null during SSR and hydration.
|
|
88
|
+
*/
|
|
89
|
+
function useLocalStorageStateBrowser(key, initializer = null) {
|
|
90
|
+
const [initialValue] = React.useState(initializer);
|
|
91
|
+
const area = window.localStorage;
|
|
92
|
+
const subscribeKey = React.useCallback(callbark => subscribe(area, key, callbark), [area, key]);
|
|
93
|
+
const getKeySnapshot = React.useCallback(() => getSnapshot(area, key) ?? initialValue, [area, initialValue, key]);
|
|
94
|
+
|
|
95
|
+
// Start with null for the hydration, and then switch to the actual value.
|
|
96
|
+
const getKeyServerSnapshot = () => null;
|
|
97
|
+
const storedValue = React.useSyncExternalStore(subscribeKey, getKeySnapshot, getKeyServerSnapshot);
|
|
98
|
+
const setStoredValue = React.useCallback(value => {
|
|
99
|
+
const valueToStore = value instanceof Function ? value(storedValue) : value;
|
|
100
|
+
setValue(area, key, valueToStore);
|
|
101
|
+
}, [area, key, storedValue]);
|
|
102
|
+
const [nonStoredValue, setNonStoredValue] = React.useState(initialValue);
|
|
103
|
+
if (!key) {
|
|
104
|
+
return [nonStoredValue, setNonStoredValue];
|
|
105
|
+
}
|
|
106
|
+
return [storedValue, setStoredValue];
|
|
107
|
+
}
|
|
108
|
+
export default typeof window === 'undefined' ? useLocalStorageStateServer : useLocalStorageStateBrowser;
|