@mui/system 5.1.1 → 5.2.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.
Files changed (59) hide show
  1. package/Box/Box.spec.d.ts +1 -1
  2. package/CHANGELOG.md +265 -3
  3. package/breakpoints.js +1 -1
  4. package/createBox.d.ts +3 -3
  5. package/createBox.js +1 -1
  6. package/createBox.spec.d.ts +1 -0
  7. package/createTheme/createSpacing.d.ts +10 -10
  8. package/cssVars/createCssVarsProvider.d.ts +24 -10
  9. package/cssVars/createCssVarsProvider.js +80 -12
  10. package/cssVars/createCssVarsProvider.spec.d.ts +1 -1
  11. package/cssVars/cssVarsParser.d.ts +68 -68
  12. package/cssVars/cssVarsParser.js +18 -17
  13. package/cssVars/getInitColorSchemeScript.d.ts +12 -12
  14. package/cssVars/getInitColorSchemeScript.js +7 -7
  15. package/cssVars/index.d.ts +2 -2
  16. package/cssVars/useCurrentColorScheme.d.ts +50 -50
  17. package/esm/breakpoints.js +1 -1
  18. package/esm/createBox.js +1 -1
  19. package/esm/cssVars/createCssVarsProvider.js +76 -12
  20. package/esm/cssVars/cssVarsParser.js +18 -16
  21. package/esm/cssVars/getInitColorSchemeScript.js +7 -7
  22. package/esm/index.js +1 -0
  23. package/esm/styleFunctionSx/styleFunctionSx.js +20 -18
  24. package/esm/sx/index.js +1 -0
  25. package/esm/sx/sx.js +12 -0
  26. package/esm/useThemeProps/getThemeProps.js +2 -17
  27. package/index.d.ts +2 -0
  28. package/index.js +10 -1
  29. package/index.spec.d.ts +1 -1
  30. package/legacy/breakpoints.js +1 -1
  31. package/legacy/createBox.js +1 -1
  32. package/legacy/cssVars/createCssVarsProvider.js +79 -11
  33. package/legacy/cssVars/cssVarsParser.js +20 -14
  34. package/legacy/cssVars/getInitColorSchemeScript.js +2 -3
  35. package/legacy/index.js +2 -1
  36. package/legacy/styleFunctionSx/styleFunctionSx.js +19 -17
  37. package/legacy/sx/index.js +1 -0
  38. package/legacy/sx/sx.js +13 -0
  39. package/legacy/useThemeProps/getThemeProps.js +2 -17
  40. package/modern/breakpoints.js +1 -1
  41. package/modern/createBox.js +1 -1
  42. package/modern/cssVars/createCssVarsProvider.js +74 -12
  43. package/modern/cssVars/cssVarsParser.js +18 -16
  44. package/modern/cssVars/getInitColorSchemeScript.js +7 -7
  45. package/modern/index.js +2 -1
  46. package/modern/styleFunctionSx/styleFunctionSx.js +20 -18
  47. package/modern/sx/index.js +1 -0
  48. package/modern/sx/sx.js +12 -0
  49. package/modern/useThemeProps/getThemeProps.js +2 -17
  50. package/package.json +6 -6
  51. package/styleFunctionSx/styleFunctionSx.d.ts +1 -1
  52. package/styleFunctionSx/styleFunctionSx.js +20 -18
  53. package/styleFunctionSx/styleFunctionSx.spec.d.ts +1 -1
  54. package/sx/index.d.ts +1 -0
  55. package/sx/index.js +15 -0
  56. package/sx/package.json +6 -0
  57. package/sx/sx.d.ts +4 -0
  58. package/sx/sx.js +22 -0
  59. package/useThemeProps/getThemeProps.js +2 -17
@@ -1,68 +1,68 @@
1
- declare type NestedRecord<V = any> = {
2
- [k: string | number]: NestedRecord<V> | V;
3
- };
4
- /**
5
- * This function create an object from keys, value and then assign to target
6
- *
7
- * @param {Object} obj : the target object to be assigned
8
- * @param {string[]} keys
9
- * @param {string | number} value
10
- *
11
- * @example
12
- * const source = {}
13
- * assignNestedKeys(source, ['palette', 'primary'], 'var(--palette-primary)')
14
- * console.log(source) // { palette: { primary: 'var(--palette-primary)' } }
15
- *
16
- * @example
17
- * const source = { palette: { primary: 'var(--palette-primary)' } }
18
- * assignNestedKeys(source, ['palette', 'secondary'], 'var(--palette-secondary)')
19
- * console.log(source) // { palette: { primary: 'var(--palette-primary)', secondary: 'var(--palette-secondary)' } }
20
- */
21
- export declare const assignNestedKeys: <Object_1 = NestedRecord<any>, Value = any>(obj: Object_1, keys: Array<string>, value: Value) => void;
22
- /**
23
- *
24
- * @param {Object} obj : source object
25
- * @param {Function} callback : a function that will be called when
26
- * - the deepest key in source object is reached
27
- * - the value of the deepest key is NOT `undefined` | `null`
28
- *
29
- * @example
30
- * walkObjectDeep({ palette: { primary: { main: '#000000' } } }, console.log)
31
- * // ['palette', 'primary', 'main'] '#000000'
32
- */
33
- export declare const walkObjectDeep: <Value, T = Record<string, any>>(obj: T, callback: (keys: Array<string>, value: Value, scope: Record<string, string | number>) => void) => void;
34
- /**
35
- * a function that parse theme and return { css, vars }
36
- *
37
- * @param {Object} theme
38
- * @param {{
39
- * prefix?: string,
40
- * basePrefix?: string,
41
- * shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean
42
- * }} options.
43
- * `basePrefix`: defined by design system.
44
- * `prefix`: defined by application
45
- *
46
- * This function also mutate the string value of theme input by replacing `basePrefix` (if existed) with `prefix`
47
- *
48
- * @returns {{ css: Object, vars: Object }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme)
49
- *
50
- * @example
51
- * const { css, vars } = parser({
52
- * fontSize: 12,
53
- * lineHeight: 1.2,
54
- * palette: { primary: { 500: '#000000' } }
55
- * })
56
- *
57
- * console.log(css) // { '--fontSize': '12px', '--lineHeight': 1.2, '--palette-primary-500': '#000000' }
58
- * console.log(vars) // { fontSize: '--fontSize', lineHeight: '--lineHeight', palette: { primary: { 500: 'var(--palette-primary-500)' } } }
59
- */
60
- export default function cssVarsParser(theme: Record<string, any>, options?: {
61
- prefix?: string;
62
- basePrefix?: string;
63
- shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean;
64
- }): {
65
- css: NestedRecord<string>;
66
- vars: NestedRecord<string>;
67
- };
68
- export {};
1
+ declare type NestedRecord<V = any> = {
2
+ [k: string | number]: NestedRecord<V> | V;
3
+ };
4
+ /**
5
+ * This function create an object from keys, value and then assign to target
6
+ *
7
+ * @param {Object} obj : the target object to be assigned
8
+ * @param {string[]} keys
9
+ * @param {string | number} value
10
+ *
11
+ * @example
12
+ * const source = {}
13
+ * assignNestedKeys(source, ['palette', 'primary'], 'var(--palette-primary)')
14
+ * console.log(source) // { palette: { primary: 'var(--palette-primary)' } }
15
+ *
16
+ * @example
17
+ * const source = { palette: { primary: 'var(--palette-primary)' } }
18
+ * assignNestedKeys(source, ['palette', 'secondary'], 'var(--palette-secondary)')
19
+ * console.log(source) // { palette: { primary: 'var(--palette-primary)', secondary: 'var(--palette-secondary)' } }
20
+ */
21
+ export declare const assignNestedKeys: <Object_1 = NestedRecord<any>, Value = any>(obj: Object_1, keys: Array<string>, value: Value) => void;
22
+ /**
23
+ *
24
+ * @param {Object} obj : source object
25
+ * @param {Function} callback : a function that will be called when
26
+ * - the deepest key in source object is reached
27
+ * - the value of the deepest key is NOT `undefined` | `null`
28
+ *
29
+ * @example
30
+ * walkObjectDeep({ palette: { primary: { main: '#000000' } } }, console.log)
31
+ * // ['palette', 'primary', 'main'] '#000000'
32
+ */
33
+ export declare const walkObjectDeep: <Value, T = Record<string, any>>(obj: T, callback: (keys: Array<string>, value: Value, scope: Record<string, string | number>) => void, shouldSkipPaths?: ((keys: Array<string>) => boolean) | undefined) => void;
34
+ /**
35
+ * a function that parse theme and return { css, vars }
36
+ *
37
+ * @param {Object} theme
38
+ * @param {{
39
+ * prefix?: string,
40
+ * basePrefix?: string,
41
+ * shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean
42
+ * }} options.
43
+ * `basePrefix`: defined by design system.
44
+ * `prefix`: defined by application
45
+ *
46
+ * This function also mutate the string value of theme input by replacing `basePrefix` (if existed) with `prefix`
47
+ *
48
+ * @returns {{ css: Object, vars: Object }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme)
49
+ *
50
+ * @example
51
+ * const { css, vars } = parser({
52
+ * fontSize: 12,
53
+ * lineHeight: 1.2,
54
+ * palette: { primary: { 500: '#000000' } }
55
+ * })
56
+ *
57
+ * console.log(css) // { '--fontSize': '12px', '--lineHeight': 1.2, '--palette-primary-500': '#000000' }
58
+ * console.log(vars) // { fontSize: '--fontSize', lineHeight: '--lineHeight', palette: { primary: { 500: 'var(--palette-primary-500)' } } }
59
+ */
60
+ export default function cssVarsParser(theme: Record<string, any>, options?: {
61
+ prefix?: string;
62
+ basePrefix?: string;
63
+ shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean;
64
+ }): {
65
+ css: NestedRecord<string>;
66
+ vars: NestedRecord<string>;
67
+ };
68
+ export {};
@@ -1,7 +1,5 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
3
  Object.defineProperty(exports, "__esModule", {
6
4
  value: true
7
5
  });
@@ -9,8 +7,6 @@ exports.assignNestedKeys = void 0;
9
7
  exports.default = cssVarsParser;
10
8
  exports.walkObjectDeep = void 0;
11
9
 
12
- var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
13
-
14
10
  /**
15
11
  * This function create an object from keys, value and then assign to target
16
12
  *
@@ -59,14 +55,16 @@ const assignNestedKeys = (obj, keys, value) => {
59
55
 
60
56
  exports.assignNestedKeys = assignNestedKeys;
61
57
 
62
- const walkObjectDeep = (obj, callback) => {
58
+ const walkObjectDeep = (obj, callback, shouldSkipPaths) => {
63
59
  function recurse(object, parentKeys = []) {
64
60
  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);
61
+ if (!shouldSkipPaths || shouldSkipPaths && !shouldSkipPaths([...parentKeys, key])) {
62
+ if (value !== undefined && value !== null) {
63
+ if (typeof value === 'object' && Object.keys(value).length > 0) {
64
+ recurse(value, [...parentKeys, key]);
65
+ } else {
66
+ callback([...parentKeys, key], value, object);
67
+ }
70
68
  }
71
69
  }
72
70
  });
@@ -80,7 +78,7 @@ exports.walkObjectDeep = walkObjectDeep;
80
78
  const getCssValue = (keys, value) => {
81
79
  if (typeof value === 'number') {
82
80
  if (['lineHeight', 'fontWeight', 'opacity', 'zIndex'].some(prop => keys.includes(prop))) {
83
- // css property that are unitless
81
+ // CSS property that are unitless
84
82
  return value;
85
83
  }
86
84
 
@@ -118,9 +116,6 @@ const getCssValue = (keys, value) => {
118
116
 
119
117
 
120
118
  function cssVarsParser(theme, options) {
121
- const clonedTheme = (0, _extends2.default)({}, theme);
122
- delete clonedTheme.vars; // remove 'vars' from the structure
123
-
124
119
  const {
125
120
  prefix,
126
121
  basePrefix = '',
@@ -128,13 +123,18 @@ function cssVarsParser(theme, options) {
128
123
  } = options || {};
129
124
  const css = {};
130
125
  const vars = {};
131
- walkObjectDeep(clonedTheme, (keys, val, scope) => {
126
+ walkObjectDeep(theme, (keys, val, scope) => {
132
127
  if (typeof val === 'string' || typeof val === 'number') {
133
128
  let value = val;
134
129
 
135
130
  if (typeof value === 'string' && value.startsWith('var')) {
136
131
  // 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
132
+ if (!basePrefix && prefix) {
133
+ value = value.replace(/var\(--/g, `var(--${prefix}-`);
134
+ } else {
135
+ value = prefix ? value.replace(new RegExp(basePrefix, 'g'), prefix) : value.replace(new RegExp(`${basePrefix}-`, 'g'), '');
136
+ } // scope is the deepest object in the tree, keys is the theme path keys
137
+
138
138
 
139
139
  scope[keys.slice(-1)[0]] = value;
140
140
  }
@@ -148,7 +148,8 @@ function cssVarsParser(theme, options) {
148
148
  assignNestedKeys(vars, keys, `var(${cssVar})`);
149
149
  }
150
150
  }
151
- });
151
+ }, keys => keys[0] === 'vars' // skip 'vars/*' paths
152
+ );
152
153
  return {
153
154
  css,
154
155
  vars
@@ -1,12 +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;
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
+ enableSystem?: boolean;
7
+ defaultLightColorScheme?: string;
8
+ defaultDarkColorScheme?: string;
9
+ modeStorageKey?: string;
10
+ colorSchemeStorageKey?: string;
11
+ attribute?: string;
12
+ }): JSX.Element;
@@ -23,7 +23,7 @@ exports.DEFAULT_ATTRIBUTE = DEFAULT_ATTRIBUTE;
23
23
 
24
24
  function getInitColorSchemeScript(options) {
25
25
  const {
26
- defaultMode = 'light',
26
+ enableSystem,
27
27
  defaultLightColorScheme = 'light',
28
28
  defaultDarkColorScheme = 'dark',
29
29
  modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
@@ -36,23 +36,23 @@ function getInitColorSchemeScript(options) {
36
36
  __html: `(function() { try {
37
37
  var mode = localStorage.getItem('${modeStorageKey}');
38
38
  var colorScheme = '';
39
- if (mode === 'system' || (!mode && ${defaultMode} === 'system')) {
39
+ if (mode === 'system' || (!mode && !!${enableSystem})) {
40
40
  // handle system mode
41
41
  var mql = window.matchMedia('(prefers-color-scheme: dark)');
42
42
  if (mql.matches) {
43
- colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || ${defaultLightColorScheme};
43
+ colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || '${defaultDarkColorScheme}';
44
44
  } else {
45
- colorScheme = localStorage.getItem('${colorSchemeStorageKey}-light') || ${defaultDarkColorScheme};
45
+ colorScheme = localStorage.getItem('${colorSchemeStorageKey}-light') || '${defaultLightColorScheme}';
46
46
  }
47
47
  }
48
48
  if (mode === 'light') {
49
- colorScheme = localStorage.getItem('${colorSchemeStorageKey}-light') || ${defaultLightColorScheme};
49
+ colorScheme = localStorage.getItem('${colorSchemeStorageKey}-light') || '${defaultLightColorScheme}';
50
50
  }
51
51
  if (mode === 'dark') {
52
- colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || ${defaultDarkColorScheme};
52
+ colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || '${defaultDarkColorScheme}';
53
53
  }
54
54
  if (colorScheme) {
55
- document.body.setAttribute('${attribute}', colorScheme);
55
+ document.documentElement.setAttribute('${attribute}', colorScheme);
56
56
  }
57
57
  } catch (e) {} })();`
58
58
  }
@@ -1,2 +1,2 @@
1
- export { default } from './createCssVarsProvider';
2
- export type { BuildCssVarsTheme } from './createCssVarsProvider';
1
+ export { default } from './createCssVarsProvider';
2
+ export type { BuildCssVarsTheme } from './createCssVarsProvider';
@@ -1,50 +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>;
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>;
@@ -95,7 +95,7 @@ export function createEmptyBreakpointObject(breakpointsInput = {}) {
95
95
  export function removeUnusedBreakpoints(breakpointKeys, style) {
96
96
  return breakpointKeys.reduce((acc, key) => {
97
97
  const breakpointOutput = acc[key];
98
- const isBreakpointUnused = Object.keys(breakpointOutput).length === 0;
98
+ const isBreakpointUnused = !breakpointOutput || Object.keys(breakpointOutput).length === 0;
99
99
 
100
100
  if (isBreakpointUnused) {
101
101
  delete acc[key];
package/esm/createBox.js CHANGED
@@ -54,7 +54,7 @@ export default function createBox(options = {}) {
54
54
  /**
55
55
  * @ignore
56
56
  */
57
- sx: PropTypes.oneOfType([PropTypes.object, PropTypes.array])
57
+ sx: PropTypes.oneOfType([PropTypes.object, PropTypes.array, PropTypes.func])
58
58
  } : void 0;
59
59
  return Box;
60
60
  }
@@ -2,25 +2,35 @@ import _extends from "@babel/runtime/helpers/esm/extends";
2
2
  import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3
3
  import { formatMuiErrorMessage as _formatMuiErrorMessage } from "@mui/utils";
4
4
  const _excluded = ["colorSchemes"],
5
- _excluded2 = ["colorSchemes"];
5
+ _excluded2 = ["colorSchemes"],
6
+ _excluded3 = ["components"];
6
7
  import * as React from 'react';
7
8
  import PropTypes from 'prop-types';
8
9
  import { GlobalStyles } from '@mui/styled-engine';
9
10
  import { deepmerge } from '@mui/utils';
11
+ import createSpacing from '../createTheme/createSpacing';
12
+ import createBreakpoints from '../createTheme/createBreakpoints';
10
13
  import cssVarsParser from './cssVarsParser';
11
14
  import ThemeProvider from '../ThemeProvider';
12
15
  import getInitColorSchemeScript, { DEFAULT_ATTRIBUTE, DEFAULT_MODE_STORAGE_KEY } from './getInitColorSchemeScript';
13
16
  import useCurrentColorScheme from './useCurrentColorScheme';
14
17
  import { jsx as _jsx } from "react/jsx-runtime";
15
18
  import { jsxs as _jsxs } from "react/jsx-runtime";
19
+ export const DISABLE_CSS_TRANSITION = '*{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}';
16
20
  export default function createCssVarsProvider(options) {
21
+ var _baseTheme$breakpoint;
22
+
17
23
  const {
18
24
  theme: baseTheme = {},
19
25
  defaultMode: desisgnSystemMode = 'light',
20
26
  defaultColorScheme: designSystemColorScheme,
27
+ disableTransitionOnChange = false,
28
+ enableColorScheme = true,
21
29
  prefix: designSystemPrefix = '',
22
30
  shouldSkipGeneratingVar
23
31
  } = options;
32
+ const systemSpacing = createSpacing(baseTheme.spacing);
33
+ const systemBreakpoints = createBreakpoints((_baseTheme$breakpoint = baseTheme.breakpoints) != null ? _baseTheme$breakpoint : {});
24
34
 
25
35
  if (!baseTheme.colorSchemes || typeof designSystemColorScheme === 'string' && !baseTheme.colorSchemes[designSystemColorScheme] || typeof designSystemColorScheme === 'object' && !baseTheme.colorSchemes[designSystemColorScheme == null ? void 0 : designSystemColorScheme.light] || typeof designSystemColorScheme === 'object' && !baseTheme.colorSchemes[designSystemColorScheme == null ? void 0 : designSystemColorScheme.dark]) {
26
36
  console.error(`MUI: \`${designSystemColorScheme}\` does not exist in \`theme.colorSchemes\`.`);
@@ -47,17 +57,28 @@ export default function createCssVarsProvider(options) {
47
57
  defaultMode = desisgnSystemMode,
48
58
  defaultColorScheme = designSystemColorScheme
49
59
  }) {
60
+ // make sure that baseTheme is always independent of each <CssVarsProvider /> call.
61
+ // JSON.parse(JSON.stringify(...)) is okay to be used as long as the baseTheme is a plain object.
62
+ const clonedBaseTheme = React.useMemo(() => JSON.parse(JSON.stringify(baseTheme)), []);
63
+
50
64
  const {
51
65
  colorSchemes: baseColorSchemes = {}
52
- } = baseTheme,
53
- restBaseTheme = _objectWithoutPropertiesLoose(baseTheme, _excluded);
66
+ } = clonedBaseTheme,
67
+ restBaseTheme = _objectWithoutPropertiesLoose(clonedBaseTheme, _excluded);
54
68
 
55
69
  const {
56
70
  colorSchemes: colorSchemesProp = {}
57
71
  } = themeProp,
58
72
  restThemeProp = _objectWithoutPropertiesLoose(themeProp, _excluded2);
59
73
 
60
- let mergedTheme = deepmerge(restBaseTheme, restThemeProp);
74
+ const hasMounted = React.useRef(null); // eslint-disable-next-line prefer-const
75
+
76
+ let _deepmerge = deepmerge(restBaseTheme, restThemeProp),
77
+ {
78
+ components = {}
79
+ } = _deepmerge,
80
+ mergedTheme = _objectWithoutPropertiesLoose(_deepmerge, _excluded3);
81
+
61
82
  const colorSchemes = deepmerge(baseColorSchemes, colorSchemesProp);
62
83
  const allColorSchemes = Object.keys(colorSchemes);
63
84
  const defaultLightColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.light;
@@ -65,6 +86,7 @@ export default function createCssVarsProvider(options) {
65
86
  const {
66
87
  mode,
67
88
  setMode,
89
+ systemMode,
68
90
  lightColorScheme,
69
91
  darkColorScheme,
70
92
  colorScheme,
@@ -100,7 +122,11 @@ export default function createCssVarsProvider(options) {
100
122
  shouldSkipGeneratingVar
101
123
  });
102
124
  mergedTheme = _extends({}, mergedTheme, colorSchemes[resolvedColorScheme], {
103
- vars: rootVars
125
+ components,
126
+ colorSchemes,
127
+ vars: rootVars,
128
+ spacing: themeProp.spacing ? createSpacing(themeProp.spacing) : systemSpacing,
129
+ breakpoints: themeProp.breakpoints ? createBreakpoints(themeProp.breakpoints) : systemBreakpoints
104
130
  });
105
131
  const styleSheet = {};
106
132
  Object.entries(colorSchemes).forEach(([key, scheme]) => {
@@ -112,10 +138,7 @@ export default function createCssVarsProvider(options) {
112
138
  basePrefix: designSystemPrefix,
113
139
  shouldSkipGeneratingVar
114
140
  });
115
-
116
- if (key === resolvedColorScheme) {
117
- mergedTheme.vars = _extends({}, mergedTheme.vars, vars);
118
- }
141
+ mergedTheme.vars = deepmerge(mergedTheme.vars, vars);
119
142
 
120
143
  const resolvedDefaultColorScheme = (() => {
121
144
  if (typeof defaultColorScheme === 'string') {
@@ -137,9 +160,50 @@ export default function createCssVarsProvider(options) {
137
160
  });
138
161
  React.useEffect(() => {
139
162
  if (colorScheme) {
140
- document.body.setAttribute(attribute, colorScheme);
163
+ // attaches attribute to <html> because the css variables are attached to :root (html)
164
+ document.documentElement.setAttribute(attribute, colorScheme);
141
165
  }
142
166
  }, [colorScheme, attribute]);
167
+ React.useEffect(() => {
168
+ if (!mode || !enableColorScheme) {
169
+ return undefined;
170
+ }
171
+
172
+ const priorColorScheme = document.documentElement.style.getPropertyValue('color-scheme'); // `color-scheme` tells browser to render built-in elements according to its value: `light` or `dark`
173
+
174
+ if (mode === 'system') {
175
+ document.documentElement.style.setProperty('color-scheme', systemMode);
176
+ } else {
177
+ document.documentElement.style.setProperty('color-scheme', mode);
178
+ }
179
+
180
+ return () => {
181
+ document.documentElement.style.setProperty('color-scheme', priorColorScheme);
182
+ };
183
+ }, [mode, systemMode]);
184
+ React.useEffect(() => {
185
+ let timer;
186
+
187
+ if (disableTransitionOnChange && hasMounted.current) {
188
+ // credit: https://github.com/pacocoursey/next-themes/blob/b5c2bad50de2d61ad7b52a9c5cdc801a78507d7a/index.tsx#L313
189
+ const css = document.createElement('style');
190
+ css.appendChild(document.createTextNode(DISABLE_CSS_TRANSITION));
191
+ document.head.appendChild(css); // Force browser repaint
192
+
193
+ (() => window.getComputedStyle(document.body))();
194
+
195
+ timer = setTimeout(() => {
196
+ document.head.removeChild(css);
197
+ }, 1);
198
+ }
199
+
200
+ return () => {
201
+ clearTimeout(timer);
202
+ };
203
+ }, [colorScheme]);
204
+ React.useEffect(() => {
205
+ hasMounted.current = true;
206
+ }, []);
143
207
  return /*#__PURE__*/_jsxs(ColorSchemeContext.Provider, {
144
208
  value: {
145
209
  mode,
@@ -170,7 +234,7 @@ export default function createCssVarsProvider(options) {
170
234
  attribute: PropTypes.string,
171
235
 
172
236
  /**
173
- * Your component tree.
237
+ * The component tree.
174
238
  */
175
239
  children: PropTypes.node,
176
240
 
@@ -190,7 +254,7 @@ export default function createCssVarsProvider(options) {
190
254
  modeStorageKey: PropTypes.string,
191
255
 
192
256
  /**
193
- * css variable prefix
257
+ * CSS variable prefix.
194
258
  */
195
259
  prefix: PropTypes.string,
196
260