|
| 1 | +import merge from 'lodash.merge' |
| 2 | +import { Formatter } from 'style-dictionary' |
| 3 | + |
| 4 | +const themes = ['light', 'dark'] |
| 5 | + |
| 6 | +const kebabCase = (str: string) => str && str.toLowerCase().replaceAll(' ', '-') |
| 7 | + |
| 8 | +/** |
| 9 | + * This function transforms tokens into a nested object |
| 10 | + * structure ready for Tailwind. The conditional statements |
| 11 | + * are largely to handle creating both static and dynamic |
| 12 | + * tokens. E.g. A given token needs three variants: |
| 13 | + * dynamic, static light, and static dark. This function gets |
| 14 | + * run twice: once to create all static tokens, and another |
| 15 | + * time to create the dynamic tokens which places values on |
| 16 | + * the parent (e.g. the root object, or 'legacy') and uses the |
| 17 | + * appropriate color variable. |
| 18 | + */ |
| 19 | +function createColorTokensFromGroup(tokens, staticTheme = true) { |
| 20 | + const colorTokens = {} |
| 21 | + tokens.forEach(({ type, name, ...t }) => { |
| 22 | + if (type === 'color') { |
| 23 | + /** |
| 24 | + * The following conditions are in order to properly group |
| 25 | + * color tokens and format into a nested object structure |
| 26 | + * for use in Tailwind. |
| 27 | + */ |
| 28 | + let colorGroup = colorTokens[t.attributes.type] ?? {} |
| 29 | + |
| 30 | + const tItem = kebabCase(t.attributes.item) |
| 31 | + const tSubItem = kebabCase(t.attributes.subitem) |
| 32 | + |
| 33 | + /** |
| 34 | + * `state` is for the deepest level on a token. |
| 35 | + * E.g. `icon` in colors.systemfeedback.success.icon |
| 36 | + */ |
| 37 | + if (t.attributes.state) { |
| 38 | + if (!staticTheme) { |
| 39 | + // If not on a static theme, do not place within `dark` or `light` groups |
| 40 | + colorTokens[tItem] = colorTokens[tItem] || {} |
| 41 | + const tokenGroup = colorTokens[tItem][tSubItem] ?? {} |
| 42 | + colorTokens[tItem][tSubItem] = merge(tokenGroup, { |
| 43 | + [t.attributes.state]: t.value |
| 44 | + }) |
| 45 | + } else { |
| 46 | + // If on a static theme, place within `dark` or `light` groups |
| 47 | + const tokenGroup = colorGroup[tItem] |
| 48 | + colorGroup[tItem] = merge(tokenGroup, { |
| 49 | + [tSubItem]: t.value |
| 50 | + }) |
| 51 | + } |
| 52 | + } else if (tSubItem) { |
| 53 | + /** |
| 54 | + * If not on a static theme AND theme is determined by `type` |
| 55 | + * property do not place within `dark` or `light` groups |
| 56 | + */ |
| 57 | + if (themes.includes(t.attributes.type) && !staticTheme) { |
| 58 | + const tokenGroup = colorTokens[tItem] ?? {} |
| 59 | + colorTokens[tItem] = merge(tokenGroup, { |
| 60 | + [tSubItem]: t.value |
| 61 | + }) |
| 62 | + |
| 63 | + /** |
| 64 | + * If not on a static theme AND theme is determined by `item` |
| 65 | + * property (e.g. legacy tokens) do not place within `dark` |
| 66 | + * or `light` groups |
| 67 | + */ |
| 68 | + } else if (themes.includes(t.attributes.item) && !staticTheme) { |
| 69 | + const tokenGroup = colorTokens[t.attributes.type] ?? {} |
| 70 | + colorTokens[t.attributes.type] = merge(tokenGroup, { |
| 71 | + [tSubItem]: t.value |
| 72 | + }) |
| 73 | + } else { |
| 74 | + // If on a static theme, place within `dark` or `light` groups |
| 75 | + const tokenGroup = colorGroup[tItem] |
| 76 | + colorGroup[tItem] = merge(tokenGroup, { |
| 77 | + [tSubItem]: t.value |
| 78 | + }) |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * If `item` property is the token name, don't nest inside object |
| 83 | + */ |
| 84 | + } else if (t.attributes.item) { |
| 85 | + colorGroup[tItem] = t.value |
| 86 | + |
| 87 | + /** |
| 88 | + * If `item` property is the token name, set directly on colorGroup |
| 89 | + */ |
| 90 | + } else if (t.attributes.type) { |
| 91 | + colorGroup = t.value |
| 92 | + } |
| 93 | + |
| 94 | + if (Object.keys(colorGroup).length > 0) { |
| 95 | + colorTokens[t.attributes.type] = colorGroup |
| 96 | + } |
| 97 | + } |
| 98 | + }) |
| 99 | + return colorTokens |
| 100 | +} |
| 101 | + |
| 102 | +function createDynamicColorTokens(tokens) { |
| 103 | + return createColorTokensFromGroup(tokens, true) |
| 104 | +} |
| 105 | + |
| 106 | +export default (({ dictionary }) => { |
| 107 | + const colorTokens = createDynamicColorTokens(dictionary.allTokens) |
| 108 | + |
| 109 | + const borderRadii = new Map([['none', '0']]) |
| 110 | + const spacing = new Map<string | number, string | number>([[0, 0]]) // Initialize with option for 0 spacing |
| 111 | + const gradients = new Map() |
| 112 | + const boxShadows = new Map([['none', 'none']]) |
| 113 | + const dropShadows = new Map<string, string | string[]>([ |
| 114 | + ['none', '0 0 #0000'] |
| 115 | + ]) |
| 116 | + |
| 117 | + // Format all other tokens |
| 118 | + dictionary.allTokens.forEach(({ type, name, ...t }) => { |
| 119 | + const attributes = t.attributes! |
| 120 | + if (attributes.category === 'radius') { |
| 121 | + if (attributes.type === 'full') { |
| 122 | + borderRadii.set(attributes.type, '9999px') |
| 123 | + } else { |
| 124 | + borderRadii.set(attributes.type!, t.value) |
| 125 | + } |
| 126 | + } else if (attributes.category === 'spacing') { |
| 127 | + spacing.set(attributes.type!, t.value) |
| 128 | + } else if (type === 'custom-gradient') { |
| 129 | + const [, ...pathParts] = t.path |
| 130 | + gradients.set(pathParts.join('-'), t.value) |
| 131 | + } else if (type === 'custom-shadow') { |
| 132 | + const [, ...pathParts] = t.path |
| 133 | + boxShadows.set( |
| 134 | + pathParts |
| 135 | + .filter((v) => !['elevation', 'light', 'dark'].includes(v)) |
| 136 | + .join('-') |
| 137 | + .replaceAll(' ', '-'), |
| 138 | + t.value.boxShadow |
| 139 | + ) |
| 140 | + dropShadows.set( |
| 141 | + pathParts |
| 142 | + .filter((v) => !['elevation', 'light', 'dark'].includes(v)) |
| 143 | + .join('-') |
| 144 | + .replaceAll(' ', '-'), |
| 145 | + t.value.dropShadow |
| 146 | + ) |
| 147 | + } |
| 148 | + }) |
| 149 | + |
| 150 | + // Note: replace strips out 'light-mode' and 'dark-mode' inside media queries |
| 151 | + return `module.exports = ${JSON.stringify( |
| 152 | + { |
| 153 | + colors: colorTokens, |
| 154 | + spacing: Object.fromEntries(spacing), |
| 155 | + borderRadius: Object.fromEntries(borderRadii), |
| 156 | + boxShadow: Object.fromEntries(boxShadows), |
| 157 | + dropShadow: Object.fromEntries(dropShadows), |
| 158 | + gradients: Object.fromEntries(gradients) |
| 159 | + }, |
| 160 | + null, |
| 161 | + ' '.repeat(2) |
| 162 | + )}` |
| 163 | +}) as Formatter |
0 commit comments