|
1 | 1 | import plugin from "tailwindcss/plugin"
|
2 |
| -import type { CSSRuleObject } from "tailwindcss/types/config" |
3 | 2 | import type { IMaterialDesignThemeTokens } from "../../declaration/material-design-theme-token.interface"
|
4 | 3 | import type { IProvider } from "../../declaration/provider.interface"
|
5 | 4 | import { Strings } from "../../utils/strings"
|
| 5 | +import { Validates } from "../../utils/validates" |
6 | 6 |
|
7 | 7 | export type TColorProviderConstructorParams = {
|
8 | 8 | readonly prefix: string
|
9 |
| - readonly defaultTokens: IMaterialDesignThemeTokens | Record<string, string> |
10 |
| - readonly useDefaultValue: boolean |
| 9 | + readonly customTokens: IMaterialDesignThemeTokens | Record<string, string> |
| 10 | + readonly hardcodeDefaultValue: boolean |
11 | 11 | readonly excludedTokens: Array<(keyof IMaterialDesignThemeTokens) | {}>
|
12 | 12 | }
|
13 | 13 |
|
@@ -82,7 +82,7 @@ class DefaultMaterialDesignThemeTokens implements IMaterialDesignThemeTokens {
|
82 | 82 | onTertiaryFixed = '#1e1341'
|
83 | 83 | onTertiaryFixedVariant = '#4a4070'
|
84 | 84 |
|
85 |
| - public get defaultTokenValues() { |
| 85 | + public get defaultTokenRecord() { |
86 | 86 | return {
|
87 | 87 | primaryPaletteKeyColor: this.primaryPaletteKeyColor,
|
88 | 88 | secondaryPaletteKeyColor: this.secondaryPaletteKeyColor,
|
@@ -145,51 +145,41 @@ class DefaultMaterialDesignThemeTokens implements IMaterialDesignThemeTokens {
|
145 | 145 | export class ColorProvider extends DefaultMaterialDesignThemeTokens implements IProvider {
|
146 | 146 | public prefix
|
147 | 147 | public tokens
|
148 |
| - public useDefaultValue |
| 148 | + public hardcodeDefaultValue |
149 | 149 | public excludedTokens
|
150 | 150 |
|
151 | 151 | constructor(params: Partial<TColorProviderConstructorParams>) {
|
152 | 152 | super()
|
153 | 153 | this.prefix = params.prefix ?? 'md-sys-color'
|
154 |
| - this.useDefaultValue = params.useDefaultValue ?? true |
| 154 | + this.hardcodeDefaultValue = params.hardcodeDefaultValue ?? true |
155 | 155 | this.excludedTokens = params.excludedTokens ?? []
|
156 |
| - this.tokens = this.validate((params.defaultTokens ?? {}) as Record<string, string>) |
| 156 | + this.tokens = this.validate([(params.customTokens ?? {}) as Record<string, string>, this.defaultTokenRecord, this.excludedTokens]) |
157 | 157 | }
|
158 | 158 |
|
159 |
| - protected validate(tokens: Record<string, string>) { |
160 |
| - const newTokens = {} as Record<string, string> |
161 |
| - for (const token of Object.entries(this.defaultTokenValues)) { |
162 |
| - const tokenName = token[0] |
163 |
| - const defaultTokenValue = token[1] |
164 |
| - |
165 |
| - if (this.excludedTokens.includes(tokenName)) { |
166 |
| - continue |
167 |
| - } |
168 |
| - |
169 |
| - if (!Object.hasOwn(tokens, tokenName)) { |
170 |
| - newTokens[tokenName] = defaultTokenValue |
171 |
| - } else { |
172 |
| - newTokens[tokenName] = tokens[tokenName] |
173 |
| - } |
174 |
| - } |
175 |
| - return newTokens |
| 159 | + protected validate(params: Parameters<typeof Validates.validate>) { |
| 160 | + return Validates.validate(...params) |
176 | 161 | }
|
177 | 162 |
|
178 |
| - protected transformTokensToCssRuleObjectRecord(prefix: string, tokens: Record<string, string>) { |
179 |
| - const newTokens = {} as Record<string, CSSRuleObject> |
180 |
| - for (const token in tokens) { |
181 |
| - const className = Strings.toKebabCase(token) |
182 |
| - const cssVariantToken = `--${prefix}-${Strings.toKebabCase(token)}` |
183 |
| - const cssVariantValue = tokens[token] |
184 |
| - |
185 |
| - newTokens[`.bg-${className}`] = { 'background-color': `var(${cssVariantToken}, ${this.useDefaultValue ? cssVariantValue : ''})` } |
186 |
| - newTokens[`.text-${className}`] = { color: `var(${cssVariantToken}, ${this.useDefaultValue ? cssVariantValue : ''})` } |
187 |
| - } |
188 |
| - return newTokens |
| 163 | + protected transformTokensToCssRuleObject(prefix: string, tokens: Record<string, string>, hardcodeDefaultValue: boolean) { |
| 164 | + const textTokens = Validates.transformTokenRecordToCssRuleObject( |
| 165 | + tokens, |
| 166 | + (name) => `.text-${Strings.toKebabCase(name)}`, |
| 167 | + (name, value) => ({ |
| 168 | + 'color': `var(--${prefix}-${Strings.toKebabCase(name)}, ${hardcodeDefaultValue ? value : ''})` |
| 169 | + }) |
| 170 | + ) |
| 171 | + const bgTokens = Validates.transformTokenRecordToCssRuleObject( |
| 172 | + tokens, |
| 173 | + (name) => `.bg-${Strings.toKebabCase(name)}`, |
| 174 | + (name, value) => ({ |
| 175 | + 'background-color': `var(--${prefix}-${Strings.toKebabCase(name)}, ${hardcodeDefaultValue ? value : ''})` |
| 176 | + }) |
| 177 | + ) |
| 178 | + return Object.assign({}, textTokens, bgTokens) |
189 | 179 | }
|
190 | 180 |
|
191 | 181 | getPlugin() {
|
192 |
| - const tokens = this.transformTokensToCssRuleObjectRecord(this.prefix, this.tokens!) |
| 182 | + const tokens = this.transformTokensToCssRuleObject(this.prefix, this.tokens, this.hardcodeDefaultValue) |
193 | 183 | return plugin(({ addUtilities }) => {
|
194 | 184 | addUtilities(tokens)
|
195 | 185 | })
|
|
0 commit comments