@mui/system 5.8.4 → 5.8.7

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 (41) hide show
  1. package/Box/Box.spec.d.ts +1 -1
  2. package/CHANGELOG.md +185 -4
  3. package/Container/Container.d.ts +13 -13
  4. package/Container/ContainerProps.d.ts +40 -40
  5. package/Container/containerClasses.d.ts +22 -22
  6. package/Container/createContainer.d.ts +18 -18
  7. package/createBox.spec.d.ts +1 -1
  8. package/createTheme/createSpacing.d.ts +10 -10
  9. package/cssVars/createCssVarsProvider.d.ts +13 -18
  10. package/cssVars/createCssVarsProvider.js +13 -17
  11. package/cssVars/createCssVarsProvider.spec.d.ts +1 -1
  12. package/cssVars/createGetCssVar.d.ts +5 -5
  13. package/cssVars/createGetCssVar.js +2 -2
  14. package/cssVars/cssVarsParser.d.ts +65 -70
  15. package/cssVars/cssVarsParser.js +4 -19
  16. package/cssVars/getInitColorSchemeScript.d.ts +45 -40
  17. package/cssVars/getInitColorSchemeScript.js +7 -0
  18. package/cssVars/index.d.ts +3 -2
  19. package/cssVars/index.js +9 -1
  20. package/cssVars/useCurrentColorScheme.d.ts +53 -53
  21. package/esm/cssVars/createCssVarsProvider.js +13 -16
  22. package/esm/cssVars/createGetCssVar.js +2 -2
  23. package/esm/cssVars/cssVarsParser.js +4 -19
  24. package/esm/cssVars/getInitColorSchemeScript.js +7 -0
  25. package/esm/cssVars/index.js +2 -1
  26. package/index.js +1 -1
  27. package/index.spec.d.ts +1 -1
  28. package/legacy/cssVars/createCssVarsProvider.js +13 -17
  29. package/legacy/cssVars/createGetCssVar.js +3 -3
  30. package/legacy/cssVars/cssVarsParser.js +4 -20
  31. package/legacy/cssVars/getInitColorSchemeScript.js +3 -1
  32. package/legacy/cssVars/index.js +2 -1
  33. package/legacy/index.js +1 -1
  34. package/modern/cssVars/createCssVarsProvider.js +13 -16
  35. package/modern/cssVars/createGetCssVar.js +2 -2
  36. package/modern/cssVars/cssVarsParser.js +4 -19
  37. package/modern/cssVars/getInitColorSchemeScript.js +7 -0
  38. package/modern/cssVars/index.js +2 -1
  39. package/modern/index.js +1 -1
  40. package/package.json +5 -5
  41. package/styleFunctionSx/styleFunctionSx.spec.d.ts +1 -1
@@ -1,70 +1,65 @@
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, arrayKeys?: Array<string>) => 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, arrayKeys: Array<string>) => 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
- * the CSS variable value will be adjusted based on the provided `basePrefix` & `prefix` which can be found in `parsedTheme`.
47
- *
48
- * @returns {{ css: Object, vars: Object, parsedTheme: typeof theme }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme), and `parsedTheme` is the cloned version of theme.
49
- *
50
- * @example
51
- * const { css, vars, parsedTheme } = parser({
52
- * fontSize: 12,
53
- * lineHeight: 1.2,
54
- * palette: { primary: { 500: 'var(--color)' } }
55
- * }, { prefix: 'foo' })
56
- *
57
- * console.log(css) // { '--foo-fontSize': '12px', '--foo-lineHeight': 1.2, '--foo-palette-primary-500': 'var(--foo-color)' }
58
- * console.log(vars) // { fontSize: '--foo-fontSize', lineHeight: '--foo-lineHeight', palette: { primary: { 500: 'var(--foo-palette-primary-500)' } } }
59
- * console.log(parsedTheme) // { fontSize: 12, lineHeight: 1.2, palette: { primary: { 500: 'var(--foo-color)' } } }
60
- */
61
- export default function cssVarsParser<T extends Record<string, any>>(theme: T, options?: {
62
- prefix?: string;
63
- basePrefix?: string;
64
- shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean;
65
- }): {
66
- css: NestedRecord<string>;
67
- vars: NestedRecord<string>;
68
- parsedTheme: T;
69
- };
70
- 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, arrayKeys?: Array<string>) => 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, arrayKeys: Array<string>) => 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
+ * shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean
41
+ * }} options.
42
+ * `prefix`: The prefix of the generated CSS variables. This function does not change the value.
43
+ *
44
+ * @returns {{ css: Object, vars: Object, parsedTheme: typeof theme }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme), and `parsedTheme` is the cloned version of theme.
45
+ *
46
+ * @example
47
+ * const { css, vars, parsedTheme } = parser({
48
+ * fontSize: 12,
49
+ * lineHeight: 1.2,
50
+ * palette: { primary: { 500: 'var(--color)' } }
51
+ * }, { prefix: 'foo' })
52
+ *
53
+ * console.log(css) // { '--foo-fontSize': '12px', '--foo-lineHeight': 1.2, '--foo-palette-primary-500': 'var(--color)' }
54
+ * console.log(vars) // { fontSize: 'var(--foo-fontSize)', lineHeight: 'var(--foo-lineHeight)', palette: { primary: { 500: 'var(--foo-palette-primary-500)' } } }
55
+ * console.log(parsedTheme) // { fontSize: 12, lineHeight: 1.2, palette: { primary: { 500: 'var(--color)' } } }
56
+ */
57
+ export default function cssVarsParser<T extends Record<string, any>>(theme: T, options?: {
58
+ prefix?: string;
59
+ shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean;
60
+ }): {
61
+ css: NestedRecord<string>;
62
+ vars: NestedRecord<string>;
63
+ parsedTheme: T;
64
+ };
65
+ export {};
@@ -102,13 +102,9 @@ const getCssValue = (keys, value) => {
102
102
  * @param {Object} theme
103
103
  * @param {{
104
104
  * prefix?: string,
105
- * basePrefix?: string,
106
105
  * shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean
107
106
  * }} options.
108
- * `basePrefix`: defined by design system.
109
- * `prefix`: defined by application
110
- *
111
- * the CSS variable value will be adjusted based on the provided `basePrefix` & `prefix` which can be found in `parsedTheme`.
107
+ * `prefix`: The prefix of the generated CSS variables. This function does not change the value.
112
108
  *
113
109
  * @returns {{ css: Object, vars: Object, parsedTheme: typeof theme }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme), and `parsedTheme` is the cloned version of theme.
114
110
  *
@@ -119,16 +115,15 @@ const getCssValue = (keys, value) => {
119
115
  * palette: { primary: { 500: 'var(--color)' } }
120
116
  * }, { prefix: 'foo' })
121
117
  *
122
- * console.log(css) // { '--foo-fontSize': '12px', '--foo-lineHeight': 1.2, '--foo-palette-primary-500': 'var(--foo-color)' }
123
- * console.log(vars) // { fontSize: '--foo-fontSize', lineHeight: '--foo-lineHeight', palette: { primary: { 500: 'var(--foo-palette-primary-500)' } } }
124
- * console.log(parsedTheme) // { fontSize: 12, lineHeight: 1.2, palette: { primary: { 500: 'var(--foo-color)' } } }
118
+ * console.log(css) // { '--foo-fontSize': '12px', '--foo-lineHeight': 1.2, '--foo-palette-primary-500': 'var(--color)' }
119
+ * console.log(vars) // { fontSize: 'var(--foo-fontSize)', lineHeight: 'var(--foo-lineHeight)', palette: { primary: { 500: 'var(--foo-palette-primary-500)' } } }
120
+ * console.log(parsedTheme) // { fontSize: 12, lineHeight: 1.2, palette: { primary: { 500: 'var(--color)' } } }
125
121
  */
126
122
 
127
123
 
128
124
  function cssVarsParser(theme, options) {
129
125
  const {
130
126
  prefix,
131
- basePrefix = '',
132
127
  shouldSkipGeneratingVar
133
128
  } = options || {};
134
129
  const css = {};
@@ -136,16 +131,6 @@ function cssVarsParser(theme, options) {
136
131
  const parsedTheme = {};
137
132
  walkObjectDeep(theme, (keys, value, arrayKeys) => {
138
133
  if (typeof value === 'string' || typeof value === 'number') {
139
- if (typeof value === 'string' && value.match(/var\(\s*--/)) {
140
- // for CSS variable, apply prefix or remove basePrefix from the variable
141
- if (!basePrefix && prefix) {
142
- value = value.replace(/var\(\s*--/g, `var(--${prefix}-`);
143
- } else {
144
- value = prefix ? value.replace(new RegExp(`var\\(\\s*--${basePrefix}`, 'g'), `var(--${prefix}`) // removing spaces
145
- : value.replace(new RegExp(`var\\(\\s*--${basePrefix}-`, 'g'), 'var(--');
146
- }
147
- }
148
-
149
134
  if (!shouldSkipGeneratingVar || shouldSkipGeneratingVar && !shouldSkipGeneratingVar(keys, value)) {
150
135
  // only create css & var if `shouldSkipGeneratingVar` return false
151
136
  const cssVar = `--${prefix ? `${prefix}-` : ''}${keys.join('-')}`;
@@ -1,40 +1,45 @@
1
- /// <reference types="react" />
2
- export declare const DEFAULT_MODE_STORAGE_KEY = "mode";
3
- export declare const DEFAULT_COLOR_SCHEME_STORAGE_KEY = "color-scheme";
4
- export declare const DEFAULT_ATTRIBUTE = "data-color-scheme";
5
- export interface GetInitColorSchemeScriptOptions {
6
- /**
7
- * If `true`, the initial color scheme is set to the user's prefers-color-scheme mode
8
- * @default false
9
- */
10
- enableSystem?: boolean;
11
- /**
12
- * The default color scheme to be used on the light mode
13
- */
14
- defaultLightColorScheme?: string;
15
- /**
16
- * The default color scheme to be used on the dark mode
17
- */
18
- defaultDarkColorScheme?: string;
19
- /**
20
- * The node (provided as string) used to attach the color-scheme attribute
21
- * @default 'document.documentElement'
22
- */
23
- colorSchemeNode?: string;
24
- /**
25
- * localStorage key used to store `mode`
26
- * @default 'mode'
27
- */
28
- modeStorageKey?: string;
29
- /**
30
- * localStorage key used to store `colorScheme`
31
- * @default 'color-scheme'
32
- */
33
- colorSchemeStorageKey?: string;
34
- /**
35
- * DOM attribute for applying color scheme
36
- * @default 'data-color-scheme'
37
- */
38
- attribute?: string;
39
- }
40
- export default function getInitColorSchemeScript(options?: GetInitColorSchemeScriptOptions): JSX.Element;
1
+ /// <reference types="react" />
2
+ export declare const DEFAULT_MODE_STORAGE_KEY = "mode";
3
+ export declare const DEFAULT_COLOR_SCHEME_STORAGE_KEY = "color-scheme";
4
+ export declare const DEFAULT_ATTRIBUTE = "data-color-scheme";
5
+ export interface GetInitColorSchemeScriptOptions {
6
+ /**
7
+ * Indicate to the browser which color scheme is used (light or dark) for rendering built-in UI
8
+ * @default true
9
+ */
10
+ enableColorScheme?: boolean;
11
+ /**
12
+ * If `true`, the initial color scheme is set to the user's prefers-color-scheme mode
13
+ * @default false
14
+ */
15
+ enableSystem?: boolean;
16
+ /**
17
+ * The default color scheme to be used on the light mode
18
+ */
19
+ defaultLightColorScheme?: string;
20
+ /**
21
+ * The default color scheme to be used on the dark mode
22
+ */
23
+ defaultDarkColorScheme?: string;
24
+ /**
25
+ * The node (provided as string) used to attach the color-scheme attribute
26
+ * @default 'document.documentElement'
27
+ */
28
+ colorSchemeNode?: string;
29
+ /**
30
+ * localStorage key used to store `mode`
31
+ * @default 'mode'
32
+ */
33
+ modeStorageKey?: string;
34
+ /**
35
+ * localStorage key used to store `colorScheme`
36
+ * @default 'color-scheme'
37
+ */
38
+ colorSchemeStorageKey?: string;
39
+ /**
40
+ * DOM attribute for applying color scheme
41
+ * @default 'data-color-scheme'
42
+ */
43
+ attribute?: string;
44
+ }
45
+ export default function getInitColorSchemeScript(options?: GetInitColorSchemeScriptOptions): JSX.Element;
@@ -23,6 +23,7 @@ exports.DEFAULT_ATTRIBUTE = DEFAULT_ATTRIBUTE;
23
23
 
24
24
  function getInitColorSchemeScript(options) {
25
25
  const {
26
+ enableColorScheme = true,
26
27
  enableSystem = false,
27
28
  defaultLightColorScheme = 'light',
28
29
  defaultDarkColorScheme = 'dark',
@@ -36,13 +37,16 @@ function getInitColorSchemeScript(options) {
36
37
  dangerouslySetInnerHTML: {
37
38
  __html: `(function() { try {
38
39
  var mode = localStorage.getItem('${modeStorageKey}');
40
+ var cssColorScheme = mode;
39
41
  var colorScheme = '';
40
42
  if (mode === 'system' || (!mode && !!${enableSystem})) {
41
43
  // handle system mode
42
44
  var mql = window.matchMedia('(prefers-color-scheme: dark)');
43
45
  if (mql.matches) {
46
+ cssColorScheme = 'dark';
44
47
  colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || '${defaultDarkColorScheme}';
45
48
  } else {
49
+ cssColorScheme = 'light';
46
50
  colorScheme = localStorage.getItem('${colorSchemeStorageKey}-light') || '${defaultLightColorScheme}';
47
51
  }
48
52
  }
@@ -55,6 +59,9 @@ function getInitColorSchemeScript(options) {
55
59
  if (colorScheme) {
56
60
  ${colorSchemeNode}.setAttribute('${attribute}', colorScheme);
57
61
  }
62
+ if (${enableColorScheme} && !!cssColorScheme) {
63
+ ${colorSchemeNode}.style.setProperty('color-scheme', cssColorScheme);
64
+ }
58
65
  } catch (e) {} })();`
59
66
  }
60
67
  });
@@ -1,2 +1,3 @@
1
- export { default } from './createCssVarsProvider';
2
- export type { CreateCssVarsProviderResult } from './createCssVarsProvider';
1
+ export { default } from './createCssVarsProvider';
2
+ export type { CreateCssVarsProviderResult, CssVarsProviderConfig, ColorSchemeContextValue, } from './createCssVarsProvider';
3
+ export { default as getInitColorSchemeScript } from './getInitColorSchemeScript';
package/cssVars/index.js CHANGED
@@ -11,5 +11,13 @@ Object.defineProperty(exports, "default", {
11
11
  return _createCssVarsProvider.default;
12
12
  }
13
13
  });
14
+ Object.defineProperty(exports, "getInitColorSchemeScript", {
15
+ enumerable: true,
16
+ get: function () {
17
+ return _getInitColorSchemeScript.default;
18
+ }
19
+ });
20
+
21
+ var _createCssVarsProvider = _interopRequireDefault(require("./createCssVarsProvider"));
14
22
 
15
- var _createCssVarsProvider = _interopRequireDefault(require("./createCssVarsProvider"));
23
+ var _getInitColorSchemeScript = _interopRequireDefault(require("./getInitColorSchemeScript"));
@@ -1,53 +1,53 @@
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
- interface UseCurrentColoSchemeOptions<SupportedColorScheme extends string> {
44
- defaultLightColorScheme: SupportedColorScheme;
45
- defaultDarkColorScheme: SupportedColorScheme;
46
- supportedColorSchemes: Array<SupportedColorScheme>;
47
- defaultMode?: Mode;
48
- modeStorageKey?: string;
49
- colorSchemeStorageKey?: string;
50
- storageWindow?: Window | null;
51
- }
52
- export default function useCurrentColorScheme<SupportedColorScheme extends string>(options: UseCurrentColoSchemeOptions<SupportedColorScheme>): Result<SupportedColorScheme>;
53
- export {};
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
+ interface UseCurrentColoSchemeOptions<SupportedColorScheme extends string> {
44
+ defaultLightColorScheme: SupportedColorScheme;
45
+ defaultDarkColorScheme: SupportedColorScheme;
46
+ supportedColorSchemes: Array<SupportedColorScheme>;
47
+ defaultMode?: Mode;
48
+ modeStorageKey?: string;
49
+ colorSchemeStorageKey?: string;
50
+ storageWindow?: Window | null;
51
+ }
52
+ export default function useCurrentColorScheme<SupportedColorScheme extends string>(options: UseCurrentColoSchemeOptions<SupportedColorScheme>): Result<SupportedColorScheme>;
53
+ export {};
@@ -1,7 +1,7 @@
1
1
  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
- const _excluded = ["colorSchemes", "components"];
4
+ const _excluded = ["colorSchemes", "components", "cssVarPrefix"];
5
5
  import * as React from 'react';
6
6
  import PropTypes from 'prop-types';
7
7
  import { deepmerge, unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils';
@@ -10,7 +10,6 @@ import cssVarsParser from './cssVarsParser';
10
10
  import ThemeProvider from '../ThemeProvider';
11
11
  import systemGetInitColorSchemeScript, { DEFAULT_ATTRIBUTE, DEFAULT_COLOR_SCHEME_STORAGE_KEY, DEFAULT_MODE_STORAGE_KEY } from './getInitColorSchemeScript';
12
12
  import useCurrentColorScheme from './useCurrentColorScheme';
13
- import createGetCssVar from './createGetCssVar';
14
13
  import { jsx as _jsx } from "react/jsx-runtime";
15
14
  import { jsxs as _jsxs } from "react/jsx-runtime";
16
15
  export const DISABLE_CSS_TRANSITION = '*{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}';
@@ -24,8 +23,7 @@ export default function createCssVarsProvider(options) {
24
23
  defaultColorScheme: designSystemColorScheme,
25
24
  disableTransitionOnChange: designSystemTransitionOnChange = false,
26
25
  enableColorScheme: designSystemEnableColorScheme = true,
27
- prefix: designSystemPrefix = '',
28
- shouldSkipGeneratingVar,
26
+ shouldSkipGeneratingVar: designSystemShouldSkipGeneratingVar,
29
27
  resolveTheme
30
28
  } = options;
31
29
 
@@ -48,7 +46,6 @@ export default function createCssVarsProvider(options) {
48
46
  function CssVarsProvider({
49
47
  children,
50
48
  theme: themeProp = defaultTheme,
51
- prefix = designSystemPrefix,
52
49
  modeStorageKey = defaultModeStorageKey,
53
50
  colorSchemeStorageKey = defaultColorSchemeStorageKey,
54
51
  attribute = defaultAttribute,
@@ -59,13 +56,15 @@ export default function createCssVarsProvider(options) {
59
56
  storageWindow = typeof window === 'undefined' ? undefined : window,
60
57
  documentNode = typeof document === 'undefined' ? undefined : document,
61
58
  colorSchemeNode = typeof document === 'undefined' ? undefined : document.documentElement,
62
- colorSchemeSelector = ':root'
59
+ colorSchemeSelector = ':root',
60
+ shouldSkipGeneratingVar = designSystemShouldSkipGeneratingVar
63
61
  }) {
64
62
  const hasMounted = React.useRef(false);
65
63
 
66
64
  const {
67
65
  colorSchemes = {},
68
- components = {}
66
+ components = {},
67
+ cssVarPrefix
69
68
  } = themeProp,
70
69
  restThemeProp = _objectWithoutPropertiesLoose(themeProp, _excluded);
71
70
 
@@ -110,16 +109,14 @@ export default function createCssVarsProvider(options) {
110
109
  vars: rootVars,
111
110
  parsedTheme
112
111
  } = cssVarsParser(theme, {
113
- prefix,
114
- basePrefix: designSystemPrefix,
112
+ prefix: cssVarPrefix,
115
113
  shouldSkipGeneratingVar
116
114
  });
117
115
  theme = _extends({}, parsedTheme, {
118
116
  components,
119
117
  colorSchemes,
120
- prefix,
118
+ cssVarPrefix,
121
119
  vars: rootVars,
122
- getCssVar: createGetCssVar(prefix),
123
120
  getColorSchemeSelector: targetColorScheme => `[${attribute}="${targetColorScheme}"] &`
124
121
  });
125
122
  const defaultColorSchemeStyleSheet = {};
@@ -130,8 +127,7 @@ export default function createCssVarsProvider(options) {
130
127
  vars,
131
128
  parsedTheme: parsedScheme
132
129
  } = cssVarsParser(scheme, {
133
- prefix,
134
- basePrefix: designSystemPrefix,
130
+ prefix: cssVarPrefix,
135
131
  shouldSkipGeneratingVar
136
132
  });
137
133
  theme.vars = deepmerge(theme.vars, vars);
@@ -295,9 +291,9 @@ export default function createCssVarsProvider(options) {
295
291
  modeStorageKey: PropTypes.string,
296
292
 
297
293
  /**
298
- * CSS variable prefix.
294
+ * A function to determine if the key, value should be attached as CSS Variable
299
295
  */
300
- prefix: PropTypes.string,
296
+ shouldSkipGeneratingVar: PropTypes.func,
301
297
 
302
298
  /**
303
299
  * The window that attaches the 'storage' event listener
@@ -314,7 +310,8 @@ export default function createCssVarsProvider(options) {
314
310
  const getInitColorSchemeScript = params => systemGetInitColorSchemeScript(_extends({
315
311
  attribute: defaultAttribute,
316
312
  colorSchemeStorageKey: defaultColorSchemeStorageKey,
317
- modeStorageKey: defaultModeStorageKey
313
+ modeStorageKey: defaultModeStorageKey,
314
+ enableColorScheme: designSystemEnableColorScheme
318
315
  }, params));
319
316
 
320
317
  return {
@@ -18,8 +18,8 @@ export default function createGetCssVar(prefix = '') {
18
18
  } // AdditionalVars makes `getCssVar` less strict, so it can be use like this `getCssVar('non-mui-variable')` without type error.
19
19
 
20
20
 
21
- const getCssVar = (field, ...vars) => {
22
- return `var(--${prefix ? `${prefix}-` : ''}${field}${appendVar(...vars)})`;
21
+ const getCssVar = (field, ...fallbacks) => {
22
+ return `var(--${prefix ? `${prefix}-` : ''}${field}${appendVar(...fallbacks)})`;
23
23
  };
24
24
 
25
25
  return getCssVar;
@@ -88,13 +88,9 @@ const getCssValue = (keys, value) => {
88
88
  * @param {Object} theme
89
89
  * @param {{
90
90
  * prefix?: string,
91
- * basePrefix?: string,
92
91
  * shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean
93
92
  * }} options.
94
- * `basePrefix`: defined by design system.
95
- * `prefix`: defined by application
96
- *
97
- * the CSS variable value will be adjusted based on the provided `basePrefix` & `prefix` which can be found in `parsedTheme`.
93
+ * `prefix`: The prefix of the generated CSS variables. This function does not change the value.
98
94
  *
99
95
  * @returns {{ css: Object, vars: Object, parsedTheme: typeof theme }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme), and `parsedTheme` is the cloned version of theme.
100
96
  *
@@ -105,16 +101,15 @@ const getCssValue = (keys, value) => {
105
101
  * palette: { primary: { 500: 'var(--color)' } }
106
102
  * }, { prefix: 'foo' })
107
103
  *
108
- * console.log(css) // { '--foo-fontSize': '12px', '--foo-lineHeight': 1.2, '--foo-palette-primary-500': 'var(--foo-color)' }
109
- * console.log(vars) // { fontSize: '--foo-fontSize', lineHeight: '--foo-lineHeight', palette: { primary: { 500: 'var(--foo-palette-primary-500)' } } }
110
- * console.log(parsedTheme) // { fontSize: 12, lineHeight: 1.2, palette: { primary: { 500: 'var(--foo-color)' } } }
104
+ * console.log(css) // { '--foo-fontSize': '12px', '--foo-lineHeight': 1.2, '--foo-palette-primary-500': 'var(--color)' }
105
+ * console.log(vars) // { fontSize: 'var(--foo-fontSize)', lineHeight: 'var(--foo-lineHeight)', palette: { primary: { 500: 'var(--foo-palette-primary-500)' } } }
106
+ * console.log(parsedTheme) // { fontSize: 12, lineHeight: 1.2, palette: { primary: { 500: 'var(--color)' } } }
111
107
  */
112
108
 
113
109
 
114
110
  export default function cssVarsParser(theme, options) {
115
111
  const {
116
112
  prefix,
117
- basePrefix = '',
118
113
  shouldSkipGeneratingVar
119
114
  } = options || {};
120
115
  const css = {};
@@ -122,16 +117,6 @@ export default function cssVarsParser(theme, options) {
122
117
  const parsedTheme = {};
123
118
  walkObjectDeep(theme, (keys, value, arrayKeys) => {
124
119
  if (typeof value === 'string' || typeof value === 'number') {
125
- if (typeof value === 'string' && value.match(/var\(\s*--/)) {
126
- // for CSS variable, apply prefix or remove basePrefix from the variable
127
- if (!basePrefix && prefix) {
128
- value = value.replace(/var\(\s*--/g, `var(--${prefix}-`);
129
- } else {
130
- value = prefix ? value.replace(new RegExp(`var\\(\\s*--${basePrefix}`, 'g'), `var(--${prefix}`) // removing spaces
131
- : value.replace(new RegExp(`var\\(\\s*--${basePrefix}-`, 'g'), 'var(--');
132
- }
133
- }
134
-
135
120
  if (!shouldSkipGeneratingVar || shouldSkipGeneratingVar && !shouldSkipGeneratingVar(keys, value)) {
136
121
  // only create css & var if `shouldSkipGeneratingVar` return false
137
122
  const cssVar = `--${prefix ? `${prefix}-` : ''}${keys.join('-')}`;
@@ -5,6 +5,7 @@ export const DEFAULT_COLOR_SCHEME_STORAGE_KEY = 'color-scheme';
5
5
  export const DEFAULT_ATTRIBUTE = 'data-color-scheme';
6
6
  export default function getInitColorSchemeScript(options) {
7
7
  const {
8
+ enableColorScheme = true,
8
9
  enableSystem = false,
9
10
  defaultLightColorScheme = 'light',
10
11
  defaultDarkColorScheme = 'dark',
@@ -18,13 +19,16 @@ export default function getInitColorSchemeScript(options) {
18
19
  dangerouslySetInnerHTML: {
19
20
  __html: `(function() { try {
20
21
  var mode = localStorage.getItem('${modeStorageKey}');
22
+ var cssColorScheme = mode;
21
23
  var colorScheme = '';
22
24
  if (mode === 'system' || (!mode && !!${enableSystem})) {
23
25
  // handle system mode
24
26
  var mql = window.matchMedia('(prefers-color-scheme: dark)');
25
27
  if (mql.matches) {
28
+ cssColorScheme = 'dark';
26
29
  colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || '${defaultDarkColorScheme}';
27
30
  } else {
31
+ cssColorScheme = 'light';
28
32
  colorScheme = localStorage.getItem('${colorSchemeStorageKey}-light') || '${defaultLightColorScheme}';
29
33
  }
30
34
  }
@@ -37,6 +41,9 @@ export default function getInitColorSchemeScript(options) {
37
41
  if (colorScheme) {
38
42
  ${colorSchemeNode}.setAttribute('${attribute}', colorScheme);
39
43
  }
44
+ if (${enableColorScheme} && !!cssColorScheme) {
45
+ ${colorSchemeNode}.style.setProperty('color-scheme', cssColorScheme);
46
+ }
40
47
  } catch (e) {} })();`
41
48
  }
42
49
  });
@@ -1 +1,2 @@
1
- export { default } from './createCssVarsProvider';
1
+ export { default } from './createCssVarsProvider';
2
+ export { default as getInitColorSchemeScript } from './getInitColorSchemeScript';