@mui/system 5.0.4 → 5.1.1

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.
Files changed (63) hide show
  1. package/CHANGELOG.md +287 -6
  2. package/breakpoints.js +41 -8
  3. package/createBox.d.ts +5 -1
  4. package/createBox.js +5 -3
  5. package/createStyled.d.ts +16 -190
  6. package/createStyled.js +5 -1
  7. package/createTheme/createBreakpoints.js +2 -2
  8. package/cssVars/createCssVarsProvider.d.ts +131 -0
  9. package/cssVars/createCssVarsProvider.js +228 -0
  10. package/cssVars/createCssVarsProvider.spec.d.ts +1 -0
  11. package/cssVars/cssVarsParser.d.ts +68 -0
  12. package/cssVars/cssVarsParser.js +156 -0
  13. package/cssVars/getInitColorSchemeScript.d.ts +12 -0
  14. package/cssVars/getInitColorSchemeScript.js +60 -0
  15. package/cssVars/index.d.ts +2 -0
  16. package/cssVars/index.js +15 -0
  17. package/cssVars/package.json +6 -0
  18. package/cssVars/useCurrentColorScheme.d.ts +50 -0
  19. package/cssVars/useCurrentColorScheme.js +235 -0
  20. package/esm/breakpoints.js +39 -8
  21. package/esm/createBox.js +5 -3
  22. package/esm/createStyled.js +5 -1
  23. package/esm/createTheme/createBreakpoints.js +2 -2
  24. package/esm/cssVars/createCssVarsProvider.js +207 -0
  25. package/esm/cssVars/cssVarsParser.js +141 -0
  26. package/esm/cssVars/getInitColorSchemeScript.js +42 -0
  27. package/esm/cssVars/index.js +1 -0
  28. package/esm/cssVars/useCurrentColorScheme.js +217 -0
  29. package/esm/index.js +2 -1
  30. package/esm/styleFunctionSx/extendSxProp.js +20 -1
  31. package/esm/styleFunctionSx/styleFunctionSx.js +45 -35
  32. package/index.d.ts +6 -0
  33. package/index.js +11 -2
  34. package/legacy/breakpoints.js +39 -8
  35. package/legacy/createBox.js +6 -3
  36. package/legacy/createStyled.js +5 -1
  37. package/legacy/createTheme/createBreakpoints.js +2 -2
  38. package/legacy/cssVars/createCssVarsProvider.js +215 -0
  39. package/legacy/cssVars/cssVarsParser.js +153 -0
  40. package/legacy/cssVars/getInitColorSchemeScript.js +27 -0
  41. package/legacy/cssVars/index.js +1 -0
  42. package/legacy/cssVars/useCurrentColorScheme.js +231 -0
  43. package/legacy/index.js +3 -2
  44. package/legacy/styleFunctionSx/extendSxProp.js +21 -1
  45. package/legacy/styleFunctionSx/styleFunctionSx.js +44 -34
  46. package/modern/breakpoints.js +39 -8
  47. package/modern/createBox.js +5 -3
  48. package/modern/createStyled.js +5 -1
  49. package/modern/createTheme/createBreakpoints.js +2 -2
  50. package/modern/cssVars/createCssVarsProvider.js +207 -0
  51. package/modern/cssVars/cssVarsParser.js +141 -0
  52. package/modern/cssVars/getInitColorSchemeScript.js +42 -0
  53. package/modern/cssVars/index.js +1 -0
  54. package/modern/cssVars/useCurrentColorScheme.js +217 -0
  55. package/modern/index.js +3 -2
  56. package/modern/styleFunctionSx/extendSxProp.js +20 -1
  57. package/modern/styleFunctionSx/styleFunctionSx.js +45 -35
  58. package/package.json +8 -9
  59. package/style.d.ts +1 -1
  60. package/styleFunctionSx/extendSxProp.js +21 -1
  61. package/styleFunctionSx/styleFunctionSx.d.ts +7 -1
  62. package/styleFunctionSx/styleFunctionSx.js +46 -36
  63. package/styleFunctionSx/styleFunctionSx.spec.d.ts +1 -0
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.assignNestedKeys = void 0;
9
+ exports.default = cssVarsParser;
10
+ exports.walkObjectDeep = void 0;
11
+
12
+ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
13
+
14
+ /**
15
+ * This function create an object from keys, value and then assign to target
16
+ *
17
+ * @param {Object} obj : the target object to be assigned
18
+ * @param {string[]} keys
19
+ * @param {string | number} value
20
+ *
21
+ * @example
22
+ * const source = {}
23
+ * assignNestedKeys(source, ['palette', 'primary'], 'var(--palette-primary)')
24
+ * console.log(source) // { palette: { primary: 'var(--palette-primary)' } }
25
+ *
26
+ * @example
27
+ * const source = { palette: { primary: 'var(--palette-primary)' } }
28
+ * assignNestedKeys(source, ['palette', 'secondary'], 'var(--palette-secondary)')
29
+ * console.log(source) // { palette: { primary: 'var(--palette-primary)', secondary: 'var(--palette-secondary)' } }
30
+ */
31
+ const assignNestedKeys = (obj, keys, value) => {
32
+ let temp = obj;
33
+ keys.forEach((k, index) => {
34
+ if (index === keys.length - 1) {
35
+ if (temp && typeof temp === 'object') {
36
+ temp[k] = value;
37
+ }
38
+ } else if (temp && typeof temp === 'object') {
39
+ if (!temp[k]) {
40
+ temp[k] = {};
41
+ }
42
+
43
+ temp = temp[k];
44
+ }
45
+ });
46
+ };
47
+ /**
48
+ *
49
+ * @param {Object} obj : source object
50
+ * @param {Function} callback : a function that will be called when
51
+ * - the deepest key in source object is reached
52
+ * - the value of the deepest key is NOT `undefined` | `null`
53
+ *
54
+ * @example
55
+ * walkObjectDeep({ palette: { primary: { main: '#000000' } } }, console.log)
56
+ * // ['palette', 'primary', 'main'] '#000000'
57
+ */
58
+
59
+
60
+ exports.assignNestedKeys = assignNestedKeys;
61
+
62
+ const walkObjectDeep = (obj, callback) => {
63
+ function recurse(object, parentKeys = []) {
64
+ Object.entries(object).forEach(([key, value]) => {
65
+ if (value !== undefined && value !== null) {
66
+ if (typeof value === 'object' && Object.keys(value).length > 0) {
67
+ recurse(value, [...parentKeys, key]);
68
+ } else {
69
+ callback([...parentKeys, key], value, object);
70
+ }
71
+ }
72
+ });
73
+ }
74
+
75
+ recurse(obj);
76
+ };
77
+
78
+ exports.walkObjectDeep = walkObjectDeep;
79
+
80
+ const getCssValue = (keys, value) => {
81
+ if (typeof value === 'number') {
82
+ if (['lineHeight', 'fontWeight', 'opacity', 'zIndex'].some(prop => keys.includes(prop))) {
83
+ // css property that are unitless
84
+ return value;
85
+ }
86
+
87
+ return `${value}px`;
88
+ }
89
+
90
+ return value;
91
+ };
92
+ /**
93
+ * a function that parse theme and return { css, vars }
94
+ *
95
+ * @param {Object} theme
96
+ * @param {{
97
+ * prefix?: string,
98
+ * basePrefix?: string,
99
+ * shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean
100
+ * }} options.
101
+ * `basePrefix`: defined by design system.
102
+ * `prefix`: defined by application
103
+ *
104
+ * This function also mutate the string value of theme input by replacing `basePrefix` (if existed) with `prefix`
105
+ *
106
+ * @returns {{ css: Object, vars: Object }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme)
107
+ *
108
+ * @example
109
+ * const { css, vars } = parser({
110
+ * fontSize: 12,
111
+ * lineHeight: 1.2,
112
+ * palette: { primary: { 500: '#000000' } }
113
+ * })
114
+ *
115
+ * console.log(css) // { '--fontSize': '12px', '--lineHeight': 1.2, '--palette-primary-500': '#000000' }
116
+ * console.log(vars) // { fontSize: '--fontSize', lineHeight: '--lineHeight', palette: { primary: { 500: 'var(--palette-primary-500)' } } }
117
+ */
118
+
119
+
120
+ function cssVarsParser(theme, options) {
121
+ const clonedTheme = (0, _extends2.default)({}, theme);
122
+ delete clonedTheme.vars; // remove 'vars' from the structure
123
+
124
+ const {
125
+ prefix,
126
+ basePrefix = '',
127
+ shouldSkipGeneratingVar
128
+ } = options || {};
129
+ const css = {};
130
+ const vars = {};
131
+ walkObjectDeep(clonedTheme, (keys, val, scope) => {
132
+ if (typeof val === 'string' || typeof val === 'number') {
133
+ let value = val;
134
+
135
+ if (typeof value === 'string' && value.startsWith('var')) {
136
+ // replace the value of the `scope` object with the prefix or remove basePrefix from the value
137
+ value = prefix ? value.replace(basePrefix, prefix) : value.replace(`${basePrefix}-`, ''); // scope is the deepest object in the tree, keys is the theme path keys
138
+
139
+ scope[keys.slice(-1)[0]] = value;
140
+ }
141
+
142
+ if (!shouldSkipGeneratingVar || shouldSkipGeneratingVar && !shouldSkipGeneratingVar(keys, value)) {
143
+ // only create css & var if `shouldSkipGeneratingVar` return false
144
+ const cssVar = `--${prefix ? `${prefix}-` : ''}${keys.join('-')}`;
145
+ Object.assign(css, {
146
+ [cssVar]: getCssValue(keys, value)
147
+ });
148
+ assignNestedKeys(vars, keys, `var(${cssVar})`);
149
+ }
150
+ }
151
+ });
152
+ return {
153
+ css,
154
+ vars
155
+ };
156
+ }
@@ -0,0 +1,12 @@
1
+ /// <reference types="react" />
2
+ export declare const DEFAULT_MODE_STORAGE_KEY = "mui-mode";
3
+ export declare const DEFAULT_COLOR_SCHEME_STORAGE_KEY = "mui-color-scheme";
4
+ export declare const DEFAULT_ATTRIBUTE = "data-mui-color-scheme";
5
+ export default function getInitColorSchemeScript(options?: {
6
+ defaultMode?: 'light' | 'dark' | 'system';
7
+ defaultLightColorScheme?: string;
8
+ defaultDarkColorScheme?: string;
9
+ modeStorageKey?: string;
10
+ colorSchemeStorageKey?: string;
11
+ attribute?: string;
12
+ }): JSX.Element;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.DEFAULT_MODE_STORAGE_KEY = exports.DEFAULT_COLOR_SCHEME_STORAGE_KEY = exports.DEFAULT_ATTRIBUTE = void 0;
7
+ exports.default = getInitColorSchemeScript;
8
+
9
+ var React = _interopRequireWildcard(require("react"));
10
+
11
+ var _jsxRuntime = require("react/jsx-runtime");
12
+
13
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
14
+
15
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
16
+
17
+ const DEFAULT_MODE_STORAGE_KEY = 'mui-mode';
18
+ exports.DEFAULT_MODE_STORAGE_KEY = DEFAULT_MODE_STORAGE_KEY;
19
+ const DEFAULT_COLOR_SCHEME_STORAGE_KEY = 'mui-color-scheme';
20
+ exports.DEFAULT_COLOR_SCHEME_STORAGE_KEY = DEFAULT_COLOR_SCHEME_STORAGE_KEY;
21
+ const DEFAULT_ATTRIBUTE = 'data-mui-color-scheme';
22
+ exports.DEFAULT_ATTRIBUTE = DEFAULT_ATTRIBUTE;
23
+
24
+ function getInitColorSchemeScript(options) {
25
+ const {
26
+ defaultMode = 'light',
27
+ defaultLightColorScheme = 'light',
28
+ defaultDarkColorScheme = 'dark',
29
+ modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
30
+ colorSchemeStorageKey = DEFAULT_COLOR_SCHEME_STORAGE_KEY,
31
+ attribute = DEFAULT_ATTRIBUTE
32
+ } = options || {};
33
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)("script", {
34
+ // eslint-disable-next-line react/no-danger
35
+ dangerouslySetInnerHTML: {
36
+ __html: `(function() { try {
37
+ var mode = localStorage.getItem('${modeStorageKey}');
38
+ var colorScheme = '';
39
+ if (mode === 'system' || (!mode && ${defaultMode} === 'system')) {
40
+ // handle system mode
41
+ var mql = window.matchMedia('(prefers-color-scheme: dark)');
42
+ if (mql.matches) {
43
+ colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || ${defaultLightColorScheme};
44
+ } else {
45
+ colorScheme = localStorage.getItem('${colorSchemeStorageKey}-light') || ${defaultDarkColorScheme};
46
+ }
47
+ }
48
+ if (mode === 'light') {
49
+ colorScheme = localStorage.getItem('${colorSchemeStorageKey}-light') || ${defaultLightColorScheme};
50
+ }
51
+ if (mode === 'dark') {
52
+ colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || ${defaultDarkColorScheme};
53
+ }
54
+ if (colorScheme) {
55
+ document.body.setAttribute('${attribute}', colorScheme);
56
+ }
57
+ } catch (e) {} })();`
58
+ }
59
+ });
60
+ }
@@ -0,0 +1,2 @@
1
+ export { default } from './createCssVarsProvider';
2
+ export type { BuildCssVarsTheme } from './createCssVarsProvider';
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ Object.defineProperty(exports, "default", {
9
+ enumerable: true,
10
+ get: function () {
11
+ return _createCssVarsProvider.default;
12
+ }
13
+ });
14
+
15
+ var _createCssVarsProvider = _interopRequireDefault(require("./createCssVarsProvider"));
@@ -0,0 +1,6 @@
1
+ {
2
+ "sideEffects": false,
3
+ "module": "../esm/cssVars/index.js",
4
+ "main": "./index.js",
5
+ "types": "./index.d.ts"
6
+ }
@@ -0,0 +1,50 @@
1
+ export declare type Mode = 'light' | 'dark' | 'system';
2
+ export declare type SystemMode = Exclude<Mode, 'system'>;
3
+ export interface State<SupportedColorScheme extends string> {
4
+ /**
5
+ * User selected mode.
6
+ * Note: on the server, mode is always undefined
7
+ */
8
+ mode: Mode | undefined;
9
+ /**
10
+ * Only valid if `mode: 'system'`, either 'light' | 'dark'.
11
+ */
12
+ systemMode: SystemMode | undefined;
13
+ /**
14
+ * The color scheme for the light mode.
15
+ */
16
+ lightColorScheme: SupportedColorScheme;
17
+ /**
18
+ * The color scheme for the dark mode.
19
+ */
20
+ darkColorScheme: SupportedColorScheme;
21
+ }
22
+ export declare type Result<SupportedColorScheme extends string> = State<SupportedColorScheme> & {
23
+ /**
24
+ * The current application color scheme. It is always `undefined` on the server.
25
+ */
26
+ colorScheme: SupportedColorScheme | undefined;
27
+ /**
28
+ * `mode` is saved to internal state and localStorage
29
+ * If `mode` is null, it will be reset to the defaultMode
30
+ */
31
+ setMode: (mode: Mode | null) => void;
32
+ /**
33
+ * `colorScheme` is saved to internal state and localStorage
34
+ * If `colorScheme` is null, it will be reset to the defaultColorScheme (light | dark)
35
+ */
36
+ setColorScheme: (colorScheme: SupportedColorScheme | Partial<{
37
+ light: SupportedColorScheme | null;
38
+ dark: SupportedColorScheme | null;
39
+ }> | null) => void;
40
+ };
41
+ export declare function getSystemMode(mode: undefined | string): SystemMode | undefined;
42
+ export declare function getColorScheme<SupportedColorScheme extends string>(state: State<SupportedColorScheme>): SupportedColorScheme | undefined;
43
+ export default function useCurrentColorScheme<SupportedColorScheme extends string>(options: {
44
+ defaultLightColorScheme: SupportedColorScheme;
45
+ defaultDarkColorScheme: SupportedColorScheme;
46
+ supportedColorSchemes: Array<SupportedColorScheme>;
47
+ defaultMode?: Mode;
48
+ modeStorageKey?: string;
49
+ colorSchemeStorageKey?: string;
50
+ }): Result<SupportedColorScheme>;
@@ -0,0 +1,235 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = useCurrentColorScheme;
9
+ exports.getColorScheme = getColorScheme;
10
+ exports.getSystemMode = getSystemMode;
11
+
12
+ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
13
+
14
+ var React = _interopRequireWildcard(require("react"));
15
+
16
+ var _getInitColorSchemeScript = require("./getInitColorSchemeScript");
17
+
18
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
19
+
20
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
21
+
22
+ function getSystemMode(mode) {
23
+ if (typeof window !== 'undefined' && mode === 'system') {
24
+ const mql = window.matchMedia('(prefers-color-scheme: dark)');
25
+
26
+ if (mql.matches) {
27
+ return 'dark';
28
+ }
29
+
30
+ return 'light';
31
+ }
32
+
33
+ return undefined;
34
+ }
35
+
36
+ function processState(state, callback) {
37
+ if (state.mode === 'light' || state.mode === 'system' && state.systemMode === 'light') {
38
+ return callback('light');
39
+ }
40
+
41
+ if (state.mode === 'dark' || state.mode === 'system' && state.systemMode === 'dark') {
42
+ return callback('dark');
43
+ }
44
+
45
+ return undefined;
46
+ }
47
+
48
+ function getColorScheme(state) {
49
+ return processState(state, mode => {
50
+ if (mode === 'light') {
51
+ return state.lightColorScheme;
52
+ }
53
+
54
+ if (mode === 'dark') {
55
+ return state.darkColorScheme;
56
+ }
57
+
58
+ return undefined;
59
+ });
60
+ }
61
+
62
+ function resolveValue(key, defaultValue) {
63
+ if (typeof window === 'undefined') {
64
+ return undefined;
65
+ }
66
+
67
+ let value;
68
+
69
+ try {
70
+ value = localStorage.getItem(key) || undefined;
71
+ } catch (e) {// Unsupported
72
+ }
73
+
74
+ return value || defaultValue;
75
+ }
76
+
77
+ function useCurrentColorScheme(options) {
78
+ const {
79
+ defaultMode = 'light',
80
+ defaultLightColorScheme,
81
+ defaultDarkColorScheme,
82
+ supportedColorSchemes = [],
83
+ modeStorageKey = _getInitColorSchemeScript.DEFAULT_MODE_STORAGE_KEY,
84
+ colorSchemeStorageKey = _getInitColorSchemeScript.DEFAULT_COLOR_SCHEME_STORAGE_KEY
85
+ } = options;
86
+ const joinedColorSchemes = supportedColorSchemes.join(',');
87
+ const [state, setState] = React.useState(() => {
88
+ const initialMode = resolveValue(modeStorageKey, defaultMode);
89
+ return {
90
+ mode: initialMode,
91
+ systemMode: getSystemMode(initialMode),
92
+ lightColorScheme: resolveValue(`${colorSchemeStorageKey}-light`) || defaultLightColorScheme,
93
+ darkColorScheme: resolveValue(`${colorSchemeStorageKey}-dark`) || defaultDarkColorScheme
94
+ };
95
+ });
96
+ const colorScheme = getColorScheme(state);
97
+ const setMode = React.useCallback(mode => {
98
+ setState(currentState => {
99
+ const newMode = !mode ? defaultMode : mode;
100
+
101
+ if (typeof localStorage !== 'undefined') {
102
+ localStorage.setItem(modeStorageKey, newMode);
103
+ }
104
+
105
+ return (0, _extends2.default)({}, currentState, {
106
+ mode: newMode,
107
+ systemMode: getSystemMode(newMode)
108
+ });
109
+ });
110
+ }, [modeStorageKey, defaultMode]);
111
+ const setColorScheme = React.useCallback(value => {
112
+ if (!value || typeof value === 'string') {
113
+ if (value && !supportedColorSchemes.includes(value)) {
114
+ console.error(`\`${value}\` does not exist in \`theme.colorSchemes\`.`);
115
+ } else {
116
+ setState(currentState => {
117
+ const newState = (0, _extends2.default)({}, currentState);
118
+
119
+ if (!value) {
120
+ // reset to default color scheme
121
+ newState.lightColorScheme = defaultLightColorScheme;
122
+ newState.darkColorScheme = defaultDarkColorScheme;
123
+ return newState;
124
+ }
125
+
126
+ processState(currentState, mode => {
127
+ localStorage.setItem(`${colorSchemeStorageKey}-${mode}`, value);
128
+
129
+ if (mode === 'light') {
130
+ newState.lightColorScheme = value;
131
+ }
132
+
133
+ if (mode === 'dark') {
134
+ newState.darkColorScheme = value;
135
+ }
136
+ });
137
+ return newState;
138
+ });
139
+ }
140
+ } else if (value.light && !supportedColorSchemes.includes(value.light) || value.dark && !supportedColorSchemes.includes(value.dark)) {
141
+ console.error(`\`${value}\` does not exist in \`theme.colorSchemes\`.`);
142
+ } else {
143
+ setState(currentState => {
144
+ const newState = (0, _extends2.default)({}, currentState);
145
+
146
+ if (value.light || value.light === null) {
147
+ newState.lightColorScheme = value.light === null ? defaultLightColorScheme : value.light;
148
+ }
149
+
150
+ if (value.dark || value.dark === null) {
151
+ newState.darkColorScheme = value.dark === null ? defaultDarkColorScheme : value.dark;
152
+ }
153
+
154
+ return newState;
155
+ });
156
+
157
+ if (value.light) {
158
+ localStorage.setItem(`${colorSchemeStorageKey}-light`, value.light);
159
+ }
160
+
161
+ if (value.dark) {
162
+ localStorage.setItem(`${colorSchemeStorageKey}-dark`, value.dark);
163
+ }
164
+ }
165
+ }, [colorSchemeStorageKey, supportedColorSchemes, defaultLightColorScheme, defaultDarkColorScheme]);
166
+ const handleMediaQuery = React.useCallback(e => {
167
+ if (state.mode === 'system') {
168
+ setState(currentState => (0, _extends2.default)({}, currentState, {
169
+ systemMode: e.matches ? 'dark' : 'light'
170
+ }));
171
+ }
172
+ }, [state.mode]); // Ref hack to avoid adding handleMediaQuery as a dep
173
+
174
+ const mediaListener = React.useRef(handleMediaQuery);
175
+ mediaListener.current = handleMediaQuery;
176
+ React.useEffect(() => {
177
+ const handler = (...args) => mediaListener.current(...args); // Always listen to System preference
178
+
179
+
180
+ const media = window.matchMedia('(prefers-color-scheme: dark)'); // Intentionally use deprecated listener methods to support iOS & old browsers
181
+
182
+ media.addListener(handler);
183
+ handler(media);
184
+ return () => media.removeListener(handler);
185
+ }, []); // Save mode, lightColorScheme & darkColorScheme to localStorage
186
+
187
+ React.useEffect(() => {
188
+ if (state.mode) {
189
+ localStorage.setItem(modeStorageKey, state.mode);
190
+ }
191
+
192
+ processState(state, mode => {
193
+ if (mode === 'light') {
194
+ localStorage.setItem(`${colorSchemeStorageKey}-light`, state.lightColorScheme);
195
+ }
196
+
197
+ if (mode === 'dark') {
198
+ localStorage.setItem(`${colorSchemeStorageKey}-dark`, state.darkColorScheme);
199
+ }
200
+ });
201
+ }, [state, colorSchemeStorageKey, modeStorageKey]); // Handle when localStorage has changed
202
+
203
+ React.useEffect(() => {
204
+ const handleStorage = event => {
205
+ const value = event.newValue;
206
+
207
+ if (typeof event.key === 'string' && event.key.startsWith(colorSchemeStorageKey) && (!value || joinedColorSchemes.match(value))) {
208
+ // If the key is deleted, value will be null then reset color scheme to the default one.
209
+ if (event.key.endsWith('light')) {
210
+ setColorScheme({
211
+ light: value
212
+ });
213
+ }
214
+
215
+ if (event.key.endsWith('dark')) {
216
+ setColorScheme({
217
+ dark: value
218
+ });
219
+ }
220
+ }
221
+
222
+ if (event.key === modeStorageKey && (!value || ['light', 'dark', 'system'].includes(value))) {
223
+ setMode(value || defaultMode);
224
+ }
225
+ };
226
+
227
+ window.addEventListener('storage', handleStorage);
228
+ return () => window.removeEventListener('storage', handleStorage);
229
+ }, [setColorScheme, setMode, modeStorageKey, colorSchemeStorageKey, joinedColorSchemes, defaultMode]);
230
+ return (0, _extends2.default)({}, state, {
231
+ colorScheme,
232
+ setMode,
233
+ setColorScheme
234
+ });
235
+ }
@@ -8,12 +8,12 @@ export const values = {
8
8
  xs: 0,
9
9
  // phone
10
10
  sm: 600,
11
- // tablets
11
+ // tablet
12
12
  md: 900,
13
13
  // small laptop
14
14
  lg: 1200,
15
15
  // desktop
16
- xl: 1536 // large screens
16
+ xl: 1536 // large screen
17
17
 
18
18
  };
19
19
  const defaultBreakpoints = {
@@ -108,11 +108,41 @@ export function mergeBreakpointsInOrder(breakpointsInput, ...styles) {
108
108
  const emptyBreakpoints = createEmptyBreakpointObject(breakpointsInput);
109
109
  const mergedOutput = [emptyBreakpoints, ...styles].reduce((prev, next) => deepmerge(prev, next), {});
110
110
  return removeUnusedBreakpoints(Object.keys(emptyBreakpoints), mergedOutput);
111
+ } // compute base for responsive values; e.g.,
112
+ // [1,2,3] => {xs: true, sm: true, md: true}
113
+ // {xs: 1, sm: 2, md: 3} => {xs: true, sm: true, md: true}
114
+
115
+ export function computeBreakpointsBase(breakpointValues, themeBreakpoints) {
116
+ // fixed value
117
+ if (typeof breakpointValues !== 'object') {
118
+ return {};
119
+ }
120
+
121
+ const base = {};
122
+ const breakpointsKeys = Object.keys(themeBreakpoints);
123
+
124
+ if (Array.isArray(breakpointValues)) {
125
+ breakpointsKeys.forEach((breakpoint, i) => {
126
+ if (i < breakpointValues.length) {
127
+ base[breakpoint] = true;
128
+ }
129
+ });
130
+ } else {
131
+ breakpointsKeys.forEach(breakpoint => {
132
+ if (breakpointValues[breakpoint] != null) {
133
+ base[breakpoint] = true;
134
+ }
135
+ });
136
+ }
137
+
138
+ return base;
111
139
  }
112
140
  export function resolveBreakpointValues({
113
141
  values: breakpointValues,
114
- base
142
+ breakpoints: themeBreakpoints,
143
+ base: customBase
115
144
  }) {
145
+ const base = customBase || computeBreakpointsBase(breakpointValues, themeBreakpoints);
116
146
  const keys = Object.keys(base);
117
147
 
118
148
  if (keys.length === 0) {
@@ -120,14 +150,15 @@ export function resolveBreakpointValues({
120
150
  }
121
151
 
122
152
  let previous;
123
- return keys.reduce((acc, breakpoint) => {
124
- if (typeof breakpointValues === 'object') {
125
- acc[breakpoint] = breakpointValues[breakpoint] != null ? breakpointValues[breakpoint] : breakpointValues[previous];
153
+ return keys.reduce((acc, breakpoint, i) => {
154
+ if (Array.isArray(breakpointValues)) {
155
+ acc[breakpoint] = breakpointValues[i] != null ? breakpointValues[i] : breakpointValues[previous];
156
+ previous = i;
126
157
  } else {
127
- acc[breakpoint] = breakpointValues;
158
+ acc[breakpoint] = breakpointValues[breakpoint] != null ? breakpointValues[breakpoint] : breakpointValues[previous] || breakpointValues;
159
+ previous = breakpoint;
128
160
  }
129
161
 
130
- previous = breakpoint;
131
162
  return acc;
132
163
  }, {});
133
164
  }
package/esm/createBox.js CHANGED
@@ -10,7 +10,9 @@ import useTheme from './useTheme';
10
10
  import { jsx as _jsx } from "react/jsx-runtime";
11
11
  export default function createBox(options = {}) {
12
12
  const {
13
- defaultTheme
13
+ defaultTheme,
14
+ defaultClassName = 'MuiBox-root',
15
+ generateClassName
14
16
  } = options;
15
17
  const BoxRoot = styled('div')(styleFunctionSx);
16
18
  const Box = /*#__PURE__*/React.forwardRef(function Box(inProps, ref) {
@@ -26,7 +28,7 @@ export default function createBox(options = {}) {
26
28
  return /*#__PURE__*/_jsx(BoxRoot, _extends({
27
29
  as: component,
28
30
  ref: ref,
29
- className: clsx(className, 'MuiBox-root'),
31
+ className: clsx(className, generateClassName ? generateClassName(defaultClassName) : defaultClassName),
30
32
  theme: theme
31
33
  }, other));
32
34
  });
@@ -52,7 +54,7 @@ export default function createBox(options = {}) {
52
54
  /**
53
55
  * @ignore
54
56
  */
55
- sx: PropTypes.object
57
+ sx: PropTypes.oneOfType([PropTypes.object, PropTypes.array])
56
58
  } : void 0;
57
59
  return Box;
58
60
  }
@@ -115,7 +115,11 @@ export default function createStyled(input = {}) {
115
115
 
116
116
  const muiStyledResolver = (styleArg, ...expressions) => {
117
117
  const expressionsWithDefaultTheme = expressions ? expressions.map(stylesArg => {
118
- return typeof stylesArg === 'function' ? _ref => {
118
+ // On the server emotion doesn't use React.forwardRef for creating components, so the created
119
+ // component stays as a function. This condition makes sure that we do not interpolate functions
120
+ // which are basically components used as a selectors.
121
+ // eslint-disable-next-line no-underscore-dangle
122
+ return typeof stylesArg === 'function' && stylesArg.__emotion_real !== stylesArg ? _ref => {
119
123
  let {
120
124
  theme: themeInput
121
125
  } = _ref,
@@ -13,12 +13,12 @@ export default function createBreakpoints(breakpoints) {
13
13
  xs: 0,
14
14
  // phone
15
15
  sm: 600,
16
- // tablets
16
+ // tablet
17
17
  md: 900,
18
18
  // small laptop
19
19
  lg: 1200,
20
20
  // desktop
21
- xl: 1536 // large screens
21
+ xl: 1536 // large screen
22
22
 
23
23
  },
24
24
  unit = 'px',