@mui/system 5.6.3 → 5.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/Box/Box.spec.d.ts +1 -1
  2. package/CHANGELOG.md +238 -0
  3. package/Container/Container.d.ts +13 -0
  4. package/Container/Container.js +81 -0
  5. package/Container/ContainerProps.d.ts +40 -0
  6. package/Container/ContainerProps.js +5 -0
  7. package/Container/containerClasses.d.ts +22 -0
  8. package/Container/containerClasses.js +17 -0
  9. package/Container/createContainer.d.ts +18 -0
  10. package/Container/createContainer.js +172 -0
  11. package/Container/index.d.ts +5 -0
  12. package/Container/index.js +42 -0
  13. package/Container/package.json +6 -0
  14. package/ThemeProvider/ThemeProvider.d.ts +1 -1
  15. package/createBox.spec.d.ts +1 -1
  16. package/createStyled.js +3 -1
  17. package/createTheme/createBreakpoints.d.ts +5 -0
  18. package/createTheme/createSpacing.d.ts +10 -10
  19. package/cssVars/createCssVarsProvider.d.ts +25 -0
  20. package/cssVars/createCssVarsProvider.js +74 -57
  21. package/cssVars/createCssVarsProvider.spec.d.ts +1 -1
  22. package/cssVars/createGetCssVar.d.ts +5 -5
  23. package/cssVars/cssVarsParser.d.ts +70 -70
  24. package/cssVars/cssVarsParser.js +11 -9
  25. package/cssVars/getInitColorSchemeScript.d.ts +40 -12
  26. package/cssVars/getInitColorSchemeScript.js +4 -3
  27. package/cssVars/index.d.ts +2 -2
  28. package/cssVars/useCurrentColorScheme.d.ts +53 -50
  29. package/cssVars/useCurrentColorScheme.js +17 -7
  30. package/esm/Container/Container.js +70 -0
  31. package/esm/Container/ContainerProps.js +1 -0
  32. package/esm/Container/containerClasses.js +6 -0
  33. package/esm/Container/createContainer.js +151 -0
  34. package/esm/Container/index.js +3 -0
  35. package/esm/createStyled.js +3 -1
  36. package/esm/cssVars/createCssVarsProvider.js +76 -57
  37. package/esm/cssVars/cssVarsParser.js +11 -9
  38. package/esm/cssVars/getInitColorSchemeScript.js +4 -3
  39. package/esm/cssVars/useCurrentColorScheme.js +17 -7
  40. package/esm/index.js +4 -1
  41. package/index.d.ts +6 -0
  42. package/index.js +32 -2
  43. package/index.spec.d.ts +1 -1
  44. package/legacy/Container/Container.js +70 -0
  45. package/legacy/Container/ContainerProps.js +1 -0
  46. package/legacy/Container/containerClasses.js +6 -0
  47. package/legacy/Container/createContainer.js +148 -0
  48. package/legacy/Container/index.js +3 -0
  49. package/legacy/createStyled.js +3 -1
  50. package/legacy/cssVars/createCssVarsProvider.js +83 -55
  51. package/legacy/cssVars/cssVarsParser.js +11 -7
  52. package/legacy/cssVars/getInitColorSchemeScript.js +6 -3
  53. package/legacy/cssVars/useCurrentColorScheme.js +20 -9
  54. package/legacy/index.js +5 -2
  55. package/modern/Container/Container.js +70 -0
  56. package/modern/Container/ContainerProps.js +1 -0
  57. package/modern/Container/containerClasses.js +6 -0
  58. package/modern/Container/createContainer.js +151 -0
  59. package/modern/Container/index.js +3 -0
  60. package/modern/createStyled.js +3 -1
  61. package/modern/cssVars/createCssVarsProvider.js +76 -55
  62. package/modern/cssVars/cssVarsParser.js +11 -9
  63. package/modern/cssVars/getInitColorSchemeScript.js +4 -3
  64. package/modern/cssVars/useCurrentColorScheme.js +17 -7
  65. package/modern/index.js +5 -2
  66. package/package.json +5 -5
  67. package/styleFunctionSx/styleFunctionSx.spec.d.ts +1 -1
@@ -0,0 +1,70 @@
1
+ import PropTypes from 'prop-types';
2
+ import createContainer from './createContainer';
3
+ /**
4
+ *
5
+ * Demos:
6
+ *
7
+ * - [Container (Material UI)](https://mui.com/material-ui/react-container/)
8
+ * - [Container (MUI System)](https://mui.com/system/react-container/)
9
+ *
10
+ * API:
11
+ *
12
+ * - [Container API](https://mui.com/system/api/container/)
13
+ */
14
+
15
+ const Container = createContainer();
16
+ process.env.NODE_ENV !== "production" ? Container.propTypes
17
+ /* remove-proptypes */
18
+ = {
19
+ // ----------------------------- Warning --------------------------------
20
+ // | These PropTypes are generated from the TypeScript type definitions |
21
+ // | To update them edit TypeScript types and run "yarn proptypes" |
22
+ // ----------------------------------------------------------------------
23
+
24
+ /**
25
+ * @ignore
26
+ */
27
+ children: PropTypes.node,
28
+
29
+ /**
30
+ * Override or extend the styles applied to the component.
31
+ */
32
+ classes: PropTypes.object,
33
+
34
+ /**
35
+ * The component used for the root node.
36
+ * Either a string to use a HTML element or a component.
37
+ */
38
+ component: PropTypes.elementType,
39
+
40
+ /**
41
+ * If `true`, the left and right padding is removed.
42
+ * @default false
43
+ */
44
+ disableGutters: PropTypes.bool,
45
+
46
+ /**
47
+ * Set the max-width to match the min-width of the current breakpoint.
48
+ * This is useful if you'd prefer to design for a fixed set of sizes
49
+ * instead of trying to accommodate a fully fluid viewport.
50
+ * It's fluid by default.
51
+ * @default false
52
+ */
53
+ fixed: PropTypes.bool,
54
+
55
+ /**
56
+ * Determine the max-width of the container.
57
+ * The container width grows with the size of the screen.
58
+ * Set to `false` to disable `maxWidth`.
59
+ * @default 'lg'
60
+ */
61
+ maxWidth: PropTypes
62
+ /* @typescript-to-proptypes-ignore */
63
+ .oneOfType([PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl', false]), PropTypes.string]),
64
+
65
+ /**
66
+ * The system prop that allows defining system overrides as well as additional CSS styles.
67
+ */
68
+ sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object])
69
+ } : void 0;
70
+ export default Container;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import { unstable_generateUtilityClass as generateUtilityClass, unstable_generateUtilityClasses as generateUtilityClasses } from '@mui/utils';
2
+ export function getContainerUtilityClass(slot) {
3
+ return generateUtilityClass('MuiContainer', slot);
4
+ }
5
+ const containerClasses = generateUtilityClasses('MuiContainer', ['root', 'disableGutters', 'fixed', 'maxWidthXs', 'maxWidthSm', 'maxWidthMd', 'maxWidthLg', 'maxWidthXl']);
6
+ export default containerClasses;
@@ -0,0 +1,151 @@
1
+ import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
2
+ import _extends from "@babel/runtime/helpers/esm/extends";
3
+ const _excluded = ["className", "component", "disableGutters", "fixed", "maxWidth", "classes"];
4
+ import * as React from 'react';
5
+ import PropTypes from 'prop-types';
6
+ import clsx from 'clsx';
7
+ import { unstable_capitalize as capitalize, unstable_composeClasses as composeClasses, unstable_generateUtilityClass as generateUtilityClass } from '@mui/utils';
8
+ import useThemePropsSystem from '../useThemeProps';
9
+ import systemStyled from '../styled';
10
+ import createTheme from '../createTheme';
11
+ import { jsx as _jsx } from "react/jsx-runtime";
12
+ const defaultTheme = createTheme();
13
+ const defaultCreateStyledComponent = systemStyled('div', {
14
+ name: 'MuiContainer',
15
+ slot: 'Root',
16
+ overridesResolver: (props, styles) => {
17
+ const {
18
+ ownerState
19
+ } = props;
20
+ return [styles.root, styles[`maxWidth${capitalize(String(ownerState.maxWidth))}`], ownerState.fixed && styles.fixed, ownerState.disableGutters && styles.disableGutters];
21
+ }
22
+ });
23
+
24
+ const useThemePropsDefault = inProps => useThemePropsSystem({
25
+ props: inProps,
26
+ name: 'MuiContainer',
27
+ defaultTheme
28
+ });
29
+
30
+ const useUtilityClasses = (ownerState, componentName) => {
31
+ const getContainerUtilityClass = slot => {
32
+ return generateUtilityClass(componentName, slot);
33
+ };
34
+
35
+ const {
36
+ classes,
37
+ fixed,
38
+ disableGutters,
39
+ maxWidth
40
+ } = ownerState;
41
+ const slots = {
42
+ root: ['root', maxWidth && `maxWidth${capitalize(String(maxWidth))}`, fixed && 'fixed', disableGutters && 'disableGutters']
43
+ };
44
+ return composeClasses(slots, getContainerUtilityClass, classes);
45
+ };
46
+
47
+ export default function createContainer(options = {}) {
48
+ const {
49
+ // This will allow adding custom styled fn (for example for custom sx style function)
50
+ createStyledComponent = defaultCreateStyledComponent,
51
+ useThemeProps = useThemePropsDefault,
52
+ componentName = 'MuiContainer'
53
+ } = options;
54
+ const ContainerRoot = createStyledComponent(({
55
+ theme,
56
+ ownerState
57
+ }) => _extends({
58
+ width: '100%',
59
+ marginLeft: 'auto',
60
+ boxSizing: 'border-box',
61
+ marginRight: 'auto',
62
+ display: 'block'
63
+ }, !ownerState.disableGutters && {
64
+ paddingLeft: theme.spacing(2),
65
+ paddingRight: theme.spacing(2),
66
+ // @ts-ignore module augmentation fails if custom breakpoints are used
67
+ [theme.breakpoints.up('sm')]: {
68
+ paddingLeft: theme.spacing(3),
69
+ paddingRight: theme.spacing(3)
70
+ }
71
+ }), ({
72
+ theme,
73
+ ownerState
74
+ }) => ownerState.fixed && Object.keys(theme.breakpoints.values).reduce((acc, breakpointValueKey) => {
75
+ const breakpoint = breakpointValueKey;
76
+ const value = theme.breakpoints.values[breakpoint];
77
+
78
+ if (value !== 0) {
79
+ // @ts-ignore
80
+ acc[theme.breakpoints.up(breakpoint)] = {
81
+ maxWidth: `${value}${theme.breakpoints.unit}`
82
+ };
83
+ }
84
+
85
+ return acc;
86
+ }, {}), ({
87
+ theme,
88
+ ownerState
89
+ }) => _extends({}, ownerState.maxWidth === 'xs' && {
90
+ // @ts-ignore module augmentation fails if custom breakpoints are used
91
+ [theme.breakpoints.up('xs')]: {
92
+ // @ts-ignore module augmentation fails if custom breakpoints are used
93
+ maxWidth: Math.max(theme.breakpoints.values.xs, 444)
94
+ }
95
+ }, ownerState.maxWidth && // @ts-ignore module augmentation fails if custom breakpoints are used
96
+ ownerState.maxWidth !== 'xs' && {
97
+ // @ts-ignore module augmentation fails if custom breakpoints are used
98
+ [theme.breakpoints.up(ownerState.maxWidth)]: {
99
+ // @ts-ignore module augmentation fails if custom breakpoints are used
100
+ maxWidth: `${theme.breakpoints.values[ownerState.maxWidth]}${theme.breakpoints.unit}`
101
+ }
102
+ }));
103
+ const Container = /*#__PURE__*/React.forwardRef(function Container(inProps, ref) {
104
+ const props = useThemeProps(inProps);
105
+
106
+ const {
107
+ className,
108
+ component = 'div',
109
+ disableGutters = false,
110
+ fixed = false,
111
+ maxWidth = 'lg'
112
+ } = props,
113
+ other = _objectWithoutPropertiesLoose(props, _excluded);
114
+
115
+ const ownerState = _extends({}, props, {
116
+ component,
117
+ disableGutters,
118
+ fixed,
119
+ maxWidth
120
+ }); // @ts-ignore module augmentation fails if custom breakpoints are used
121
+
122
+
123
+ const classes = useUtilityClasses(ownerState, componentName);
124
+ return (
125
+ /*#__PURE__*/
126
+ // @ts-ignore theme is injected by the styled util
127
+ _jsx(ContainerRoot, _extends({
128
+ as: component // @ts-ignore module augmentation fails if custom breakpoints are used
129
+ ,
130
+ ownerState: ownerState,
131
+ className: clsx(classes.root, className),
132
+ ref: ref
133
+ }, other))
134
+ );
135
+ });
136
+ process.env.NODE_ENV !== "production" ? Container.propTypes
137
+ /* remove-proptypes */
138
+ = {
139
+ children: PropTypes.node,
140
+ classes: PropTypes.object,
141
+ className: PropTypes.string,
142
+ component: PropTypes.elementType,
143
+ disableGutters: PropTypes.bool,
144
+ fixed: PropTypes.bool,
145
+ maxWidth: PropTypes
146
+ /* @typescript-to-proptypes-ignore */
147
+ .oneOfType([PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl', false]), PropTypes.string]),
148
+ sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object])
149
+ } : void 0;
150
+ return Container;
151
+ }
@@ -0,0 +1,3 @@
1
+ export { default } from './Container';
2
+ export { default as containerClasses } from './containerClasses';
3
+ export * from './containerClasses';
@@ -140,7 +140,9 @@ export default function createStyled(input = {}) {
140
140
  if (styleOverrides) {
141
141
  const resolvedStyleOverrides = {};
142
142
  Object.entries(styleOverrides).forEach(([slotKey, slotStyle]) => {
143
- resolvedStyleOverrides[slotKey] = typeof slotStyle === 'function' ? slotStyle(props) : slotStyle;
143
+ resolvedStyleOverrides[slotKey] = typeof slotStyle === 'function' ? slotStyle(_extends({}, props, {
144
+ theme
145
+ })) : slotStyle;
144
146
  });
145
147
  return overridesResolver(props, resolvedStyleOverrides);
146
148
  }
@@ -1,18 +1,14 @@
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"],
5
- _excluded2 = ["colorSchemes"],
6
- _excluded3 = ["components"];
4
+ const _excluded = ["colorSchemes", "components"];
7
5
  import * as React from 'react';
8
6
  import PropTypes from 'prop-types';
9
- import { GlobalStyles } from '@mui/styled-engine';
10
7
  import { deepmerge, unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils';
11
- import createSpacing from '../createTheme/createSpacing';
12
- import createBreakpoints from '../createTheme/createBreakpoints';
8
+ import { GlobalStyles } from '@mui/styled-engine';
13
9
  import cssVarsParser from './cssVarsParser';
14
10
  import ThemeProvider from '../ThemeProvider';
15
- import getInitColorSchemeScript, { DEFAULT_ATTRIBUTE, DEFAULT_MODE_STORAGE_KEY } from './getInitColorSchemeScript';
11
+ import getInitColorSchemeScript, { DEFAULT_ATTRIBUTE, DEFAULT_COLOR_SCHEME_STORAGE_KEY, DEFAULT_MODE_STORAGE_KEY } from './getInitColorSchemeScript';
16
12
  import useCurrentColorScheme from './useCurrentColorScheme';
17
13
  import createGetCssVar from './createGetCssVar';
18
14
  import { jsx as _jsx } from "react/jsx-runtime";
@@ -20,7 +16,7 @@ import { jsxs as _jsxs } from "react/jsx-runtime";
20
16
  export const DISABLE_CSS_TRANSITION = '*{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}';
21
17
  export default function createCssVarsProvider(options) {
22
18
  const {
23
- theme: baseTheme = {},
19
+ theme: defaultTheme = {},
24
20
  defaultMode: desisgnSystemMode = 'light',
25
21
  defaultColorScheme: designSystemColorScheme,
26
22
  disableTransitionOnChange: designSystemTransitionOnChange = false,
@@ -29,10 +25,8 @@ export default function createCssVarsProvider(options) {
29
25
  shouldSkipGeneratingVar,
30
26
  resolveTheme
31
27
  } = options;
32
- const systemSpacing = createSpacing(baseTheme.spacing);
33
- const systemBreakpoints = createBreakpoints(baseTheme.breakpoints ?? {});
34
28
 
35
- if (!baseTheme.colorSchemes || typeof designSystemColorScheme === 'string' && !baseTheme.colorSchemes[designSystemColorScheme] || typeof designSystemColorScheme === 'object' && !baseTheme.colorSchemes[designSystemColorScheme?.light] || typeof designSystemColorScheme === 'object' && !baseTheme.colorSchemes[designSystemColorScheme?.dark]) {
29
+ if (!defaultTheme.colorSchemes || typeof designSystemColorScheme === 'string' && !defaultTheme.colorSchemes[designSystemColorScheme] || typeof designSystemColorScheme === 'object' && !defaultTheme.colorSchemes[designSystemColorScheme?.light] || typeof designSystemColorScheme === 'object' && !defaultTheme.colorSchemes[designSystemColorScheme?.dark]) {
36
30
  console.error(`MUI: \`${designSystemColorScheme}\` does not exist in \`theme.colorSchemes\`.`);
37
31
  }
38
32
 
@@ -50,34 +44,28 @@ export default function createCssVarsProvider(options) {
50
44
 
51
45
  function CssVarsProvider({
52
46
  children,
53
- theme: themeProp = {},
47
+ theme: themeProp = defaultTheme,
54
48
  prefix = designSystemPrefix,
55
49
  modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
50
+ colorSchemeStorageKey = DEFAULT_COLOR_SCHEME_STORAGE_KEY,
56
51
  attribute = DEFAULT_ATTRIBUTE,
57
52
  defaultMode = desisgnSystemMode,
58
53
  defaultColorScheme = designSystemColorScheme,
59
54
  disableTransitionOnChange = designSystemTransitionOnChange,
60
- enableColorScheme = designSystemEnableColorScheme
55
+ enableColorScheme = designSystemEnableColorScheme,
56
+ storageWindow = typeof window === 'undefined' ? undefined : window,
57
+ documentNode = typeof document === 'undefined' ? undefined : document,
58
+ colorSchemeNode = typeof document === 'undefined' ? undefined : document.documentElement,
59
+ colorSchemeSelector = ':root'
61
60
  }) {
62
- const {
63
- colorSchemes: baseColorSchemes = {}
64
- } = baseTheme,
65
- restBaseTheme = _objectWithoutPropertiesLoose(baseTheme, _excluded);
61
+ const hasMounted = React.useRef(false);
66
62
 
67
63
  const {
68
- colorSchemes: colorSchemesProp = {}
69
- } = themeProp,
70
- restThemeProp = _objectWithoutPropertiesLoose(themeProp, _excluded2);
71
-
72
- const hasMounted = React.useRef(false); // eslint-disable-next-line prefer-const
73
-
74
- let _deepmerge = deepmerge(restBaseTheme, restThemeProp),
75
- {
64
+ colorSchemes = {},
76
65
  components = {}
77
- } = _deepmerge,
78
- mergedTheme = _objectWithoutPropertiesLoose(_deepmerge, _excluded3);
66
+ } = themeProp,
67
+ restThemeProp = _objectWithoutPropertiesLoose(themeProp, _excluded);
79
68
 
80
- const colorSchemes = deepmerge(baseColorSchemes, colorSchemesProp);
81
69
  const allColorSchemes = Object.keys(colorSchemes);
82
70
  const defaultLightColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.light;
83
71
  const defaultDarkColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.dark;
@@ -94,7 +82,9 @@ export default function createCssVarsProvider(options) {
94
82
  defaultLightColorScheme,
95
83
  defaultDarkColorScheme,
96
84
  modeStorageKey,
97
- defaultMode
85
+ colorSchemeStorageKey,
86
+ defaultMode,
87
+ storageWindow
98
88
  });
99
89
 
100
90
  const resolvedColorScheme = (() => {
@@ -111,22 +101,21 @@ export default function createCssVarsProvider(options) {
111
101
  return colorScheme;
112
102
  })();
113
103
 
104
+ let theme = restThemeProp;
114
105
  const {
115
106
  css: rootCss,
116
107
  vars: rootVars,
117
108
  parsedTheme
118
- } = cssVarsParser(mergedTheme, {
109
+ } = cssVarsParser(theme, {
119
110
  prefix,
120
111
  basePrefix: designSystemPrefix,
121
112
  shouldSkipGeneratingVar
122
113
  });
123
- mergedTheme = _extends({}, parsedTheme, {
114
+ theme = _extends({}, parsedTheme, {
124
115
  components,
125
116
  colorSchemes,
126
117
  prefix,
127
118
  vars: rootVars,
128
- spacing: themeProp.spacing ? createSpacing(themeProp.spacing) : systemSpacing,
129
- breakpoints: themeProp.breakpoints ? createBreakpoints(themeProp.breakpoints) : systemBreakpoints,
130
119
  getCssVar: createGetCssVar(prefix)
131
120
  });
132
121
  const styleSheet = {};
@@ -140,10 +129,16 @@ export default function createCssVarsProvider(options) {
140
129
  basePrefix: designSystemPrefix,
141
130
  shouldSkipGeneratingVar
142
131
  });
143
- mergedTheme.vars = deepmerge(mergedTheme.vars, vars);
132
+ theme.vars = deepmerge(theme.vars, vars);
144
133
 
145
134
  if (key === resolvedColorScheme) {
146
- mergedTheme = _extends({}, mergedTheme, parsedScheme);
135
+ theme = _extends({}, theme, parsedScheme);
136
+
137
+ if (theme.palette) {
138
+ // assign runtime mode & colorScheme
139
+ theme.palette.mode = mode;
140
+ theme.palette.colorScheme = resolvedColorScheme;
141
+ }
147
142
  }
148
143
 
149
144
  const resolvedDefaultColorScheme = (() => {
@@ -159,54 +154,54 @@ export default function createCssVarsProvider(options) {
159
154
  })();
160
155
 
161
156
  if (key === resolvedDefaultColorScheme) {
162
- styleSheet[':root'] = css;
157
+ styleSheet[colorSchemeSelector] = css;
163
158
  } else {
164
- styleSheet[`[${attribute}="${key}"]`] = css;
159
+ styleSheet[`${colorSchemeSelector === ':root' ? '' : colorSchemeSelector}[${attribute}="${key}"]`] = css;
165
160
  }
166
161
  });
167
162
  React.useEffect(() => {
168
- if (colorScheme) {
163
+ if (colorScheme && colorSchemeNode) {
169
164
  // attaches attribute to <html> because the css variables are attached to :root (html)
170
- document.documentElement.setAttribute(attribute, colorScheme);
165
+ colorSchemeNode.setAttribute(attribute, colorScheme);
171
166
  }
172
- }, [colorScheme, attribute]);
167
+ }, [colorScheme, attribute, colorSchemeNode]);
173
168
  useEnhancedEffect(() => {
174
- if (!mode || !enableColorScheme) {
169
+ if (!mode || !enableColorScheme || !colorSchemeNode) {
175
170
  return undefined;
176
171
  }
177
172
 
178
- 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
+ const priorColorScheme = colorSchemeNode.style.getPropertyValue('color-scheme'); // `color-scheme` tells browser to render built-in elements according to its value: `light` or `dark`
179
174
 
180
175
  if (mode === 'system') {
181
- document.documentElement.style.setProperty('color-scheme', systemMode);
176
+ colorSchemeNode.style.setProperty('color-scheme', systemMode);
182
177
  } else {
183
- document.documentElement.style.setProperty('color-scheme', mode);
178
+ colorSchemeNode.style.setProperty('color-scheme', mode);
184
179
  }
185
180
 
186
181
  return () => {
187
- document.documentElement.style.setProperty('color-scheme', priorColorScheme);
182
+ colorSchemeNode.style.setProperty('color-scheme', priorColorScheme);
188
183
  };
189
- }, [mode, systemMode, enableColorScheme]);
184
+ }, [mode, systemMode, enableColorScheme, colorSchemeNode]);
190
185
  React.useEffect(() => {
191
186
  let timer;
192
187
 
193
- if (disableTransitionOnChange && hasMounted.current) {
188
+ if (disableTransitionOnChange && hasMounted.current && documentNode) {
194
189
  // credit: https://github.com/pacocoursey/next-themes/blob/b5c2bad50de2d61ad7b52a9c5cdc801a78507d7a/index.tsx#L313
195
- const css = document.createElement('style');
196
- css.appendChild(document.createTextNode(DISABLE_CSS_TRANSITION));
197
- document.head.appendChild(css); // Force browser repaint
190
+ const css = documentNode.createElement('style');
191
+ css.appendChild(documentNode.createTextNode(DISABLE_CSS_TRANSITION));
192
+ documentNode.head.appendChild(css); // Force browser repaint
198
193
 
199
- (() => window.getComputedStyle(document.body))();
194
+ (() => window.getComputedStyle(documentNode.body))();
200
195
 
201
196
  timer = setTimeout(() => {
202
- document.head.removeChild(css);
197
+ documentNode.head.removeChild(css);
203
198
  }, 1);
204
199
  }
205
200
 
206
201
  return () => {
207
202
  clearTimeout(timer);
208
203
  };
209
- }, [colorScheme, disableTransitionOnChange]);
204
+ }, [colorScheme, disableTransitionOnChange, documentNode]);
210
205
  React.useEffect(() => {
211
206
  hasMounted.current = true;
212
207
  return () => {
@@ -225,12 +220,12 @@ export default function createCssVarsProvider(options) {
225
220
  },
226
221
  children: [/*#__PURE__*/_jsx(GlobalStyles, {
227
222
  styles: {
228
- ':root': rootCss
223
+ [colorSchemeSelector]: rootCss
229
224
  }
230
225
  }), /*#__PURE__*/_jsx(GlobalStyles, {
231
226
  styles: styleSheet
232
227
  }), /*#__PURE__*/_jsx(ThemeProvider, {
233
- theme: resolveTheme ? resolveTheme(mergedTheme) : mergedTheme,
228
+ theme: resolveTheme ? resolveTheme(theme) : theme,
234
229
  children: children
235
230
  })]
236
231
  });
@@ -247,6 +242,21 @@ export default function createCssVarsProvider(options) {
247
242
  */
248
243
  children: PropTypes.node,
249
244
 
245
+ /**
246
+ * The node used to attach the color-scheme attribute
247
+ */
248
+ colorSchemeNode: PropTypes.any,
249
+
250
+ /**
251
+ * The CSS selector for attaching the generated custom properties
252
+ */
253
+ colorSchemeSelector: PropTypes.string,
254
+
255
+ /**
256
+ * localStorage key used to store `colorScheme`
257
+ */
258
+ colorSchemeStorageKey: PropTypes.string,
259
+
250
260
  /**
251
261
  * The initial color scheme used.
252
262
  */
@@ -262,6 +272,11 @@ export default function createCssVarsProvider(options) {
262
272
  */
263
273
  disableTransitionOnChange: PropTypes.bool,
264
274
 
275
+ /**
276
+ * The document to attach the attribute to
277
+ */
278
+ documentNode: PropTypes.any,
279
+
265
280
  /**
266
281
  * Indicate to the browser which color scheme is used (light or dark) for rendering built-in UI
267
282
  */
@@ -277,6 +292,12 @@ export default function createCssVarsProvider(options) {
277
292
  */
278
293
  prefix: PropTypes.string,
279
294
 
295
+ /**
296
+ * The window that attaches the 'storage' event listener
297
+ * @default window
298
+ */
299
+ storageWindow: PropTypes.any,
300
+
280
301
  /**
281
302
  * The calculated theme object that will be passed through context.
282
303
  */
@@ -15,16 +15,18 @@
15
15
  * assignNestedKeys(source, ['palette', 'secondary'], 'var(--palette-secondary)')
16
16
  * console.log(source) // { palette: { primary: 'var(--palette-primary)', secondary: 'var(--palette-secondary)' } }
17
17
  */
18
- export const assignNestedKeys = (obj, keys, value) => {
18
+ export const assignNestedKeys = (obj, keys, value, arrayKeys = []) => {
19
19
  let temp = obj;
20
20
  keys.forEach((k, index) => {
21
21
  if (index === keys.length - 1) {
22
- if (temp && typeof temp === 'object') {
22
+ if (Array.isArray(temp)) {
23
+ temp[Number(k)] = value;
24
+ } else if (temp && typeof temp === 'object') {
23
25
  temp[k] = value;
24
26
  }
25
27
  } else if (temp && typeof temp === 'object') {
26
28
  if (!temp[k]) {
27
- temp[k] = {};
29
+ temp[k] = arrayKeys.includes(k) ? [] : {};
28
30
  }
29
31
 
30
32
  temp = temp[k];
@@ -44,14 +46,14 @@ export const assignNestedKeys = (obj, keys, value) => {
44
46
  */
45
47
 
46
48
  export const walkObjectDeep = (obj, callback, shouldSkipPaths) => {
47
- function recurse(object, parentKeys = []) {
49
+ function recurse(object, parentKeys = [], arrayKeys = []) {
48
50
  Object.entries(object).forEach(([key, value]) => {
49
51
  if (!shouldSkipPaths || shouldSkipPaths && !shouldSkipPaths([...parentKeys, key])) {
50
52
  if (value !== undefined && value !== null) {
51
53
  if (typeof value === 'object' && Object.keys(value).length > 0) {
52
- recurse(value, [...parentKeys, key]);
54
+ recurse(value, [...parentKeys, key], Array.isArray(value) ? [...arrayKeys, key] : arrayKeys);
53
55
  } else {
54
- callback([...parentKeys, key], value, object);
56
+ callback([...parentKeys, key], value, arrayKeys);
55
57
  }
56
58
  }
57
59
  }
@@ -118,7 +120,7 @@ export default function cssVarsParser(theme, options) {
118
120
  const css = {};
119
121
  const vars = {};
120
122
  const parsedTheme = {};
121
- walkObjectDeep(theme, (keys, value) => {
123
+ walkObjectDeep(theme, (keys, value, arrayKeys) => {
122
124
  if (typeof value === 'string' || typeof value === 'number') {
123
125
  if (typeof value === 'string' && value.match(/var\(\s*--/)) {
124
126
  // for CSS variable, apply prefix or remove basePrefix from the variable
@@ -136,11 +138,11 @@ export default function cssVarsParser(theme, options) {
136
138
  Object.assign(css, {
137
139
  [cssVar]: getCssValue(keys, value)
138
140
  });
139
- assignNestedKeys(vars, keys, `var(${cssVar})`);
141
+ assignNestedKeys(vars, keys, `var(${cssVar})`, arrayKeys);
140
142
  }
141
143
  }
142
144
 
143
- assignNestedKeys(parsedTheme, keys, value);
145
+ assignNestedKeys(parsedTheme, keys, value, arrayKeys);
144
146
  }, keys => keys[0] === 'vars' // skip 'vars/*' paths
145
147
  );
146
148
  return {
@@ -5,12 +5,13 @@ export const DEFAULT_COLOR_SCHEME_STORAGE_KEY = 'mui-color-scheme';
5
5
  export const DEFAULT_ATTRIBUTE = 'data-mui-color-scheme';
6
6
  export default function getInitColorSchemeScript(options) {
7
7
  const {
8
- enableSystem,
8
+ enableSystem = false,
9
9
  defaultLightColorScheme = 'light',
10
10
  defaultDarkColorScheme = 'dark',
11
11
  modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
12
12
  colorSchemeStorageKey = DEFAULT_COLOR_SCHEME_STORAGE_KEY,
13
- attribute = DEFAULT_ATTRIBUTE
13
+ attribute = DEFAULT_ATTRIBUTE,
14
+ colorSchemeNode = 'document.documentElement'
14
15
  } = options || {};
15
16
  return /*#__PURE__*/_jsx("script", {
16
17
  // eslint-disable-next-line react/no-danger
@@ -34,7 +35,7 @@ export default function getInitColorSchemeScript(options) {
34
35
  colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || '${defaultDarkColorScheme}';
35
36
  }
36
37
  if (colorScheme) {
37
- document.documentElement.setAttribute('${attribute}', colorScheme);
38
+ ${colorSchemeNode}.setAttribute('${attribute}', colorScheme);
38
39
  }
39
40
  } catch (e) {} })();`
40
41
  }