UNPKG

9.99 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.default = createPalette;
9exports.dark = exports.light = void 0;
10
11var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
12
13var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
14
15var _utils = require("@material-ui/utils");
16
17var _common = _interopRequireDefault(require("../colors/common"));
18
19var _grey = _interopRequireDefault(require("../colors/grey"));
20
21var _indigo = _interopRequireDefault(require("../colors/indigo"));
22
23var _pink = _interopRequireDefault(require("../colors/pink"));
24
25var _red = _interopRequireDefault(require("../colors/red"));
26
27var _orange = _interopRequireDefault(require("../colors/orange"));
28
29var _blue = _interopRequireDefault(require("../colors/blue"));
30
31var _green = _interopRequireDefault(require("../colors/green"));
32
33var _colorManipulator = require("./colorManipulator");
34
35var light = {
36 // The colors used to style the text.
37 text: {
38 // The most important text.
39 primary: 'rgba(0, 0, 0, 0.87)',
40 // Secondary text.
41 secondary: 'rgba(0, 0, 0, 0.54)',
42 // Disabled text have even lower visual prominence.
43 disabled: 'rgba(0, 0, 0, 0.38)',
44 // Text hints.
45 hint: 'rgba(0, 0, 0, 0.38)'
46 },
47 // The color used to divide different elements.
48 divider: 'rgba(0, 0, 0, 0.12)',
49 // The background colors used to style the surfaces.
50 // Consistency between these values is important.
51 background: {
52 paper: _common.default.white,
53 default: _grey.default[50]
54 },
55 // The colors used to style the action elements.
56 action: {
57 // The color of an active action like an icon button.
58 active: 'rgba(0, 0, 0, 0.54)',
59 // The color of an hovered action.
60 hover: 'rgba(0, 0, 0, 0.04)',
61 hoverOpacity: 0.04,
62 // The color of a selected action.
63 selected: 'rgba(0, 0, 0, 0.08)',
64 selectedOpacity: 0.08,
65 // The color of a disabled action.
66 disabled: 'rgba(0, 0, 0, 0.26)',
67 // The background color of a disabled action.
68 disabledBackground: 'rgba(0, 0, 0, 0.12)',
69 disabledOpacity: 0.38,
70 focus: 'rgba(0, 0, 0, 0.12)',
71 focusOpacity: 0.12,
72 activatedOpacity: 0.12
73 }
74};
75exports.light = light;
76var dark = {
77 text: {
78 primary: _common.default.white,
79 secondary: 'rgba(255, 255, 255, 0.7)',
80 disabled: 'rgba(255, 255, 255, 0.5)',
81 hint: 'rgba(255, 255, 255, 0.5)',
82 icon: 'rgba(255, 255, 255, 0.5)'
83 },
84 divider: 'rgba(255, 255, 255, 0.12)',
85 background: {
86 paper: _grey.default[800],
87 default: '#303030'
88 },
89 action: {
90 active: _common.default.white,
91 hover: 'rgba(255, 255, 255, 0.08)',
92 hoverOpacity: 0.08,
93 selected: 'rgba(255, 255, 255, 0.16)',
94 selectedOpacity: 0.16,
95 disabled: 'rgba(255, 255, 255, 0.3)',
96 disabledBackground: 'rgba(255, 255, 255, 0.12)',
97 disabledOpacity: 0.38,
98 focus: 'rgba(255, 255, 255, 0.12)',
99 focusOpacity: 0.12,
100 activatedOpacity: 0.24
101 }
102};
103exports.dark = dark;
104
105function addLightOrDark(intent, direction, shade, tonalOffset) {
106 var tonalOffsetLight = tonalOffset.light || tonalOffset;
107 var tonalOffsetDark = tonalOffset.dark || tonalOffset * 1.5;
108
109 if (!intent[direction]) {
110 if (intent.hasOwnProperty(shade)) {
111 intent[direction] = intent[shade];
112 } else if (direction === 'light') {
113 intent.light = (0, _colorManipulator.lighten)(intent.main, tonalOffsetLight);
114 } else if (direction === 'dark') {
115 intent.dark = (0, _colorManipulator.darken)(intent.main, tonalOffsetDark);
116 }
117 }
118}
119
120function createPalette(palette) {
121 var _palette$primary = palette.primary,
122 primary = _palette$primary === void 0 ? {
123 light: _indigo.default[300],
124 main: _indigo.default[500],
125 dark: _indigo.default[700]
126 } : _palette$primary,
127 _palette$secondary = palette.secondary,
128 secondary = _palette$secondary === void 0 ? {
129 light: _pink.default.A200,
130 main: _pink.default.A400,
131 dark: _pink.default.A700
132 } : _palette$secondary,
133 _palette$error = palette.error,
134 error = _palette$error === void 0 ? {
135 light: _red.default[300],
136 main: _red.default[500],
137 dark: _red.default[700]
138 } : _palette$error,
139 _palette$warning = palette.warning,
140 warning = _palette$warning === void 0 ? {
141 light: _orange.default[300],
142 main: _orange.default[500],
143 dark: _orange.default[700]
144 } : _palette$warning,
145 _palette$info = palette.info,
146 info = _palette$info === void 0 ? {
147 light: _blue.default[300],
148 main: _blue.default[500],
149 dark: _blue.default[700]
150 } : _palette$info,
151 _palette$success = palette.success,
152 success = _palette$success === void 0 ? {
153 light: _green.default[300],
154 main: _green.default[500],
155 dark: _green.default[700]
156 } : _palette$success,
157 _palette$type = palette.type,
158 type = _palette$type === void 0 ? 'light' : _palette$type,
159 _palette$contrastThre = palette.contrastThreshold,
160 contrastThreshold = _palette$contrastThre === void 0 ? 3 : _palette$contrastThre,
161 _palette$tonalOffset = palette.tonalOffset,
162 tonalOffset = _palette$tonalOffset === void 0 ? 0.2 : _palette$tonalOffset,
163 other = (0, _objectWithoutProperties2.default)(palette, ["primary", "secondary", "error", "warning", "info", "success", "type", "contrastThreshold", "tonalOffset"]); // Use the same logic as
164 // Bootstrap: https://github.com/twbs/bootstrap/blob/1d6e3710dd447de1a200f29e8fa521f8a0908f70/scss/_functions.scss#L59
165 // and material-components-web https://github.com/material-components/material-components-web/blob/ac46b8863c4dab9fc22c4c662dc6bd1b65dd652f/packages/mdc-theme/_functions.scss#L54
166
167 function getContrastText(background) {
168 var contrastText = (0, _colorManipulator.getContrastRatio)(background, dark.text.primary) >= contrastThreshold ? dark.text.primary : light.text.primary;
169
170 if (process.env.NODE_ENV !== 'production') {
171 var contrast = (0, _colorManipulator.getContrastRatio)(background, contrastText);
172
173 if (contrast < 3) {
174 console.error(["Material-UI: The contrast ratio of ".concat(contrast, ":1 for ").concat(contrastText, " on ").concat(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'));
175 }
176 }
177
178 return contrastText;
179 }
180
181 var augmentColor = function augmentColor(color) {
182 var mainShade = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 500;
183 var lightShade = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 300;
184 var darkShade = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 700;
185 color = (0, _extends2.default)({}, color);
186
187 if (!color.main && color[mainShade]) {
188 color.main = color[mainShade];
189 }
190
191 if (!color.main) {
192 throw new Error(process.env.NODE_ENV !== "production" ? "Material-UI: The color provided to augmentColor(color) is invalid.\nThe color object needs to have a `main` property or a `".concat(mainShade, "` property.") : (0, _utils.formatMuiErrorMessage)(4, mainShade));
193 }
194
195 if (typeof color.main !== 'string') {
196 throw new Error(process.env.NODE_ENV !== "production" ? "Material-UI: The color provided to augmentColor(color) is invalid.\n`color.main` should be a string, but `".concat(JSON.stringify(color.main), "` was provided instead.\n\nDid you intend to use one of the following approaches?\n\nimport {\xA0green } from \"@material-ui/core/colors\";\n\nconst theme1 = createTheme({ palette: {\n primary: green,\n} });\n\nconst theme2 = createTheme({ palette: {\n primary: { main: green[500] },\n} });") : _formatMuiErrorMessage(5, JSON.stringify(color.main)));
197 }
198
199 addLightOrDark(color, 'light', lightShade, tonalOffset);
200 addLightOrDark(color, 'dark', darkShade, tonalOffset);
201
202 if (!color.contrastText) {
203 color.contrastText = getContrastText(color.main);
204 }
205
206 return color;
207 };
208
209 var types = {
210 dark: dark,
211 light: light
212 };
213
214 if (process.env.NODE_ENV !== 'production') {
215 if (!types[type]) {
216 console.error("Material-UI: The palette type `".concat(type, "` is not supported."));
217 }
218 }
219
220 var paletteOutput = (0, _utils.deepmerge)((0, _extends2.default)({
221 // A collection of common colors.
222 common: _common.default,
223 // The palette type, can be light or dark.
224 type: type,
225 // The colors used to represent primary interface elements for a user.
226 primary: augmentColor(primary),
227 // The colors used to represent secondary interface elements for a user.
228 secondary: augmentColor(secondary, 'A400', 'A200', 'A700'),
229 // The colors used to represent interface elements that the user should be made aware of.
230 error: augmentColor(error),
231 // The colors used to represent potentially dangerous actions or important messages.
232 warning: augmentColor(warning),
233 // The colors used to present information to the user that is neutral and not necessarily important.
234 info: augmentColor(info),
235 // The colors used to indicate the successful completion of an action that user triggered.
236 success: augmentColor(success),
237 // The grey colors.
238 grey: _grey.default,
239 // Used by `getContrastText()` to maximize the contrast between
240 // the background and the text.
241 contrastThreshold: contrastThreshold,
242 // Takes a background color and returns the text color that maximizes the contrast.
243 getContrastText: getContrastText,
244 // Generate a rich color object.
245 augmentColor: augmentColor,
246 // Used by the functions below to shift a color's luminance by approximately
247 // two indexes within its tonal palette.
248 // E.g., shift from Red 500 to Red 300 or Red 700.
249 tonalOffset: tonalOffset
250 }, types[type]), other);
251 return paletteOutput;
252}
\No newline at end of file