Skip to content

Commit 1181729

Browse files
committed
fix: apply validates to color
1 parent 87ad5cf commit 1181729

File tree

2 files changed

+26
-38
lines changed

2 files changed

+26
-38
lines changed

src/declaration/material-design-theme-token.interface.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,3 @@ export interface IMaterialDesignThemeTokens {
5555
onTertiaryFixed: string
5656
onTertiaryFixedVariant: string
5757
}
58-
59-
// export interface IMaterialDesignThemeToken

src/tokens/internal/color.ts

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import plugin from "tailwindcss/plugin"
2-
import type { CSSRuleObject } from "tailwindcss/types/config"
32
import type { IMaterialDesignThemeTokens } from "../../declaration/material-design-theme-token.interface"
43
import type { IProvider } from "../../declaration/provider.interface"
54
import { Strings } from "../../utils/strings"
5+
import { Validates } from "../../utils/validates"
66

77
export type TColorProviderConstructorParams = {
88
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
1111
readonly excludedTokens: Array<(keyof IMaterialDesignThemeTokens) | {}>
1212
}
1313

@@ -82,7 +82,7 @@ class DefaultMaterialDesignThemeTokens implements IMaterialDesignThemeTokens {
8282
onTertiaryFixed = '#1e1341'
8383
onTertiaryFixedVariant = '#4a4070'
8484

85-
public get defaultTokenValues() {
85+
public get defaultTokenRecord() {
8686
return {
8787
primaryPaletteKeyColor: this.primaryPaletteKeyColor,
8888
secondaryPaletteKeyColor: this.secondaryPaletteKeyColor,
@@ -145,51 +145,41 @@ class DefaultMaterialDesignThemeTokens implements IMaterialDesignThemeTokens {
145145
export class ColorProvider extends DefaultMaterialDesignThemeTokens implements IProvider {
146146
public prefix
147147
public tokens
148-
public useDefaultValue
148+
public hardcodeDefaultValue
149149
public excludedTokens
150150

151151
constructor(params: Partial<TColorProviderConstructorParams>) {
152152
super()
153153
this.prefix = params.prefix ?? 'md-sys-color'
154-
this.useDefaultValue = params.useDefaultValue ?? true
154+
this.hardcodeDefaultValue = params.hardcodeDefaultValue ?? true
155155
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])
157157
}
158158

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)
176161
}
177162

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)
189179
}
190180

191181
getPlugin() {
192-
const tokens = this.transformTokensToCssRuleObjectRecord(this.prefix, this.tokens!)
182+
const tokens = this.transformTokensToCssRuleObject(this.prefix, this.tokens, this.hardcodeDefaultValue)
193183
return plugin(({ addUtilities }) => {
194184
addUtilities(tokens)
195185
})

0 commit comments

Comments
 (0)