UNPKG

10.9 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.dark = void 0;
8exports.default = createPalette;
9exports.light = void 0;
10var _formatMuiErrorMessage2 = _interopRequireDefault(require("@mui/utils/formatMuiErrorMessage"));
11var _deepmerge = _interopRequireDefault(require("@mui/utils/deepmerge"));
12var _colorManipulator = require("@mui/system/colorManipulator");
13var _common = _interopRequireDefault(require("../colors/common"));
14var _grey = _interopRequireDefault(require("../colors/grey"));
15var _purple = _interopRequireDefault(require("../colors/purple"));
16var _red = _interopRequireDefault(require("../colors/red"));
17var _orange = _interopRequireDefault(require("../colors/orange"));
18var _blue = _interopRequireDefault(require("../colors/blue"));
19var _lightBlue = _interopRequireDefault(require("../colors/lightBlue"));
20var _green = _interopRequireDefault(require("../colors/green"));
21function getLight() {
22 return {
23 // The colors used to style the text.
24 text: {
25 // The most important text.
26 primary: 'rgba(0, 0, 0, 0.87)',
27 // Secondary text.
28 secondary: 'rgba(0, 0, 0, 0.6)',
29 // Disabled text have even lower visual prominence.
30 disabled: 'rgba(0, 0, 0, 0.38)'
31 },
32 // The color used to divide different elements.
33 divider: 'rgba(0, 0, 0, 0.12)',
34 // The background colors used to style the surfaces.
35 // Consistency between these values is important.
36 background: {
37 paper: _common.default.white,
38 default: _common.default.white
39 },
40 // The colors used to style the action elements.
41 action: {
42 // The color of an active action like an icon button.
43 active: 'rgba(0, 0, 0, 0.54)',
44 // The color of an hovered action.
45 hover: 'rgba(0, 0, 0, 0.04)',
46 hoverOpacity: 0.04,
47 // The color of a selected action.
48 selected: 'rgba(0, 0, 0, 0.08)',
49 selectedOpacity: 0.08,
50 // The color of a disabled action.
51 disabled: 'rgba(0, 0, 0, 0.26)',
52 // The background color of a disabled action.
53 disabledBackground: 'rgba(0, 0, 0, 0.12)',
54 disabledOpacity: 0.38,
55 focus: 'rgba(0, 0, 0, 0.12)',
56 focusOpacity: 0.12,
57 activatedOpacity: 0.12
58 }
59 };
60}
61const light = exports.light = getLight();
62function getDark() {
63 return {
64 text: {
65 primary: _common.default.white,
66 secondary: 'rgba(255, 255, 255, 0.7)',
67 disabled: 'rgba(255, 255, 255, 0.5)',
68 icon: 'rgba(255, 255, 255, 0.5)'
69 },
70 divider: 'rgba(255, 255, 255, 0.12)',
71 background: {
72 paper: '#121212',
73 default: '#121212'
74 },
75 action: {
76 active: _common.default.white,
77 hover: 'rgba(255, 255, 255, 0.08)',
78 hoverOpacity: 0.08,
79 selected: 'rgba(255, 255, 255, 0.16)',
80 selectedOpacity: 0.16,
81 disabled: 'rgba(255, 255, 255, 0.3)',
82 disabledBackground: 'rgba(255, 255, 255, 0.12)',
83 disabledOpacity: 0.38,
84 focus: 'rgba(255, 255, 255, 0.12)',
85 focusOpacity: 0.12,
86 activatedOpacity: 0.24
87 }
88 };
89}
90const dark = exports.dark = getDark();
91function addLightOrDark(intent, direction, shade, tonalOffset) {
92 const tonalOffsetLight = tonalOffset.light || tonalOffset;
93 const tonalOffsetDark = tonalOffset.dark || tonalOffset * 1.5;
94 if (!intent[direction]) {
95 if (intent.hasOwnProperty(shade)) {
96 intent[direction] = intent[shade];
97 } else if (direction === 'light') {
98 intent.light = (0, _colorManipulator.lighten)(intent.main, tonalOffsetLight);
99 } else if (direction === 'dark') {
100 intent.dark = (0, _colorManipulator.darken)(intent.main, tonalOffsetDark);
101 }
102 }
103}
104function getDefaultPrimary(mode = 'light') {
105 if (mode === 'dark') {
106 return {
107 main: _blue.default[200],
108 light: _blue.default[50],
109 dark: _blue.default[400]
110 };
111 }
112 return {
113 main: _blue.default[700],
114 light: _blue.default[400],
115 dark: _blue.default[800]
116 };
117}
118function getDefaultSecondary(mode = 'light') {
119 if (mode === 'dark') {
120 return {
121 main: _purple.default[200],
122 light: _purple.default[50],
123 dark: _purple.default[400]
124 };
125 }
126 return {
127 main: _purple.default[500],
128 light: _purple.default[300],
129 dark: _purple.default[700]
130 };
131}
132function getDefaultError(mode = 'light') {
133 if (mode === 'dark') {
134 return {
135 main: _red.default[500],
136 light: _red.default[300],
137 dark: _red.default[700]
138 };
139 }
140 return {
141 main: _red.default[700],
142 light: _red.default[400],
143 dark: _red.default[800]
144 };
145}
146function getDefaultInfo(mode = 'light') {
147 if (mode === 'dark') {
148 return {
149 main: _lightBlue.default[400],
150 light: _lightBlue.default[300],
151 dark: _lightBlue.default[700]
152 };
153 }
154 return {
155 main: _lightBlue.default[700],
156 light: _lightBlue.default[500],
157 dark: _lightBlue.default[900]
158 };
159}
160function getDefaultSuccess(mode = 'light') {
161 if (mode === 'dark') {
162 return {
163 main: _green.default[400],
164 light: _green.default[300],
165 dark: _green.default[700]
166 };
167 }
168 return {
169 main: _green.default[800],
170 light: _green.default[500],
171 dark: _green.default[900]
172 };
173}
174function getDefaultWarning(mode = 'light') {
175 if (mode === 'dark') {
176 return {
177 main: _orange.default[400],
178 light: _orange.default[300],
179 dark: _orange.default[700]
180 };
181 }
182 return {
183 main: '#ed6c02',
184 // closest to orange[800] that pass 3:1.
185 light: _orange.default[500],
186 dark: _orange.default[900]
187 };
188}
189function createPalette(palette) {
190 const {
191 mode = 'light',
192 contrastThreshold = 3,
193 tonalOffset = 0.2,
194 ...other
195 } = palette;
196 const primary = palette.primary || getDefaultPrimary(mode);
197 const secondary = palette.secondary || getDefaultSecondary(mode);
198 const error = palette.error || getDefaultError(mode);
199 const info = palette.info || getDefaultInfo(mode);
200 const success = palette.success || getDefaultSuccess(mode);
201 const warning = palette.warning || getDefaultWarning(mode);
202
203 // Use the same logic as
204 // Bootstrap: https://github.com/twbs/bootstrap/blob/1d6e3710dd447de1a200f29e8fa521f8a0908f70/scss/_functions.scss#L59
205 // and material-components-web https://github.com/material-components/material-components-web/blob/ac46b8863c4dab9fc22c4c662dc6bd1b65dd652f/packages/mdc-theme/_functions.scss#L54
206 function getContrastText(background) {
207 const contrastText = (0, _colorManipulator.getContrastRatio)(background, dark.text.primary) >= contrastThreshold ? dark.text.primary : light.text.primary;
208 if (process.env.NODE_ENV !== 'production') {
209 const contrast = (0, _colorManipulator.getContrastRatio)(background, contrastText);
210 if (contrast < 3) {
211 console.error([`MUI: The contrast ratio of ${contrast}:1 for ${contrastText} on ${background}`, 'falls below the WCAG recommended absolute minimum contrast ratio of 3:1.', 'https://www.w3.org/TR/2008/REC-WCAG20-20081211/#visual-audio-contrast-contrast'].join('\n'));
212 }
213 }
214 return contrastText;
215 }
216 const augmentColor = ({
217 color,
218 name,
219 mainShade = 500,
220 lightShade = 300,
221 darkShade = 700
222 }) => {
223 color = {
224 ...color
225 };
226 if (!color.main && color[mainShade]) {
227 color.main = color[mainShade];
228 }
229 if (!color.hasOwnProperty('main')) {
230 throw new Error(process.env.NODE_ENV !== "production" ? `MUI: The color${name ? ` (${name})` : ''} provided to augmentColor(color) is invalid.\n` + `The color object needs to have a \`main\` property or a \`${mainShade}\` property.` : (0, _formatMuiErrorMessage2.default)(11, name ? ` (${name})` : '', mainShade));
231 }
232 if (typeof color.main !== 'string') {
233 throw new Error(process.env.NODE_ENV !== "production" ? `MUI: The color${name ? ` (${name})` : ''} provided to augmentColor(color) is invalid.\n` + `\`color.main\` should be a string, but \`${JSON.stringify(color.main)}\` was provided instead.\n` + '\n' + 'Did you intend to use one of the following approaches?\n' + '\n' + 'import { green } from "@mui/material/colors";\n' + '\n' + 'const theme1 = createTheme({ palette: {\n' + ' primary: green,\n' + '} });\n' + '\n' + 'const theme2 = createTheme({ palette: {\n' + ' primary: { main: green[500] },\n' + '} });' : (0, _formatMuiErrorMessage2.default)(12, name ? ` (${name})` : '', JSON.stringify(color.main)));
234 }
235 addLightOrDark(color, 'light', lightShade, tonalOffset);
236 addLightOrDark(color, 'dark', darkShade, tonalOffset);
237 if (!color.contrastText) {
238 color.contrastText = getContrastText(color.main);
239 }
240 return color;
241 };
242 let modeHydrated;
243 if (mode === 'light') {
244 modeHydrated = getLight();
245 } else if (mode === 'dark') {
246 modeHydrated = getDark();
247 }
248 if (process.env.NODE_ENV !== 'production') {
249 if (!modeHydrated) {
250 console.error(`MUI: The palette mode \`${mode}\` is not supported.`);
251 }
252 }
253 const paletteOutput = (0, _deepmerge.default)({
254 // A collection of common colors.
255 common: {
256 ..._common.default
257 },
258 // prevent mutable object.
259 // The palette mode, can be light or dark.
260 mode,
261 // The colors used to represent primary interface elements for a user.
262 primary: augmentColor({
263 color: primary,
264 name: 'primary'
265 }),
266 // The colors used to represent secondary interface elements for a user.
267 secondary: augmentColor({
268 color: secondary,
269 name: 'secondary',
270 mainShade: 'A400',
271 lightShade: 'A200',
272 darkShade: 'A700'
273 }),
274 // The colors used to represent interface elements that the user should be made aware of.
275 error: augmentColor({
276 color: error,
277 name: 'error'
278 }),
279 // The colors used to represent potentially dangerous actions or important messages.
280 warning: augmentColor({
281 color: warning,
282 name: 'warning'
283 }),
284 // The colors used to present information to the user that is neutral and not necessarily important.
285 info: augmentColor({
286 color: info,
287 name: 'info'
288 }),
289 // The colors used to indicate the successful completion of an action that user triggered.
290 success: augmentColor({
291 color: success,
292 name: 'success'
293 }),
294 // The grey colors.
295 grey: _grey.default,
296 // Used by `getContrastText()` to maximize the contrast between
297 // the background and the text.
298 contrastThreshold,
299 // Takes a background color and returns the text color that maximizes the contrast.
300 getContrastText,
301 // Generate a rich color object.
302 augmentColor,
303 // Used by the functions below to shift a color's luminance by approximately
304 // two indexes within its tonal palette.
305 // E.g., shift from Red 500 to Red 300 or Red 700.
306 tonalOffset,
307 // The light and dark mode object.
308 ...modeHydrated
309 }, other);
310 return paletteOutput;
311}
\No newline at end of file