@mui/system 5.2.0 → 5.2.4

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 (52) hide show
  1. package/Box/Box.spec.d.ts +1 -1
  2. package/CHANGELOG.md +244 -1
  3. package/createBox.d.ts +3 -3
  4. package/createBox.spec.d.ts +1 -0
  5. package/createTheme/createBreakpoints.d.ts +6 -0
  6. package/createTheme/createBreakpoints.js +16 -0
  7. package/createTheme/createSpacing.d.ts +10 -10
  8. package/cssVars/createCssVarsProvider.d.ts +24 -10
  9. package/cssVars/createCssVarsProvider.js +81 -8
  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 +1 -1
  15. package/cssVars/index.d.ts +2 -2
  16. package/cssVars/useCurrentColorScheme.d.ts +50 -50
  17. package/esm/createTheme/createBreakpoints.js +16 -0
  18. package/esm/cssVars/createCssVarsProvider.js +77 -8
  19. package/esm/cssVars/cssVarsParser.js +18 -16
  20. package/esm/cssVars/getInitColorSchemeScript.js +1 -1
  21. package/esm/index.js +1 -0
  22. package/esm/sx/index.js +1 -0
  23. package/esm/sx/sx.js +12 -0
  24. package/esm/useThemeProps/getThemeProps.js +2 -17
  25. package/index.d.ts +2 -0
  26. package/index.js +10 -1
  27. package/index.spec.d.ts +1 -1
  28. package/legacy/createTheme/createBreakpoints.js +16 -0
  29. package/legacy/cssVars/createCssVarsProvider.js +80 -8
  30. package/legacy/cssVars/cssVarsParser.js +20 -14
  31. package/legacy/cssVars/getInitColorSchemeScript.js +1 -1
  32. package/legacy/index.js +2 -1
  33. package/legacy/sx/index.js +1 -0
  34. package/legacy/sx/sx.js +13 -0
  35. package/legacy/useThemeProps/getThemeProps.js +2 -17
  36. package/modern/createTheme/createBreakpoints.js +16 -0
  37. package/modern/cssVars/createCssVarsProvider.js +75 -8
  38. package/modern/cssVars/cssVarsParser.js +18 -16
  39. package/modern/cssVars/getInitColorSchemeScript.js +1 -1
  40. package/modern/index.js +2 -1
  41. package/modern/sx/index.js +1 -0
  42. package/modern/sx/sx.js +12 -0
  43. package/modern/useThemeProps/getThemeProps.js +2 -17
  44. package/package.json +5 -5
  45. package/styleFunctionSx/styleFunctionSx.d.ts +1 -1
  46. package/styleFunctionSx/styleFunctionSx.spec.d.ts +1 -1
  47. package/sx/index.d.ts +1 -0
  48. package/sx/index.js +15 -0
  49. package/sx/package.json +6 -0
  50. package/sx/sx.d.ts +4 -0
  51. package/sx/sx.js +22 -0
  52. 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
- enableSystem?: boolean;
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;
@@ -52,7 +52,7 @@ function getInitColorSchemeScript(options) {
52
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>;
@@ -51,6 +51,21 @@ export default function createBreakpoints(breakpoints) {
51
51
  return up(key);
52
52
  }
53
53
 
54
+ function not(key) {
55
+ // handle first and last key separately, for better readability
56
+ const keyIndex = keys.indexOf(key);
57
+
58
+ if (keyIndex === 0) {
59
+ return up(keys[1]);
60
+ }
61
+
62
+ if (keyIndex === keys.length - 1) {
63
+ return down(keys[keyIndex]);
64
+ }
65
+
66
+ return between(key, keys[keys.indexOf(key) + 1]).replace('@media', '@media not all and');
67
+ }
68
+
54
69
  return _extends({
55
70
  keys,
56
71
  values,
@@ -58,6 +73,7 @@ export default function createBreakpoints(breakpoints) {
58
73
  down,
59
74
  between,
60
75
  only,
76
+ not,
61
77
  unit
62
78
  }, other);
63
79
  }
@@ -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(false); // 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,8 +122,11 @@ export default function createCssVarsProvider(options) {
100
122
  shouldSkipGeneratingVar
101
123
  });
102
124
  mergedTheme = _extends({}, mergedTheme, colorSchemes[resolvedColorScheme], {
125
+ components,
103
126
  colorSchemes,
104
- vars: rootVars
127
+ vars: rootVars,
128
+ spacing: themeProp.spacing ? createSpacing(themeProp.spacing) : systemSpacing,
129
+ breakpoints: themeProp.breakpoints ? createBreakpoints(themeProp.breakpoints) : systemBreakpoints
105
130
  });
106
131
  const styleSheet = {};
107
132
  Object.entries(colorSchemes).forEach(([key, scheme]) => {
@@ -135,9 +160,53 @@ export default function createCssVarsProvider(options) {
135
160
  });
136
161
  React.useEffect(() => {
137
162
  if (colorScheme) {
138
- 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);
139
165
  }
140
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
+ return () => {
207
+ hasMounted.current = false;
208
+ };
209
+ }, []);
141
210
  return /*#__PURE__*/_jsxs(ColorSchemeContext.Provider, {
142
211
  value: {
143
212
  mode,
@@ -168,7 +237,7 @@ export default function createCssVarsProvider(options) {
168
237
  attribute: PropTypes.string,
169
238
 
170
239
  /**
171
- * Your component tree.
240
+ * The component tree.
172
241
  */
173
242
  children: PropTypes.node,
174
243
 
@@ -188,7 +257,7 @@ export default function createCssVarsProvider(options) {
188
257
  modeStorageKey: PropTypes.string,
189
258
 
190
259
  /**
191
- * css variable prefix
260
+ * CSS variable prefix.
192
261
  */
193
262
  prefix: PropTypes.string,
194
263
 
@@ -1,5 +1,3 @@
1
- import _extends from "@babel/runtime/helpers/esm/extends";
2
-
3
1
  /**
4
2
  * This function create an object from keys, value and then assign to target
5
3
  *
@@ -45,14 +43,16 @@ export const assignNestedKeys = (obj, keys, value) => {
45
43
  * // ['palette', 'primary', 'main'] '#000000'
46
44
  */
47
45
 
48
- export const walkObjectDeep = (obj, callback) => {
46
+ export const walkObjectDeep = (obj, callback, shouldSkipPaths) => {
49
47
  function recurse(object, parentKeys = []) {
50
48
  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);
49
+ if (!shouldSkipPaths || shouldSkipPaths && !shouldSkipPaths([...parentKeys, key])) {
50
+ if (value !== undefined && value !== null) {
51
+ if (typeof value === 'object' && Object.keys(value).length > 0) {
52
+ recurse(value, [...parentKeys, key]);
53
+ } else {
54
+ callback([...parentKeys, key], value, object);
55
+ }
56
56
  }
57
57
  }
58
58
  });
@@ -64,7 +64,7 @@ export const walkObjectDeep = (obj, callback) => {
64
64
  const getCssValue = (keys, value) => {
65
65
  if (typeof value === 'number') {
66
66
  if (['lineHeight', 'fontWeight', 'opacity', 'zIndex'].some(prop => keys.includes(prop))) {
67
- // css property that are unitless
67
+ // CSS property that are unitless
68
68
  return value;
69
69
  }
70
70
 
@@ -102,10 +102,6 @@ const getCssValue = (keys, value) => {
102
102
 
103
103
 
104
104
  export default function cssVarsParser(theme, options) {
105
- const clonedTheme = _extends({}, theme);
106
-
107
- delete clonedTheme.vars; // remove 'vars' from the structure
108
-
109
105
  const {
110
106
  prefix,
111
107
  basePrefix = '',
@@ -113,13 +109,18 @@ export default function cssVarsParser(theme, options) {
113
109
  } = options || {};
114
110
  const css = {};
115
111
  const vars = {};
116
- walkObjectDeep(clonedTheme, (keys, val, scope) => {
112
+ walkObjectDeep(theme, (keys, val, scope) => {
117
113
  if (typeof val === 'string' || typeof val === 'number') {
118
114
  let value = val;
119
115
 
120
116
  if (typeof value === 'string' && value.startsWith('var')) {
121
117
  // 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
118
+ if (!basePrefix && prefix) {
119
+ value = value.replace(/var\(--/g, `var(--${prefix}-`);
120
+ } else {
121
+ value = prefix ? value.replace(new RegExp(basePrefix, 'g'), prefix) : value.replace(new RegExp(`${basePrefix}-`, 'g'), '');
122
+ } // scope is the deepest object in the tree, keys is the theme path keys
123
+
123
124
 
124
125
  scope[keys.slice(-1)[0]] = value;
125
126
  }
@@ -133,7 +134,8 @@ export default function cssVarsParser(theme, options) {
133
134
  assignNestedKeys(vars, keys, `var(${cssVar})`);
134
135
  }
135
136
  }
136
- });
137
+ }, keys => keys[0] === 'vars' // skip 'vars/*' paths
138
+ );
137
139
  return {
138
140
  css,
139
141
  vars
@@ -34,7 +34,7 @@ export default function getInitColorSchemeScript(options) {
34
34
  colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || '${defaultDarkColorScheme}';
35
35
  }
36
36
  if (colorScheme) {
37
- document.body.setAttribute('${attribute}', colorScheme);
37
+ document.documentElement.setAttribute('${attribute}', colorScheme);
38
38
  }
39
39
  } catch (e) {} })();`
40
40
  }
package/esm/index.js CHANGED
@@ -22,6 +22,7 @@ export { default as style, getPath } from './style';
22
22
  export { default as typography } from './typography';
23
23
  export * from './typography';
24
24
  export { default as unstable_styleFunctionSx, extendSxProp as unstable_extendSxProp } from './styleFunctionSx';
25
+ export { default as experimental_sx } from './sx';
25
26
  export { default as unstable_getThemeValue } from './getThemeValue';
26
27
  export { default as Box } from './Box';
27
28
  export { default as createBox } from './createBox';
@@ -0,0 +1 @@
1
+ export { default } from './sx';