@mui/system 5.6.2 → 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.d.ts +18 -1
- package/Box/Box.js +26 -0
- package/Box/Box.spec.d.ts +1 -1
- package/CHANGELOG.md +225 -0
- package/ThemeProvider/ThemeProvider.d.ts +7 -1
- package/ThemeProvider/ThemeProvider.js +9 -2
- package/createBox.js +0 -26
- package/createBox.spec.d.ts +1 -1
- package/createStyled.js +3 -1
- package/createTheme/createSpacing.d.ts +10 -10
- package/cssVars/createCssVarsProvider.d.ts +25 -0
- package/cssVars/createCssVarsProvider.js +74 -57
- package/cssVars/createCssVarsProvider.spec.d.ts +1 -1
- package/cssVars/createGetCssVar.d.ts +5 -5
- package/cssVars/cssVarsParser.d.ts +70 -70
- package/cssVars/cssVarsParser.js +11 -9
- 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/Box/Box.js +25 -0
- package/esm/ThemeProvider/ThemeProvider.js +9 -2
- package/esm/createBox.js +0 -25
- package/esm/createStyled.js +3 -1
- package/esm/cssVars/createCssVarsProvider.js +76 -57
- package/esm/cssVars/cssVarsParser.js +11 -9
- package/esm/cssVars/getInitColorSchemeScript.js +4 -3
- package/esm/cssVars/useCurrentColorScheme.js +17 -7
- package/esm/spacing.js +3 -1
- package/esm/style.js +7 -1
- package/index.js +1 -1
- package/index.spec.d.ts +1 -1
- package/legacy/Box/Box.js +25 -0
- package/legacy/ThemeProvider/ThemeProvider.js +9 -2
- package/legacy/createBox.js +0 -25
- package/legacy/createStyled.js +3 -1
- package/legacy/cssVars/createCssVarsProvider.js +83 -55
- package/legacy/cssVars/cssVarsParser.js +11 -7
- package/legacy/cssVars/getInitColorSchemeScript.js +6 -3
- package/legacy/cssVars/useCurrentColorScheme.js +20 -9
- package/legacy/index.js +1 -1
- package/legacy/spacing.js +3 -1
- package/legacy/style.js +5 -1
- package/modern/Box/Box.js +25 -0
- package/modern/ThemeProvider/ThemeProvider.js +9 -2
- package/modern/createBox.js +0 -25
- package/modern/createStyled.js +3 -1
- package/modern/cssVars/createCssVarsProvider.js +76 -55
- package/modern/cssVars/cssVarsParser.js +11 -9
- package/modern/cssVars/getInitColorSchemeScript.js +4 -3
- package/modern/cssVars/useCurrentColorScheme.js +17 -7
- package/modern/index.js +1 -1
- package/modern/spacing.js +1 -1
- package/modern/style.js +7 -1
- package/package.json +5 -5
- package/spacing.js +3 -1
- package/style.js +7 -1
- package/styleFunctionSx/styleFunctionSx.d.ts +3 -1
- 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
package/legacy/spacing.js
CHANGED
|
@@ -50,7 +50,9 @@ var marginKeys = ['m', 'mt', 'mr', 'mb', 'ml', 'mx', 'my', 'margin', 'marginTop'
|
|
|
50
50
|
var paddingKeys = ['p', 'pt', 'pr', 'pb', 'pl', 'px', 'py', 'padding', 'paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft', 'paddingX', 'paddingY', 'paddingInline', 'paddingInlineStart', 'paddingInlineEnd', 'paddingBlock', 'paddingBlockStart', 'paddingBlockEnd'];
|
|
51
51
|
var spacingKeys = [].concat(marginKeys, paddingKeys);
|
|
52
52
|
export function createUnaryUnit(theme, themeKey, defaultValue, propName) {
|
|
53
|
-
var
|
|
53
|
+
var _getPath;
|
|
54
|
+
|
|
55
|
+
var themeSpacing = (_getPath = getPath(theme, themeKey)) != null ? _getPath : defaultValue;
|
|
54
56
|
|
|
55
57
|
if (typeof themeSpacing === 'number') {
|
|
56
58
|
return function (abs) {
|
package/legacy/style.js
CHANGED
package/modern/Box/Box.js
CHANGED
|
@@ -1,3 +1,28 @@
|
|
|
1
|
+
import PropTypes from 'prop-types';
|
|
1
2
|
import createBox from '../createBox';
|
|
2
3
|
const Box = createBox();
|
|
4
|
+
process.env.NODE_ENV !== "production" ? Box.propTypes
|
|
5
|
+
/* remove-proptypes */
|
|
6
|
+
= {
|
|
7
|
+
// ----------------------------- Warning --------------------------------
|
|
8
|
+
// | These PropTypes are generated from the TypeScript type definitions |
|
|
9
|
+
// | To update them edit the d.ts file and run "yarn proptypes" |
|
|
10
|
+
// ----------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @ignore
|
|
14
|
+
*/
|
|
15
|
+
children: PropTypes.node,
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The component used for the root node.
|
|
19
|
+
* Either a string to use a HTML element or a component.
|
|
20
|
+
*/
|
|
21
|
+
component: PropTypes.elementType,
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The system prop that allows defining system overrides as well as additional CSS styles.
|
|
25
|
+
*/
|
|
26
|
+
sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object])
|
|
27
|
+
} : void 0;
|
|
3
28
|
export default Box;
|
|
@@ -38,7 +38,14 @@ function ThemeProvider(props) {
|
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
process.env.NODE_ENV !== "production" ? ThemeProvider.propTypes
|
|
41
|
+
process.env.NODE_ENV !== "production" ? ThemeProvider.propTypes
|
|
42
|
+
/* remove-proptypes */
|
|
43
|
+
= {
|
|
44
|
+
// ----------------------------- Warning --------------------------------
|
|
45
|
+
// | These PropTypes are generated from the TypeScript type definitions |
|
|
46
|
+
// | To update them edit the d.ts file and run "yarn proptypes" |
|
|
47
|
+
// ----------------------------------------------------------------------
|
|
48
|
+
|
|
42
49
|
/**
|
|
43
50
|
* Your component tree.
|
|
44
51
|
*/
|
|
@@ -47,7 +54,7 @@ process.env.NODE_ENV !== "production" ? ThemeProvider.propTypes = {
|
|
|
47
54
|
/**
|
|
48
55
|
* A theme object. You can provide a function to extend the outer theme.
|
|
49
56
|
*/
|
|
50
|
-
theme: PropTypes.oneOfType([PropTypes.
|
|
57
|
+
theme: PropTypes.oneOfType([PropTypes.func, PropTypes.object]).isRequired
|
|
51
58
|
} : void 0;
|
|
52
59
|
|
|
53
60
|
if (process.env.NODE_ENV !== 'production') {
|
package/modern/createBox.js
CHANGED
|
@@ -2,7 +2,6 @@ import _extends from "@babel/runtime/helpers/esm/extends";
|
|
|
2
2
|
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
|
3
3
|
const _excluded = ["className", "component"];
|
|
4
4
|
import * as React from 'react';
|
|
5
|
-
import PropTypes from 'prop-types';
|
|
6
5
|
import clsx from 'clsx';
|
|
7
6
|
import styled from '@mui/styled-engine';
|
|
8
7
|
import defaultStyleFunctionSx, { extendSxProp } from './styleFunctionSx';
|
|
@@ -33,29 +32,5 @@ export default function createBox(options = {}) {
|
|
|
33
32
|
theme: theme
|
|
34
33
|
}, other));
|
|
35
34
|
});
|
|
36
|
-
process.env.NODE_ENV !== "production" ? Box.propTypes
|
|
37
|
-
/* remove-proptypes */
|
|
38
|
-
= {
|
|
39
|
-
// ----------------------------- Warning --------------------------------
|
|
40
|
-
// | These PropTypes are generated from the TypeScript type definitions |
|
|
41
|
-
// | To update them edit the d.ts file and run "yarn proptypes" |
|
|
42
|
-
// ----------------------------------------------------------------------
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* @ignore
|
|
46
|
-
*/
|
|
47
|
-
children: PropTypes.node,
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* The component used for the root node.
|
|
51
|
-
* Either a string to use a HTML element or a component.
|
|
52
|
-
*/
|
|
53
|
-
component: PropTypes.elementType,
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* @ignore
|
|
57
|
-
*/
|
|
58
|
-
sx: PropTypes.oneOfType([PropTypes.object, PropTypes.array, PropTypes.func])
|
|
59
|
-
} : void 0;
|
|
60
35
|
return Box;
|
|
61
36
|
}
|
package/modern/createStyled.js
CHANGED
|
@@ -140,7 +140,9 @@ export default function createStyled(input = {}) {
|
|
|
140
140
|
if (styleOverrides) {
|
|
141
141
|
const resolvedStyleOverrides = {};
|
|
142
142
|
Object.entries(styleOverrides).forEach(([slotKey, slotStyle]) => {
|
|
143
|
-
resolvedStyleOverrides[slotKey] = typeof slotStyle === 'function' ? slotStyle(props
|
|
143
|
+
resolvedStyleOverrides[slotKey] = typeof slotStyle === 'function' ? slotStyle(_extends({}, props, {
|
|
144
|
+
theme
|
|
145
|
+
})) : slotStyle;
|
|
144
146
|
});
|
|
145
147
|
return overridesResolver(props, resolvedStyleOverrides);
|
|
146
148
|
}
|
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
2
|
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
|
3
3
|
import { formatMuiErrorMessage as _formatMuiErrorMessage } from "@mui/utils";
|
|
4
|
-
const _excluded = ["colorSchemes"]
|
|
5
|
-
_excluded2 = ["colorSchemes"],
|
|
6
|
-
_excluded3 = ["components"];
|
|
4
|
+
const _excluded = ["colorSchemes", "components"];
|
|
7
5
|
import * as React from 'react';
|
|
8
6
|
import PropTypes from 'prop-types';
|
|
9
|
-
import { GlobalStyles } from '@mui/styled-engine';
|
|
10
7
|
import { deepmerge, unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils';
|
|
11
|
-
import
|
|
12
|
-
import createBreakpoints from '../createTheme/createBreakpoints';
|
|
8
|
+
import { GlobalStyles } from '@mui/styled-engine';
|
|
13
9
|
import cssVarsParser from './cssVarsParser';
|
|
14
10
|
import ThemeProvider from '../ThemeProvider';
|
|
15
|
-
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';
|
|
16
12
|
import useCurrentColorScheme from './useCurrentColorScheme';
|
|
17
13
|
import createGetCssVar from './createGetCssVar';
|
|
18
14
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
@@ -20,7 +16,7 @@ import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
20
16
|
export const DISABLE_CSS_TRANSITION = '*{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}';
|
|
21
17
|
export default function createCssVarsProvider(options) {
|
|
22
18
|
const {
|
|
23
|
-
theme:
|
|
19
|
+
theme: defaultTheme = {},
|
|
24
20
|
defaultMode: desisgnSystemMode = 'light',
|
|
25
21
|
defaultColorScheme: designSystemColorScheme,
|
|
26
22
|
disableTransitionOnChange: designSystemTransitionOnChange = false,
|
|
@@ -29,10 +25,8 @@ export default function createCssVarsProvider(options) {
|
|
|
29
25
|
shouldSkipGeneratingVar,
|
|
30
26
|
resolveTheme
|
|
31
27
|
} = options;
|
|
32
|
-
const systemSpacing = createSpacing(baseTheme.spacing);
|
|
33
|
-
const systemBreakpoints = createBreakpoints(baseTheme.breakpoints ?? {});
|
|
34
28
|
|
|
35
|
-
if (!
|
|
29
|
+
if (!defaultTheme.colorSchemes || typeof designSystemColorScheme === 'string' && !defaultTheme.colorSchemes[designSystemColorScheme] || typeof designSystemColorScheme === 'object' && !defaultTheme.colorSchemes[designSystemColorScheme?.light] || typeof designSystemColorScheme === 'object' && !defaultTheme.colorSchemes[designSystemColorScheme?.dark]) {
|
|
36
30
|
console.error(`MUI: \`${designSystemColorScheme}\` does not exist in \`theme.colorSchemes\`.`);
|
|
37
31
|
}
|
|
38
32
|
|
|
@@ -50,34 +44,28 @@ export default function createCssVarsProvider(options) {
|
|
|
50
44
|
|
|
51
45
|
function CssVarsProvider({
|
|
52
46
|
children,
|
|
53
|
-
theme: themeProp =
|
|
47
|
+
theme: themeProp = defaultTheme,
|
|
54
48
|
prefix = designSystemPrefix,
|
|
55
49
|
modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
|
|
50
|
+
colorSchemeStorageKey = DEFAULT_COLOR_SCHEME_STORAGE_KEY,
|
|
56
51
|
attribute = DEFAULT_ATTRIBUTE,
|
|
57
52
|
defaultMode = desisgnSystemMode,
|
|
58
53
|
defaultColorScheme = designSystemColorScheme,
|
|
59
54
|
disableTransitionOnChange = designSystemTransitionOnChange,
|
|
60
|
-
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'
|
|
61
60
|
}) {
|
|
62
|
-
const
|
|
63
|
-
colorSchemes: baseColorSchemes = {}
|
|
64
|
-
} = baseTheme,
|
|
65
|
-
restBaseTheme = _objectWithoutPropertiesLoose(baseTheme, _excluded);
|
|
61
|
+
const hasMounted = React.useRef(false);
|
|
66
62
|
|
|
67
63
|
const {
|
|
68
|
-
colorSchemes
|
|
69
|
-
} = themeProp,
|
|
70
|
-
restThemeProp = _objectWithoutPropertiesLoose(themeProp, _excluded2);
|
|
71
|
-
|
|
72
|
-
const hasMounted = React.useRef(false); // eslint-disable-next-line prefer-const
|
|
73
|
-
|
|
74
|
-
let _deepmerge = deepmerge(restBaseTheme, restThemeProp),
|
|
75
|
-
{
|
|
64
|
+
colorSchemes = {},
|
|
76
65
|
components = {}
|
|
77
|
-
} =
|
|
78
|
-
|
|
66
|
+
} = themeProp,
|
|
67
|
+
restThemeProp = _objectWithoutPropertiesLoose(themeProp, _excluded);
|
|
79
68
|
|
|
80
|
-
const colorSchemes = deepmerge(baseColorSchemes, colorSchemesProp);
|
|
81
69
|
const allColorSchemes = Object.keys(colorSchemes);
|
|
82
70
|
const defaultLightColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.light;
|
|
83
71
|
const defaultDarkColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.dark;
|
|
@@ -94,7 +82,9 @@ export default function createCssVarsProvider(options) {
|
|
|
94
82
|
defaultLightColorScheme,
|
|
95
83
|
defaultDarkColorScheme,
|
|
96
84
|
modeStorageKey,
|
|
97
|
-
|
|
85
|
+
colorSchemeStorageKey,
|
|
86
|
+
defaultMode,
|
|
87
|
+
storageWindow
|
|
98
88
|
});
|
|
99
89
|
|
|
100
90
|
const resolvedColorScheme = (() => {
|
|
@@ -111,22 +101,21 @@ export default function createCssVarsProvider(options) {
|
|
|
111
101
|
return colorScheme;
|
|
112
102
|
})();
|
|
113
103
|
|
|
104
|
+
let theme = restThemeProp;
|
|
114
105
|
const {
|
|
115
106
|
css: rootCss,
|
|
116
107
|
vars: rootVars,
|
|
117
108
|
parsedTheme
|
|
118
|
-
} = cssVarsParser(
|
|
109
|
+
} = cssVarsParser(theme, {
|
|
119
110
|
prefix,
|
|
120
111
|
basePrefix: designSystemPrefix,
|
|
121
112
|
shouldSkipGeneratingVar
|
|
122
113
|
});
|
|
123
|
-
|
|
114
|
+
theme = _extends({}, parsedTheme, {
|
|
124
115
|
components,
|
|
125
116
|
colorSchemes,
|
|
126
117
|
prefix,
|
|
127
118
|
vars: rootVars,
|
|
128
|
-
spacing: themeProp.spacing ? createSpacing(themeProp.spacing) : systemSpacing,
|
|
129
|
-
breakpoints: themeProp.breakpoints ? createBreakpoints(themeProp.breakpoints) : systemBreakpoints,
|
|
130
119
|
getCssVar: createGetCssVar(prefix)
|
|
131
120
|
});
|
|
132
121
|
const styleSheet = {};
|
|
@@ -140,10 +129,16 @@ export default function createCssVarsProvider(options) {
|
|
|
140
129
|
basePrefix: designSystemPrefix,
|
|
141
130
|
shouldSkipGeneratingVar
|
|
142
131
|
});
|
|
143
|
-
|
|
132
|
+
theme.vars = deepmerge(theme.vars, vars);
|
|
144
133
|
|
|
145
134
|
if (key === resolvedColorScheme) {
|
|
146
|
-
|
|
135
|
+
theme = _extends({}, theme, parsedScheme);
|
|
136
|
+
|
|
137
|
+
if (theme.palette) {
|
|
138
|
+
// assign runtime mode & colorScheme
|
|
139
|
+
theme.palette.mode = mode;
|
|
140
|
+
theme.palette.colorScheme = resolvedColorScheme;
|
|
141
|
+
}
|
|
147
142
|
}
|
|
148
143
|
|
|
149
144
|
const resolvedDefaultColorScheme = (() => {
|
|
@@ -159,54 +154,54 @@ export default function createCssVarsProvider(options) {
|
|
|
159
154
|
})();
|
|
160
155
|
|
|
161
156
|
if (key === resolvedDefaultColorScheme) {
|
|
162
|
-
styleSheet[
|
|
157
|
+
styleSheet[colorSchemeSelector] = css;
|
|
163
158
|
} else {
|
|
164
|
-
styleSheet[
|
|
159
|
+
styleSheet[`${colorSchemeSelector === ':root' ? '' : colorSchemeSelector}[${attribute}="${key}"]`] = css;
|
|
165
160
|
}
|
|
166
161
|
});
|
|
167
162
|
React.useEffect(() => {
|
|
168
|
-
if (colorScheme) {
|
|
163
|
+
if (colorScheme && colorSchemeNode) {
|
|
169
164
|
// attaches attribute to <html> because the css variables are attached to :root (html)
|
|
170
|
-
|
|
165
|
+
colorSchemeNode.setAttribute(attribute, colorScheme);
|
|
171
166
|
}
|
|
172
|
-
}, [colorScheme, attribute]);
|
|
167
|
+
}, [colorScheme, attribute, colorSchemeNode]);
|
|
173
168
|
useEnhancedEffect(() => {
|
|
174
|
-
if (!mode || !enableColorScheme) {
|
|
169
|
+
if (!mode || !enableColorScheme || !colorSchemeNode) {
|
|
175
170
|
return undefined;
|
|
176
171
|
}
|
|
177
172
|
|
|
178
|
-
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`
|
|
179
174
|
|
|
180
175
|
if (mode === 'system') {
|
|
181
|
-
|
|
176
|
+
colorSchemeNode.style.setProperty('color-scheme', systemMode);
|
|
182
177
|
} else {
|
|
183
|
-
|
|
178
|
+
colorSchemeNode.style.setProperty('color-scheme', mode);
|
|
184
179
|
}
|
|
185
180
|
|
|
186
181
|
return () => {
|
|
187
|
-
|
|
182
|
+
colorSchemeNode.style.setProperty('color-scheme', priorColorScheme);
|
|
188
183
|
};
|
|
189
|
-
}, [mode, systemMode, enableColorScheme]);
|
|
184
|
+
}, [mode, systemMode, enableColorScheme, colorSchemeNode]);
|
|
190
185
|
React.useEffect(() => {
|
|
191
186
|
let timer;
|
|
192
187
|
|
|
193
|
-
if (disableTransitionOnChange && hasMounted.current) {
|
|
188
|
+
if (disableTransitionOnChange && hasMounted.current && documentNode) {
|
|
194
189
|
// credit: https://github.com/pacocoursey/next-themes/blob/b5c2bad50de2d61ad7b52a9c5cdc801a78507d7a/index.tsx#L313
|
|
195
|
-
const css =
|
|
196
|
-
css.appendChild(
|
|
197
|
-
|
|
190
|
+
const css = documentNode.createElement('style');
|
|
191
|
+
css.appendChild(documentNode.createTextNode(DISABLE_CSS_TRANSITION));
|
|
192
|
+
documentNode.head.appendChild(css); // Force browser repaint
|
|
198
193
|
|
|
199
|
-
(() => window.getComputedStyle(
|
|
194
|
+
(() => window.getComputedStyle(documentNode.body))();
|
|
200
195
|
|
|
201
196
|
timer = setTimeout(() => {
|
|
202
|
-
|
|
197
|
+
documentNode.head.removeChild(css);
|
|
203
198
|
}, 1);
|
|
204
199
|
}
|
|
205
200
|
|
|
206
201
|
return () => {
|
|
207
202
|
clearTimeout(timer);
|
|
208
203
|
};
|
|
209
|
-
}, [colorScheme, disableTransitionOnChange]);
|
|
204
|
+
}, [colorScheme, disableTransitionOnChange, documentNode]);
|
|
210
205
|
React.useEffect(() => {
|
|
211
206
|
hasMounted.current = true;
|
|
212
207
|
return () => {
|
|
@@ -225,12 +220,12 @@ export default function createCssVarsProvider(options) {
|
|
|
225
220
|
},
|
|
226
221
|
children: [/*#__PURE__*/_jsx(GlobalStyles, {
|
|
227
222
|
styles: {
|
|
228
|
-
|
|
223
|
+
[colorSchemeSelector]: rootCss
|
|
229
224
|
}
|
|
230
225
|
}), /*#__PURE__*/_jsx(GlobalStyles, {
|
|
231
226
|
styles: styleSheet
|
|
232
227
|
}), /*#__PURE__*/_jsx(ThemeProvider, {
|
|
233
|
-
theme: resolveTheme ? resolveTheme(
|
|
228
|
+
theme: resolveTheme ? resolveTheme(theme) : theme,
|
|
234
229
|
children: children
|
|
235
230
|
})]
|
|
236
231
|
});
|
|
@@ -247,6 +242,21 @@ export default function createCssVarsProvider(options) {
|
|
|
247
242
|
*/
|
|
248
243
|
children: PropTypes.node,
|
|
249
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
|
+
|
|
250
260
|
/**
|
|
251
261
|
* The initial color scheme used.
|
|
252
262
|
*/
|
|
@@ -262,6 +272,11 @@ export default function createCssVarsProvider(options) {
|
|
|
262
272
|
*/
|
|
263
273
|
disableTransitionOnChange: PropTypes.bool,
|
|
264
274
|
|
|
275
|
+
/**
|
|
276
|
+
* The document to attach the attribute to
|
|
277
|
+
*/
|
|
278
|
+
documentNode: PropTypes.any,
|
|
279
|
+
|
|
265
280
|
/**
|
|
266
281
|
* Indicate to the browser which color scheme is used (light or dark) for rendering built-in UI
|
|
267
282
|
*/
|
|
@@ -277,6 +292,12 @@ export default function createCssVarsProvider(options) {
|
|
|
277
292
|
*/
|
|
278
293
|
prefix: PropTypes.string,
|
|
279
294
|
|
|
295
|
+
/**
|
|
296
|
+
* The window that attaches the 'storage' event listener
|
|
297
|
+
* @default window
|
|
298
|
+
*/
|
|
299
|
+
storageWindow: PropTypes.any,
|
|
300
|
+
|
|
280
301
|
/**
|
|
281
302
|
* The calculated theme object that will be passed through context.
|
|
282
303
|
*/
|
|
@@ -15,16 +15,18 @@
|
|
|
15
15
|
* assignNestedKeys(source, ['palette', 'secondary'], 'var(--palette-secondary)')
|
|
16
16
|
* console.log(source) // { palette: { primary: 'var(--palette-primary)', secondary: 'var(--palette-secondary)' } }
|
|
17
17
|
*/
|
|
18
|
-
export const assignNestedKeys = (obj, keys, value) => {
|
|
18
|
+
export const assignNestedKeys = (obj, keys, value, arrayKeys = []) => {
|
|
19
19
|
let temp = obj;
|
|
20
20
|
keys.forEach((k, index) => {
|
|
21
21
|
if (index === keys.length - 1) {
|
|
22
|
-
if (temp
|
|
22
|
+
if (Array.isArray(temp)) {
|
|
23
|
+
temp[Number(k)] = value;
|
|
24
|
+
} else if (temp && typeof temp === 'object') {
|
|
23
25
|
temp[k] = value;
|
|
24
26
|
}
|
|
25
27
|
} else if (temp && typeof temp === 'object') {
|
|
26
28
|
if (!temp[k]) {
|
|
27
|
-
temp[k] = {};
|
|
29
|
+
temp[k] = arrayKeys.includes(k) ? [] : {};
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
temp = temp[k];
|
|
@@ -44,14 +46,14 @@ export const assignNestedKeys = (obj, keys, value) => {
|
|
|
44
46
|
*/
|
|
45
47
|
|
|
46
48
|
export const walkObjectDeep = (obj, callback, shouldSkipPaths) => {
|
|
47
|
-
function recurse(object, parentKeys = []) {
|
|
49
|
+
function recurse(object, parentKeys = [], arrayKeys = []) {
|
|
48
50
|
Object.entries(object).forEach(([key, value]) => {
|
|
49
51
|
if (!shouldSkipPaths || shouldSkipPaths && !shouldSkipPaths([...parentKeys, key])) {
|
|
50
52
|
if (value !== undefined && value !== null) {
|
|
51
53
|
if (typeof value === 'object' && Object.keys(value).length > 0) {
|
|
52
|
-
recurse(value, [...parentKeys, key]);
|
|
54
|
+
recurse(value, [...parentKeys, key], Array.isArray(value) ? [...arrayKeys, key] : arrayKeys);
|
|
53
55
|
} else {
|
|
54
|
-
callback([...parentKeys, key], value,
|
|
56
|
+
callback([...parentKeys, key], value, arrayKeys);
|
|
55
57
|
}
|
|
56
58
|
}
|
|
57
59
|
}
|
|
@@ -118,7 +120,7 @@ export default function cssVarsParser(theme, options) {
|
|
|
118
120
|
const css = {};
|
|
119
121
|
const vars = {};
|
|
120
122
|
const parsedTheme = {};
|
|
121
|
-
walkObjectDeep(theme, (keys, value) => {
|
|
123
|
+
walkObjectDeep(theme, (keys, value, arrayKeys) => {
|
|
122
124
|
if (typeof value === 'string' || typeof value === 'number') {
|
|
123
125
|
if (typeof value === 'string' && value.match(/var\(\s*--/)) {
|
|
124
126
|
// for CSS variable, apply prefix or remove basePrefix from the variable
|
|
@@ -136,11 +138,11 @@ export default function cssVarsParser(theme, options) {
|
|
|
136
138
|
Object.assign(css, {
|
|
137
139
|
[cssVar]: getCssValue(keys, value)
|
|
138
140
|
});
|
|
139
|
-
assignNestedKeys(vars, keys, `var(${cssVar})
|
|
141
|
+
assignNestedKeys(vars, keys, `var(${cssVar})`, arrayKeys);
|
|
140
142
|
}
|
|
141
143
|
}
|
|
142
144
|
|
|
143
|
-
assignNestedKeys(parsedTheme, keys, value);
|
|
145
|
+
assignNestedKeys(parsedTheme, keys, value, arrayKeys);
|
|
144
146
|
}, keys => keys[0] === 'vars' // skip 'vars/*' paths
|
|
145
147
|
);
|
|
146
148
|
return {
|
|
@@ -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/modern/spacing.js
CHANGED
|
@@ -43,7 +43,7 @@ const marginKeys = ['m', 'mt', 'mr', 'mb', 'ml', 'mx', 'my', 'margin', 'marginTo
|
|
|
43
43
|
const paddingKeys = ['p', 'pt', 'pr', 'pb', 'pl', 'px', 'py', 'padding', 'paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft', 'paddingX', 'paddingY', 'paddingInline', 'paddingInlineStart', 'paddingInlineEnd', 'paddingBlock', 'paddingBlockStart', 'paddingBlockEnd'];
|
|
44
44
|
const spacingKeys = [...marginKeys, ...paddingKeys];
|
|
45
45
|
export function createUnaryUnit(theme, themeKey, defaultValue, propName) {
|
|
46
|
-
const themeSpacing = getPath(theme, themeKey)
|
|
46
|
+
const themeSpacing = getPath(theme, themeKey) ?? defaultValue;
|
|
47
47
|
|
|
48
48
|
if (typeof themeSpacing === 'number') {
|
|
49
49
|
return abs => {
|