|
| 1 | +/** |
| 2 | + * Generate Color Utilities (Classes and SCSS Placeholders) for color dependencies |
| 3 | + * according to definitions made by DB UI v3 |
| 4 | + */ |
| 5 | + |
| 6 | +const prefix = 'db'; |
| 7 | + |
| 8 | +const generateInteractiveVariants = (currentColorObj, cssProp) => { |
| 9 | + return ` |
| 10 | + &:hover { |
| 11 | + ${cssProp}: $${prefix}-${currentColorObj.hover.name}; |
| 12 | + } |
| 13 | +
|
| 14 | + &:active { |
| 15 | + ${cssProp}: $${prefix}-${currentColorObj.pressed.name}; |
| 16 | + } |
| 17 | + `; |
| 18 | +}; |
| 19 | + |
| 20 | +/** |
| 21 | + * backgrounds have more than one variant with the same color for text (on-color) |
| 22 | + * e.g. neutral with variants 1-6 or transparent-full or transparent-semi |
| 23 | + */ |
| 24 | + |
| 25 | +const generateBGVariants = (value, index, currentColorObj, baseColorObj) => { |
| 26 | + return ` |
| 27 | +%${prefix}-bg-${value}-${index} { |
| 28 | + background-color: $${prefix}-${currentColorObj.enabled.name}; |
| 29 | + color: $${prefix}-${baseColorObj.enabled.name}; |
| 30 | +
|
| 31 | + &-ia, |
| 32 | + &[data-variant="ia"] { |
| 33 | + @extend %${prefix}-bg-${value}-${index}; |
| 34 | + ${generateInteractiveVariants(currentColorObj, 'background-color')} |
| 35 | + } |
| 36 | +
|
| 37 | + &-text-ia, |
| 38 | + &[data-variant="text-ia"] { |
| 39 | + color: $${prefix}-${baseColorObj.enabled.name}; |
| 40 | + ${generateInteractiveVariants(baseColorObj, 'color')} |
| 41 | + } |
| 42 | +
|
| 43 | + %weak { |
| 44 | + color: $${prefix}-${baseColorObj.weak.enabled.name}; |
| 45 | +
|
| 46 | + &-ia, |
| 47 | + &[data-variant="ia"] { |
| 48 | + color: $${prefix}-${baseColorObj.weak.enabled.name}; |
| 49 | + ${generateInteractiveVariants(baseColorObj.weak, 'color')} |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | +`; |
| 54 | +}; |
| 55 | + |
| 56 | +/** |
| 57 | + * generates color utility classes and scss placeholders for non-interactive and interactive variants |
| 58 | + * of color combination (background-color and color) based on definitions made by DB UI v3 |
| 59 | + * |
| 60 | + * @param {*} colorToken scss transform obj generated by styleDictionary |
| 61 | + * @returns scss string |
| 62 | + */ |
| 63 | +exports.generateColorUtilitityPlaceholder = (colorToken) => { |
| 64 | + let output = ''; |
| 65 | + |
| 66 | + Object.keys(colorToken).forEach((value, index) => { |
| 67 | + output += `/** |
| 68 | +* ${value.toUpperCase()} - Placeholder Utilities |
| 69 | +**/ |
| 70 | +`; |
| 71 | + // text colors with interactive variant, e.g. primary |
| 72 | + if (colorToken[value].enabled) { |
| 73 | + output += `%${prefix}-text-${value} { |
| 74 | + color: $${prefix}-${colorToken[value].enabled.name}; |
| 75 | +
|
| 76 | + &-ia, |
| 77 | + &[data-variant="ia"] { |
| 78 | + color: $${prefix}-${colorToken[value].enabled.name}; |
| 79 | + ${generateInteractiveVariants(colorToken[value], 'color')} |
| 80 | + } |
| 81 | +} |
| 82 | +`; |
| 83 | + |
| 84 | + // text and background colors |
| 85 | + output += ` |
| 86 | +%${prefix}-bg-${value} { |
| 87 | + background-color: $${prefix}-${colorToken[value].enabled.name}; |
| 88 | + color: $${prefix}-${colorToken[value].on.enabled.name}; |
| 89 | + |
| 90 | + &-ia, |
| 91 | + &[data-variant="ia"] { |
| 92 | + @extend %${prefix}-bg-${value}; |
| 93 | + ${generateInteractiveVariants(colorToken[value], 'background-color')} |
| 94 | + } |
| 95 | +
|
| 96 | + &-text-ia, |
| 97 | + &[data-variant="text-ia"] { |
| 98 | + color: $${prefix}-${colorToken[value].on.enabled.name}; |
| 99 | + ${generateInteractiveVariants(colorToken[value].on, 'color')} |
| 100 | + } |
| 101 | +}`; |
| 102 | + } |
| 103 | + |
| 104 | + Object.keys(colorToken[value].bg).forEach((variant) => { |
| 105 | + if (colorToken[value].bg[variant].enabled) { |
| 106 | + output += generateBGVariants( |
| 107 | + value, |
| 108 | + variant, |
| 109 | + colorToken[value].bg[variant], |
| 110 | + colorToken[value].on.bg |
| 111 | + ); |
| 112 | + } else { |
| 113 | + Object.keys(colorToken[value].bg[variant]).forEach( |
| 114 | + (childVariant) => { |
| 115 | + output += generateBGVariants( |
| 116 | + value, |
| 117 | + variant + '-' + childVariant, |
| 118 | + colorToken[value].bg[variant][childVariant], |
| 119 | + colorToken[value].on.bg |
| 120 | + ); |
| 121 | + } |
| 122 | + ); |
| 123 | + } |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + return output; |
| 128 | +}; |
0 commit comments