@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
|
@@ -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/esm/index.js
CHANGED
|
@@ -36,4 +36,5 @@ export { default as useThemeProps, getThemeProps } from './useThemeProps';
|
|
|
36
36
|
export { default as useTheme } from './useTheme';
|
|
37
37
|
export { default as useThemeWithoutDefault } from './useThemeWithoutDefault';
|
|
38
38
|
export * from './colorManipulator';
|
|
39
|
-
export { default as ThemeProvider } from './ThemeProvider';
|
|
39
|
+
export { default as ThemeProvider } from './ThemeProvider';
|
|
40
|
+
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
|
}
|
|
@@ -14,53 +14,63 @@ function callIfFn(maybeFn, arg) {
|
|
|
14
14
|
|
|
15
15
|
function styleFunctionSx(props) {
|
|
16
16
|
const {
|
|
17
|
-
sx
|
|
17
|
+
sx,
|
|
18
18
|
theme = {}
|
|
19
19
|
} = props || {};
|
|
20
20
|
|
|
21
|
-
if (!
|
|
22
|
-
return null;
|
|
21
|
+
if (!sx) {
|
|
22
|
+
return null; // emotion & styled-components will neglect null
|
|
23
23
|
}
|
|
24
|
+
/*
|
|
25
|
+
* Receive `sxInput` as object or callback
|
|
26
|
+
* and then recursively check keys & values to create media query object styles.
|
|
27
|
+
* (the result will be used in `styled`)
|
|
28
|
+
*/
|
|
24
29
|
|
|
25
|
-
if (typeof styles === 'function') {
|
|
26
|
-
return styles(theme);
|
|
27
|
-
}
|
|
28
30
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return styles;
|
|
32
|
-
}
|
|
31
|
+
function traverse(sxInput) {
|
|
32
|
+
let sxObject = sxInput;
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
if (typeof sxInput === 'function') {
|
|
35
|
+
sxObject = sxInput(theme);
|
|
36
|
+
} else if (typeof sxInput !== 'object') {
|
|
37
|
+
// value
|
|
38
|
+
return sxInput;
|
|
39
|
+
}
|
|
39
40
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
theme
|
|
46
|
-
}, value, x => ({
|
|
47
|
-
[styleKey]: x
|
|
48
|
-
}));
|
|
41
|
+
const emptyBreakpoints = createEmptyBreakpointObject(theme.breakpoints);
|
|
42
|
+
const breakpointsKeys = Object.keys(emptyBreakpoints);
|
|
43
|
+
let css = emptyBreakpoints;
|
|
44
|
+
Object.keys(sxObject).forEach(styleKey => {
|
|
45
|
+
const value = callIfFn(sxObject[styleKey], theme);
|
|
49
46
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
theme
|
|
54
|
-
});
|
|
47
|
+
if (typeof value === 'object') {
|
|
48
|
+
if (propToStyleFunction[styleKey]) {
|
|
49
|
+
css = merge(css, getThemeValue(styleKey, value, theme));
|
|
55
50
|
} else {
|
|
56
|
-
|
|
51
|
+
const breakpointsValues = handleBreakpoints({
|
|
52
|
+
theme
|
|
53
|
+
}, value, x => ({
|
|
54
|
+
[styleKey]: x
|
|
55
|
+
}));
|
|
56
|
+
|
|
57
|
+
if (objectsHaveSameKeys(breakpointsValues, value)) {
|
|
58
|
+
css[styleKey] = styleFunctionSx({
|
|
59
|
+
sx: value,
|
|
60
|
+
theme
|
|
61
|
+
});
|
|
62
|
+
} else {
|
|
63
|
+
css = merge(css, breakpointsValues);
|
|
64
|
+
}
|
|
57
65
|
}
|
|
66
|
+
} else {
|
|
67
|
+
css = merge(css, getThemeValue(styleKey, value, theme));
|
|
58
68
|
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
return
|
|
69
|
+
});
|
|
70
|
+
return removeUnusedBreakpoints(breakpointsKeys, css);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return Array.isArray(sx) ? sx.map(traverse) : traverse(sx);
|
|
64
74
|
}
|
|
65
75
|
|
|
66
76
|
styleFunctionSx.filterProps = ['sx'];
|
package/flexbox.js
CHANGED
|
@@ -5,7 +5,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
|
-
exports.
|
|
8
|
+
exports.order = exports.justifySelf = exports.justifyItems = exports.justifyContent = exports.flexWrap = exports.flexShrink = exports.flexGrow = exports.flexDirection = exports.flexBasis = exports.flex = exports.default = exports.alignSelf = exports.alignItems = exports.alignContent = void 0;
|
|
9
9
|
|
|
10
10
|
var _style = _interopRequireDefault(require("./style"));
|
|
11
11
|
|
package/getThemeValue.js
CHANGED
|
@@ -5,7 +5,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
|
-
exports.
|
|
8
|
+
exports.propToStyleFunction = exports.default = void 0;
|
|
9
9
|
|
|
10
10
|
var _borders = _interopRequireDefault(require("./borders"));
|
|
11
11
|
|
package/grid.js
CHANGED
|
@@ -5,7 +5,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
|
-
exports.
|
|
8
|
+
exports.rowGap = exports.gridTemplateRows = exports.gridTemplateColumns = exports.gridTemplateAreas = exports.gridRow = exports.gridColumn = exports.gridAutoRows = exports.gridAutoFlow = exports.gridAutoColumns = exports.gridArea = exports.gap = exports.default = exports.columnGap = void 0;
|
|
9
9
|
|
|
10
10
|
var _style = _interopRequireDefault(require("./style"));
|
|
11
11
|
|
package/index.d.ts
CHANGED
|
@@ -107,6 +107,9 @@ export {
|
|
|
107
107
|
GlobalStyles,
|
|
108
108
|
GlobalStylesProps,
|
|
109
109
|
StyledEngineProvider,
|
|
110
|
+
Interpolation,
|
|
111
|
+
CSSInterpolation,
|
|
112
|
+
CSSObject,
|
|
110
113
|
} from '@mui/styled-engine';
|
|
111
114
|
|
|
112
115
|
export * from './style';
|
|
@@ -154,3 +157,6 @@ export * from './colorManipulator';
|
|
|
154
157
|
|
|
155
158
|
export { default as ThemeProvider } from './ThemeProvider';
|
|
156
159
|
export * from './ThemeProvider';
|
|
160
|
+
|
|
161
|
+
export { default as unstable_createCssVarsProvider } from './cssVars';
|
|
162
|
+
export * from './cssVars';
|