@veltra/styles 1.2.33 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@ import { withUnit } from '@veltra/utils'
3
3
  import { reactive, toRaw, watch } from 'vue'
4
4
 
5
5
  import { componentCssVarsDarkDecls, componentCssVarsLightDecls } from './component-css-vars'
6
- import { hexRgbOnly, hexWithAlpha, mixColor } from './helper'
6
+ import { hexRgbOnly, hexWithAlpha, isHexColor, mixColor } from './helper'
7
7
  import type { Theme } from './type'
8
8
 
9
9
  type RecursivePartial<T> = {
@@ -112,11 +112,13 @@ export class UITheme {
112
112
  private renderBGColorAlpha(theme: Theme): string {
113
113
  const { color } = theme.bg
114
114
  const alphas = [70]
115
- const lines: string[] = Object.keys(color).map(
116
- (type) => `--u-bg-color-${type}-alpha: ${color[type as keyof typeof color]}aa`
117
- )
115
+ const lines: string[] = []
118
116
  for (const type of Object.keys(color)) {
119
- const hex = color[type as keyof typeof color] as `#${string}`
117
+ const value = color[type as keyof typeof color]
118
+ // 非 hex 颜色(如 glass 主题的 rgba())无法推导 alpha token,跳过避免生成 NaN 声明
119
+ if (typeof value !== 'string' || !isHexColor(value)) continue
120
+ const hex = value as `#${string}`
121
+ lines.push(`--u-bg-color-${type}-alpha: ${hex}aa`)
120
122
  for (const a of alphas) {
121
123
  lines.push(`--u-bg-color-${type}-a-${a}: ${hexWithAlpha(hex, a)}`)
122
124
  }
@@ -130,26 +132,33 @@ export class UITheme {
130
132
  const lines: string[] = []
131
133
 
132
134
  for (const type of Object.keys(theme.color)) {
133
- const hex = theme.color[type as keyof typeof theme.color] as `#${string}`
135
+ const value = theme.color[type as keyof typeof theme.color]
136
+ if (typeof value !== 'string' || !isHexColor(value)) continue
137
+ const hex = value as `#${string}`
134
138
  for (const a of alphas) {
135
139
  lines.push(`--u-color-${type}-a-${a}: ${hexWithAlpha(hex, a)}`)
136
140
  }
137
141
  }
138
142
 
139
143
  const borderHex = theme.border.color as `#${string}`
140
- for (const a of alphas) {
141
- lines.push(`--u-border-color-a-${a}: ${hexWithAlpha(borderHex, a)}`)
144
+ if (isHexColor(borderHex)) {
145
+ for (const a of alphas) {
146
+ lines.push(`--u-border-color-a-${a}: ${hexWithAlpha(borderHex, a)}`)
147
+ }
142
148
  }
143
149
 
144
150
  const shadowHex = hexRgbOnly(theme.shadow.color)
145
- for (const a of alphas) {
146
- lines.push(`--u-shadow-color-a-${a}: ${hexWithAlpha(shadowHex, a)}`)
151
+ // 阴影色非 hex(如 rgba())时无法推导 alpha token,跳过避免生成 NaN 声明
152
+ if (isHexColor(shadowHex)) {
153
+ for (const a of alphas) {
154
+ lines.push(`--u-shadow-color-a-${a}: ${hexWithAlpha(shadowHex, a)}`)
155
+ }
147
156
  }
148
157
 
149
158
  const textColor = theme['text-color']
150
159
  for (const type of Object.keys(textColor)) {
151
160
  const value = textColor[type as keyof typeof textColor]
152
- if (typeof value === 'string' && /^#[0-9a-fA-F]{6}$/.test(value)) {
161
+ if (typeof value === 'string' && isHexColor(value)) {
153
162
  const hex = value as `#${string}`
154
163
  for (const a of alphas) {
155
164
  lines.push(`--u-text-color-${type}-a-${a}: ${hexWithAlpha(hex, a)}`)
@@ -169,6 +178,10 @@ export class UITheme {
169
178
  const borderColor = theme.border.color as `#${string}`
170
179
  const shadowColor = hexRgbOnly(theme.shadow.color)
171
180
 
181
+ // 依赖色含非 hex 值(如 glass 主题的 rgba())时整体跳过,避免生成 NaN 声明;
182
+ // 组件侧 var() fallback 会接管,行为等同于此前的无效声明
183
+ if (![hover, top, black, primary, borderColor].every(isHexColor)) return ''
184
+
172
185
  const itemBg = mixColor(hover, top, 0.28)
173
186
  const itemHoverBg = mixColor(primary, itemBg as `#${string}`, 0.96)
174
187
  const itemActiveBg = mixColor(primary, itemBg as `#${string}`, 0.95)
@@ -178,6 +191,9 @@ export class UITheme {
178
191
  const formHeaderBg = mixColor(top, hover, 0.04)
179
192
 
180
193
  const kbdInset = mixColor(top, black, 0.25)
194
+ const kbdDropShadow = isHexColor(shadowColor)
195
+ ? hexWithAlpha(shadowColor, 60)
196
+ : 'rgba(0, 0, 0, 0.6)'
181
197
 
182
198
  const lines = [
183
199
  `--u-collapse-item-bg: ${itemBg}`,
@@ -188,7 +204,7 @@ export class UITheme {
188
204
  `--u-batch-edit-form-header-bg: ${formHeaderBg}`,
189
205
  `--u-kbd-inset-shadow: ${kbdInset}`,
190
206
  `--u-kbd-border-shadow: ${hexWithAlpha(borderColor, 50)}`,
191
- `--u-kbd-drop-shadow: ${hexWithAlpha(shadowColor, 60)}`
207
+ `--u-kbd-drop-shadow: ${kbdDropShadow}`
192
208
  ]
193
209
 
194
210
  return lines.join(';')