|
| 1 | +/* |
| 2 | + * Copyright 2025 coze-dev Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +const plugin = require('tailwindcss/plugin'); |
| 18 | + |
| 19 | +const lightModeVariables = require('./light'); |
| 20 | +const darkModeVariables = require('./dark'); |
| 21 | + |
| 22 | +// 用于生成 CSS 变量的帮助函数 |
| 23 | +function generateCssVariables(variables, theme) { |
| 24 | + return Object.entries(variables).reduce((acc, [key, value]) => { |
| 25 | + acc[`--${key}`] = theme ? theme(value) : value; |
| 26 | + return acc; |
| 27 | + }, {}); |
| 28 | +} |
| 29 | + |
| 30 | +// 样式语义化 |
| 31 | +function generateSemanticVariables(semantics, theme, property) { |
| 32 | + return Object.entries(semantics).map(([key, value]) => ({ |
| 33 | + [`.${key}`]: { |
| 34 | + [property]: theme(value), |
| 35 | + }, |
| 36 | + })); |
| 37 | +} |
| 38 | + |
| 39 | +const semanticForeground = { |
| 40 | + /* Theme */ |
| 41 | + 'coz-fg-hglt-plus': 'colors.foreground.5', |
| 42 | + 'coz-fg-hglt-plus-dim': 'colors.foreground.5', |
| 43 | + 'coz-fg-hglt': 'colors.brand.5', |
| 44 | + 'coz-fg-hglt-dim': 'colors.brand.3', |
| 45 | + 'coz-fg-plus': 'colors.foreground.4', |
| 46 | + 'coz-fg': 'colors.foreground.3', |
| 47 | + 'coz-fg-primary': 'colors.foreground.3', |
| 48 | + 'coz-fg-secondary': 'colors.foreground.2', |
| 49 | + 'coz-fg-dim': 'colors.foreground.1', |
| 50 | + 'coz-fg-white': 'colors.foreground.7', |
| 51 | + 'coz-fg-white-dim': 'colors.foreground.white', |
| 52 | + 'coz-fg-hglt-ai': 'colors.purple.5', |
| 53 | + 'coz-fg-hglt-ai-dim': 'colors.purple.3', |
| 54 | + /* Functional Color */ |
| 55 | + 'coz-fg-hglt-red': 'colors.red.5', |
| 56 | + 'coz-fg-hglt-red-dim': 'colors.red.3', |
| 57 | + 'coz-fg-hglt-yellow': 'colors.yellow.5', |
| 58 | + 'coz-fg-hglt-yellow-dim': 'colors.yellow.3', |
| 59 | + 'coz-fg-hglt-green': 'colors.green.5', |
| 60 | + 'coz-fg-hglt-green-dim': 'colors.green.3', |
| 61 | + /* Chart, Tag Only */ |
| 62 | + 'coz-fg-color-orange': 'colors.yellow.5', |
| 63 | + 'coz-fg-color-orange-dim': 'colors.yellow.3', |
| 64 | + 'coz-fg-color-emerald': 'colors.green.5', |
| 65 | + 'coz-fg-color-emerald-dim': 'colors.green.3', |
| 66 | + 'coz-fg-color-cyan': 'colors.cyan.50', |
| 67 | + 'coz-fg-color-cyan-dim': 'colors.cyan.30', |
| 68 | + 'coz-fg-color-blue': 'colors.blue.50', |
| 69 | + 'coz-fg-color-blue-dim': 'colors.blue.30', |
| 70 | + 'coz-fg-color-purple': 'colors.purple.50', |
| 71 | + 'coz-fg-color-purple-dim': 'colors.purple.30', |
| 72 | + 'coz-fg-color-magenta': 'colors.magenta.50', |
| 73 | + 'coz-fg-color-magenta-dim': 'colors.magenta.3', |
| 74 | + 'coz-fg-color-yellow': 'colors.yellow.50', |
| 75 | + 'coz-fg-color-yellow-dim': 'colors.yellow.30', |
| 76 | + /* Code Only */ |
| 77 | + 'coz-fg-hglt-orange': 'colors.orange.5', |
| 78 | + 'coz-fg-hglt-orange-dim': 'colors.orange.3', |
| 79 | + 'coz-fg-hglt-emerald': 'colors.emerald.5', |
| 80 | + 'coz-fg-hglt-emerald-dim': 'colors.emerald.3', |
| 81 | + 'coz-fg-hglt-cyan': 'colors.cyan.5', |
| 82 | + 'coz-fg-hglt-cyan-dim': 'colors.cyan.3', |
| 83 | + 'coz-fg-hglt-blue': 'colors.blue.5', |
| 84 | + 'coz-fg-hglt-blue-dim': 'colors.blue.3', |
| 85 | + 'coz-fg-hglt-purple': 'colors.purple.5', |
| 86 | + 'coz-fg-hglt-purple-dim': 'colors.purple.3', |
| 87 | + 'coz-fg-hglt-magenta': 'colors.magenta.5', |
| 88 | + 'coz-fg-hglt-magenta-dim': 'colors.magenta.3', |
| 89 | + /* branding Only */ |
| 90 | + 'coz-fg-color-brand': 'colors.brand.50', |
| 91 | + 'coz-fg-color-brand-dim': 'colors.brand.30', |
| 92 | + 'coz-fg-color-alternative': 'colors.alternative.50', |
| 93 | + 'coz-fg-color-alternative-dim': 'colors.alternative.30', |
| 94 | +}; |
| 95 | + |
| 96 | +const semanticMiddleground = { |
| 97 | + /* Theme */ |
| 98 | + 'coz-mg-hglt-plus-pressed': 'colors.brand.7', |
| 99 | + 'coz-mg-hglt-plus-hovered': 'colors.brand.6', |
| 100 | + 'coz-mg-hglt-plus': 'colors.brand.5', |
| 101 | + 'coz-mg-hglt-plus-dim': 'colors.brand.3', |
| 102 | + 'coz-mg-hglt-secondary-pressed': 'colors.brand.2', |
| 103 | + 'coz-mg-hglt-secondary-hovered': 'colors.brand.1', |
| 104 | + 'coz-mg-hglt-secondary': 'colors.brand.0', |
| 105 | + 'coz-mg-hglt-secondary-red': 'colors.red.0', |
| 106 | + 'coz-mg-hglt-secondary-yellow': 'colors.yellow.0', |
| 107 | + 'coz-mg-hglt-secondary-green': 'colors.green.0', |
| 108 | + 'coz-mg-plus-pressed': 'colors.background.8', |
| 109 | + 'coz-mg-plus-hovered': 'colors.background.7', |
| 110 | + 'coz-mg-plus': 'colors.background.6', |
| 111 | + 'coz-mg-hglt-pressed': 'colors.brand.3', |
| 112 | + 'coz-mg-hglt-hovered': 'colors.brand.2', |
| 113 | + 'coz-mg-hglt-plus-ai-pressed': 'colors.purple.7', |
| 114 | + 'coz-mg-hglt-plus-ai-hovered': 'colors.purple.6', |
| 115 | + 'coz-mg-hglt-plus-ai': 'colors.purple.5', |
| 116 | + 'coz-mg-hglt-plus-ai-dim': 'colors.purple.3', |
| 117 | + 'coz-mg-hglt': 'colors.brand.1', |
| 118 | + 'coz-mg-hglt-ai-pressed': 'colors.purple.3', |
| 119 | + 'coz-mg-hglt-ai-hovered': 'colors.purple.2', |
| 120 | + 'coz-mg-hglt-ai': 'colors.purple.1', |
| 121 | + /* Functional Color */ |
| 122 | + 'coz-mg-hglt-plus-red-pressed': 'colors.red.7', |
| 123 | + 'coz-mg-hglt-plus-red-hovered': 'colors.red.6', |
| 124 | + 'coz-mg-hglt-plus-red': 'colors.red.5', |
| 125 | + 'coz-mg-hglt-plus-red-dim': 'colors.red.3', |
| 126 | + 'coz-mg-hglt-plus-yellow-pressed': 'colors.yellow.7', |
| 127 | + 'coz-mg-hglt-plus-yellow-hovered': 'colors.yellow.6', |
| 128 | + 'coz-mg-hglt-plus-yellow': 'colors.yellow.5', |
| 129 | + 'coz-mg-hglt-plus-yellow-dim': 'colors.yellow.3', |
| 130 | + 'coz-mg-hglt-plus-green-pressed': 'colors.green.7', |
| 131 | + 'coz-mg-hglt-plus-green-hovered': 'colors.green.6', |
| 132 | + 'coz-mg-hglt-plus-green': 'colors.green.5', |
| 133 | + 'coz-mg-hglt-plus-green-dim': 'colors.green.3', |
| 134 | + 'coz-mg-hglt-red-pressed': 'colors.red.3', |
| 135 | + 'coz-mg-hglt-red-hovered': 'colors.red.2', |
| 136 | + 'coz-mg-hglt-red': 'colors.red.1', |
| 137 | + 'coz-mg-hglt-yellow-pressed': 'colors.yellow.3', |
| 138 | + 'coz-mg-hglt-yellow-hovered': 'colors.yellow.2', |
| 139 | + 'coz-mg-hglt-yellow': 'colors.yellow.1', |
| 140 | + 'coz-mg-hglt-green-pressed': 'colors.green.3', |
| 141 | + 'coz-mg-hglt-green-hovered': 'colors.green.2', |
| 142 | + 'coz-mg-hglt-green': 'colors.green.1', |
| 143 | + /* Card, Tag, Avatar Only */ |
| 144 | + 'coz-mg-color-plus-orange': 'colors.yellow.5', |
| 145 | + 'coz-mg-color-plus-emerald': 'colors.green.5', |
| 146 | + 'coz-mg-color-plus-cyan': 'colors.cyan.50', |
| 147 | + 'coz-mg-color-plus-blue': 'colors.blue.50', |
| 148 | + 'coz-mg-color-plus-purple': 'colors.purple.50', |
| 149 | + 'coz-mg-color-plus-magenta': 'colors.magenta.50', |
| 150 | + 'coz-mg-color-plus-yellow': 'colors.yellow.50', |
| 151 | + 'coz-mg-color-orange-pressed': 'colors.yellow.3', |
| 152 | + 'coz-mg-color-orange-hovered': 'colors.yellow.2', |
| 153 | + 'coz-mg-color-orange': 'colors.yellow.1', |
| 154 | + 'coz-mg-color-emerald-pressed': 'colors.green.3', |
| 155 | + 'coz-mg-color-emerald-hovered': 'colors.green.2', |
| 156 | + 'coz-mg-color-emerald': 'colors.green.1', |
| 157 | + 'coz-mg-color-cyan-pressed': 'colors.cyan.30', |
| 158 | + 'coz-mg-color-cyan-hovered': 'colors.cyan.20', |
| 159 | + 'coz-mg-color-cyan': 'colors.cyan.10', |
| 160 | + 'coz-mg-color-blue-pressed': 'colors.blue.30', |
| 161 | + 'coz-mg-color-blue-hovered': 'colors.blue.20', |
| 162 | + 'coz-mg-color-blue': 'colors.blue.10', |
| 163 | + 'coz-mg-color-purple-pressed': 'colors.purple.30', |
| 164 | + 'coz-mg-color-purple-hovered': 'colors.purple.20', |
| 165 | + 'coz-mg-color-purple': 'colors.purple.10', |
| 166 | + 'coz-mg-color-magenta-pressed': 'colors.magenta.30', |
| 167 | + 'coz-mg-color-magenta-hovered': 'colors.magenta.20', |
| 168 | + 'coz-mg-color-magenta': 'colors.magenta.10', |
| 169 | + 'coz-mg-primary-pressed': 'colors.background.7', |
| 170 | + 'coz-mg-primary-hovered': 'colors.background.6', |
| 171 | + 'coz-mg-primary': 'colors.background.5', |
| 172 | + 'coz-mg-secondary-pressed': 'colors.background.6', |
| 173 | + 'coz-mg-secondary-hovered': 'colors.background.5', |
| 174 | + 'coz-mg-secondary': 'colors.background.4', |
| 175 | + 'coz-mg': 'colors.background.4', |
| 176 | + 'coz-mg-mask': 'colors.mask.5', |
| 177 | + 'coz-mg-table-fixed-hovered': 'colors.background.0', |
| 178 | + 'coz-mg-card-pressed': 'colors.background.3', |
| 179 | + 'coz-mg-card-hovered': 'colors.background.3', |
| 180 | + 'coz-mg-card': 'colors.background.3', |
| 181 | + /** brand */ |
| 182 | + 'coz-mg-color-plus-brand': 'colors.brand.50', |
| 183 | +}; |
| 184 | + |
| 185 | +const semanticBackground = { |
| 186 | + 'coz-bg-max': 'colors.background.3', |
| 187 | + 'coz-bg-plus': 'colors.background.2', |
| 188 | + 'coz-bg-primary': 'colors.background.1', |
| 189 | + 'coz-bg': 'colors.background.1', |
| 190 | + 'coz-bg-secondary': 'colors.background.0', |
| 191 | +}; |
| 192 | + |
| 193 | +const semanticShadow = { |
| 194 | + 'coz-shadow': 'boxShadow.normal', |
| 195 | + 'coz-shadow-large': 'boxShadow.large', |
| 196 | + 'coz-shadow-default': 'boxShadow.normal', |
| 197 | + 'coz-shadow-small': 'boxShadow.small', |
| 198 | +}; |
| 199 | + |
| 200 | +// Add button rounded definitions |
| 201 | +const buttonRounded = { |
| 202 | + 'coz-btn-rounded-large': 'btnBorderRadius.large', |
| 203 | + 'coz-btn-rounded-normal': 'btnBorderRadius.normal', |
| 204 | + 'coz-btn-rounded-small': 'btnBorderRadius.small', |
| 205 | + 'coz-btn-rounded-mini': 'btnBorderRadius.mini', |
| 206 | +}; |
| 207 | + |
| 208 | +const inputRounded = { |
| 209 | + 'coz-input-rounded-large': 'inputBorderRadius.large', |
| 210 | + 'coz-input-rounded-normal': 'inputBorderRadius.normal', |
| 211 | + 'coz-input-rounded-small': 'inputBorderRadius.small', |
| 212 | +}; |
| 213 | + |
| 214 | +const inputHeight = { |
| 215 | + 'coz-input-height-large': 'inputHeight.large', |
| 216 | + 'coz-input-height-normal': 'inputHeight.normal', |
| 217 | + 'coz-input-height-small': 'inputHeight.small', |
| 218 | +}; |
| 219 | + |
| 220 | +const semanticStroke = { |
| 221 | + 'coz-stroke-hglt': 'colors.brand.5', |
| 222 | + 'coz-stroke-plus': 'colors.stroke.6', |
| 223 | + 'coz-stroke-primary': 'colors.stroke.5', |
| 224 | + 'coz-stroke-hglt-red': 'colors.red.5', |
| 225 | + 'coz-stroke-hglt-yellow': 'colors.yellow.5', |
| 226 | + 'coz-stroke-hglt-green': 'colors.green.5', |
| 227 | + 'coz-stroke-color-orange': 'colors.yellow.5', |
| 228 | + 'coz-stroke-color-emerald': 'colors.green.5', |
| 229 | + 'coz-stroke-color-cyan': 'colors.cyan.50', |
| 230 | + 'coz-stroke-color-blue': 'colors.blue.50', |
| 231 | + 'coz-stroke-color-purple': 'colors.purple.50', |
| 232 | + 'coz-stroke-color-magenta': 'colors.magenta.50', |
| 233 | + 'coz-stroke-color-yellow': 'colors.yellow.50', |
| 234 | + 'coz-stroke-color-brand': 'colors.brand.50', |
| 235 | + 'coz-stroke-opaque': 'colors.stroke.opaque', |
| 236 | + 'coz-stroke-max': 'colors.stroke.max', |
| 237 | +}; |
| 238 | + |
| 239 | +function genTailwindPlugin(defaultCls, darkCls) { |
| 240 | + return plugin(function ({ addBase, addUtilities, theme }) { |
| 241 | + addBase({ |
| 242 | + [defaultCls]: generateCssVariables(lightModeVariables), |
| 243 | + [darkCls]: generateCssVariables(darkModeVariables), |
| 244 | + }); |
| 245 | + |
| 246 | + addBase({ |
| 247 | + [defaultCls]: { |
| 248 | + ...generateCssVariables(semanticForeground, theme), |
| 249 | + ...generateCssVariables(semanticMiddleground, theme), |
| 250 | + ...generateCssVariables(semanticBackground, theme), |
| 251 | + ...generateCssVariables(semanticStroke, theme), |
| 252 | + ...generateCssVariables(semanticShadow, theme), |
| 253 | + ...generateCssVariables(buttonRounded, theme), |
| 254 | + ...generateCssVariables(inputRounded, theme), |
| 255 | + ...generateCssVariables(inputHeight, theme), |
| 256 | + }, |
| 257 | + }); |
| 258 | + |
| 259 | + addUtilities([ |
| 260 | + ...generateSemanticVariables(semanticForeground, theme, 'color'), |
| 261 | + ...generateSemanticVariables( |
| 262 | + semanticMiddleground, |
| 263 | + theme, |
| 264 | + 'background-color', |
| 265 | + ), |
| 266 | + ...generateSemanticVariables( |
| 267 | + semanticBackground, |
| 268 | + theme, |
| 269 | + 'background-color', |
| 270 | + ), |
| 271 | + ...generateSemanticVariables(semanticStroke, theme, 'border-color'), |
| 272 | + ...generateSemanticVariables(semanticShadow, theme, 'box-shadow'), |
| 273 | + ...generateSemanticVariables(buttonRounded, theme, 'border-radius'), |
| 274 | + ...generateSemanticVariables(inputRounded, theme, 'border-radius'), |
| 275 | + ...generateSemanticVariables(inputHeight, theme, 'height'), |
| 276 | + ]); |
| 277 | + }); |
| 278 | +} |
| 279 | + |
| 280 | +module.exports = { |
| 281 | + genTailwindPlugin, |
| 282 | +}; |
0 commit comments