@@ -5,9 +5,79 @@ import { Strings } from "../../utils/strings"
5
5
import { Validates } from "../../utils/validates"
6
6
7
7
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
+ */
8
21
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
+ */
9
44
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
+ */
10
63
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
+ */
11
81
readonly excludedTokens : Array < ( keyof IMaterialDesignThemeTokens ) | { } >
12
82
}
13
83
@@ -147,32 +217,36 @@ export class ColorProvider extends DefaultMaterialDesignThemeTokens implements I
147
217
public tokens
148
218
public hardcodeDefaultValue
149
219
public excludedTokens
220
+ public customTokens
150
221
151
222
constructor ( params : Partial < TColorProviderConstructorParams > ) {
152
223
super ( )
153
224
this . prefix = params . prefix ?? 'md-sys-color'
154
225
this . hardcodeDefaultValue = params . hardcodeDefaultValue ?? true
155
226
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 ] )
157
229
}
158
230
159
231
protected validate ( params : Parameters < typeof Validates . validate > ) {
160
232
return Validates . validate ( ...params )
161
233
}
162
234
163
235
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
+
164
238
const textTokens = Validates . transformTokenRecordToCssRuleObject (
165
239
tokens ,
166
240
( name ) => `.text-${ Strings . toKebabCase ( name ) } ` ,
167
241
( name , value ) => ( {
168
- 'color' : `var(-- ${ prefix } - ${ Strings . toKebabCase ( name ) } , ${ hardcodeDefaultValue ? value : '' } )`
242
+ 'color' : cssPropertyComputed ( name , value )
169
243
} )
170
244
)
171
245
const bgTokens = Validates . transformTokenRecordToCssRuleObject (
172
246
tokens ,
173
247
( name ) => `.bg-${ Strings . toKebabCase ( name ) } ` ,
174
248
( name , value ) => ( {
175
- 'background-color' : `var(-- ${ prefix } - ${ Strings . toKebabCase ( name ) } , ${ hardcodeDefaultValue ? value : '' } )`
249
+ 'background-color' : cssPropertyComputed ( name , value )
176
250
} )
177
251
)
178
252
return Object . assign ( { } , textTokens , bgTokens )
0 commit comments