@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
|
@@ -6,11 +6,6 @@ export declare const DEFAULT_MODE_STORAGE_KEY = "mode";
|
|
|
6
6
|
export declare const DEFAULT_COLOR_SCHEME_STORAGE_KEY = "color-scheme";
|
|
7
7
|
export declare const DEFAULT_ATTRIBUTE = "data-color-scheme";
|
|
8
8
|
export interface InitColorSchemeScriptProps {
|
|
9
|
-
/**
|
|
10
|
-
* The mode to be used for the first visit
|
|
11
|
-
* @default 'light'
|
|
12
|
-
*/
|
|
13
|
-
defaultMode?: 'light' | 'dark' | 'system';
|
|
14
9
|
/**
|
|
15
10
|
* The default color scheme to be used on the light mode
|
|
16
11
|
* @default 'light'
|
|
@@ -39,6 +34,9 @@ export interface InitColorSchemeScriptProps {
|
|
|
39
34
|
/**
|
|
40
35
|
* DOM attribute for applying color scheme
|
|
41
36
|
* @default 'data-color-scheme'
|
|
37
|
+
*
|
|
38
|
+
* @example '.mode-%s' // for class based color scheme
|
|
39
|
+
* @example '[data-mode-%s]' // for data-attribute without '='
|
|
42
40
|
*/
|
|
43
41
|
attribute?: string;
|
|
44
42
|
/**
|
|
@@ -8,7 +8,6 @@ export const DEFAULT_COLOR_SCHEME_STORAGE_KEY = 'color-scheme';
|
|
|
8
8
|
export const DEFAULT_ATTRIBUTE = 'data-color-scheme';
|
|
9
9
|
export default function InitColorSchemeScript(options) {
|
|
10
10
|
const {
|
|
11
|
-
defaultMode = 'light',
|
|
12
11
|
defaultLightColorScheme = 'light',
|
|
13
12
|
defaultDarkColorScheme = 'dark',
|
|
14
13
|
modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
|
|
@@ -17,6 +16,24 @@ export default function InitColorSchemeScript(options) {
|
|
|
17
16
|
colorSchemeNode = 'document.documentElement',
|
|
18
17
|
nonce
|
|
19
18
|
} = options || {};
|
|
19
|
+
let setter = '';
|
|
20
|
+
if (attribute.startsWith('.')) {
|
|
21
|
+
const selector = attribute.substring(1);
|
|
22
|
+
setter += `${colorSchemeNode}.classList.remove('${selector}'.replace('%s', light), '${selector}'.replace('%s', dark));
|
|
23
|
+
${colorSchemeNode}.classList.add('${selector}'.replace('%s', colorScheme));`;
|
|
24
|
+
}
|
|
25
|
+
const matches = attribute.match(/\[([^\]]+)\]/); // case [data-color-scheme=%s] or [data-color-scheme]
|
|
26
|
+
if (matches) {
|
|
27
|
+
const [attr, value] = matches[1].split('=');
|
|
28
|
+
if (!value) {
|
|
29
|
+
setter += `${colorSchemeNode}.removeAttribute('${attr}'.replace('%s', light));
|
|
30
|
+
${colorSchemeNode}.removeAttribute('${attr}'.replace('%s', dark));`;
|
|
31
|
+
}
|
|
32
|
+
setter += `
|
|
33
|
+
${colorSchemeNode}.setAttribute('${attr}'.replace('%s', colorScheme), ${value ? `${value}.replace('%s', colorScheme)` : '""'});`;
|
|
34
|
+
} else {
|
|
35
|
+
setter += `${colorSchemeNode}.setAttribute('${attribute}', colorScheme);`;
|
|
36
|
+
}
|
|
20
37
|
return /*#__PURE__*/_jsx("script", {
|
|
21
38
|
suppressHydrationWarning: true,
|
|
22
39
|
nonce: typeof window === 'undefined' ? nonce : ''
|
|
@@ -25,25 +42,27 @@ export default function InitColorSchemeScript(options) {
|
|
|
25
42
|
dangerouslySetInnerHTML: {
|
|
26
43
|
__html: `(function() {
|
|
27
44
|
try {
|
|
28
|
-
var mode = localStorage.getItem('${modeStorageKey}') || '
|
|
45
|
+
var mode = localStorage.getItem('${modeStorageKey}') || 'system';
|
|
29
46
|
var colorScheme = '';
|
|
47
|
+
var dark = localStorage.getItem('${colorSchemeStorageKey}-dark') || '${defaultDarkColorScheme}';
|
|
48
|
+
var light = localStorage.getItem('${colorSchemeStorageKey}-light') || '${defaultLightColorScheme}';
|
|
30
49
|
if (mode === 'system') {
|
|
31
50
|
// handle system mode
|
|
32
51
|
var mql = window.matchMedia('(prefers-color-scheme: dark)');
|
|
33
52
|
if (mql.matches) {
|
|
34
|
-
colorScheme =
|
|
53
|
+
colorScheme = dark
|
|
35
54
|
} else {
|
|
36
|
-
colorScheme =
|
|
55
|
+
colorScheme = light
|
|
37
56
|
}
|
|
38
57
|
}
|
|
39
58
|
if (mode === 'light') {
|
|
40
|
-
colorScheme =
|
|
59
|
+
colorScheme = light;
|
|
41
60
|
}
|
|
42
61
|
if (mode === 'dark') {
|
|
43
|
-
colorScheme =
|
|
62
|
+
colorScheme = dark;
|
|
44
63
|
}
|
|
45
64
|
if (colorScheme) {
|
|
46
|
-
${
|
|
65
|
+
${setter}
|
|
47
66
|
}
|
|
48
67
|
} catch(e){}})();`
|
|
49
68
|
}
|
|
@@ -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
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import InitColorSchemeScript from '../InitColorSchemeScript';
|
|
3
|
-
import {
|
|
3
|
+
import { Result } from './useCurrentColorScheme';
|
|
4
4
|
|
|
5
5
|
export interface ColorSchemeContextValue<SupportedColorScheme extends string>
|
|
6
6
|
extends Result<SupportedColorScheme> {
|
|
@@ -29,11 +29,6 @@ export interface CssVarsProviderConfig<ColorScheme extends string> {
|
|
|
29
29
|
* - provides object if the design system has default light & dark color schemes
|
|
30
30
|
*/
|
|
31
31
|
defaultColorScheme: ColorScheme | { light: ColorScheme; dark: ColorScheme };
|
|
32
|
-
/**
|
|
33
|
-
* Design system default mode
|
|
34
|
-
* @default 'light'
|
|
35
|
-
*/
|
|
36
|
-
defaultMode?: Mode;
|
|
37
32
|
/**
|
|
38
33
|
* Disable CSS transitions when switching between modes or color schemes
|
|
39
34
|
* @default false
|
|
@@ -55,6 +50,7 @@ export interface CreateCssVarsProviderResult<
|
|
|
55
50
|
{
|
|
56
51
|
cssVarPrefix?: string;
|
|
57
52
|
colorSchemes: Record<ColorScheme, Record<string, any>>;
|
|
53
|
+
colorSchemeSelector?: 'media' | 'class' | 'data' | string;
|
|
58
54
|
}
|
|
59
55
|
>;
|
|
60
56
|
/**
|
|
@@ -67,11 +63,6 @@ export interface CreateCssVarsProviderResult<
|
|
|
67
63
|
* @default document
|
|
68
64
|
*/
|
|
69
65
|
colorSchemeNode?: Element | null;
|
|
70
|
-
/**
|
|
71
|
-
* The CSS selector for attaching the generated custom properties
|
|
72
|
-
* @default ':root'
|
|
73
|
-
*/
|
|
74
|
-
colorSchemeSelector?: string;
|
|
75
66
|
/**
|
|
76
67
|
* The window that attaches the 'storage' event listener
|
|
77
68
|
* @default window
|
|
@@ -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,9 +1,10 @@
|
|
|
1
1
|
import { DefaultCssVarsTheme } from './prepareCssVars';
|
|
2
2
|
interface Theme extends DefaultCssVarsTheme {
|
|
3
3
|
cssVarPrefix?: string;
|
|
4
|
+
colorSchemeSelector?: 'media' | string;
|
|
4
5
|
shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean;
|
|
5
6
|
}
|
|
6
|
-
declare function createCssVarsTheme<T extends Theme, ThemeVars extends Record<string, any>>(theme: T): T & {
|
|
7
|
+
declare function createCssVarsTheme<T extends Theme, ThemeVars extends Record<string, any>>({ colorSchemeSelector, ...theme }: T): T & {
|
|
7
8
|
vars: ThemeVars;
|
|
8
9
|
generateThemeVars: () => ThemeVars;
|
|
9
10
|
generateStyleSheets: () => Record<string, any>[];
|
|
@@ -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 @@
|
|
|
1
|
+
export declare function createGetColorSchemeSelector<T extends string>(selector: 'media' | 'class' | 'data' | string): (colorScheme: T) => string;
|
|
@@ -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/cssVars/index.d.ts
CHANGED
|
@@ -4,3 +4,4 @@ export { default as prepareCssVars } from './prepareCssVars';
|
|
|
4
4
|
export { default as prepareTypographyVars } from './prepareTypographyVars';
|
|
5
5
|
export type { ExtractTypographyTokens } from './prepareTypographyVars';
|
|
6
6
|
export { default as createCssVarsTheme } from './createCssVarsTheme';
|
|
7
|
+
export { createGetColorSchemeSelector } from './getColorSchemeSelector';
|
package/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,10 +1,11 @@
|
|
|
1
1
|
export interface DefaultCssVarsTheme {
|
|
2
|
-
attribute?: string;
|
|
3
2
|
colorSchemes?: Record<string, any>;
|
|
4
3
|
defaultColorScheme?: string;
|
|
5
4
|
}
|
|
6
|
-
declare function prepareCssVars<T extends DefaultCssVarsTheme, ThemeVars extends Record<string, any>>(theme: T,
|
|
5
|
+
declare function prepareCssVars<T extends DefaultCssVarsTheme, ThemeVars extends Record<string, any>>(theme: T, parserConfig?: {
|
|
7
6
|
prefix?: string;
|
|
7
|
+
colorSchemeSelector?: 'media' | 'class' | 'data' | string;
|
|
8
|
+
disableCssColorScheme?: boolean;
|
|
8
9
|
shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean;
|
|
9
10
|
getSelector?: (colorScheme: keyof T['colorSchemes'] | undefined, css: Record<string, any>) => string | Record<string, any>;
|
|
10
11
|
}): {
|
|
@@ -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
|
};
|
|
@@ -5,11 +5,11 @@ export interface State<SupportedColorScheme extends string> {
|
|
|
5
5
|
* User selected mode.
|
|
6
6
|
* Note: on the server, mode is always undefined
|
|
7
7
|
*/
|
|
8
|
-
mode:
|
|
8
|
+
mode: 'light' | 'dark' | 'system' | undefined;
|
|
9
9
|
/**
|
|
10
10
|
* Only valid if `mode: 'system'`, either 'light' | 'dark'.
|
|
11
11
|
*/
|
|
12
|
-
systemMode:
|
|
12
|
+
systemMode: 'light' | 'dark' | undefined;
|
|
13
13
|
/**
|
|
14
14
|
* The color scheme for the light mode.
|
|
15
15
|
*/
|
package/index.js
CHANGED
|
@@ -8,7 +8,6 @@ export const DEFAULT_COLOR_SCHEME_STORAGE_KEY = 'color-scheme';
|
|
|
8
8
|
export const DEFAULT_ATTRIBUTE = 'data-color-scheme';
|
|
9
9
|
export default function InitColorSchemeScript(options) {
|
|
10
10
|
const {
|
|
11
|
-
defaultMode = 'light',
|
|
12
11
|
defaultLightColorScheme = 'light',
|
|
13
12
|
defaultDarkColorScheme = 'dark',
|
|
14
13
|
modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
|
|
@@ -17,6 +16,24 @@ export default function InitColorSchemeScript(options) {
|
|
|
17
16
|
colorSchemeNode = 'document.documentElement',
|
|
18
17
|
nonce
|
|
19
18
|
} = options || {};
|
|
19
|
+
let setter = '';
|
|
20
|
+
if (attribute.startsWith('.')) {
|
|
21
|
+
const selector = attribute.substring(1);
|
|
22
|
+
setter += `${colorSchemeNode}.classList.remove('${selector}'.replace('%s', light), '${selector}'.replace('%s', dark));
|
|
23
|
+
${colorSchemeNode}.classList.add('${selector}'.replace('%s', colorScheme));`;
|
|
24
|
+
}
|
|
25
|
+
const matches = attribute.match(/\[([^\]]+)\]/); // case [data-color-scheme=%s] or [data-color-scheme]
|
|
26
|
+
if (matches) {
|
|
27
|
+
const [attr, value] = matches[1].split('=');
|
|
28
|
+
if (!value) {
|
|
29
|
+
setter += `${colorSchemeNode}.removeAttribute('${attr}'.replace('%s', light));
|
|
30
|
+
${colorSchemeNode}.removeAttribute('${attr}'.replace('%s', dark));`;
|
|
31
|
+
}
|
|
32
|
+
setter += `
|
|
33
|
+
${colorSchemeNode}.setAttribute('${attr}'.replace('%s', colorScheme), ${value ? `${value}.replace('%s', colorScheme)` : '""'});`;
|
|
34
|
+
} else {
|
|
35
|
+
setter += `${colorSchemeNode}.setAttribute('${attribute}', colorScheme);`;
|
|
36
|
+
}
|
|
20
37
|
return /*#__PURE__*/_jsx("script", {
|
|
21
38
|
suppressHydrationWarning: true,
|
|
22
39
|
nonce: typeof window === 'undefined' ? nonce : ''
|
|
@@ -25,25 +42,27 @@ export default function InitColorSchemeScript(options) {
|
|
|
25
42
|
dangerouslySetInnerHTML: {
|
|
26
43
|
__html: `(function() {
|
|
27
44
|
try {
|
|
28
|
-
var mode = localStorage.getItem('${modeStorageKey}') || '
|
|
45
|
+
var mode = localStorage.getItem('${modeStorageKey}') || 'system';
|
|
29
46
|
var colorScheme = '';
|
|
47
|
+
var dark = localStorage.getItem('${colorSchemeStorageKey}-dark') || '${defaultDarkColorScheme}';
|
|
48
|
+
var light = localStorage.getItem('${colorSchemeStorageKey}-light') || '${defaultLightColorScheme}';
|
|
30
49
|
if (mode === 'system') {
|
|
31
50
|
// handle system mode
|
|
32
51
|
var mql = window.matchMedia('(prefers-color-scheme: dark)');
|
|
33
52
|
if (mql.matches) {
|
|
34
|
-
colorScheme =
|
|
53
|
+
colorScheme = dark
|
|
35
54
|
} else {
|
|
36
|
-
colorScheme =
|
|
55
|
+
colorScheme = light
|
|
37
56
|
}
|
|
38
57
|
}
|
|
39
58
|
if (mode === 'light') {
|
|
40
|
-
colorScheme =
|
|
59
|
+
colorScheme = light;
|
|
41
60
|
}
|
|
42
61
|
if (mode === 'dark') {
|
|
43
|
-
colorScheme =
|
|
62
|
+
colorScheme = dark;
|
|
44
63
|
}
|
|
45
64
|
if (colorScheme) {
|
|
46
|
-
${
|
|
65
|
+
${setter}
|
|
47
66
|
}
|
|
48
67
|
} catch(e){}})();`
|
|
49
68
|
}
|