@mui/system 6.0.0-beta.0 → 6.0.0-beta.3
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 +473 -31
- package/InitColorSchemeScript/InitColorSchemeScript.d.ts +3 -5
- package/InitColorSchemeScript/InitColorSchemeScript.js +26 -7
- package/Unstable_Grid/createGrid.js +2 -2
- package/createTheme/applyStyles.js +10 -4
- package/cssVars/createCssVarsProvider.d.ts +2 -11
- package/cssVars/createCssVarsProvider.js +33 -72
- package/cssVars/createCssVarsTheme.d.ts +2 -1
- package/cssVars/createCssVarsTheme.js +10 -2
- package/cssVars/getColorSchemeSelector.d.ts +1 -0
- package/cssVars/getColorSchemeSelector.js +26 -0
- package/cssVars/index.d.ts +1 -0
- package/cssVars/index.js +2 -1
- package/cssVars/prepareCssVars.d.ts +3 -2
- package/cssVars/prepareCssVars.js +58 -16
- package/cssVars/useCurrentColorScheme.d.ts +2 -2
- package/index.js +1 -1
- package/modern/InitColorSchemeScript/InitColorSchemeScript.js +26 -7
- package/modern/Unstable_Grid/createGrid.js +2 -2
- package/modern/createTheme/applyStyles.js +10 -4
- package/modern/cssVars/createCssVarsProvider.js +33 -72
- package/modern/cssVars/createCssVarsTheme.js +10 -2
- package/modern/cssVars/getColorSchemeSelector.js +26 -0
- package/modern/cssVars/index.js +2 -1
- package/modern/cssVars/prepareCssVars.js +58 -16
- package/modern/index.js +1 -1
- package/node/InitColorSchemeScript/InitColorSchemeScript.js +26 -7
- package/node/Unstable_Grid/createGrid.js +2 -2
- package/node/createTheme/applyStyles.js +11 -4
- package/node/cssVars/createCssVarsProvider.js +33 -72
- package/node/cssVars/createCssVarsTheme.js +10 -2
- package/node/cssVars/getColorSchemeSelector.js +32 -0
- package/node/cssVars/index.js +8 -1
- package/node/cssVars/prepareCssVars.js +61 -16
- package/node/index.js +1 -1
- package/package.json +7 -7
|
@@ -88,7 +88,7 @@ export default function createGrid(options = {}) {
|
|
|
88
88
|
rowSpacing: rowSpacingProp = spacingProp,
|
|
89
89
|
columnSpacing: columnSpacingProp = spacingProp,
|
|
90
90
|
unstable_level: level = 0,
|
|
91
|
-
...
|
|
91
|
+
...other
|
|
92
92
|
} = props;
|
|
93
93
|
const size = parseResponsiveProp(sizeProp, theme.breakpoints, val => val !== false);
|
|
94
94
|
const offset = parseResponsiveProp(offsetProp, theme.breakpoints);
|
|
@@ -115,7 +115,7 @@ export default function createGrid(options = {}) {
|
|
|
115
115
|
as: component,
|
|
116
116
|
ownerState: ownerState,
|
|
117
117
|
className: clsx(classes.root, className),
|
|
118
|
-
...
|
|
118
|
+
...other,
|
|
119
119
|
children: React.Children.map(children, child => {
|
|
120
120
|
if ( /*#__PURE__*/React.isValidElement(child) && isMuiElement(child, ['Grid'])) {
|
|
121
121
|
return /*#__PURE__*/React.cloneElement(child, {
|
|
@@ -59,10 +59,16 @@
|
|
|
59
59
|
export default function applyStyles(key, styles) {
|
|
60
60
|
// @ts-expect-error this is 'any' type
|
|
61
61
|
const theme = this;
|
|
62
|
-
if (theme.vars
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
62
|
+
if (theme.vars) {
|
|
63
|
+
if (!theme.colorSchemes?.[key] || typeof theme.getColorSchemeSelector !== 'function') {
|
|
64
|
+
return {};
|
|
65
|
+
}
|
|
66
|
+
// If CssVarsProvider is used as a provider, returns '*:where({selector}) &'
|
|
67
|
+
let selector = theme.getColorSchemeSelector(key);
|
|
68
|
+
if (selector.includes('data-') || selector.includes('.')) {
|
|
69
|
+
// '*' is required as a workaround for Emotion issue (https://github.com/emotion-js/emotion/issues/2836)
|
|
70
|
+
selector = `*:where(${selector.replace(/\s*&$/, '')}) &`;
|
|
71
|
+
}
|
|
66
72
|
return {
|
|
67
73
|
[selector]: styles
|
|
68
74
|
};
|
|
@@ -4,7 +4,7 @@ import PropTypes from 'prop-types';
|
|
|
4
4
|
import { GlobalStyles } from '@mui/styled-engine';
|
|
5
5
|
import { useTheme as muiUseTheme } from '@mui/private-theming';
|
|
6
6
|
import ThemeProvider from '../ThemeProvider';
|
|
7
|
-
import InitColorSchemeScript, {
|
|
7
|
+
import InitColorSchemeScript, { DEFAULT_COLOR_SCHEME_STORAGE_KEY, DEFAULT_MODE_STORAGE_KEY } from '../InitColorSchemeScript/InitColorSchemeScript';
|
|
8
8
|
import useCurrentColorScheme from './useCurrentColorScheme';
|
|
9
9
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
10
10
|
export const DISABLE_CSS_TRANSITION = '*{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}';
|
|
@@ -18,17 +18,12 @@ export default function createCssVarsProvider(options) {
|
|
|
18
18
|
* It should also ideally have a vars object created using `prepareCssVars`.
|
|
19
19
|
*/
|
|
20
20
|
theme: defaultTheme = {},
|
|
21
|
-
attribute: defaultAttribute = DEFAULT_ATTRIBUTE,
|
|
22
21
|
modeStorageKey: defaultModeStorageKey = DEFAULT_MODE_STORAGE_KEY,
|
|
23
22
|
colorSchemeStorageKey: defaultColorSchemeStorageKey = DEFAULT_COLOR_SCHEME_STORAGE_KEY,
|
|
24
|
-
defaultMode: designSystemMode = 'light',
|
|
25
|
-
defaultColorScheme: designSystemColorScheme,
|
|
26
23
|
disableTransitionOnChange: designSystemTransitionOnChange = false,
|
|
24
|
+
defaultColorScheme,
|
|
27
25
|
resolveTheme
|
|
28
26
|
} = options;
|
|
29
|
-
if (!defaultTheme.colorSchemes || typeof designSystemColorScheme === 'string' && !defaultTheme.colorSchemes[designSystemColorScheme] || typeof designSystemColorScheme === 'object' && !defaultTheme.colorSchemes[designSystemColorScheme?.light] || typeof designSystemColorScheme === 'object' && !defaultTheme.colorSchemes[designSystemColorScheme?.dark]) {
|
|
30
|
-
console.error(`MUI: \`${designSystemColorScheme}\` does not exist in \`theme.colorSchemes\`.`);
|
|
31
|
-
}
|
|
32
27
|
const ColorSchemeContext = /*#__PURE__*/React.createContext(undefined);
|
|
33
28
|
if (process.env.NODE_ENV !== 'production') {
|
|
34
29
|
ColorSchemeContext.displayName = 'ColorSchemeContext';
|
|
@@ -46,14 +41,10 @@ export default function createCssVarsProvider(options) {
|
|
|
46
41
|
theme: themeProp = defaultTheme,
|
|
47
42
|
modeStorageKey = defaultModeStorageKey,
|
|
48
43
|
colorSchemeStorageKey = defaultColorSchemeStorageKey,
|
|
49
|
-
attribute = defaultAttribute,
|
|
50
|
-
defaultMode = designSystemMode,
|
|
51
|
-
defaultColorScheme = designSystemColorScheme,
|
|
52
44
|
disableTransitionOnChange = designSystemTransitionOnChange,
|
|
53
45
|
storageWindow = typeof window === 'undefined' ? undefined : window,
|
|
54
46
|
documentNode = typeof document === 'undefined' ? undefined : document,
|
|
55
47
|
colorSchemeNode = typeof document === 'undefined' ? undefined : document.documentElement,
|
|
56
|
-
colorSchemeSelector = ':root',
|
|
57
48
|
disableNestedContext = false,
|
|
58
49
|
disableStyleSheetGeneration = false
|
|
59
50
|
} = props;
|
|
@@ -68,9 +59,11 @@ export default function createCssVarsProvider(options) {
|
|
|
68
59
|
cssVarPrefix,
|
|
69
60
|
...restThemeProp
|
|
70
61
|
} = scopedTheme || themeProp;
|
|
71
|
-
const
|
|
62
|
+
const joinedColorSchemes = Object.keys(colorSchemes).filter(k => !!colorSchemes[k]).join(',');
|
|
63
|
+
const allColorSchemes = React.useMemo(() => joinedColorSchemes.split(','), [joinedColorSchemes]);
|
|
72
64
|
const defaultLightColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.light;
|
|
73
65
|
const defaultDarkColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.dark;
|
|
66
|
+
const defaultMode = colorSchemes[defaultLightColorScheme] && colorSchemes[defaultDarkColorScheme] ? 'system' : colorSchemes[restThemeProp.defaultColorScheme]?.palette?.mode;
|
|
74
67
|
|
|
75
68
|
// 1. Get the data about the `mode`, `colorScheme`, and setter functions.
|
|
76
69
|
const {
|
|
@@ -96,27 +89,9 @@ export default function createCssVarsProvider(options) {
|
|
|
96
89
|
mode = ctx.mode;
|
|
97
90
|
colorScheme = ctx.colorScheme;
|
|
98
91
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
// This scope occurs on the server
|
|
104
|
-
if (defaultMode === 'system') {
|
|
105
|
-
return designSystemMode;
|
|
106
|
-
}
|
|
107
|
-
return defaultMode;
|
|
108
|
-
})();
|
|
109
|
-
const calculatedColorScheme = (() => {
|
|
110
|
-
if (!colorScheme) {
|
|
111
|
-
// This scope occurs on the server
|
|
112
|
-
if (calculatedMode === 'dark') {
|
|
113
|
-
return defaultDarkColorScheme;
|
|
114
|
-
}
|
|
115
|
-
// use light color scheme, if default mode is 'light' | 'system'
|
|
116
|
-
return defaultLightColorScheme;
|
|
117
|
-
}
|
|
118
|
-
return colorScheme;
|
|
119
|
-
})();
|
|
92
|
+
|
|
93
|
+
// `colorScheme` is undefined on the server
|
|
94
|
+
const calculatedColorScheme = colorScheme || restThemeProp.defaultColorScheme;
|
|
120
95
|
|
|
121
96
|
// 2. get the `vars` object that refers to the CSS custom properties
|
|
122
97
|
const themeVars = restThemeProp.generateThemeVars?.() || restThemeProp.vars;
|
|
@@ -153,30 +128,34 @@ export default function createCssVarsProvider(options) {
|
|
|
153
128
|
}
|
|
154
129
|
}
|
|
155
130
|
});
|
|
156
|
-
const resolvedDefaultColorScheme = (() => {
|
|
157
|
-
if (typeof defaultColorScheme === 'string') {
|
|
158
|
-
return defaultColorScheme;
|
|
159
|
-
}
|
|
160
|
-
if (defaultMode === 'dark') {
|
|
161
|
-
return defaultColorScheme.dark;
|
|
162
|
-
}
|
|
163
|
-
return defaultColorScheme.light;
|
|
164
|
-
})();
|
|
165
|
-
themeProp.defaultColorScheme = resolvedDefaultColorScheme;
|
|
166
|
-
themeProp.colorSchemeSelector = colorSchemeSelector;
|
|
167
|
-
themeProp.attribute = attribute;
|
|
168
|
-
if (!theme.getColorSchemeSelector) {
|
|
169
|
-
theme.getColorSchemeSelector = targetColorScheme => `[${attribute}="${targetColorScheme}"] &`;
|
|
170
|
-
}
|
|
171
131
|
|
|
172
132
|
// 5. Declaring effects
|
|
173
133
|
// 5.1 Updates the selector value to use the current color scheme which tells CSS to use the proper stylesheet.
|
|
134
|
+
const colorSchemeSelector = restThemeProp.colorSchemeSelector;
|
|
174
135
|
React.useEffect(() => {
|
|
175
|
-
if (colorScheme && colorSchemeNode) {
|
|
176
|
-
|
|
177
|
-
|
|
136
|
+
if (colorScheme && colorSchemeNode && colorSchemeSelector && colorSchemeSelector !== 'media') {
|
|
137
|
+
const selector = colorSchemeSelector.replace('%s', colorScheme);
|
|
138
|
+
if (selector.startsWith('.')) {
|
|
139
|
+
colorSchemeNode.classList.remove(...allColorSchemes.map(scheme => colorSchemeSelector.substring(1).replace('%s', scheme)));
|
|
140
|
+
colorSchemeNode.classList.add(selector.substring(1));
|
|
141
|
+
} else {
|
|
142
|
+
const matches = selector.match(/\[([^\]]+)\]/);
|
|
143
|
+
if (matches) {
|
|
144
|
+
const [attr, value] = matches[1].split('=');
|
|
145
|
+
if (!value) {
|
|
146
|
+
// for attributes like `data-theme-dark`, `data-theme-light`
|
|
147
|
+
// remove all the existing data attributes before setting the new one
|
|
148
|
+
allColorSchemes.forEach(scheme => {
|
|
149
|
+
colorSchemeNode.removeAttribute(attr.replace(colorScheme, scheme));
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
colorSchemeNode.setAttribute(attr, value ? value.replace(/"|'/g, '') : '');
|
|
153
|
+
} else {
|
|
154
|
+
colorSchemeNode.setAttribute(selector, colorScheme);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
178
157
|
}
|
|
179
|
-
}, [colorScheme,
|
|
158
|
+
}, [colorScheme, colorSchemeSelector, colorSchemeNode, allColorSchemes]);
|
|
180
159
|
|
|
181
160
|
// 5.2 Remove the CSS transition when color scheme changes to create instant experience.
|
|
182
161
|
// credit: https://github.com/pacocoursey/next-themes/blob/b5c2bad50de2d61ad7b52a9c5cdc801a78507d7a/index.tsx#L313
|
|
@@ -237,10 +216,6 @@ export default function createCssVarsProvider(options) {
|
|
|
237
216
|
});
|
|
238
217
|
}
|
|
239
218
|
process.env.NODE_ENV !== "production" ? CssVarsProvider.propTypes = {
|
|
240
|
-
/**
|
|
241
|
-
* The body attribute name to attach colorScheme.
|
|
242
|
-
*/
|
|
243
|
-
attribute: PropTypes.string,
|
|
244
219
|
/**
|
|
245
220
|
* The component tree.
|
|
246
221
|
*/
|
|
@@ -249,22 +224,10 @@ export default function createCssVarsProvider(options) {
|
|
|
249
224
|
* The node used to attach the color-scheme attribute
|
|
250
225
|
*/
|
|
251
226
|
colorSchemeNode: PropTypes.any,
|
|
252
|
-
/**
|
|
253
|
-
* The CSS selector for attaching the generated custom properties
|
|
254
|
-
*/
|
|
255
|
-
colorSchemeSelector: PropTypes.string,
|
|
256
227
|
/**
|
|
257
228
|
* localStorage key used to store `colorScheme`
|
|
258
229
|
*/
|
|
259
230
|
colorSchemeStorageKey: PropTypes.string,
|
|
260
|
-
/**
|
|
261
|
-
* The initial color scheme used.
|
|
262
|
-
*/
|
|
263
|
-
defaultColorScheme: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
|
|
264
|
-
/**
|
|
265
|
-
* The initial mode used.
|
|
266
|
-
*/
|
|
267
|
-
defaultMode: PropTypes.string,
|
|
268
231
|
/**
|
|
269
232
|
* If `true`, the provider creates its own context and generate stylesheet as if it is a root `CssVarsProvider`.
|
|
270
233
|
*/
|
|
@@ -297,12 +260,10 @@ export default function createCssVarsProvider(options) {
|
|
|
297
260
|
*/
|
|
298
261
|
theme: PropTypes.object
|
|
299
262
|
} : void 0;
|
|
300
|
-
const defaultLightColorScheme = typeof
|
|
301
|
-
const defaultDarkColorScheme = typeof
|
|
263
|
+
const defaultLightColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.light;
|
|
264
|
+
const defaultDarkColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.dark;
|
|
302
265
|
const getInitColorSchemeScript = params => InitColorSchemeScript({
|
|
303
|
-
attribute: defaultAttribute,
|
|
304
266
|
colorSchemeStorageKey: defaultColorSchemeStorageKey,
|
|
305
|
-
defaultMode: designSystemMode,
|
|
306
267
|
defaultLightColorScheme,
|
|
307
268
|
defaultDarkColorScheme,
|
|
308
269
|
modeStorageKey: defaultModeStorageKey,
|
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
import prepareCssVars from './prepareCssVars';
|
|
2
|
-
|
|
2
|
+
import { createGetColorSchemeSelector } from './getColorSchemeSelector';
|
|
3
|
+
import { DEFAULT_ATTRIBUTE } from '../InitColorSchemeScript/InitColorSchemeScript';
|
|
4
|
+
function createCssVarsTheme({
|
|
5
|
+
colorSchemeSelector = `[${DEFAULT_ATTRIBUTE}="%s"]`,
|
|
6
|
+
...theme
|
|
7
|
+
}) {
|
|
3
8
|
const output = theme;
|
|
4
9
|
const result = prepareCssVars(output, {
|
|
5
10
|
...theme,
|
|
6
|
-
prefix: theme.cssVarPrefix
|
|
11
|
+
prefix: theme.cssVarPrefix,
|
|
12
|
+
colorSchemeSelector
|
|
7
13
|
});
|
|
8
14
|
output.vars = result.vars;
|
|
9
15
|
output.generateThemeVars = result.generateThemeVars;
|
|
10
16
|
output.generateStyleSheets = result.generateStyleSheets;
|
|
17
|
+
output.colorSchemeSelector = colorSchemeSelector;
|
|
18
|
+
output.getColorSchemeSelector = createGetColorSchemeSelector(colorSchemeSelector);
|
|
11
19
|
return output;
|
|
12
20
|
}
|
|
13
21
|
export default createCssVarsTheme;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/* eslint-disable import/prefer-default-export */
|
|
2
|
+
export function createGetColorSchemeSelector(selector) {
|
|
3
|
+
return function getColorSchemeSelector(colorScheme) {
|
|
4
|
+
if (selector === 'media') {
|
|
5
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
6
|
+
if (colorScheme !== 'light' && colorScheme !== 'dark') {
|
|
7
|
+
console.error(`MUI: @media (prefers-color-scheme) supports only 'light' or 'dark', but receive '${colorScheme}'.`);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
return `@media (prefers-color-scheme: ${colorScheme})`;
|
|
11
|
+
}
|
|
12
|
+
if (selector) {
|
|
13
|
+
if (selector.startsWith('data-') && !selector.includes('%s')) {
|
|
14
|
+
return `[${selector}="${colorScheme}"] &`;
|
|
15
|
+
}
|
|
16
|
+
if (selector === 'class') {
|
|
17
|
+
return `.${colorScheme} &`;
|
|
18
|
+
}
|
|
19
|
+
if (selector === 'data') {
|
|
20
|
+
return `[data-${colorScheme}] &`;
|
|
21
|
+
}
|
|
22
|
+
return `${selector.replace('%s', colorScheme)} &`;
|
|
23
|
+
}
|
|
24
|
+
return '&';
|
|
25
|
+
};
|
|
26
|
+
}
|
package/modern/cssVars/index.js
CHANGED
|
@@ -3,4 +3,5 @@
|
|
|
3
3
|
export { default } from './createCssVarsProvider';
|
|
4
4
|
export { default as prepareCssVars } from './prepareCssVars';
|
|
5
5
|
export { default as prepareTypographyVars } from './prepareTypographyVars';
|
|
6
|
-
export { default as createCssVarsTheme } from './createCssVarsTheme';
|
|
6
|
+
export { default as createCssVarsTheme } from './createCssVarsTheme';
|
|
7
|
+
export { createGetColorSchemeSelector } from './getColorSchemeSelector';
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import deepmerge from '@mui/utils/deepmerge';
|
|
2
2
|
import cssVarsParser from './cssVarsParser';
|
|
3
|
-
function prepareCssVars(theme, {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
function prepareCssVars(theme, parserConfig = {}) {
|
|
4
|
+
const {
|
|
5
|
+
getSelector = defaultGetSelector,
|
|
6
|
+
disableCssColorScheme,
|
|
7
|
+
colorSchemeSelector: selector
|
|
8
|
+
} = parserConfig;
|
|
7
9
|
// @ts-ignore - ignore components do not exist
|
|
8
10
|
const {
|
|
9
11
|
colorSchemes = {},
|
|
@@ -47,6 +49,32 @@ function prepareCssVars(theme, {
|
|
|
47
49
|
vars
|
|
48
50
|
};
|
|
49
51
|
}
|
|
52
|
+
function defaultGetSelector(colorScheme) {
|
|
53
|
+
let rule = selector;
|
|
54
|
+
if (selector === 'class') {
|
|
55
|
+
rule = '.%s';
|
|
56
|
+
}
|
|
57
|
+
if (selector === 'data') {
|
|
58
|
+
rule = '[data-%s]';
|
|
59
|
+
}
|
|
60
|
+
if (selector?.startsWith('data-') && !selector.includes('%s')) {
|
|
61
|
+
// 'data-joy-color-scheme' -> '[data-joy-color-scheme="%s"]'
|
|
62
|
+
rule = `[${selector}="%s"]`;
|
|
63
|
+
}
|
|
64
|
+
if (colorScheme) {
|
|
65
|
+
if (rule === 'media') {
|
|
66
|
+
if (theme.defaultColorScheme === colorScheme) {
|
|
67
|
+
return ':root';
|
|
68
|
+
}
|
|
69
|
+
const mode = colorSchemes[colorScheme]?.palette?.mode || colorScheme;
|
|
70
|
+
return `@media (prefers-color-scheme: ${mode}) { :root`;
|
|
71
|
+
}
|
|
72
|
+
if (rule) {
|
|
73
|
+
return rule.replace('%s', String(colorScheme));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return ':root';
|
|
77
|
+
}
|
|
50
78
|
const generateThemeVars = () => {
|
|
51
79
|
let vars = {
|
|
52
80
|
...rootVars
|
|
@@ -61,37 +89,51 @@ function prepareCssVars(theme, {
|
|
|
61
89
|
const generateStyleSheets = () => {
|
|
62
90
|
const stylesheets = [];
|
|
63
91
|
const colorScheme = theme.defaultColorScheme || 'light';
|
|
64
|
-
function insertStyleSheet(
|
|
92
|
+
function insertStyleSheet(key, css) {
|
|
65
93
|
if (Object.keys(css).length) {
|
|
66
|
-
stylesheets.push(typeof
|
|
67
|
-
[
|
|
94
|
+
stylesheets.push(typeof key === 'string' ? {
|
|
95
|
+
[key]: {
|
|
68
96
|
...css
|
|
69
97
|
}
|
|
70
|
-
} :
|
|
98
|
+
} : key);
|
|
71
99
|
}
|
|
72
100
|
}
|
|
73
|
-
insertStyleSheet(getSelector
|
|
101
|
+
insertStyleSheet(getSelector(undefined, {
|
|
74
102
|
...rootCss
|
|
75
|
-
})
|
|
103
|
+
}), rootCss);
|
|
76
104
|
const {
|
|
77
105
|
[colorScheme]: defaultSchemeVal,
|
|
78
|
-
...
|
|
106
|
+
...other
|
|
79
107
|
} = colorSchemesMap;
|
|
80
108
|
if (defaultSchemeVal) {
|
|
81
109
|
// default color scheme has to come before other color schemes
|
|
82
110
|
const {
|
|
83
111
|
css
|
|
84
112
|
} = defaultSchemeVal;
|
|
85
|
-
|
|
113
|
+
const cssColorSheme = colorSchemes[colorScheme]?.palette?.mode;
|
|
114
|
+
const finalCss = !disableCssColorScheme && cssColorSheme ? {
|
|
115
|
+
colorScheme: cssColorSheme,
|
|
86
116
|
...css
|
|
87
|
-
}
|
|
117
|
+
} : {
|
|
118
|
+
...css
|
|
119
|
+
};
|
|
120
|
+
insertStyleSheet(getSelector(colorScheme, {
|
|
121
|
+
...finalCss
|
|
122
|
+
}), finalCss);
|
|
88
123
|
}
|
|
89
|
-
Object.entries(
|
|
124
|
+
Object.entries(other).forEach(([key, {
|
|
90
125
|
css
|
|
91
126
|
}]) => {
|
|
92
|
-
|
|
127
|
+
const cssColorSheme = colorSchemes[key]?.palette?.mode;
|
|
128
|
+
const finalCss = !disableCssColorScheme && cssColorSheme ? {
|
|
129
|
+
colorScheme: cssColorSheme,
|
|
130
|
+
...css
|
|
131
|
+
} : {
|
|
93
132
|
...css
|
|
94
|
-
}
|
|
133
|
+
};
|
|
134
|
+
insertStyleSheet(getSelector(key, {
|
|
135
|
+
...finalCss
|
|
136
|
+
}), finalCss);
|
|
95
137
|
});
|
|
96
138
|
return stylesheets;
|
|
97
139
|
};
|
package/modern/index.js
CHANGED
|
@@ -18,7 +18,6 @@ const DEFAULT_COLOR_SCHEME_STORAGE_KEY = exports.DEFAULT_COLOR_SCHEME_STORAGE_KE
|
|
|
18
18
|
const DEFAULT_ATTRIBUTE = exports.DEFAULT_ATTRIBUTE = 'data-color-scheme';
|
|
19
19
|
function InitColorSchemeScript(options) {
|
|
20
20
|
const {
|
|
21
|
-
defaultMode = 'light',
|
|
22
21
|
defaultLightColorScheme = 'light',
|
|
23
22
|
defaultDarkColorScheme = 'dark',
|
|
24
23
|
modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
|
|
@@ -27,6 +26,24 @@ function InitColorSchemeScript(options) {
|
|
|
27
26
|
colorSchemeNode = 'document.documentElement',
|
|
28
27
|
nonce
|
|
29
28
|
} = options || {};
|
|
29
|
+
let setter = '';
|
|
30
|
+
if (attribute.startsWith('.')) {
|
|
31
|
+
const selector = attribute.substring(1);
|
|
32
|
+
setter += `${colorSchemeNode}.classList.remove('${selector}'.replace('%s', light), '${selector}'.replace('%s', dark));
|
|
33
|
+
${colorSchemeNode}.classList.add('${selector}'.replace('%s', colorScheme));`;
|
|
34
|
+
}
|
|
35
|
+
const matches = attribute.match(/\[([^\]]+)\]/); // case [data-color-scheme=%s] or [data-color-scheme]
|
|
36
|
+
if (matches) {
|
|
37
|
+
const [attr, value] = matches[1].split('=');
|
|
38
|
+
if (!value) {
|
|
39
|
+
setter += `${colorSchemeNode}.removeAttribute('${attr}'.replace('%s', light));
|
|
40
|
+
${colorSchemeNode}.removeAttribute('${attr}'.replace('%s', dark));`;
|
|
41
|
+
}
|
|
42
|
+
setter += `
|
|
43
|
+
${colorSchemeNode}.setAttribute('${attr}'.replace('%s', colorScheme), ${value ? `${value}.replace('%s', colorScheme)` : '""'});`;
|
|
44
|
+
} else {
|
|
45
|
+
setter += `${colorSchemeNode}.setAttribute('${attribute}', colorScheme);`;
|
|
46
|
+
}
|
|
30
47
|
return /*#__PURE__*/(0, _jsxRuntime.jsx)("script", {
|
|
31
48
|
suppressHydrationWarning: true,
|
|
32
49
|
nonce: typeof window === 'undefined' ? nonce : ''
|
|
@@ -35,25 +52,27 @@ function InitColorSchemeScript(options) {
|
|
|
35
52
|
dangerouslySetInnerHTML: {
|
|
36
53
|
__html: `(function() {
|
|
37
54
|
try {
|
|
38
|
-
var mode = localStorage.getItem('${modeStorageKey}') || '
|
|
55
|
+
var mode = localStorage.getItem('${modeStorageKey}') || 'system';
|
|
39
56
|
var colorScheme = '';
|
|
57
|
+
var dark = localStorage.getItem('${colorSchemeStorageKey}-dark') || '${defaultDarkColorScheme}';
|
|
58
|
+
var light = localStorage.getItem('${colorSchemeStorageKey}-light') || '${defaultLightColorScheme}';
|
|
40
59
|
if (mode === 'system') {
|
|
41
60
|
// handle system mode
|
|
42
61
|
var mql = window.matchMedia('(prefers-color-scheme: dark)');
|
|
43
62
|
if (mql.matches) {
|
|
44
|
-
colorScheme =
|
|
63
|
+
colorScheme = dark
|
|
45
64
|
} else {
|
|
46
|
-
colorScheme =
|
|
65
|
+
colorScheme = light
|
|
47
66
|
}
|
|
48
67
|
}
|
|
49
68
|
if (mode === 'light') {
|
|
50
|
-
colorScheme =
|
|
69
|
+
colorScheme = light;
|
|
51
70
|
}
|
|
52
71
|
if (mode === 'dark') {
|
|
53
|
-
colorScheme =
|
|
72
|
+
colorScheme = dark;
|
|
54
73
|
}
|
|
55
74
|
if (colorScheme) {
|
|
56
|
-
${
|
|
75
|
+
${setter}
|
|
57
76
|
}
|
|
58
77
|
} catch(e){}})();`
|
|
59
78
|
}
|
|
@@ -98,7 +98,7 @@ function createGrid(options = {}) {
|
|
|
98
98
|
rowSpacing: rowSpacingProp = spacingProp,
|
|
99
99
|
columnSpacing: columnSpacingProp = spacingProp,
|
|
100
100
|
unstable_level: level = 0,
|
|
101
|
-
...
|
|
101
|
+
...other
|
|
102
102
|
} = props;
|
|
103
103
|
const size = parseResponsiveProp(sizeProp, theme.breakpoints, val => val !== false);
|
|
104
104
|
const offset = parseResponsiveProp(offsetProp, theme.breakpoints);
|
|
@@ -125,7 +125,7 @@ function createGrid(options = {}) {
|
|
|
125
125
|
as: component,
|
|
126
126
|
ownerState: ownerState,
|
|
127
127
|
className: (0, _clsx.default)(classes.root, className),
|
|
128
|
-
...
|
|
128
|
+
...other,
|
|
129
129
|
children: React.Children.map(children, child => {
|
|
130
130
|
if ( /*#__PURE__*/React.isValidElement(child) && (0, _isMuiElement.default)(child, ['Grid'])) {
|
|
131
131
|
var _unstable_level;
|
|
@@ -65,10 +65,17 @@ exports.default = applyStyles;
|
|
|
65
65
|
function applyStyles(key, styles) {
|
|
66
66
|
// @ts-expect-error this is 'any' type
|
|
67
67
|
const theme = this;
|
|
68
|
-
if (theme.vars
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
68
|
+
if (theme.vars) {
|
|
69
|
+
var _theme$colorSchemes;
|
|
70
|
+
if (!((_theme$colorSchemes = theme.colorSchemes) != null && _theme$colorSchemes[key]) || typeof theme.getColorSchemeSelector !== 'function') {
|
|
71
|
+
return {};
|
|
72
|
+
}
|
|
73
|
+
// If CssVarsProvider is used as a provider, returns '*:where({selector}) &'
|
|
74
|
+
let selector = theme.getColorSchemeSelector(key);
|
|
75
|
+
if (selector.includes('data-') || selector.includes('.')) {
|
|
76
|
+
// '*' is required as a workaround for Emotion issue (https://github.com/emotion-js/emotion/issues/2836)
|
|
77
|
+
selector = `*:where(${selector.replace(/\s*&$/, '')}) &`;
|
|
78
|
+
}
|
|
72
79
|
return {
|
|
73
80
|
[selector]: styles
|
|
74
81
|
};
|