@mui/system 5.0.3 → 5.1.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/CHANGELOG.md +274 -0
- package/borders.js +1 -1
- package/breakpoints.js +45 -11
- package/colorManipulator.js +8 -8
- package/createBox.d.ts +5 -1
- package/createBox.js +5 -3
- package/createStyled.d.ts +16 -190
- package/createStyled.js +6 -2
- package/createTheme/createBreakpoints.d.ts +53 -6
- package/createTheme/createBreakpoints.js +3 -3
- package/cssVars/createCssVarsProvider.d.ts +131 -0
- package/cssVars/createCssVarsProvider.js +228 -0
- package/cssVars/createCssVarsProvider.spec.d.ts +1 -0
- package/cssVars/cssVarsParser.d.ts +68 -0
- package/cssVars/cssVarsParser.js +156 -0
- package/cssVars/getInitColorSchemeScript.d.ts +12 -0
- package/cssVars/getInitColorSchemeScript.js +60 -0
- package/cssVars/index.d.ts +2 -0
- package/cssVars/index.js +15 -0
- package/cssVars/package.json +6 -0
- package/cssVars/useCurrentColorScheme.d.ts +50 -0
- package/cssVars/useCurrentColorScheme.js +235 -0
- package/display.js +1 -1
- package/esm/breakpoints.js +39 -8
- package/esm/createBox.js +5 -3
- package/esm/createStyled.js +5 -1
- package/esm/createTheme/createBreakpoints.js +2 -2
- package/esm/cssVars/createCssVarsProvider.js +207 -0
- package/esm/cssVars/cssVarsParser.js +141 -0
- package/esm/cssVars/getInitColorSchemeScript.js +42 -0
- package/esm/cssVars/index.js +1 -0
- package/esm/cssVars/useCurrentColorScheme.js +217 -0
- package/esm/index.js +2 -1
- package/esm/styleFunctionSx/extendSxProp.js +20 -1
- package/esm/styleFunctionSx/styleFunctionSx.js +45 -35
- package/flexbox.js +1 -1
- package/getThemeValue.js +1 -1
- package/grid.js +1 -1
- package/index.d.ts +6 -0
- package/index.js +77 -68
- package/legacy/breakpoints.js +39 -8
- package/legacy/createBox.js +6 -3
- package/legacy/createStyled.js +5 -1
- package/legacy/createTheme/createBreakpoints.js +2 -2
- package/legacy/cssVars/createCssVarsProvider.js +215 -0
- package/legacy/cssVars/cssVarsParser.js +153 -0
- package/legacy/cssVars/getInitColorSchemeScript.js +27 -0
- package/legacy/cssVars/index.js +1 -0
- package/legacy/cssVars/useCurrentColorScheme.js +231 -0
- package/legacy/index.js +3 -2
- package/legacy/styleFunctionSx/extendSxProp.js +21 -1
- package/legacy/styleFunctionSx/styleFunctionSx.js +44 -34
- package/modern/breakpoints.js +39 -8
- package/modern/createBox.js +5 -3
- package/modern/createStyled.js +5 -1
- package/modern/createTheme/createBreakpoints.js +2 -2
- package/modern/cssVars/createCssVarsProvider.js +207 -0
- package/modern/cssVars/cssVarsParser.js +141 -0
- package/modern/cssVars/getInitColorSchemeScript.js +42 -0
- package/modern/cssVars/index.js +1 -0
- package/modern/cssVars/useCurrentColorScheme.js +217 -0
- package/modern/index.js +3 -2
- package/modern/styleFunctionSx/extendSxProp.js +20 -1
- package/modern/styleFunctionSx/styleFunctionSx.js +45 -35
- package/package.json +8 -8
- package/palette.js +1 -1
- package/positions.js +1 -1
- package/sizing.js +1 -1
- package/spacing.js +3 -3
- package/style.d.ts +2 -2
- package/style.js +1 -1
- package/styleFunctionSx/extendSxProp.js +21 -1
- package/styleFunctionSx/styleFunctionSx.d.ts +7 -1
- package/styleFunctionSx/styleFunctionSx.js +46 -36
- package/styleFunctionSx/styleFunctionSx.spec.d.ts +1 -0
- package/typography.js +1 -1
- package/useTheme.js +1 -1
|
@@ -13,12 +13,12 @@ export default function createBreakpoints(breakpoints) {
|
|
|
13
13
|
xs: 0,
|
|
14
14
|
// phone
|
|
15
15
|
sm: 600,
|
|
16
|
-
//
|
|
16
|
+
// tablet
|
|
17
17
|
md: 900,
|
|
18
18
|
// small laptop
|
|
19
19
|
lg: 1200,
|
|
20
20
|
// desktop
|
|
21
|
-
xl: 1536 // large
|
|
21
|
+
xl: 1536 // large screen
|
|
22
22
|
|
|
23
23
|
},
|
|
24
24
|
unit = 'px',
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
+
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
|
3
|
+
import { formatMuiErrorMessage as _formatMuiErrorMessage } from "@mui/utils";
|
|
4
|
+
const _excluded = ["colorSchemes"],
|
|
5
|
+
_excluded2 = ["colorSchemes"];
|
|
6
|
+
import * as React from 'react';
|
|
7
|
+
import PropTypes from 'prop-types';
|
|
8
|
+
import { GlobalStyles } from '@mui/styled-engine';
|
|
9
|
+
import { deepmerge } from '@mui/utils';
|
|
10
|
+
import cssVarsParser from './cssVarsParser';
|
|
11
|
+
import ThemeProvider from '../ThemeProvider';
|
|
12
|
+
import getInitColorSchemeScript, { DEFAULT_ATTRIBUTE, DEFAULT_MODE_STORAGE_KEY } from './getInitColorSchemeScript';
|
|
13
|
+
import useCurrentColorScheme from './useCurrentColorScheme';
|
|
14
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
15
|
+
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
16
|
+
export default function createCssVarsProvider(options) {
|
|
17
|
+
const {
|
|
18
|
+
theme: baseTheme = {},
|
|
19
|
+
defaultMode: desisgnSystemMode = 'light',
|
|
20
|
+
defaultColorScheme: designSystemColorScheme,
|
|
21
|
+
prefix: designSystemPrefix = '',
|
|
22
|
+
shouldSkipGeneratingVar
|
|
23
|
+
} = options;
|
|
24
|
+
|
|
25
|
+
if (!baseTheme.colorSchemes || typeof designSystemColorScheme === 'string' && !baseTheme.colorSchemes[designSystemColorScheme] || typeof designSystemColorScheme === 'object' && !baseTheme.colorSchemes[designSystemColorScheme?.light] || typeof designSystemColorScheme === 'object' && !baseTheme.colorSchemes[designSystemColorScheme?.dark]) {
|
|
26
|
+
console.error(`MUI: \`${designSystemColorScheme}\` does not exist in \`theme.colorSchemes\`.`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const ColorSchemeContext = /*#__PURE__*/React.createContext(undefined);
|
|
30
|
+
|
|
31
|
+
const useColorScheme = () => {
|
|
32
|
+
const value = React.useContext(ColorSchemeContext);
|
|
33
|
+
|
|
34
|
+
if (!value) {
|
|
35
|
+
throw new Error(process.env.NODE_ENV !== "production" ? `MUI: \`useColorScheme\` must be called under <CssVarsProvider />` : _formatMuiErrorMessage(19));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return value;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
function CssVarsProvider({
|
|
42
|
+
children,
|
|
43
|
+
theme: themeProp = {},
|
|
44
|
+
prefix = designSystemPrefix,
|
|
45
|
+
modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
|
|
46
|
+
attribute = DEFAULT_ATTRIBUTE,
|
|
47
|
+
defaultMode = desisgnSystemMode,
|
|
48
|
+
defaultColorScheme = designSystemColorScheme
|
|
49
|
+
}) {
|
|
50
|
+
const {
|
|
51
|
+
colorSchemes: baseColorSchemes = {}
|
|
52
|
+
} = baseTheme,
|
|
53
|
+
restBaseTheme = _objectWithoutPropertiesLoose(baseTheme, _excluded);
|
|
54
|
+
|
|
55
|
+
const {
|
|
56
|
+
colorSchemes: colorSchemesProp = {}
|
|
57
|
+
} = themeProp,
|
|
58
|
+
restThemeProp = _objectWithoutPropertiesLoose(themeProp, _excluded2);
|
|
59
|
+
|
|
60
|
+
let mergedTheme = deepmerge(restBaseTheme, restThemeProp);
|
|
61
|
+
const colorSchemes = deepmerge(baseColorSchemes, colorSchemesProp);
|
|
62
|
+
const allColorSchemes = Object.keys(colorSchemes);
|
|
63
|
+
const defaultLightColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.light;
|
|
64
|
+
const defaultDarkColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.dark;
|
|
65
|
+
const {
|
|
66
|
+
mode,
|
|
67
|
+
setMode,
|
|
68
|
+
lightColorScheme,
|
|
69
|
+
darkColorScheme,
|
|
70
|
+
colorScheme,
|
|
71
|
+
setColorScheme
|
|
72
|
+
} = useCurrentColorScheme({
|
|
73
|
+
supportedColorSchemes: allColorSchemes,
|
|
74
|
+
defaultLightColorScheme,
|
|
75
|
+
defaultDarkColorScheme,
|
|
76
|
+
modeStorageKey,
|
|
77
|
+
defaultMode
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const resolvedColorScheme = (() => {
|
|
81
|
+
if (!colorScheme) {
|
|
82
|
+
// This scope occurs on the server
|
|
83
|
+
if (defaultMode === 'dark') {
|
|
84
|
+
return defaultDarkColorScheme;
|
|
85
|
+
} // use light color scheme, if default mode is 'light' | 'auto'
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
return defaultLightColorScheme;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return colorScheme;
|
|
92
|
+
})();
|
|
93
|
+
|
|
94
|
+
const {
|
|
95
|
+
css: rootCss,
|
|
96
|
+
vars: rootVars
|
|
97
|
+
} = cssVarsParser(mergedTheme, {
|
|
98
|
+
prefix,
|
|
99
|
+
basePrefix: designSystemPrefix,
|
|
100
|
+
shouldSkipGeneratingVar
|
|
101
|
+
});
|
|
102
|
+
mergedTheme = _extends({}, mergedTheme, colorSchemes[resolvedColorScheme], {
|
|
103
|
+
vars: rootVars
|
|
104
|
+
});
|
|
105
|
+
const styleSheet = {};
|
|
106
|
+
Object.entries(colorSchemes).forEach(([key, scheme]) => {
|
|
107
|
+
const {
|
|
108
|
+
css,
|
|
109
|
+
vars
|
|
110
|
+
} = cssVarsParser(scheme, {
|
|
111
|
+
prefix,
|
|
112
|
+
basePrefix: designSystemPrefix,
|
|
113
|
+
shouldSkipGeneratingVar
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
if (key === resolvedColorScheme) {
|
|
117
|
+
mergedTheme.vars = _extends({}, mergedTheme.vars, vars);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const resolvedDefaultColorScheme = (() => {
|
|
121
|
+
if (typeof defaultColorScheme === 'string') {
|
|
122
|
+
return defaultColorScheme;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (defaultMode === 'dark') {
|
|
126
|
+
return defaultColorScheme.dark;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return defaultColorScheme.light;
|
|
130
|
+
})();
|
|
131
|
+
|
|
132
|
+
if (key === resolvedDefaultColorScheme) {
|
|
133
|
+
styleSheet[':root'] = css;
|
|
134
|
+
} else {
|
|
135
|
+
styleSheet[`[${attribute}="${key}"]`] = css;
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
React.useEffect(() => {
|
|
139
|
+
if (colorScheme) {
|
|
140
|
+
document.body.setAttribute(attribute, colorScheme);
|
|
141
|
+
}
|
|
142
|
+
}, [colorScheme, attribute]);
|
|
143
|
+
return /*#__PURE__*/_jsxs(ColorSchemeContext.Provider, {
|
|
144
|
+
value: {
|
|
145
|
+
mode,
|
|
146
|
+
setMode,
|
|
147
|
+
lightColorScheme,
|
|
148
|
+
darkColorScheme,
|
|
149
|
+
colorScheme,
|
|
150
|
+
setColorScheme,
|
|
151
|
+
allColorSchemes
|
|
152
|
+
},
|
|
153
|
+
children: [/*#__PURE__*/_jsx(GlobalStyles, {
|
|
154
|
+
styles: {
|
|
155
|
+
':root': rootCss
|
|
156
|
+
}
|
|
157
|
+
}), /*#__PURE__*/_jsx(GlobalStyles, {
|
|
158
|
+
styles: styleSheet
|
|
159
|
+
}), /*#__PURE__*/_jsx(ThemeProvider, {
|
|
160
|
+
theme: mergedTheme,
|
|
161
|
+
children: children
|
|
162
|
+
})]
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
process.env.NODE_ENV !== "production" ? CssVarsProvider.propTypes = {
|
|
167
|
+
/**
|
|
168
|
+
* The body attribute name to attach colorScheme.
|
|
169
|
+
*/
|
|
170
|
+
attribute: PropTypes.string,
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Your component tree.
|
|
174
|
+
*/
|
|
175
|
+
children: PropTypes.node,
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The initial color scheme used.
|
|
179
|
+
*/
|
|
180
|
+
defaultColorScheme: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* The initial mode used.
|
|
184
|
+
*/
|
|
185
|
+
defaultMode: PropTypes.string,
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* The key in the local storage used to store current color scheme.
|
|
189
|
+
*/
|
|
190
|
+
modeStorageKey: PropTypes.string,
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* css variable prefix
|
|
194
|
+
*/
|
|
195
|
+
prefix: PropTypes.string,
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* The calculated theme object that will be passed through context.
|
|
199
|
+
*/
|
|
200
|
+
theme: PropTypes.object
|
|
201
|
+
} : void 0;
|
|
202
|
+
return {
|
|
203
|
+
CssVarsProvider,
|
|
204
|
+
useColorScheme,
|
|
205
|
+
getInitColorSchemeScript
|
|
206
|
+
};
|
|
207
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* This function create an object from keys, value and then assign to target
|
|
5
|
+
*
|
|
6
|
+
* @param {Object} obj : the target object to be assigned
|
|
7
|
+
* @param {string[]} keys
|
|
8
|
+
* @param {string | number} value
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const source = {}
|
|
12
|
+
* assignNestedKeys(source, ['palette', 'primary'], 'var(--palette-primary)')
|
|
13
|
+
* console.log(source) // { palette: { primary: 'var(--palette-primary)' } }
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const source = { palette: { primary: 'var(--palette-primary)' } }
|
|
17
|
+
* assignNestedKeys(source, ['palette', 'secondary'], 'var(--palette-secondary)')
|
|
18
|
+
* console.log(source) // { palette: { primary: 'var(--palette-primary)', secondary: 'var(--palette-secondary)' } }
|
|
19
|
+
*/
|
|
20
|
+
export const assignNestedKeys = (obj, keys, value) => {
|
|
21
|
+
let temp = obj;
|
|
22
|
+
keys.forEach((k, index) => {
|
|
23
|
+
if (index === keys.length - 1) {
|
|
24
|
+
if (temp && typeof temp === 'object') {
|
|
25
|
+
temp[k] = value;
|
|
26
|
+
}
|
|
27
|
+
} else if (temp && typeof temp === 'object') {
|
|
28
|
+
if (!temp[k]) {
|
|
29
|
+
temp[k] = {};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
temp = temp[k];
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
*
|
|
38
|
+
* @param {Object} obj : source object
|
|
39
|
+
* @param {Function} callback : a function that will be called when
|
|
40
|
+
* - the deepest key in source object is reached
|
|
41
|
+
* - the value of the deepest key is NOT `undefined` | `null`
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* walkObjectDeep({ palette: { primary: { main: '#000000' } } }, console.log)
|
|
45
|
+
* // ['palette', 'primary', 'main'] '#000000'
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
export const walkObjectDeep = (obj, callback) => {
|
|
49
|
+
function recurse(object, parentKeys = []) {
|
|
50
|
+
Object.entries(object).forEach(([key, value]) => {
|
|
51
|
+
if (value !== undefined && value !== null) {
|
|
52
|
+
if (typeof value === 'object' && Object.keys(value).length > 0) {
|
|
53
|
+
recurse(value, [...parentKeys, key]);
|
|
54
|
+
} else {
|
|
55
|
+
callback([...parentKeys, key], value, object);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
recurse(obj);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const getCssValue = (keys, value) => {
|
|
65
|
+
if (typeof value === 'number') {
|
|
66
|
+
if (['lineHeight', 'fontWeight', 'opacity', 'zIndex'].some(prop => keys.includes(prop))) {
|
|
67
|
+
// css property that are unitless
|
|
68
|
+
return value;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return `${value}px`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return value;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* a function that parse theme and return { css, vars }
|
|
78
|
+
*
|
|
79
|
+
* @param {Object} theme
|
|
80
|
+
* @param {{
|
|
81
|
+
* prefix?: string,
|
|
82
|
+
* basePrefix?: string,
|
|
83
|
+
* shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean
|
|
84
|
+
* }} options.
|
|
85
|
+
* `basePrefix`: defined by design system.
|
|
86
|
+
* `prefix`: defined by application
|
|
87
|
+
*
|
|
88
|
+
* This function also mutate the string value of theme input by replacing `basePrefix` (if existed) with `prefix`
|
|
89
|
+
*
|
|
90
|
+
* @returns {{ css: Object, vars: Object }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme)
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* const { css, vars } = parser({
|
|
94
|
+
* fontSize: 12,
|
|
95
|
+
* lineHeight: 1.2,
|
|
96
|
+
* palette: { primary: { 500: '#000000' } }
|
|
97
|
+
* })
|
|
98
|
+
*
|
|
99
|
+
* console.log(css) // { '--fontSize': '12px', '--lineHeight': 1.2, '--palette-primary-500': '#000000' }
|
|
100
|
+
* console.log(vars) // { fontSize: '--fontSize', lineHeight: '--lineHeight', palette: { primary: { 500: 'var(--palette-primary-500)' } } }
|
|
101
|
+
*/
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
export default function cssVarsParser(theme, options) {
|
|
105
|
+
const clonedTheme = _extends({}, theme);
|
|
106
|
+
|
|
107
|
+
delete clonedTheme.vars; // remove 'vars' from the structure
|
|
108
|
+
|
|
109
|
+
const {
|
|
110
|
+
prefix,
|
|
111
|
+
basePrefix = '',
|
|
112
|
+
shouldSkipGeneratingVar
|
|
113
|
+
} = options || {};
|
|
114
|
+
const css = {};
|
|
115
|
+
const vars = {};
|
|
116
|
+
walkObjectDeep(clonedTheme, (keys, val, scope) => {
|
|
117
|
+
if (typeof val === 'string' || typeof val === 'number') {
|
|
118
|
+
let value = val;
|
|
119
|
+
|
|
120
|
+
if (typeof value === 'string' && value.startsWith('var')) {
|
|
121
|
+
// replace the value of the `scope` object with the prefix or remove basePrefix from the value
|
|
122
|
+
value = prefix ? value.replace(basePrefix, prefix) : value.replace(`${basePrefix}-`, ''); // scope is the deepest object in the tree, keys is the theme path keys
|
|
123
|
+
|
|
124
|
+
scope[keys.slice(-1)[0]] = value;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (!shouldSkipGeneratingVar || shouldSkipGeneratingVar && !shouldSkipGeneratingVar(keys, value)) {
|
|
128
|
+
// only create css & var if `shouldSkipGeneratingVar` return false
|
|
129
|
+
const cssVar = `--${prefix ? `${prefix}-` : ''}${keys.join('-')}`;
|
|
130
|
+
Object.assign(css, {
|
|
131
|
+
[cssVar]: getCssValue(keys, value)
|
|
132
|
+
});
|
|
133
|
+
assignNestedKeys(vars, keys, `var(${cssVar})`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
return {
|
|
138
|
+
css,
|
|
139
|
+
vars
|
|
140
|
+
};
|
|
141
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
export const DEFAULT_MODE_STORAGE_KEY = 'mui-mode';
|
|
4
|
+
export const DEFAULT_COLOR_SCHEME_STORAGE_KEY = 'mui-color-scheme';
|
|
5
|
+
export const DEFAULT_ATTRIBUTE = 'data-mui-color-scheme';
|
|
6
|
+
export default function getInitColorSchemeScript(options) {
|
|
7
|
+
const {
|
|
8
|
+
defaultMode = 'light',
|
|
9
|
+
defaultLightColorScheme = 'light',
|
|
10
|
+
defaultDarkColorScheme = 'dark',
|
|
11
|
+
modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
|
|
12
|
+
colorSchemeStorageKey = DEFAULT_COLOR_SCHEME_STORAGE_KEY,
|
|
13
|
+
attribute = DEFAULT_ATTRIBUTE
|
|
14
|
+
} = options || {};
|
|
15
|
+
return /*#__PURE__*/_jsx("script", {
|
|
16
|
+
// eslint-disable-next-line react/no-danger
|
|
17
|
+
dangerouslySetInnerHTML: {
|
|
18
|
+
__html: `(function() { try {
|
|
19
|
+
var mode = localStorage.getItem('${modeStorageKey}');
|
|
20
|
+
var colorScheme = '';
|
|
21
|
+
if (mode === 'system' || (!mode && ${defaultMode} === 'system')) {
|
|
22
|
+
// handle system mode
|
|
23
|
+
var mql = window.matchMedia('(prefers-color-scheme: dark)');
|
|
24
|
+
if (mql.matches) {
|
|
25
|
+
colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || ${defaultLightColorScheme};
|
|
26
|
+
} else {
|
|
27
|
+
colorScheme = localStorage.getItem('${colorSchemeStorageKey}-light') || ${defaultDarkColorScheme};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (mode === 'light') {
|
|
31
|
+
colorScheme = localStorage.getItem('${colorSchemeStorageKey}-light') || ${defaultLightColorScheme};
|
|
32
|
+
}
|
|
33
|
+
if (mode === 'dark') {
|
|
34
|
+
colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || ${defaultDarkColorScheme};
|
|
35
|
+
}
|
|
36
|
+
if (colorScheme) {
|
|
37
|
+
document.body.setAttribute('${attribute}', colorScheme);
|
|
38
|
+
}
|
|
39
|
+
} catch (e) {} })();`
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from './createCssVarsProvider';
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { DEFAULT_MODE_STORAGE_KEY, DEFAULT_COLOR_SCHEME_STORAGE_KEY } from './getInitColorSchemeScript';
|
|
4
|
+
export function getSystemMode(mode) {
|
|
5
|
+
if (typeof window !== 'undefined' && mode === 'system') {
|
|
6
|
+
const mql = window.matchMedia('(prefers-color-scheme: dark)');
|
|
7
|
+
|
|
8
|
+
if (mql.matches) {
|
|
9
|
+
return 'dark';
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return 'light';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function processState(state, callback) {
|
|
19
|
+
if (state.mode === 'light' || state.mode === 'system' && state.systemMode === 'light') {
|
|
20
|
+
return callback('light');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (state.mode === 'dark' || state.mode === 'system' && state.systemMode === 'dark') {
|
|
24
|
+
return callback('dark');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function getColorScheme(state) {
|
|
31
|
+
return processState(state, mode => {
|
|
32
|
+
if (mode === 'light') {
|
|
33
|
+
return state.lightColorScheme;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (mode === 'dark') {
|
|
37
|
+
return state.darkColorScheme;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return undefined;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function resolveValue(key, defaultValue) {
|
|
45
|
+
if (typeof window === 'undefined') {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let value;
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
value = localStorage.getItem(key) || undefined;
|
|
53
|
+
} catch (e) {// Unsupported
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return value || defaultValue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export default function useCurrentColorScheme(options) {
|
|
60
|
+
const {
|
|
61
|
+
defaultMode = 'light',
|
|
62
|
+
defaultLightColorScheme,
|
|
63
|
+
defaultDarkColorScheme,
|
|
64
|
+
supportedColorSchemes = [],
|
|
65
|
+
modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
|
|
66
|
+
colorSchemeStorageKey = DEFAULT_COLOR_SCHEME_STORAGE_KEY
|
|
67
|
+
} = options;
|
|
68
|
+
const joinedColorSchemes = supportedColorSchemes.join(',');
|
|
69
|
+
const [state, setState] = React.useState(() => {
|
|
70
|
+
const initialMode = resolveValue(modeStorageKey, defaultMode);
|
|
71
|
+
return {
|
|
72
|
+
mode: initialMode,
|
|
73
|
+
systemMode: getSystemMode(initialMode),
|
|
74
|
+
lightColorScheme: resolveValue(`${colorSchemeStorageKey}-light`) || defaultLightColorScheme,
|
|
75
|
+
darkColorScheme: resolveValue(`${colorSchemeStorageKey}-dark`) || defaultDarkColorScheme
|
|
76
|
+
};
|
|
77
|
+
});
|
|
78
|
+
const colorScheme = getColorScheme(state);
|
|
79
|
+
const setMode = React.useCallback(mode => {
|
|
80
|
+
setState(currentState => {
|
|
81
|
+
const newMode = !mode ? defaultMode : mode;
|
|
82
|
+
|
|
83
|
+
if (typeof localStorage !== 'undefined') {
|
|
84
|
+
localStorage.setItem(modeStorageKey, newMode);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return _extends({}, currentState, {
|
|
88
|
+
mode: newMode,
|
|
89
|
+
systemMode: getSystemMode(newMode)
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
}, [modeStorageKey, defaultMode]);
|
|
93
|
+
const setColorScheme = React.useCallback(value => {
|
|
94
|
+
if (!value || typeof value === 'string') {
|
|
95
|
+
if (value && !supportedColorSchemes.includes(value)) {
|
|
96
|
+
console.error(`\`${value}\` does not exist in \`theme.colorSchemes\`.`);
|
|
97
|
+
} else {
|
|
98
|
+
setState(currentState => {
|
|
99
|
+
const newState = _extends({}, currentState);
|
|
100
|
+
|
|
101
|
+
if (!value) {
|
|
102
|
+
// reset to default color scheme
|
|
103
|
+
newState.lightColorScheme = defaultLightColorScheme;
|
|
104
|
+
newState.darkColorScheme = defaultDarkColorScheme;
|
|
105
|
+
return newState;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
processState(currentState, mode => {
|
|
109
|
+
localStorage.setItem(`${colorSchemeStorageKey}-${mode}`, value);
|
|
110
|
+
|
|
111
|
+
if (mode === 'light') {
|
|
112
|
+
newState.lightColorScheme = value;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (mode === 'dark') {
|
|
116
|
+
newState.darkColorScheme = value;
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
return newState;
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
} else if (value.light && !supportedColorSchemes.includes(value.light) || value.dark && !supportedColorSchemes.includes(value.dark)) {
|
|
123
|
+
console.error(`\`${value}\` does not exist in \`theme.colorSchemes\`.`);
|
|
124
|
+
} else {
|
|
125
|
+
setState(currentState => {
|
|
126
|
+
const newState = _extends({}, currentState);
|
|
127
|
+
|
|
128
|
+
if (value.light || value.light === null) {
|
|
129
|
+
newState.lightColorScheme = value.light === null ? defaultLightColorScheme : value.light;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (value.dark || value.dark === null) {
|
|
133
|
+
newState.darkColorScheme = value.dark === null ? defaultDarkColorScheme : value.dark;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return newState;
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
if (value.light) {
|
|
140
|
+
localStorage.setItem(`${colorSchemeStorageKey}-light`, value.light);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (value.dark) {
|
|
144
|
+
localStorage.setItem(`${colorSchemeStorageKey}-dark`, value.dark);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}, [colorSchemeStorageKey, supportedColorSchemes, defaultLightColorScheme, defaultDarkColorScheme]);
|
|
148
|
+
const handleMediaQuery = React.useCallback(e => {
|
|
149
|
+
if (state.mode === 'system') {
|
|
150
|
+
setState(currentState => _extends({}, currentState, {
|
|
151
|
+
systemMode: e.matches ? 'dark' : 'light'
|
|
152
|
+
}));
|
|
153
|
+
}
|
|
154
|
+
}, [state.mode]); // Ref hack to avoid adding handleMediaQuery as a dep
|
|
155
|
+
|
|
156
|
+
const mediaListener = React.useRef(handleMediaQuery);
|
|
157
|
+
mediaListener.current = handleMediaQuery;
|
|
158
|
+
React.useEffect(() => {
|
|
159
|
+
const handler = (...args) => mediaListener.current(...args); // Always listen to System preference
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
const media = window.matchMedia('(prefers-color-scheme: dark)'); // Intentionally use deprecated listener methods to support iOS & old browsers
|
|
163
|
+
|
|
164
|
+
media.addListener(handler);
|
|
165
|
+
handler(media);
|
|
166
|
+
return () => media.removeListener(handler);
|
|
167
|
+
}, []); // Save mode, lightColorScheme & darkColorScheme to localStorage
|
|
168
|
+
|
|
169
|
+
React.useEffect(() => {
|
|
170
|
+
if (state.mode) {
|
|
171
|
+
localStorage.setItem(modeStorageKey, state.mode);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
processState(state, mode => {
|
|
175
|
+
if (mode === 'light') {
|
|
176
|
+
localStorage.setItem(`${colorSchemeStorageKey}-light`, state.lightColorScheme);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (mode === 'dark') {
|
|
180
|
+
localStorage.setItem(`${colorSchemeStorageKey}-dark`, state.darkColorScheme);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}, [state, colorSchemeStorageKey, modeStorageKey]); // Handle when localStorage has changed
|
|
184
|
+
|
|
185
|
+
React.useEffect(() => {
|
|
186
|
+
const handleStorage = event => {
|
|
187
|
+
const value = event.newValue;
|
|
188
|
+
|
|
189
|
+
if (typeof event.key === 'string' && event.key.startsWith(colorSchemeStorageKey) && (!value || joinedColorSchemes.match(value))) {
|
|
190
|
+
// If the key is deleted, value will be null then reset color scheme to the default one.
|
|
191
|
+
if (event.key.endsWith('light')) {
|
|
192
|
+
setColorScheme({
|
|
193
|
+
light: value
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (event.key.endsWith('dark')) {
|
|
198
|
+
setColorScheme({
|
|
199
|
+
dark: value
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (event.key === modeStorageKey && (!value || ['light', 'dark', 'system'].includes(value))) {
|
|
205
|
+
setMode(value || defaultMode);
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
window.addEventListener('storage', handleStorage);
|
|
210
|
+
return () => window.removeEventListener('storage', handleStorage);
|
|
211
|
+
}, [setColorScheme, setMode, modeStorageKey, colorSchemeStorageKey, joinedColorSchemes, defaultMode]);
|
|
212
|
+
return _extends({}, state, {
|
|
213
|
+
colorScheme,
|
|
214
|
+
setMode,
|
|
215
|
+
setColorScheme
|
|
216
|
+
});
|
|
217
|
+
}
|
package/modern/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** @license MUI v5.0
|
|
1
|
+
/** @license MUI v5.1.0
|
|
2
2
|
*
|
|
3
3
|
* This source code is licensed under the MIT license found in the
|
|
4
4
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -41,4 +41,5 @@ export { default as useThemeProps, getThemeProps } from './useThemeProps';
|
|
|
41
41
|
export { default as useTheme } from './useTheme';
|
|
42
42
|
export { default as useThemeWithoutDefault } from './useThemeWithoutDefault';
|
|
43
43
|
export * from './colorManipulator';
|
|
44
|
-
export { default as ThemeProvider } from './ThemeProvider';
|
|
44
|
+
export { default as ThemeProvider } from './ThemeProvider';
|
|
45
|
+
export { default as unstable_createCssVarsProvider } from './cssVars/createCssVarsProvider';
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
2
|
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
|
3
3
|
const _excluded = ["sx"];
|
|
4
|
+
import { isPlainObject } from '@mui/utils';
|
|
4
5
|
import { propToStyleFunction } from '../getThemeValue';
|
|
5
6
|
|
|
6
7
|
const splitProps = props => {
|
|
@@ -28,7 +29,25 @@ export default function extendSxProp(props) {
|
|
|
28
29
|
systemProps,
|
|
29
30
|
otherProps
|
|
30
31
|
} = splitProps(other);
|
|
32
|
+
let finalSx;
|
|
33
|
+
|
|
34
|
+
if (Array.isArray(inSx)) {
|
|
35
|
+
finalSx = [systemProps, ...inSx];
|
|
36
|
+
} else if (typeof inSx === 'function') {
|
|
37
|
+
finalSx = (...args) => {
|
|
38
|
+
const result = inSx(...args);
|
|
39
|
+
|
|
40
|
+
if (!isPlainObject(result)) {
|
|
41
|
+
return systemProps;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return _extends({}, systemProps, result);
|
|
45
|
+
};
|
|
46
|
+
} else {
|
|
47
|
+
finalSx = _extends({}, systemProps, inSx);
|
|
48
|
+
}
|
|
49
|
+
|
|
31
50
|
return _extends({}, otherProps, {
|
|
32
|
-
sx:
|
|
51
|
+
sx: finalSx
|
|
33
52
|
});
|
|
34
53
|
}
|