@mui/system 5.0.1 → 5.0.5

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 (61) hide show
  1. package/Box/Box.spec.d.ts +1 -1
  2. package/CHANGELOG.md +281 -0
  3. package/LICENSE +21 -21
  4. package/borders.js +1 -1
  5. package/breakpoints.js +4 -3
  6. package/colorManipulator.js +8 -8
  7. package/createStyled.d.ts +16 -215
  8. package/createStyled.js +1 -1
  9. package/createTheme/createBreakpoints.d.ts +53 -6
  10. package/createTheme/createBreakpoints.js +1 -1
  11. package/createTheme/createSpacing.d.ts +10 -10
  12. package/cssVars/createCssVarsProvider.d.ts +81 -0
  13. package/cssVars/createCssVarsProvider.js +206 -0
  14. package/cssVars/createCssVarsProvider.spec.d.ts +1 -0
  15. package/cssVars/cssVarsParser.d.ts +57 -0
  16. package/cssVars/cssVarsParser.js +126 -0
  17. package/cssVars/getInitColorSchemeScript.d.ts +7 -0
  18. package/cssVars/getInitColorSchemeScript.js +38 -0
  19. package/cssVars/index.d.ts +2 -0
  20. package/cssVars/index.js +15 -0
  21. package/cssVars/package.json +6 -0
  22. package/display.js +1 -1
  23. package/esm/cssVars/createCssVarsProvider.js +188 -0
  24. package/esm/cssVars/cssVarsParser.js +112 -0
  25. package/esm/cssVars/getInitColorSchemeScript.js +21 -0
  26. package/esm/cssVars/index.js +1 -0
  27. package/esm/index.js +2 -1
  28. package/esm/spacing.js +2 -2
  29. package/esm/styleFunctionSx/styleFunctionSx.js +6 -6
  30. package/flexbox.js +1 -1
  31. package/getThemeValue.js +1 -1
  32. package/grid.js +1 -1
  33. package/index.d.ts +6 -0
  34. package/index.js +77 -68
  35. package/index.spec.d.ts +1 -1
  36. package/legacy/cssVars/createCssVarsProvider.js +202 -0
  37. package/legacy/cssVars/cssVarsParser.js +125 -0
  38. package/legacy/cssVars/getInitColorSchemeScript.js +18 -0
  39. package/legacy/cssVars/index.js +1 -0
  40. package/legacy/index.js +3 -2
  41. package/legacy/spacing.js +2 -2
  42. package/legacy/styleFunctionSx/styleFunctionSx.js +6 -6
  43. package/modern/cssVars/createCssVarsProvider.js +188 -0
  44. package/modern/cssVars/cssVarsParser.js +112 -0
  45. package/modern/cssVars/getInitColorSchemeScript.js +21 -0
  46. package/modern/cssVars/index.js +1 -0
  47. package/modern/index.js +3 -2
  48. package/modern/spacing.js +2 -2
  49. package/modern/styleFunctionSx/styleFunctionSx.js +6 -6
  50. package/package.json +2 -2
  51. package/palette.js +1 -1
  52. package/positions.js +1 -1
  53. package/sizing.js +1 -1
  54. package/spacing.d.ts +12 -0
  55. package/spacing.js +5 -5
  56. package/style.d.ts +2 -2
  57. package/style.js +1 -1
  58. package/styleFunctionSx/styleFunctionSx.d.ts +8 -3
  59. package/styleFunctionSx/styleFunctionSx.js +6 -6
  60. package/typography.js +1 -1
  61. package/useTheme.js +1 -1
@@ -0,0 +1,188 @@
1
+ import _extends from "@babel/runtime/helpers/esm/extends";
2
+ import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3
+ import { formatMuiErrorMessage as _formatMuiErrorMessage } from "@mui/utils";
4
+ const _excluded = ["colorSchemes"],
5
+ _excluded2 = ["colorSchemes"];
6
+ import * as React from 'react';
7
+ import PropTypes from 'prop-types';
8
+ import { GlobalStyles } from '@mui/styled-engine';
9
+ import { deepmerge } from '@mui/utils';
10
+ import cssVarsParser from './cssVarsParser';
11
+ import getInitColorSchemeScript, { DEFAULT_ATTRIBUTE, DEFAULT_STORAGE_KEY } from './getInitColorSchemeScript';
12
+ import { jsx as _jsx } from "react/jsx-runtime";
13
+ import { jsxs as _jsxs } from "react/jsx-runtime";
14
+
15
+ const resolveMode = (key, fallback, supportedColorSchemes) => {
16
+ if (typeof window === 'undefined') {
17
+ return undefined;
18
+ }
19
+
20
+ let value;
21
+
22
+ try {
23
+ value = localStorage.getItem(key) || undefined;
24
+
25
+ if (!supportedColorSchemes.includes(value)) {
26
+ value = undefined;
27
+ }
28
+ } catch (e) {// Unsupported
29
+ }
30
+
31
+ return value || fallback;
32
+ };
33
+
34
+ export default function createCssVarsProvider(ThemeContext, options) {
35
+ const {
36
+ theme: baseTheme = {},
37
+ defaultColorScheme: designSystemColorScheme,
38
+ prefix: designSystemPrefix = ''
39
+ } = options;
40
+
41
+ if (!baseTheme.colorSchemes || !baseTheme.colorSchemes[designSystemColorScheme]) {
42
+ console.error(`MUI: \`${designSystemColorScheme}\` does not exist in \`theme.colorSchemes\`.`);
43
+ }
44
+
45
+ const ColorSchemeContext = /*#__PURE__*/React.createContext(undefined);
46
+
47
+ const useColorScheme = () => {
48
+ const value = React.useContext(ColorSchemeContext);
49
+
50
+ if (!value) {
51
+ throw new Error(process.env.NODE_ENV !== "production" ? `MUI: \`useColorScheme\` must be called under <CssVarsProvider />` : _formatMuiErrorMessage(19));
52
+ }
53
+
54
+ return value;
55
+ };
56
+
57
+ function CssVarsProvider({
58
+ children,
59
+ theme: themeProp = {},
60
+ prefix = designSystemPrefix,
61
+ storageKey = DEFAULT_STORAGE_KEY,
62
+ attribute = DEFAULT_ATTRIBUTE,
63
+ defaultColorScheme = designSystemColorScheme
64
+ }) {
65
+ const {
66
+ colorSchemes: baseColorSchemes = {}
67
+ } = baseTheme,
68
+ restBaseTheme = _objectWithoutPropertiesLoose(baseTheme, _excluded);
69
+
70
+ const {
71
+ colorSchemes: colorSchemesProp = {}
72
+ } = themeProp,
73
+ restThemeProp = _objectWithoutPropertiesLoose(themeProp, _excluded2);
74
+
75
+ let mergedTheme = deepmerge(restBaseTheme, restThemeProp);
76
+ const colorSchemes = deepmerge(baseColorSchemes, colorSchemesProp);
77
+ const allColorSchemes = Object.keys(colorSchemes);
78
+ const joinedColorSchemes = allColorSchemes.join(',');
79
+ const [colorScheme, setColorScheme] = React.useState(() => resolveMode(storageKey, defaultColorScheme, allColorSchemes));
80
+ const resolvedColorScheme = colorScheme || defaultColorScheme;
81
+ const {
82
+ css: rootCss,
83
+ vars: rootVars
84
+ } = cssVarsParser(mergedTheme, {
85
+ prefix
86
+ });
87
+ mergedTheme = _extends({}, mergedTheme, colorSchemes[resolvedColorScheme], {
88
+ vars: rootVars
89
+ });
90
+ const styleSheet = {};
91
+ Object.entries(colorSchemes).forEach(([key, scheme]) => {
92
+ const {
93
+ css,
94
+ vars
95
+ } = cssVarsParser(scheme, {
96
+ prefix
97
+ });
98
+
99
+ if (key === resolvedColorScheme) {
100
+ mergedTheme.vars = _extends({}, mergedTheme.vars, vars);
101
+ }
102
+
103
+ if (key === defaultColorScheme) {
104
+ styleSheet[':root'] = deepmerge(rootCss, css);
105
+ } else {
106
+ styleSheet[`[${attribute}="${key}"]`] = css;
107
+ }
108
+ });
109
+ React.useEffect(() => {
110
+ if (colorScheme) {
111
+ document.body.setAttribute(attribute, colorScheme);
112
+ localStorage.setItem(storageKey, colorScheme);
113
+ }
114
+ }, [colorScheme, attribute, storageKey]); // local storage modified in the context of another document
115
+
116
+ React.useEffect(() => {
117
+ const handleStorage = event => {
118
+ const storageColorScheme = event.newValue;
119
+
120
+ if (event.key === storageKey && joinedColorSchemes.match(storageColorScheme)) {
121
+ if (storageColorScheme) {
122
+ setColorScheme(storageColorScheme);
123
+ }
124
+ }
125
+ };
126
+
127
+ window.addEventListener('storage', handleStorage);
128
+ return () => window.removeEventListener('storage', handleStorage);
129
+ }, [setColorScheme, storageKey, joinedColorSchemes]);
130
+ const wrappedSetColorScheme = React.useCallback(val => {
131
+ if (typeof val === 'string' && !allColorSchemes.includes(val)) {
132
+ console.error(`\`${val}\` does not exist in \`theme.colorSchemes\`.`);
133
+ } else {
134
+ setColorScheme(val);
135
+ }
136
+ }, [setColorScheme, allColorSchemes]);
137
+ return /*#__PURE__*/_jsxs(ColorSchemeContext.Provider, {
138
+ value: {
139
+ colorScheme,
140
+ setColorScheme: wrappedSetColorScheme,
141
+ allColorSchemes
142
+ },
143
+ children: [/*#__PURE__*/_jsx(GlobalStyles, {
144
+ styles: styleSheet
145
+ }), /*#__PURE__*/_jsx(ThemeContext.Provider, {
146
+ value: mergedTheme,
147
+ children: children
148
+ })]
149
+ });
150
+ }
151
+
152
+ process.env.NODE_ENV !== "production" ? CssVarsProvider.propTypes = {
153
+ /**
154
+ * The body attribute name to attach colorScheme.
155
+ */
156
+ attribute: PropTypes.string,
157
+
158
+ /**
159
+ * Your component tree.
160
+ */
161
+ children: PropTypes.node,
162
+
163
+ /**
164
+ * The initial color scheme used.
165
+ */
166
+ defaultColorScheme: PropTypes.string,
167
+
168
+ /**
169
+ * css variable prefix
170
+ */
171
+ prefix: PropTypes.string,
172
+
173
+ /**
174
+ * The key in the local storage used to store current color scheme.
175
+ */
176
+ storageKey: PropTypes.string,
177
+
178
+ /**
179
+ * The calculated theme object that will be passed through context.
180
+ */
181
+ theme: PropTypes.object
182
+ } : void 0;
183
+ return {
184
+ CssVarsProvider,
185
+ useColorScheme,
186
+ getInitColorSchemeScript
187
+ };
188
+ }
@@ -0,0 +1,112 @@
1
+ /**
2
+ * This function create an object from keys, value and then assign to target
3
+ *
4
+ * @param {Object} obj : the target object to be assigned
5
+ * @param {string[]} keys
6
+ * @param {string | number} value
7
+ *
8
+ * @example
9
+ * const source = {}
10
+ * assignNestedKeys(source, ['palette', 'primary'], 'var(--palette-primary)')
11
+ * console.log(source) // { palette: { primary: 'var(--palette-primary)' } }
12
+ *
13
+ * @example
14
+ * const source = { palette: { primary: 'var(--palette-primary)' } }
15
+ * assignNestedKeys(source, ['palette', 'secondary'], 'var(--palette-secondary)')
16
+ * console.log(source) // { palette: { primary: 'var(--palette-primary)', secondary: 'var(--palette-secondary)' } }
17
+ */
18
+ export const assignNestedKeys = (obj, keys, value) => {
19
+ let temp = obj;
20
+ keys.forEach((k, index) => {
21
+ if (index === keys.length - 1) {
22
+ if (temp && typeof temp === 'object') {
23
+ temp[k] = value;
24
+ }
25
+ } else if (temp && typeof temp === 'object') {
26
+ if (!temp[k]) {
27
+ temp[k] = {};
28
+ }
29
+
30
+ temp = temp[k];
31
+ }
32
+ });
33
+ };
34
+ /**
35
+ *
36
+ * @param {Object} obj : source object
37
+ * @param {Function} callback : a function that will be called when
38
+ * - the deepest key in source object is reached
39
+ * - the value of the deepest key is NOT `undefined` | `null`
40
+ *
41
+ * @example
42
+ * walkObjectDeep({ palette: { primary: { main: '#000000' } } }, console.log)
43
+ * // ['palette', 'primary', 'main'] '#000000'
44
+ */
45
+
46
+ export const walkObjectDeep = (obj, callback) => {
47
+ function recurse(object, parentKeys = []) {
48
+ Object.entries(object).forEach(([key, value]) => {
49
+ if (value !== undefined && value !== null) {
50
+ if (typeof value === 'object' && Object.keys(value).length > 0) {
51
+ recurse(value, [...parentKeys, key]);
52
+ } else {
53
+ callback([...parentKeys, key], value);
54
+ }
55
+ }
56
+ });
57
+ }
58
+
59
+ recurse(obj);
60
+ };
61
+
62
+ const getCssValue = (keys, value) => {
63
+ if (typeof value === 'number') {
64
+ if (['lineHeight', 'fontWeight', 'opacity', 'zIndex'].some(prop => keys.includes(prop))) {
65
+ // css property that are unitless
66
+ return value;
67
+ }
68
+
69
+ return `${value}px`;
70
+ }
71
+
72
+ return value;
73
+ };
74
+ /**
75
+ * a function that parse theme and return { css, vars }
76
+ *
77
+ * @param {Object} theme
78
+ * @param {{ prefix?: string }} options
79
+ * @returns {{ css: Object, vars: Object }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme)
80
+ *
81
+ * @example
82
+ * const { css, vars } = parser({
83
+ * fontSize: 12,
84
+ * lineHeight: 1.2,
85
+ * palette: { primary: { 500: '#000000' } }
86
+ * })
87
+ *
88
+ * console.log(css) // { '--fontSize': '12px', '--lineHeight': 1.2, '--palette-primary-500': '#000000' }
89
+ * console.log(vars) // { fontSize: '--fontSize', lineHeight: '--lineHeight', palette: { primary: { 500: 'var(--palette-primary-500)' } } }
90
+ */
91
+
92
+
93
+ export default function cssVarsParser(obj, options) {
94
+ const {
95
+ prefix
96
+ } = options || {};
97
+ const css = {};
98
+ const vars = {};
99
+ walkObjectDeep(obj, (keys, value) => {
100
+ if (typeof value === 'string' || typeof value === 'number') {
101
+ const cssVar = `--${prefix ? `${prefix}-` : ''}${keys.join('-')}`;
102
+ Object.assign(css, {
103
+ [cssVar]: getCssValue(keys, value)
104
+ });
105
+ assignNestedKeys(vars, keys, `var(${cssVar})`);
106
+ }
107
+ });
108
+ return {
109
+ css,
110
+ vars
111
+ };
112
+ }
@@ -0,0 +1,21 @@
1
+ import * as React from 'react';
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ export const DEFAULT_STORAGE_KEY = 'mui-color-scheme';
4
+ export const DEFAULT_ATTRIBUTE = 'data-mui-color-scheme';
5
+ export default function getInitColorSchemeScript(options) {
6
+ const {
7
+ storageKey = DEFAULT_STORAGE_KEY,
8
+ attribute = DEFAULT_ATTRIBUTE
9
+ } = options || {};
10
+ return /*#__PURE__*/_jsx("script", {
11
+ // eslint-disable-next-line react/no-danger
12
+ dangerouslySetInnerHTML: {
13
+ __html: `(function() { try {
14
+ var colorScheme = localStorage.getItem('${storageKey}');
15
+ if (colorScheme) {
16
+ document.body.setAttribute('${attribute}', colorScheme);
17
+ }
18
+ } catch (e) {} })();`
19
+ }
20
+ });
21
+ }
@@ -0,0 +1 @@
1
+ export { default } from './createCssVarsProvider';
package/esm/index.js CHANGED
@@ -36,4 +36,5 @@ export { default as useThemeProps, getThemeProps } from './useThemeProps';
36
36
  export { default as useTheme } from './useTheme';
37
37
  export { default as useThemeWithoutDefault } from './useThemeWithoutDefault';
38
38
  export * from './colorManipulator';
39
- export { default as ThemeProvider } from './ThemeProvider';
39
+ export { default as ThemeProvider } from './ThemeProvider';
40
+ export { default as unstable_createCssVarsProvider } from './cssVars/createCssVarsProvider';
package/esm/spacing.js CHANGED
@@ -39,8 +39,8 @@ const getCssProperties = memoize(prop => {
39
39
  const direction = directions[b] || '';
40
40
  return Array.isArray(direction) ? direction.map(dir => property + dir) : [property + direction];
41
41
  });
42
- const marginKeys = ['m', 'mt', 'mr', 'mb', 'ml', 'mx', 'my', 'margin', 'marginTop', 'marginRight', 'marginBottom', 'marginLeft', 'marginX', 'marginY'];
43
- const paddingKeys = ['p', 'pt', 'pr', 'pb', 'pl', 'px', 'py', 'padding', 'paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft', 'paddingX', 'paddingY'];
42
+ const marginKeys = ['m', 'mt', 'mr', 'mb', 'ml', 'mx', 'my', 'margin', 'marginTop', 'marginRight', 'marginBottom', 'marginLeft', 'marginX', 'marginY', 'marginInline', 'marginInlineStart', 'marginInlineEnd', 'marginBlock', 'marginBlockStart', 'marginBlockEnd'];
43
+ const paddingKeys = ['p', 'pt', 'pr', 'pb', 'pl', 'px', 'py', 'padding', 'paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft', 'paddingX', 'paddingY', 'paddingInline', 'paddingInlineStart', 'paddingInlineEnd', 'paddingBlock', 'paddingBlockStart', 'paddingBlockEnd'];
44
44
  const spacingKeys = [...marginKeys, ...paddingKeys];
45
45
  export function createUnaryUnit(theme, themeKey, defaultValue, propName) {
46
46
  const themeSpacing = getPath(theme, themeKey) || defaultValue;
@@ -22,11 +22,11 @@ function styleFunctionSx(props) {
22
22
  return null;
23
23
  }
24
24
 
25
- if (typeof styles === 'function') {
26
- return styles(theme);
27
- }
25
+ let stylesObject = styles;
28
26
 
29
- if (typeof styles !== 'object') {
27
+ if (typeof styles === 'function') {
28
+ stylesObject = styles(theme);
29
+ } else if (typeof styles !== 'object') {
30
30
  // value
31
31
  return styles;
32
32
  }
@@ -34,8 +34,8 @@ function styleFunctionSx(props) {
34
34
  const emptyBreakpoints = createEmptyBreakpointObject(theme.breakpoints);
35
35
  const breakpointsKeys = Object.keys(emptyBreakpoints);
36
36
  let css = emptyBreakpoints;
37
- Object.keys(styles).forEach(styleKey => {
38
- const value = callIfFn(styles[styleKey], theme);
37
+ Object.keys(stylesObject).forEach(styleKey => {
38
+ const value = callIfFn(stylesObject[styleKey], theme);
39
39
 
40
40
  if (typeof value === 'object') {
41
41
  if (propToStyleFunction[styleKey]) {
package/flexbox.js CHANGED
@@ -5,7 +5,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports.default = exports.justifySelf = exports.justifyItems = exports.alignSelf = exports.flexShrink = exports.flexGrow = exports.flex = exports.order = exports.alignContent = exports.alignItems = exports.justifyContent = exports.flexWrap = exports.flexDirection = exports.flexBasis = void 0;
8
+ exports.order = exports.justifySelf = exports.justifyItems = exports.justifyContent = exports.flexWrap = exports.flexShrink = exports.flexGrow = exports.flexDirection = exports.flexBasis = exports.flex = exports.default = exports.alignSelf = exports.alignItems = exports.alignContent = void 0;
9
9
 
10
10
  var _style = _interopRequireDefault(require("./style"));
11
11
 
package/getThemeValue.js CHANGED
@@ -5,7 +5,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports.default = exports.propToStyleFunction = void 0;
8
+ exports.propToStyleFunction = exports.default = void 0;
9
9
 
10
10
  var _borders = _interopRequireDefault(require("./borders"));
11
11
 
package/grid.js CHANGED
@@ -5,7 +5,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports.default = exports.gridArea = exports.gridTemplateAreas = exports.gridTemplateRows = exports.gridTemplateColumns = exports.gridAutoRows = exports.gridAutoColumns = exports.gridAutoFlow = exports.gridRow = exports.gridColumn = exports.rowGap = exports.columnGap = exports.gap = void 0;
8
+ exports.rowGap = exports.gridTemplateRows = exports.gridTemplateColumns = exports.gridTemplateAreas = exports.gridRow = exports.gridColumn = exports.gridAutoRows = exports.gridAutoFlow = exports.gridAutoColumns = exports.gridArea = exports.gap = exports.default = exports.columnGap = void 0;
9
9
 
10
10
  var _style = _interopRequireDefault(require("./style"));
11
11
 
package/index.d.ts CHANGED
@@ -107,6 +107,9 @@ export {
107
107
  GlobalStyles,
108
108
  GlobalStylesProps,
109
109
  StyledEngineProvider,
110
+ Interpolation,
111
+ CSSInterpolation,
112
+ CSSObject,
110
113
  } from '@mui/styled-engine';
111
114
 
112
115
  export * from './style';
@@ -154,3 +157,6 @@ export * from './colorManipulator';
154
157
 
155
158
  export { default as ThemeProvider } from './ThemeProvider';
156
159
  export * from './ThemeProvider';
160
+
161
+ export { default as unstable_createCssVarsProvider } from './cssVars';
162
+ export * from './cssVars';