@mui/system 5.0.2 → 5.0.6
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.
- package/Box/Box.spec.d.ts +1 -1
- package/CHANGELOG.md +251 -0
- package/LICENSE +21 -21
- package/borders.js +1 -1
- package/breakpoints.js +4 -3
- package/colorManipulator.js +8 -8
- package/createStyled.d.ts +16 -215
- package/createStyled.js +6 -2
- package/createTheme/createBreakpoints.d.ts +53 -6
- package/createTheme/createBreakpoints.js +1 -1
- package/createTheme/createSpacing.d.ts +10 -10
- package/cssVars/createCssVarsProvider.d.ts +81 -0
- package/cssVars/createCssVarsProvider.js +206 -0
- package/cssVars/createCssVarsProvider.spec.d.ts +1 -0
- package/cssVars/cssVarsParser.d.ts +57 -0
- package/cssVars/cssVarsParser.js +126 -0
- package/cssVars/getInitColorSchemeScript.d.ts +7 -0
- package/cssVars/getInitColorSchemeScript.js +38 -0
- package/cssVars/index.d.ts +2 -0
- package/cssVars/index.js +15 -0
- package/cssVars/package.json +6 -0
- package/display.js +1 -1
- package/esm/createStyled.js +5 -1
- package/esm/cssVars/createCssVarsProvider.js +188 -0
- package/esm/cssVars/cssVarsParser.js +112 -0
- package/esm/cssVars/getInitColorSchemeScript.js +21 -0
- package/esm/cssVars/index.js +1 -0
- package/esm/index.js +2 -1
- package/esm/spacing.js +2 -2
- package/esm/styleFunctionSx/styleFunctionSx.js +6 -6
- package/flexbox.js +1 -1
- package/getThemeValue.js +1 -1
- package/grid.js +1 -1
- package/index.d.ts +6 -0
- package/index.js +77 -68
- package/index.spec.d.ts +1 -1
- package/legacy/createStyled.js +5 -1
- package/legacy/cssVars/createCssVarsProvider.js +202 -0
- package/legacy/cssVars/cssVarsParser.js +125 -0
- package/legacy/cssVars/getInitColorSchemeScript.js +18 -0
- package/legacy/cssVars/index.js +1 -0
- package/legacy/index.js +3 -2
- package/legacy/spacing.js +2 -2
- package/legacy/styleFunctionSx/styleFunctionSx.js +6 -6
- package/modern/createStyled.js +5 -1
- package/modern/cssVars/createCssVarsProvider.js +188 -0
- package/modern/cssVars/cssVarsParser.js +112 -0
- package/modern/cssVars/getInitColorSchemeScript.js +21 -0
- package/modern/cssVars/index.js +1 -0
- package/modern/index.js +3 -2
- package/modern/spacing.js +2 -2
- package/modern/styleFunctionSx/styleFunctionSx.js +6 -6
- package/package.json +3 -3
- package/palette.js +1 -1
- package/positions.js +1 -1
- package/sizing.js +1 -1
- package/spacing.d.ts +12 -0
- package/spacing.js +5 -5
- package/style.d.ts +2 -2
- package/style.js +1 -1
- package/styleFunctionSx/styleFunctionSx.d.ts +6 -1
- package/styleFunctionSx/styleFunctionSx.js +6 -6
- package/typography.js +1 -1
- package/useTheme.js +1 -1
|
@@ -8,20 +8,67 @@ export type Breakpoint = OverridableStringUnion<
|
|
|
8
8
|
>;
|
|
9
9
|
export const keys: Breakpoint[];
|
|
10
10
|
|
|
11
|
+
// Keep in sync with docs/src/pages/customization/breakpoints/breakpoints.md
|
|
12
|
+
// #default-branch-switch
|
|
11
13
|
export interface Breakpoints {
|
|
12
14
|
keys: Breakpoint[];
|
|
15
|
+
/**
|
|
16
|
+
* Each breakpoint (a key) matches with a fixed screen width (a value).
|
|
17
|
+
* @default {
|
|
18
|
+
* // extra-small
|
|
19
|
+
* xs: 0,
|
|
20
|
+
* // small
|
|
21
|
+
* sm: 600,
|
|
22
|
+
* // medium
|
|
23
|
+
* md: 900,
|
|
24
|
+
* // large
|
|
25
|
+
* lg: 1200,
|
|
26
|
+
* // extra-large
|
|
27
|
+
* xl: 1536,
|
|
28
|
+
* }
|
|
29
|
+
*/
|
|
13
30
|
values: { [key in Breakpoint]: number };
|
|
31
|
+
/**
|
|
32
|
+
* @param key - A breakpoint key (`xs`, `sm`, etc.) or a screen width number in px.
|
|
33
|
+
* @returns A media query string ready to be used with most styling solutions, which matches screen widths greater than the screen size given by the breakpoint key (inclusive).
|
|
34
|
+
* @see [API documentation](https://mui.com/customization/breakpoints/#theme-breakpoints-up-key-media-query)
|
|
35
|
+
*/
|
|
14
36
|
up: (key: Breakpoint | number) => string;
|
|
37
|
+
/**
|
|
38
|
+
* @param key - A breakpoint key (`xs`, `sm`, etc.) or a screen width number in px.
|
|
39
|
+
* @returns A media query string ready to be used with most styling solutions, which matches screen widths less than the screen size given by the breakpoint key (exclusive).
|
|
40
|
+
* @see [API documentation](https://mui.com/customization/breakpoints/#theme-breakpoints-down-key-media-query)
|
|
41
|
+
*/
|
|
15
42
|
down: (key: Breakpoint | number) => string;
|
|
43
|
+
/**
|
|
44
|
+
* @param start - A breakpoint key (`xs`, `sm`, etc.) or a screen width number in px.
|
|
45
|
+
* @param end - A breakpoint key (`xs`, `sm`, etc.) or a screen width number in px.
|
|
46
|
+
* @returns A media query string ready to be used with most styling solutions, which matches screen widths greater than
|
|
47
|
+
* the screen size given by the breakpoint key in the first argument (inclusive) and less than the screen size given by the breakpoint key in the second argument (exclusive).
|
|
48
|
+
* @see [API documentation](https://mui.com/customization/breakpoints/#theme-breakpoints-between-start-end-media-query)
|
|
49
|
+
*/
|
|
16
50
|
between: (start: Breakpoint | number, end: Breakpoint | number) => string;
|
|
51
|
+
/**
|
|
52
|
+
* @param key - A breakpoint key (`xs`, `sm`, etc.) or a screen width number in px.
|
|
53
|
+
* @returns A media query string ready to be used with most styling solutions, which matches screen widths starting from
|
|
54
|
+
* the screen size given by the breakpoint key (inclusive) and stopping at the screen size given by the next breakpoint key (exclusive).
|
|
55
|
+
* @see [API documentation](https://mui.com/customization/breakpoints/#theme-breakpoints-only-key-media-query)
|
|
56
|
+
*/
|
|
17
57
|
only: (key: Breakpoint) => string;
|
|
18
58
|
}
|
|
19
59
|
|
|
20
|
-
export
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
60
|
+
export interface BreakpointsOptions extends Partial<Breakpoints> {
|
|
61
|
+
/**
|
|
62
|
+
* The increment divided by 100 used to implement exclusive breakpoints.
|
|
63
|
+
* For example, `step: 5` means that `down(500)` will result in `'(max-width: 499.95px)'`.
|
|
64
|
+
* @default 5
|
|
65
|
+
*/
|
|
66
|
+
step?: number | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* The unit used for the breakpoint's values.
|
|
69
|
+
* @default 'px'
|
|
70
|
+
*/
|
|
71
|
+
unit?: string | undefined;
|
|
72
|
+
}
|
|
26
73
|
|
|
27
74
|
export default function createBreakpoints(options: BreakpointsOptions): Breakpoints;
|
|
@@ -5,8 +5,8 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
|
-
exports.default = createBreakpoints;
|
|
9
8
|
exports.breakpointKeys = void 0;
|
|
9
|
+
exports.default = createBreakpoints;
|
|
10
10
|
|
|
11
11
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
12
12
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export declare type SpacingOptions = number | Spacing | ((abs: number) => number | string) | ((abs: number | string) => number | string) | ReadonlyArray<string | number>;
|
|
2
|
-
export declare type SpacingArgument = number | string;
|
|
3
|
-
export interface Spacing {
|
|
4
|
-
(): string;
|
|
5
|
-
(value: number): string;
|
|
6
|
-
(topBottom: SpacingArgument, rightLeft: SpacingArgument): string;
|
|
7
|
-
(top: SpacingArgument, rightLeft: SpacingArgument, bottom: SpacingArgument): string;
|
|
8
|
-
(top: SpacingArgument, right: SpacingArgument, bottom: SpacingArgument, left: SpacingArgument): string;
|
|
9
|
-
}
|
|
10
|
-
export default function createSpacing(spacingInput?: SpacingOptions): Spacing;
|
|
1
|
+
export declare type SpacingOptions = number | Spacing | ((abs: number) => number | string) | ((abs: number | string) => number | string) | ReadonlyArray<string | number>;
|
|
2
|
+
export declare type SpacingArgument = number | string;
|
|
3
|
+
export interface Spacing {
|
|
4
|
+
(): string;
|
|
5
|
+
(value: number): string;
|
|
6
|
+
(topBottom: SpacingArgument, rightLeft: SpacingArgument): string;
|
|
7
|
+
(top: SpacingArgument, rightLeft: SpacingArgument, bottom: SpacingArgument): string;
|
|
8
|
+
(top: SpacingArgument, right: SpacingArgument, bottom: SpacingArgument, left: SpacingArgument): string;
|
|
9
|
+
}
|
|
10
|
+
export default function createSpacing(spacingInput?: SpacingOptions): Spacing;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
type PartialDeep<T> = {
|
|
4
|
+
[K in keyof T]?: PartialDeep<T[K]>;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export type BuildCssVarsTheme<ThemeInput> = ThemeInput extends {
|
|
8
|
+
colorSchemes: Record<string, infer Colors>;
|
|
9
|
+
}
|
|
10
|
+
? Omit<ThemeInput, 'colorSchemes'> & { vars: Omit<ThemeInput, 'colorSchemes'> & Colors }
|
|
11
|
+
: never;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* DesignSystemColorScheme: is what a design system provide by default. Mostly, `light` and `dark`
|
|
15
|
+
* ApplicationColorScheme: is what developer can extend from a design system. Ex, `comfort` `trueDark` ...any name that they want
|
|
16
|
+
*
|
|
17
|
+
* This type enhance customization experience by checking if developer has extended the colorScheme or not (usually via module augmentation)
|
|
18
|
+
* If yes, they must provide the palette of the extended colorScheme. Otherwise `theme` is optional.
|
|
19
|
+
*/
|
|
20
|
+
type DecideTheme<
|
|
21
|
+
Theme extends { colorSchemes: Record<DesignSystemColorScheme | ApplicationColorScheme, any> },
|
|
22
|
+
DesignSystemColorScheme extends string,
|
|
23
|
+
ApplicationColorScheme extends string | never,
|
|
24
|
+
> = [ApplicationColorScheme] extends [never]
|
|
25
|
+
? { theme?: PartialDeep<Theme> }
|
|
26
|
+
: {
|
|
27
|
+
theme: PartialDeep<Omit<Theme, 'colorSchemes'>> & {
|
|
28
|
+
colorSchemes: PartialDeep<
|
|
29
|
+
Record<DesignSystemColorScheme, Theme['colorSchemes'][DesignSystemColorScheme]>
|
|
30
|
+
> &
|
|
31
|
+
Record<ApplicationColorScheme, Theme['colorSchemes'][ApplicationColorScheme]>;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export interface ColorSchemeContextValue<DesignSystemColorScheme extends string> {
|
|
36
|
+
allColorSchemes: DesignSystemColorScheme[];
|
|
37
|
+
colorScheme: DesignSystemColorScheme | undefined;
|
|
38
|
+
setColorScheme: React.Dispatch<React.SetStateAction<DesignSystemColorScheme | undefined>>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export default function createCssVarsProvider<
|
|
42
|
+
ThemeInput extends {
|
|
43
|
+
colorSchemes: Record<DesignSystemColorScheme | ApplicationColorScheme, any>;
|
|
44
|
+
},
|
|
45
|
+
DesignSystemColorScheme extends string,
|
|
46
|
+
ApplicationColorScheme extends string = never,
|
|
47
|
+
>(
|
|
48
|
+
ThemeContext: React.Context<BuildCssVarsTheme<ThemeInput> | undefined>,
|
|
49
|
+
options: {
|
|
50
|
+
theme: Omit<ThemeInput, 'colorSchemes'> & {
|
|
51
|
+
colorSchemes: Record<
|
|
52
|
+
DesignSystemColorScheme,
|
|
53
|
+
ThemeInput['colorSchemes'][DesignSystemColorScheme]
|
|
54
|
+
> &
|
|
55
|
+
Partial<
|
|
56
|
+
Record<
|
|
57
|
+
ApplicationColorScheme,
|
|
58
|
+
ThemeInput['colorSchemes'][DesignSystemColorScheme | ApplicationColorScheme]
|
|
59
|
+
>
|
|
60
|
+
>;
|
|
61
|
+
};
|
|
62
|
+
defaultColorScheme: DesignSystemColorScheme;
|
|
63
|
+
prefix?: string;
|
|
64
|
+
},
|
|
65
|
+
): {
|
|
66
|
+
CssVarsProvider: (
|
|
67
|
+
props: React.PropsWithChildren<
|
|
68
|
+
{
|
|
69
|
+
defaultColorScheme?: DesignSystemColorScheme | ApplicationColorScheme;
|
|
70
|
+
storageKey?: string;
|
|
71
|
+
attribute?: string;
|
|
72
|
+
prefix?: string;
|
|
73
|
+
} & DecideTheme<ThemeInput, DesignSystemColorScheme, ApplicationColorScheme>
|
|
74
|
+
>,
|
|
75
|
+
) => React.ReactElement;
|
|
76
|
+
useColorScheme: () => ColorSchemeContextValue<DesignSystemColorScheme | ApplicationColorScheme>;
|
|
77
|
+
getInitColorSchemeScript: () => React.ReactElement;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// disable automatic export
|
|
81
|
+
export {};
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.default = createCssVarsProvider;
|
|
9
|
+
|
|
10
|
+
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
11
|
+
|
|
12
|
+
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
|
|
13
|
+
|
|
14
|
+
var _utils = require("@mui/utils");
|
|
15
|
+
|
|
16
|
+
var React = _interopRequireWildcard(require("react"));
|
|
17
|
+
|
|
18
|
+
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
19
|
+
|
|
20
|
+
var _styledEngine = require("@mui/styled-engine");
|
|
21
|
+
|
|
22
|
+
var _cssVarsParser = _interopRequireDefault(require("./cssVarsParser"));
|
|
23
|
+
|
|
24
|
+
var _getInitColorSchemeScript = _interopRequireWildcard(require("./getInitColorSchemeScript"));
|
|
25
|
+
|
|
26
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
27
|
+
|
|
28
|
+
const _excluded = ["colorSchemes"],
|
|
29
|
+
_excluded2 = ["colorSchemes"];
|
|
30
|
+
|
|
31
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
32
|
+
|
|
33
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
34
|
+
|
|
35
|
+
const resolveMode = (key, fallback, supportedColorSchemes) => {
|
|
36
|
+
if (typeof window === 'undefined') {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let value;
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
value = localStorage.getItem(key) || undefined;
|
|
44
|
+
|
|
45
|
+
if (!supportedColorSchemes.includes(value)) {
|
|
46
|
+
value = undefined;
|
|
47
|
+
}
|
|
48
|
+
} catch (e) {// Unsupported
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return value || fallback;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
function createCssVarsProvider(ThemeContext, options) {
|
|
55
|
+
const {
|
|
56
|
+
theme: baseTheme = {},
|
|
57
|
+
defaultColorScheme: designSystemColorScheme,
|
|
58
|
+
prefix: designSystemPrefix = ''
|
|
59
|
+
} = options;
|
|
60
|
+
|
|
61
|
+
if (!baseTheme.colorSchemes || !baseTheme.colorSchemes[designSystemColorScheme]) {
|
|
62
|
+
console.error(`MUI: \`${designSystemColorScheme}\` does not exist in \`theme.colorSchemes\`.`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const ColorSchemeContext = /*#__PURE__*/React.createContext(undefined);
|
|
66
|
+
|
|
67
|
+
const useColorScheme = () => {
|
|
68
|
+
const value = React.useContext(ColorSchemeContext);
|
|
69
|
+
|
|
70
|
+
if (!value) {
|
|
71
|
+
throw new Error(process.env.NODE_ENV !== "production" ? `MUI: \`useColorScheme\` must be called under <CssVarsProvider />` : (0, _utils.formatMuiErrorMessage)(19));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return value;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
function CssVarsProvider({
|
|
78
|
+
children,
|
|
79
|
+
theme: themeProp = {},
|
|
80
|
+
prefix = designSystemPrefix,
|
|
81
|
+
storageKey = _getInitColorSchemeScript.DEFAULT_STORAGE_KEY,
|
|
82
|
+
attribute = _getInitColorSchemeScript.DEFAULT_ATTRIBUTE,
|
|
83
|
+
defaultColorScheme = designSystemColorScheme
|
|
84
|
+
}) {
|
|
85
|
+
const {
|
|
86
|
+
colorSchemes: baseColorSchemes = {}
|
|
87
|
+
} = baseTheme,
|
|
88
|
+
restBaseTheme = (0, _objectWithoutPropertiesLoose2.default)(baseTheme, _excluded);
|
|
89
|
+
const {
|
|
90
|
+
colorSchemes: colorSchemesProp = {}
|
|
91
|
+
} = themeProp,
|
|
92
|
+
restThemeProp = (0, _objectWithoutPropertiesLoose2.default)(themeProp, _excluded2);
|
|
93
|
+
let mergedTheme = (0, _utils.deepmerge)(restBaseTheme, restThemeProp);
|
|
94
|
+
const colorSchemes = (0, _utils.deepmerge)(baseColorSchemes, colorSchemesProp);
|
|
95
|
+
const allColorSchemes = Object.keys(colorSchemes);
|
|
96
|
+
const joinedColorSchemes = allColorSchemes.join(',');
|
|
97
|
+
const [colorScheme, setColorScheme] = React.useState(() => resolveMode(storageKey, defaultColorScheme, allColorSchemes));
|
|
98
|
+
const resolvedColorScheme = colorScheme || defaultColorScheme;
|
|
99
|
+
const {
|
|
100
|
+
css: rootCss,
|
|
101
|
+
vars: rootVars
|
|
102
|
+
} = (0, _cssVarsParser.default)(mergedTheme, {
|
|
103
|
+
prefix
|
|
104
|
+
});
|
|
105
|
+
mergedTheme = (0, _extends2.default)({}, mergedTheme, colorSchemes[resolvedColorScheme], {
|
|
106
|
+
vars: rootVars
|
|
107
|
+
});
|
|
108
|
+
const styleSheet = {};
|
|
109
|
+
Object.entries(colorSchemes).forEach(([key, scheme]) => {
|
|
110
|
+
const {
|
|
111
|
+
css,
|
|
112
|
+
vars
|
|
113
|
+
} = (0, _cssVarsParser.default)(scheme, {
|
|
114
|
+
prefix
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
if (key === resolvedColorScheme) {
|
|
118
|
+
mergedTheme.vars = (0, _extends2.default)({}, mergedTheme.vars, vars);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (key === defaultColorScheme) {
|
|
122
|
+
styleSheet[':root'] = (0, _utils.deepmerge)(rootCss, css);
|
|
123
|
+
} else {
|
|
124
|
+
styleSheet[`[${attribute}="${key}"]`] = css;
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
React.useEffect(() => {
|
|
128
|
+
if (colorScheme) {
|
|
129
|
+
document.body.setAttribute(attribute, colorScheme);
|
|
130
|
+
localStorage.setItem(storageKey, colorScheme);
|
|
131
|
+
}
|
|
132
|
+
}, [colorScheme, attribute, storageKey]); // local storage modified in the context of another document
|
|
133
|
+
|
|
134
|
+
React.useEffect(() => {
|
|
135
|
+
const handleStorage = event => {
|
|
136
|
+
const storageColorScheme = event.newValue;
|
|
137
|
+
|
|
138
|
+
if (event.key === storageKey && joinedColorSchemes.match(storageColorScheme)) {
|
|
139
|
+
if (storageColorScheme) {
|
|
140
|
+
setColorScheme(storageColorScheme);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
window.addEventListener('storage', handleStorage);
|
|
146
|
+
return () => window.removeEventListener('storage', handleStorage);
|
|
147
|
+
}, [setColorScheme, storageKey, joinedColorSchemes]);
|
|
148
|
+
const wrappedSetColorScheme = React.useCallback(val => {
|
|
149
|
+
if (typeof val === 'string' && !allColorSchemes.includes(val)) {
|
|
150
|
+
console.error(`\`${val}\` does not exist in \`theme.colorSchemes\`.`);
|
|
151
|
+
} else {
|
|
152
|
+
setColorScheme(val);
|
|
153
|
+
}
|
|
154
|
+
}, [setColorScheme, allColorSchemes]);
|
|
155
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(ColorSchemeContext.Provider, {
|
|
156
|
+
value: {
|
|
157
|
+
colorScheme,
|
|
158
|
+
setColorScheme: wrappedSetColorScheme,
|
|
159
|
+
allColorSchemes
|
|
160
|
+
},
|
|
161
|
+
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_styledEngine.GlobalStyles, {
|
|
162
|
+
styles: styleSheet
|
|
163
|
+
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(ThemeContext.Provider, {
|
|
164
|
+
value: mergedTheme,
|
|
165
|
+
children: children
|
|
166
|
+
})]
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
process.env.NODE_ENV !== "production" ? CssVarsProvider.propTypes = {
|
|
171
|
+
/**
|
|
172
|
+
* The body attribute name to attach colorScheme.
|
|
173
|
+
*/
|
|
174
|
+
attribute: _propTypes.default.string,
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Your component tree.
|
|
178
|
+
*/
|
|
179
|
+
children: _propTypes.default.node,
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* The initial color scheme used.
|
|
183
|
+
*/
|
|
184
|
+
defaultColorScheme: _propTypes.default.string,
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* css variable prefix
|
|
188
|
+
*/
|
|
189
|
+
prefix: _propTypes.default.string,
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The key in the local storage used to store current color scheme.
|
|
193
|
+
*/
|
|
194
|
+
storageKey: _propTypes.default.string,
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* The calculated theme object that will be passed through context.
|
|
198
|
+
*/
|
|
199
|
+
theme: _propTypes.default.object
|
|
200
|
+
} : void 0;
|
|
201
|
+
return {
|
|
202
|
+
CssVarsProvider,
|
|
203
|
+
useColorScheme,
|
|
204
|
+
getInitColorSchemeScript: _getInitColorSchemeScript.default
|
|
205
|
+
};
|
|
206
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
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) => void) => void;
|
|
34
|
+
/**
|
|
35
|
+
* a function that parse theme and return { css, vars }
|
|
36
|
+
*
|
|
37
|
+
* @param {Object} theme
|
|
38
|
+
* @param {{ prefix?: string }} options
|
|
39
|
+
* @returns {{ css: Object, vars: Object }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme)
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* const { css, vars } = parser({
|
|
43
|
+
* fontSize: 12,
|
|
44
|
+
* lineHeight: 1.2,
|
|
45
|
+
* palette: { primary: { 500: '#000000' } }
|
|
46
|
+
* })
|
|
47
|
+
*
|
|
48
|
+
* console.log(css) // { '--fontSize': '12px', '--lineHeight': 1.2, '--palette-primary-500': '#000000' }
|
|
49
|
+
* console.log(vars) // { fontSize: '--fontSize', lineHeight: '--lineHeight', palette: { primary: { 500: 'var(--palette-primary-500)' } } }
|
|
50
|
+
*/
|
|
51
|
+
export default function cssVarsParser(obj: Record<string, any>, options?: {
|
|
52
|
+
prefix?: string;
|
|
53
|
+
}): {
|
|
54
|
+
css: NestedRecord<string>;
|
|
55
|
+
vars: NestedRecord<string>;
|
|
56
|
+
};
|
|
57
|
+
export {};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.assignNestedKeys = void 0;
|
|
7
|
+
exports.default = cssVarsParser;
|
|
8
|
+
exports.walkObjectDeep = void 0;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* This function create an object from keys, value and then assign to target
|
|
12
|
+
*
|
|
13
|
+
* @param {Object} obj : the target object to be assigned
|
|
14
|
+
* @param {string[]} keys
|
|
15
|
+
* @param {string | number} value
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const source = {}
|
|
19
|
+
* assignNestedKeys(source, ['palette', 'primary'], 'var(--palette-primary)')
|
|
20
|
+
* console.log(source) // { palette: { primary: 'var(--palette-primary)' } }
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* const source = { palette: { primary: 'var(--palette-primary)' } }
|
|
24
|
+
* assignNestedKeys(source, ['palette', 'secondary'], 'var(--palette-secondary)')
|
|
25
|
+
* console.log(source) // { palette: { primary: 'var(--palette-primary)', secondary: 'var(--palette-secondary)' } }
|
|
26
|
+
*/
|
|
27
|
+
const assignNestedKeys = (obj, keys, value) => {
|
|
28
|
+
let temp = obj;
|
|
29
|
+
keys.forEach((k, index) => {
|
|
30
|
+
if (index === keys.length - 1) {
|
|
31
|
+
if (temp && typeof temp === 'object') {
|
|
32
|
+
temp[k] = value;
|
|
33
|
+
}
|
|
34
|
+
} else if (temp && typeof temp === 'object') {
|
|
35
|
+
if (!temp[k]) {
|
|
36
|
+
temp[k] = {};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
temp = temp[k];
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
*
|
|
45
|
+
* @param {Object} obj : source object
|
|
46
|
+
* @param {Function} callback : a function that will be called when
|
|
47
|
+
* - the deepest key in source object is reached
|
|
48
|
+
* - the value of the deepest key is NOT `undefined` | `null`
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* walkObjectDeep({ palette: { primary: { main: '#000000' } } }, console.log)
|
|
52
|
+
* // ['palette', 'primary', 'main'] '#000000'
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
exports.assignNestedKeys = assignNestedKeys;
|
|
57
|
+
|
|
58
|
+
const walkObjectDeep = (obj, callback) => {
|
|
59
|
+
function recurse(object, parentKeys = []) {
|
|
60
|
+
Object.entries(object).forEach(([key, value]) => {
|
|
61
|
+
if (value !== undefined && value !== null) {
|
|
62
|
+
if (typeof value === 'object' && Object.keys(value).length > 0) {
|
|
63
|
+
recurse(value, [...parentKeys, key]);
|
|
64
|
+
} else {
|
|
65
|
+
callback([...parentKeys, key], value);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
recurse(obj);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
exports.walkObjectDeep = walkObjectDeep;
|
|
75
|
+
|
|
76
|
+
const getCssValue = (keys, value) => {
|
|
77
|
+
if (typeof value === 'number') {
|
|
78
|
+
if (['lineHeight', 'fontWeight', 'opacity', 'zIndex'].some(prop => keys.includes(prop))) {
|
|
79
|
+
// css property that are unitless
|
|
80
|
+
return value;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return `${value}px`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return value;
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* a function that parse theme and return { css, vars }
|
|
90
|
+
*
|
|
91
|
+
* @param {Object} theme
|
|
92
|
+
* @param {{ prefix?: string }} options
|
|
93
|
+
* @returns {{ css: Object, vars: Object }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme)
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* const { css, vars } = parser({
|
|
97
|
+
* fontSize: 12,
|
|
98
|
+
* lineHeight: 1.2,
|
|
99
|
+
* palette: { primary: { 500: '#000000' } }
|
|
100
|
+
* })
|
|
101
|
+
*
|
|
102
|
+
* console.log(css) // { '--fontSize': '12px', '--lineHeight': 1.2, '--palette-primary-500': '#000000' }
|
|
103
|
+
* console.log(vars) // { fontSize: '--fontSize', lineHeight: '--lineHeight', palette: { primary: { 500: 'var(--palette-primary-500)' } } }
|
|
104
|
+
*/
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
function cssVarsParser(obj, options) {
|
|
108
|
+
const {
|
|
109
|
+
prefix
|
|
110
|
+
} = options || {};
|
|
111
|
+
const css = {};
|
|
112
|
+
const vars = {};
|
|
113
|
+
walkObjectDeep(obj, (keys, value) => {
|
|
114
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
115
|
+
const cssVar = `--${prefix ? `${prefix}-` : ''}${keys.join('-')}`;
|
|
116
|
+
Object.assign(css, {
|
|
117
|
+
[cssVar]: getCssValue(keys, value)
|
|
118
|
+
});
|
|
119
|
+
assignNestedKeys(vars, keys, `var(${cssVar})`);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
return {
|
|
123
|
+
css,
|
|
124
|
+
vars
|
|
125
|
+
};
|
|
126
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
export declare const DEFAULT_STORAGE_KEY = "mui-color-scheme";
|
|
3
|
+
export declare const DEFAULT_ATTRIBUTE = "data-mui-color-scheme";
|
|
4
|
+
export default function getInitColorSchemeScript(options?: {
|
|
5
|
+
storageKey?: string;
|
|
6
|
+
attribute?: string;
|
|
7
|
+
}): JSX.Element;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.DEFAULT_STORAGE_KEY = exports.DEFAULT_ATTRIBUTE = void 0;
|
|
7
|
+
exports.default = getInitColorSchemeScript;
|
|
8
|
+
|
|
9
|
+
var React = _interopRequireWildcard(require("react"));
|
|
10
|
+
|
|
11
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
12
|
+
|
|
13
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
14
|
+
|
|
15
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
16
|
+
|
|
17
|
+
const DEFAULT_STORAGE_KEY = 'mui-color-scheme';
|
|
18
|
+
exports.DEFAULT_STORAGE_KEY = DEFAULT_STORAGE_KEY;
|
|
19
|
+
const DEFAULT_ATTRIBUTE = 'data-mui-color-scheme';
|
|
20
|
+
exports.DEFAULT_ATTRIBUTE = DEFAULT_ATTRIBUTE;
|
|
21
|
+
|
|
22
|
+
function getInitColorSchemeScript(options) {
|
|
23
|
+
const {
|
|
24
|
+
storageKey = DEFAULT_STORAGE_KEY,
|
|
25
|
+
attribute = DEFAULT_ATTRIBUTE
|
|
26
|
+
} = options || {};
|
|
27
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsx)("script", {
|
|
28
|
+
// eslint-disable-next-line react/no-danger
|
|
29
|
+
dangerouslySetInnerHTML: {
|
|
30
|
+
__html: `(function() { try {
|
|
31
|
+
var colorScheme = localStorage.getItem('${storageKey}');
|
|
32
|
+
if (colorScheme) {
|
|
33
|
+
document.body.setAttribute('${attribute}', colorScheme);
|
|
34
|
+
}
|
|
35
|
+
} catch (e) {} })();`
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
package/cssVars/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
Object.defineProperty(exports, "default", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: function () {
|
|
11
|
+
return _createCssVarsProvider.default;
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
var _createCssVarsProvider = _interopRequireDefault(require("./createCssVarsProvider"));
|
package/display.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.
|
|
8
|
+
exports.whiteSpace = exports.visibility = exports.textOverflow = exports.overflow = exports.displayRaw = exports.displayPrint = exports.default = void 0;
|
|
9
9
|
|
|
10
10
|
var _style = _interopRequireDefault(require("./style"));
|
|
11
11
|
|