Skip to content

Commit 8c8699c

Browse files
committed
fix: fix the problem that customTokens of color plugin is accidentally inserted into prefix
1 parent 1181729 commit 8c8699c

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

src/tokens/internal/color.ts

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,79 @@ import { Strings } from "../../utils/strings"
55
import { Validates } from "../../utils/validates"
66

77
export type TColorProviderConstructorParams = {
8+
/**
9+
* @description
10+
* For managed or public CSS variables.
11+
*
12+
* @default 'md-sys-color'
13+
*
14+
* @example If the value of prefix is 'your-prefix'.
15+
* ```css
16+
* .text-on-surface {
17+
* color: var(--your-prefix-on-surface, #181c25);
18+
* }
19+
* ```
20+
*/
821
readonly prefix: string
22+
23+
/**
24+
* @description
25+
* This option can customize the CSS value corresponding to certain tokens.
26+
* Note that this option ignores [hardcodeDefaultValue] and [prefix].
27+
*
28+
* @default []
29+
*
30+
* @example
31+
* ```typescript
32+
* const color = provideColor({
33+
* customTokens: {
34+
* // .bg-background { background-color: red; }
35+
* // .text-background { color: red; }
36+
* background: 'red',
37+
* // .bg-background { background-color: var(--my-color-surface, green); }
38+
* // .text-background { color: var(--my-color-surface, green); }
39+
* surface: 'var(--my-color-surface, green)'
40+
* },
41+
* })
42+
* ```
43+
*/
944
readonly customTokens: IMaterialDesignThemeTokens | Record<string, string>
45+
46+
/**
47+
* @description
48+
* When set to true, each token contains a default value.
49+
*
50+
* @default true
51+
*
52+
* @example
53+
* ```typescript
54+
* const color = provideColor({
55+
* // .text-on-surface { color: var(--md-sys-color-on-surface, #181c25) }
56+
* hardcodeDefaultValue: true,
57+
*
58+
* // .text-on-surface { color: var(--md-sys-color-on-surface) }
59+
* hardcodeDefaultValue: false,
60+
* })
61+
* ```
62+
*/
1063
readonly hardcodeDefaultValue: boolean
64+
65+
/**
66+
* @description
67+
* Exclude some unnecessary tokens.
68+
*
69+
* @default []
70+
*
71+
* @example
72+
* ```typescript
73+
* const color = provideColor({
74+
* excludedTokens: [
75+
* // remove .bg-background and .text-background
76+
* 'background',
77+
* ],
78+
* })
79+
* ```
80+
*/
1181
readonly excludedTokens: Array<(keyof IMaterialDesignThemeTokens) | {}>
1282
}
1383

@@ -147,32 +217,36 @@ export class ColorProvider extends DefaultMaterialDesignThemeTokens implements I
147217
public tokens
148218
public hardcodeDefaultValue
149219
public excludedTokens
220+
public customTokens
150221

151222
constructor(params: Partial<TColorProviderConstructorParams>) {
152223
super()
153224
this.prefix = params.prefix ?? 'md-sys-color'
154225
this.hardcodeDefaultValue = params.hardcodeDefaultValue ?? true
155226
this.excludedTokens = params.excludedTokens ?? []
156-
this.tokens = this.validate([(params.customTokens ?? {}) as Record<string, string>, this.defaultTokenRecord, this.excludedTokens])
227+
this.customTokens = params.customTokens ?? {}
228+
this.tokens = this.validate([(this.customTokens) as Record<string, string>, this.defaultTokenRecord, this.excludedTokens])
157229
}
158230

159231
protected validate(params: Parameters<typeof Validates.validate>) {
160232
return Validates.validate(...params)
161233
}
162234

163235
protected transformTokensToCssRuleObject(prefix: string, tokens: Record<string, string>, hardcodeDefaultValue: boolean) {
236+
const cssPropertyComputed = (name: string, value: string) => !Object.hasOwn(this.customTokens, name) ? `var(--${prefix}-${Strings.toKebabCase(name)}, ${hardcodeDefaultValue ? value : ''})` : `${value}`
237+
164238
const textTokens = Validates.transformTokenRecordToCssRuleObject(
165239
tokens,
166240
(name) => `.text-${Strings.toKebabCase(name)}`,
167241
(name, value) => ({
168-
'color': `var(--${prefix}-${Strings.toKebabCase(name)}, ${hardcodeDefaultValue ? value : ''})`
242+
'color': cssPropertyComputed(name, value)
169243
})
170244
)
171245
const bgTokens = Validates.transformTokenRecordToCssRuleObject(
172246
tokens,
173247
(name) => `.bg-${Strings.toKebabCase(name)}`,
174248
(name, value) => ({
175-
'background-color': `var(--${prefix}-${Strings.toKebabCase(name)}, ${hardcodeDefaultValue ? value : ''})`
249+
'background-color': cssPropertyComputed(name, value)
176250
})
177251
)
178252
return Object.assign({}, textTokens, bgTokens)

src/tokens/provide-color.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import { ColorProvider, type TColorProviderConstructorParams } from "./internal/
1919
* @example
2020
* ```typescript
2121
* const color = provideColor({
22-
* useDefaultValue: true,
23-
* defaultTokens: {
22+
* hardcodeDefaultValue: true,
23+
* customTokens: {
2424
* background: '#00ff00',
2525
* onBackground: '#ff00ff',
2626
* },

0 commit comments

Comments
 (0)