UNPKG

2.92 kBJavaScriptView Raw
1import { error as _error } from "@instructure/console";
2
3/*
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2015 - present Instructure, Inc.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in all
16 * copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26import { isEmpty, camelize } from '@instructure/ui-utils';
27
28/**
29 * ---
30 * category: utilities/themes
31 * ---
32 * Given a theme object, a string of space delimited prop values,
33 * and a propName prefix, combines each prop value with the
34 * propName prefix and replaces it with a corresponding value
35 * from the theme object.
36 *
37 * @param {String} componentName - the name of the component (for error messages)
38 * @param {Object} componentTheme - a theme object of keys and values
39 * @param {String} propValue - a space delimited string of values
40 * @param {String} propName - a prefix to combine with each propValue
41 * @returns {String} a string with each value replaced with a value from the theme object
42 */
43function getShorthandPropValue(componentName, componentTheme, propValue, propName) {
44 if (typeof propValue !== 'string' || isEmpty(componentTheme)) {
45 return;
46 }
47
48 return propValue.split(' ').map(function (shortHandValue) {
49 if (shortHandValue === 'auto' || shortHandValue === '0') {
50 return shortHandValue;
51 }
52
53 if (shortHandValue === 'none') {
54 return '0';
55 }
56
57 if (shortHandValue === 'circle') {
58 return '100%';
59 }
60
61 if (shortHandValue === 'pill') {
62 return '999em';
63 }
64
65 var themeVariableName = camelize("".concat(propName, "-").concat(shortHandValue));
66 var themeVariableValue = componentTheme[themeVariableName];
67
68 /*#__PURE__*/
69
70 /*#__PURE__*/
71 _error(themeVariableValue, "[".concat(componentName, "] '").concat(themeVariableName, "' is an invalid '").concat(propName, "' value."));
72
73 return themeVariableValue || '0';
74 }).join(' ').trim();
75}
76
77export default getShorthandPropValue;
78export { getShorthandPropValue };
\No newline at end of file