@@ -7,6 +7,7 @@ import {StringBuilder} from "./utils/string-builder";
77import { CalculateHexFromString } from "./utils/calculate-hex-from-string" ;
88import { findTailwindColorFromHex } from "./utils/find-tailwind-color-from-hex" ;
99import { buildModifier } from "./utils/build-modifier" ;
10+ import get from "lodash/get" ;
1011
1112export type AstDeclaration = {
1213 property : string
@@ -17,7 +18,7 @@ export type AstDeclaration = {
1718 negative ?: boolean ,
1819}
1920
20- export const classname = ( ast : AstDeclaration , config ?: Config ) : string => {
21+ export const classname = ( ast : AstDeclaration , config ?: Config ) : string | undefined => {
2122 const theme = getTailwindTheme ( config )
2223 const stringBuilder = new StringBuilder ( )
2324 let negative = ast . negative || false
@@ -54,7 +55,7 @@ export const classname = (ast: AstDeclaration, config?: Config): string => {
5455 throw new PluginNotFoundException ( ast . property )
5556 }
5657
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 ( '.' ) )
5859 if ( tailwindThemeColor && typeof tailwindThemeColor !== "object" ) {
5960 //user entered a value like "red-500". we found equivalent tailwind theme color.
6061 //return TW class directly like "bg-red-500" with modifiers and variants
@@ -63,9 +64,12 @@ export const classname = (ast: AstDeclaration, config?: Config): string => {
6364 . addValue ( ast . value )
6465 . toString ( )
6566 }
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.
6768 //try to get hex color and check if tailwind has it.
6869 const color = CalculateHexFromString ( ast . value )
70+ if ( ! color ) {
71+ return undefined
72+ }
6973 return stringBuilder
7074 . appendModifier ( buildModifier ( color . alpha || ast . modifier , theme . opacity ) )
7175 . addValue ( findTailwindColorFromHex ( color . hex , theme [ matchedPlugin . scaleKey || "colors" ] ) || StringBuilder . makeArbitrary ( color . hex ) )
0 commit comments