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