@mui/system 5.6.4 → 5.7.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/Box/Box.spec.d.ts +1 -1
- package/CHANGELOG.md +101 -0
- package/ThemeProvider/ThemeProvider.d.ts +1 -1
- package/createBox.spec.d.ts +1 -1
- package/createTheme/createSpacing.d.ts +10 -10
- package/cssVars/createCssVarsProvider.d.ts +25 -0
- package/cssVars/createCssVarsProvider.js +54 -21
- package/cssVars/createCssVarsProvider.spec.d.ts +1 -1
- package/cssVars/createGetCssVar.d.ts +5 -5
- package/cssVars/cssVarsParser.d.ts +70 -70
- package/cssVars/getInitColorSchemeScript.d.ts +40 -12
- package/cssVars/getInitColorSchemeScript.js +4 -3
- package/cssVars/index.d.ts +2 -2
- package/cssVars/useCurrentColorScheme.d.ts +53 -50
- package/cssVars/useCurrentColorScheme.js +17 -7
- package/esm/cssVars/createCssVarsProvider.js +55 -22
- package/esm/cssVars/getInitColorSchemeScript.js +4 -3
- package/esm/cssVars/useCurrentColorScheme.js +17 -7
- package/index.js +1 -1
- package/index.spec.d.ts +1 -1
- package/legacy/cssVars/createCssVarsProvider.js +61 -24
- package/legacy/cssVars/getInitColorSchemeScript.js +6 -3
- package/legacy/cssVars/useCurrentColorScheme.js +20 -9
- package/legacy/index.js +1 -1
- package/modern/cssVars/createCssVarsProvider.js +55 -22
- package/modern/cssVars/getInitColorSchemeScript.js +4 -3
- package/modern/cssVars/useCurrentColorScheme.js +17 -7
- package/modern/index.js +1 -1
- package/package.json +5 -5
- package/styleFunctionSx/styleFunctionSx.spec.d.ts +1 -1
|
@@ -66,7 +66,9 @@ export default function useCurrentColorScheme(options) {
|
|
|
66
66
|
_options$modeStorageK = options.modeStorageKey,
|
|
67
67
|
modeStorageKey = _options$modeStorageK === void 0 ? DEFAULT_MODE_STORAGE_KEY : _options$modeStorageK,
|
|
68
68
|
_options$colorSchemeS = options.colorSchemeStorageKey,
|
|
69
|
-
colorSchemeStorageKey = _options$colorSchemeS === void 0 ? DEFAULT_COLOR_SCHEME_STORAGE_KEY : _options$colorSchemeS
|
|
69
|
+
colorSchemeStorageKey = _options$colorSchemeS === void 0 ? DEFAULT_COLOR_SCHEME_STORAGE_KEY : _options$colorSchemeS,
|
|
70
|
+
_options$storageWindo = options.storageWindow,
|
|
71
|
+
storageWindow = _options$storageWindo === void 0 ? typeof window === 'undefined' ? undefined : window : _options$storageWindo;
|
|
70
72
|
var joinedColorSchemes = supportedColorSchemes.join(',');
|
|
71
73
|
|
|
72
74
|
var _React$useState = React.useState(function () {
|
|
@@ -86,6 +88,10 @@ export default function useCurrentColorScheme(options) {
|
|
|
86
88
|
setState(function (currentState) {
|
|
87
89
|
var newMode = !mode ? defaultMode : mode;
|
|
88
90
|
|
|
91
|
+
if (mode === currentState.mode) {
|
|
92
|
+
return currentState;
|
|
93
|
+
}
|
|
94
|
+
|
|
89
95
|
if (typeof localStorage !== 'undefined') {
|
|
90
96
|
localStorage.setItem(modeStorageKey, newMode);
|
|
91
97
|
}
|
|
@@ -98,7 +104,7 @@ export default function useCurrentColorScheme(options) {
|
|
|
98
104
|
}, [modeStorageKey, defaultMode]);
|
|
99
105
|
var setColorScheme = React.useCallback(function (value) {
|
|
100
106
|
if (!value || typeof value === 'string') {
|
|
101
|
-
if (value && !
|
|
107
|
+
if (value && !joinedColorSchemes.includes(value)) {
|
|
102
108
|
console.error("`".concat(value, "` does not exist in `theme.colorSchemes`."));
|
|
103
109
|
} else {
|
|
104
110
|
setState(function (currentState) {
|
|
@@ -125,7 +131,7 @@ export default function useCurrentColorScheme(options) {
|
|
|
125
131
|
return newState;
|
|
126
132
|
});
|
|
127
133
|
}
|
|
128
|
-
} else if (value.light && !
|
|
134
|
+
} else if (value.light && !joinedColorSchemes.includes(value.light) || value.dark && !joinedColorSchemes.includes(value.dark)) {
|
|
129
135
|
console.error("`".concat(value, "` does not exist in `theme.colorSchemes`."));
|
|
130
136
|
} else {
|
|
131
137
|
setState(function (currentState) {
|
|
@@ -150,7 +156,7 @@ export default function useCurrentColorScheme(options) {
|
|
|
150
156
|
localStorage.setItem("".concat(colorSchemeStorageKey, "-dark"), value.dark);
|
|
151
157
|
}
|
|
152
158
|
}
|
|
153
|
-
}, [
|
|
159
|
+
}, [joinedColorSchemes, colorSchemeStorageKey, defaultLightColorScheme, defaultDarkColorScheme]);
|
|
154
160
|
var handleMediaQuery = React.useCallback(function (e) {
|
|
155
161
|
if (state.mode === 'system') {
|
|
156
162
|
setState(function (currentState) {
|
|
@@ -218,11 +224,16 @@ export default function useCurrentColorScheme(options) {
|
|
|
218
224
|
}
|
|
219
225
|
};
|
|
220
226
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
227
|
+
if (storageWindow) {
|
|
228
|
+
// For syncing color-scheme changes between iframes
|
|
229
|
+
storageWindow.addEventListener('storage', handleStorage);
|
|
230
|
+
return function () {
|
|
231
|
+
return storageWindow.removeEventListener('storage', handleStorage);
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return undefined;
|
|
236
|
+
}, [setColorScheme, setMode, modeStorageKey, colorSchemeStorageKey, joinedColorSchemes, defaultMode, storageWindow]);
|
|
226
237
|
return _extends({}, state, {
|
|
227
238
|
colorScheme: colorScheme,
|
|
228
239
|
setMode: setMode,
|
package/legacy/index.js
CHANGED
|
@@ -8,7 +8,7 @@ import { deepmerge, unstable_useEnhancedEffect as useEnhancedEffect } from '@mui
|
|
|
8
8
|
import { GlobalStyles } from '@mui/styled-engine';
|
|
9
9
|
import cssVarsParser from './cssVarsParser';
|
|
10
10
|
import ThemeProvider from '../ThemeProvider';
|
|
11
|
-
import getInitColorSchemeScript, { DEFAULT_ATTRIBUTE, DEFAULT_MODE_STORAGE_KEY } from './getInitColorSchemeScript';
|
|
11
|
+
import getInitColorSchemeScript, { DEFAULT_ATTRIBUTE, DEFAULT_COLOR_SCHEME_STORAGE_KEY, DEFAULT_MODE_STORAGE_KEY } from './getInitColorSchemeScript';
|
|
12
12
|
import useCurrentColorScheme from './useCurrentColorScheme';
|
|
13
13
|
import createGetCssVar from './createGetCssVar';
|
|
14
14
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
@@ -47,11 +47,16 @@ export default function createCssVarsProvider(options) {
|
|
|
47
47
|
theme: themeProp = defaultTheme,
|
|
48
48
|
prefix = designSystemPrefix,
|
|
49
49
|
modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
|
|
50
|
+
colorSchemeStorageKey = DEFAULT_COLOR_SCHEME_STORAGE_KEY,
|
|
50
51
|
attribute = DEFAULT_ATTRIBUTE,
|
|
51
52
|
defaultMode = desisgnSystemMode,
|
|
52
53
|
defaultColorScheme = designSystemColorScheme,
|
|
53
54
|
disableTransitionOnChange = designSystemTransitionOnChange,
|
|
54
|
-
enableColorScheme = designSystemEnableColorScheme
|
|
55
|
+
enableColorScheme = designSystemEnableColorScheme,
|
|
56
|
+
storageWindow = typeof window === 'undefined' ? undefined : window,
|
|
57
|
+
documentNode = typeof document === 'undefined' ? undefined : document,
|
|
58
|
+
colorSchemeNode = typeof document === 'undefined' ? undefined : document.documentElement,
|
|
59
|
+
colorSchemeSelector = ':root'
|
|
55
60
|
}) {
|
|
56
61
|
const hasMounted = React.useRef(false);
|
|
57
62
|
|
|
@@ -77,7 +82,9 @@ export default function createCssVarsProvider(options) {
|
|
|
77
82
|
defaultLightColorScheme,
|
|
78
83
|
defaultDarkColorScheme,
|
|
79
84
|
modeStorageKey,
|
|
80
|
-
|
|
85
|
+
colorSchemeStorageKey,
|
|
86
|
+
defaultMode,
|
|
87
|
+
storageWindow
|
|
81
88
|
});
|
|
82
89
|
|
|
83
90
|
const resolvedColorScheme = (() => {
|
|
@@ -147,54 +154,54 @@ export default function createCssVarsProvider(options) {
|
|
|
147
154
|
})();
|
|
148
155
|
|
|
149
156
|
if (key === resolvedDefaultColorScheme) {
|
|
150
|
-
styleSheet[
|
|
157
|
+
styleSheet[colorSchemeSelector] = css;
|
|
151
158
|
} else {
|
|
152
|
-
styleSheet[
|
|
159
|
+
styleSheet[`${colorSchemeSelector === ':root' ? '' : colorSchemeSelector}[${attribute}="${key}"]`] = css;
|
|
153
160
|
}
|
|
154
161
|
});
|
|
155
162
|
React.useEffect(() => {
|
|
156
|
-
if (colorScheme) {
|
|
163
|
+
if (colorScheme && colorSchemeNode) {
|
|
157
164
|
// attaches attribute to <html> because the css variables are attached to :root (html)
|
|
158
|
-
|
|
165
|
+
colorSchemeNode.setAttribute(attribute, colorScheme);
|
|
159
166
|
}
|
|
160
|
-
}, [colorScheme, attribute]);
|
|
167
|
+
}, [colorScheme, attribute, colorSchemeNode]);
|
|
161
168
|
useEnhancedEffect(() => {
|
|
162
|
-
if (!mode || !enableColorScheme) {
|
|
169
|
+
if (!mode || !enableColorScheme || !colorSchemeNode) {
|
|
163
170
|
return undefined;
|
|
164
171
|
}
|
|
165
172
|
|
|
166
|
-
const priorColorScheme =
|
|
173
|
+
const priorColorScheme = colorSchemeNode.style.getPropertyValue('color-scheme'); // `color-scheme` tells browser to render built-in elements according to its value: `light` or `dark`
|
|
167
174
|
|
|
168
175
|
if (mode === 'system') {
|
|
169
|
-
|
|
176
|
+
colorSchemeNode.style.setProperty('color-scheme', systemMode);
|
|
170
177
|
} else {
|
|
171
|
-
|
|
178
|
+
colorSchemeNode.style.setProperty('color-scheme', mode);
|
|
172
179
|
}
|
|
173
180
|
|
|
174
181
|
return () => {
|
|
175
|
-
|
|
182
|
+
colorSchemeNode.style.setProperty('color-scheme', priorColorScheme);
|
|
176
183
|
};
|
|
177
|
-
}, [mode, systemMode, enableColorScheme]);
|
|
184
|
+
}, [mode, systemMode, enableColorScheme, colorSchemeNode]);
|
|
178
185
|
React.useEffect(() => {
|
|
179
186
|
let timer;
|
|
180
187
|
|
|
181
|
-
if (disableTransitionOnChange && hasMounted.current) {
|
|
188
|
+
if (disableTransitionOnChange && hasMounted.current && documentNode) {
|
|
182
189
|
// credit: https://github.com/pacocoursey/next-themes/blob/b5c2bad50de2d61ad7b52a9c5cdc801a78507d7a/index.tsx#L313
|
|
183
|
-
const css =
|
|
184
|
-
css.appendChild(
|
|
185
|
-
|
|
190
|
+
const css = documentNode.createElement('style');
|
|
191
|
+
css.appendChild(documentNode.createTextNode(DISABLE_CSS_TRANSITION));
|
|
192
|
+
documentNode.head.appendChild(css); // Force browser repaint
|
|
186
193
|
|
|
187
|
-
(() => window.getComputedStyle(
|
|
194
|
+
(() => window.getComputedStyle(documentNode.body))();
|
|
188
195
|
|
|
189
196
|
timer = setTimeout(() => {
|
|
190
|
-
|
|
197
|
+
documentNode.head.removeChild(css);
|
|
191
198
|
}, 1);
|
|
192
199
|
}
|
|
193
200
|
|
|
194
201
|
return () => {
|
|
195
202
|
clearTimeout(timer);
|
|
196
203
|
};
|
|
197
|
-
}, [colorScheme, disableTransitionOnChange]);
|
|
204
|
+
}, [colorScheme, disableTransitionOnChange, documentNode]);
|
|
198
205
|
React.useEffect(() => {
|
|
199
206
|
hasMounted.current = true;
|
|
200
207
|
return () => {
|
|
@@ -213,7 +220,7 @@ export default function createCssVarsProvider(options) {
|
|
|
213
220
|
},
|
|
214
221
|
children: [/*#__PURE__*/_jsx(GlobalStyles, {
|
|
215
222
|
styles: {
|
|
216
|
-
|
|
223
|
+
[colorSchemeSelector]: rootCss
|
|
217
224
|
}
|
|
218
225
|
}), /*#__PURE__*/_jsx(GlobalStyles, {
|
|
219
226
|
styles: styleSheet
|
|
@@ -235,6 +242,21 @@ export default function createCssVarsProvider(options) {
|
|
|
235
242
|
*/
|
|
236
243
|
children: PropTypes.node,
|
|
237
244
|
|
|
245
|
+
/**
|
|
246
|
+
* The node used to attach the color-scheme attribute
|
|
247
|
+
*/
|
|
248
|
+
colorSchemeNode: PropTypes.any,
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* The CSS selector for attaching the generated custom properties
|
|
252
|
+
*/
|
|
253
|
+
colorSchemeSelector: PropTypes.string,
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* localStorage key used to store `colorScheme`
|
|
257
|
+
*/
|
|
258
|
+
colorSchemeStorageKey: PropTypes.string,
|
|
259
|
+
|
|
238
260
|
/**
|
|
239
261
|
* The initial color scheme used.
|
|
240
262
|
*/
|
|
@@ -250,6 +272,11 @@ export default function createCssVarsProvider(options) {
|
|
|
250
272
|
*/
|
|
251
273
|
disableTransitionOnChange: PropTypes.bool,
|
|
252
274
|
|
|
275
|
+
/**
|
|
276
|
+
* The document to attach the attribute to
|
|
277
|
+
*/
|
|
278
|
+
documentNode: PropTypes.any,
|
|
279
|
+
|
|
253
280
|
/**
|
|
254
281
|
* Indicate to the browser which color scheme is used (light or dark) for rendering built-in UI
|
|
255
282
|
*/
|
|
@@ -265,6 +292,12 @@ export default function createCssVarsProvider(options) {
|
|
|
265
292
|
*/
|
|
266
293
|
prefix: PropTypes.string,
|
|
267
294
|
|
|
295
|
+
/**
|
|
296
|
+
* The window that attaches the 'storage' event listener
|
|
297
|
+
* @default window
|
|
298
|
+
*/
|
|
299
|
+
storageWindow: PropTypes.any,
|
|
300
|
+
|
|
268
301
|
/**
|
|
269
302
|
* The calculated theme object that will be passed through context.
|
|
270
303
|
*/
|
|
@@ -5,12 +5,13 @@ export const DEFAULT_COLOR_SCHEME_STORAGE_KEY = 'mui-color-scheme';
|
|
|
5
5
|
export const DEFAULT_ATTRIBUTE = 'data-mui-color-scheme';
|
|
6
6
|
export default function getInitColorSchemeScript(options) {
|
|
7
7
|
const {
|
|
8
|
-
enableSystem,
|
|
8
|
+
enableSystem = false,
|
|
9
9
|
defaultLightColorScheme = 'light',
|
|
10
10
|
defaultDarkColorScheme = 'dark',
|
|
11
11
|
modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
|
|
12
12
|
colorSchemeStorageKey = DEFAULT_COLOR_SCHEME_STORAGE_KEY,
|
|
13
|
-
attribute = DEFAULT_ATTRIBUTE
|
|
13
|
+
attribute = DEFAULT_ATTRIBUTE,
|
|
14
|
+
colorSchemeNode = 'document.documentElement'
|
|
14
15
|
} = options || {};
|
|
15
16
|
return /*#__PURE__*/_jsx("script", {
|
|
16
17
|
// eslint-disable-next-line react/no-danger
|
|
@@ -34,7 +35,7 @@ export default function getInitColorSchemeScript(options) {
|
|
|
34
35
|
colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || '${defaultDarkColorScheme}';
|
|
35
36
|
}
|
|
36
37
|
if (colorScheme) {
|
|
37
|
-
|
|
38
|
+
${colorSchemeNode}.setAttribute('${attribute}', colorScheme);
|
|
38
39
|
}
|
|
39
40
|
} catch (e) {} })();`
|
|
40
41
|
}
|
|
@@ -63,7 +63,8 @@ export default function useCurrentColorScheme(options) {
|
|
|
63
63
|
defaultDarkColorScheme,
|
|
64
64
|
supportedColorSchemes = [],
|
|
65
65
|
modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
|
|
66
|
-
colorSchemeStorageKey = DEFAULT_COLOR_SCHEME_STORAGE_KEY
|
|
66
|
+
colorSchemeStorageKey = DEFAULT_COLOR_SCHEME_STORAGE_KEY,
|
|
67
|
+
storageWindow = typeof window === 'undefined' ? undefined : window
|
|
67
68
|
} = options;
|
|
68
69
|
const joinedColorSchemes = supportedColorSchemes.join(',');
|
|
69
70
|
const [state, setState] = React.useState(() => {
|
|
@@ -80,6 +81,10 @@ export default function useCurrentColorScheme(options) {
|
|
|
80
81
|
setState(currentState => {
|
|
81
82
|
const newMode = !mode ? defaultMode : mode;
|
|
82
83
|
|
|
84
|
+
if (mode === currentState.mode) {
|
|
85
|
+
return currentState;
|
|
86
|
+
}
|
|
87
|
+
|
|
83
88
|
if (typeof localStorage !== 'undefined') {
|
|
84
89
|
localStorage.setItem(modeStorageKey, newMode);
|
|
85
90
|
}
|
|
@@ -92,7 +97,7 @@ export default function useCurrentColorScheme(options) {
|
|
|
92
97
|
}, [modeStorageKey, defaultMode]);
|
|
93
98
|
const setColorScheme = React.useCallback(value => {
|
|
94
99
|
if (!value || typeof value === 'string') {
|
|
95
|
-
if (value && !
|
|
100
|
+
if (value && !joinedColorSchemes.includes(value)) {
|
|
96
101
|
console.error(`\`${value}\` does not exist in \`theme.colorSchemes\`.`);
|
|
97
102
|
} else {
|
|
98
103
|
setState(currentState => {
|
|
@@ -119,7 +124,7 @@ export default function useCurrentColorScheme(options) {
|
|
|
119
124
|
return newState;
|
|
120
125
|
});
|
|
121
126
|
}
|
|
122
|
-
} else if (value.light && !
|
|
127
|
+
} else if (value.light && !joinedColorSchemes.includes(value.light) || value.dark && !joinedColorSchemes.includes(value.dark)) {
|
|
123
128
|
console.error(`\`${value}\` does not exist in \`theme.colorSchemes\`.`);
|
|
124
129
|
} else {
|
|
125
130
|
setState(currentState => {
|
|
@@ -144,7 +149,7 @@ export default function useCurrentColorScheme(options) {
|
|
|
144
149
|
localStorage.setItem(`${colorSchemeStorageKey}-dark`, value.dark);
|
|
145
150
|
}
|
|
146
151
|
}
|
|
147
|
-
}, [
|
|
152
|
+
}, [joinedColorSchemes, colorSchemeStorageKey, defaultLightColorScheme, defaultDarkColorScheme]);
|
|
148
153
|
const handleMediaQuery = React.useCallback(e => {
|
|
149
154
|
if (state.mode === 'system') {
|
|
150
155
|
setState(currentState => _extends({}, currentState, {
|
|
@@ -206,9 +211,14 @@ export default function useCurrentColorScheme(options) {
|
|
|
206
211
|
}
|
|
207
212
|
};
|
|
208
213
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
214
|
+
if (storageWindow) {
|
|
215
|
+
// For syncing color-scheme changes between iframes
|
|
216
|
+
storageWindow.addEventListener('storage', handleStorage);
|
|
217
|
+
return () => storageWindow.removeEventListener('storage', handleStorage);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return undefined;
|
|
221
|
+
}, [setColorScheme, setMode, modeStorageKey, colorSchemeStorageKey, joinedColorSchemes, defaultMode, storageWindow]);
|
|
212
222
|
return _extends({}, state, {
|
|
213
223
|
colorScheme,
|
|
214
224
|
setMode,
|
package/modern/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mui/system",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.7.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"author": "MUI Team",
|
|
6
6
|
"description": "CSS utilities for rapidly laying out custom designs.",
|
|
@@ -44,13 +44,13 @@
|
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@babel/runtime": "^7.17.2",
|
|
47
|
-
"@mui/private-theming": "^5.
|
|
48
|
-
"@mui/styled-engine": "^5.
|
|
47
|
+
"@mui/private-theming": "^5.7.0",
|
|
48
|
+
"@mui/styled-engine": "^5.7.0",
|
|
49
49
|
"@mui/types": "^7.1.3",
|
|
50
|
-
"@mui/utils": "^5.
|
|
50
|
+
"@mui/utils": "^5.7.0",
|
|
51
51
|
"clsx": "^1.1.1",
|
|
52
52
|
"csstype": "^3.0.11",
|
|
53
|
-
"prop-types": "^15.
|
|
53
|
+
"prop-types": "^15.8.1"
|
|
54
54
|
},
|
|
55
55
|
"sideEffects": false,
|
|
56
56
|
"publishConfig": {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|