@@ -7,6 +7,7 @@ import {StringBuilder} from "./utils/string-builder";
7
7
import { CalculateHexFromString } from "./utils/calculate-hex-from-string" ;
8
8
import { findTailwindColorFromHex } from "./utils/find-tailwind-color-from-hex" ;
9
9
import { buildModifier } from "./utils/build-modifier" ;
10
+ import get from "lodash/get" ;
10
11
11
12
export type AstDeclaration = {
12
13
property : string
@@ -17,7 +18,7 @@ export type AstDeclaration = {
17
18
negative ?: boolean ,
18
19
}
19
20
20
- export const classname = ( ast : AstDeclaration , config ?: Config ) : string => {
21
+ export const classname = ( ast : AstDeclaration , config ?: Config ) : string | undefined => {
21
22
const theme = getTailwindTheme ( config )
22
23
const stringBuilder = new StringBuilder ( )
23
24
let negative = ast . negative || false
@@ -54,7 +55,7 @@ export const classname = (ast: AstDeclaration, config?: Config): string => {
54
55
throw new PluginNotFoundException ( ast . property )
55
56
}
56
57
57
- let tailwindThemeColor = ast . value . split ( '-' ) . reduce ( ( acc , val ) => acc [ val ] , theme [ matchedPlugin . scaleKey || "colors" ] as any )
58
+ let tailwindThemeColor = get ( theme [ matchedPlugin . scaleKey || "colors" ] as any , ast . value . split ( '-' ) . join ( '.' ) )
58
59
if ( tailwindThemeColor && typeof tailwindThemeColor !== "object" ) {
59
60
//user entered a value like "red-500". we found equivalent tailwind theme color.
60
61
//return TW class directly like "bg-red-500" with modifiers and variants
@@ -63,9 +64,12 @@ export const classname = (ast: AstDeclaration, config?: Config): string => {
63
64
. addValue ( ast . value )
64
65
. toString ( )
65
66
}
66
- //at this point we know user entered a value like "#ff0000", or just "red" maybe rgba, hsla, etc.
67
+ //at this point we know user entered a value like "#ff0000", or rgba, hsla, etc.
67
68
//try to get hex color and check if tailwind has it.
68
69
const color = CalculateHexFromString ( ast . value )
70
+ if ( ! color ) {
71
+ return undefined
72
+ }
69
73
return stringBuilder
70
74
. appendModifier ( buildModifier ( color . alpha || ast . modifier , theme . opacity ) )
71
75
. addValue ( findTailwindColorFromHex ( color . hex , theme [ matchedPlugin . scaleKey || "colors" ] ) || StringBuilder . makeArbitrary ( color . hex ) )
0 commit comments