Skip to content

Commit c228ac5

Browse files
author
Cem Yılmaz
committed
white or black detection fixed
1 parent 0914a33 commit c228ac5

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

src/classname.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {StringBuilder} from "./utils/string-builder";
77
import {CalculateHexFromString} from "./utils/calculate-hex-from-string";
88
import {findTailwindColorFromHex} from "./utils/find-tailwind-color-from-hex";
99
import {buildModifier} from "./utils/build-modifier";
10+
import get from "lodash/get";
1011

1112
export 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))

src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import {parse} from "./parse";
22
import {classname} from "./classname";
33

4-
export {parse, classname}
5-
6-
console.log(parse("w-1/2"))
4+
export {parse, classname}

src/utils/calculate-hex-from-string.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import {colord} from "colord";
22

3-
export const CalculateHexFromString = (input: string): { hex: string, alpha: string | undefined } => {
3+
export const CalculateHexFromString = (input: string): { hex: string, alpha: string | undefined } | undefined => {
44
const color = colord(input)
55
const alpha = color.alpha()
66

7+
if(!color.isValid()){
8+
return undefined
9+
}
10+
711
return {
812
hex: color.alpha(1).toHex(),
913
alpha: alpha !== 1 ? alpha.toString() : undefined

src/utils/find-tailwind-color-from-hex.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
import {colord} from "colord";
2+
13
export const findTailwindColorFromHex = (colorInput: string | undefined, colors: any) => {
24
if (!colorInput) return false
35

6+
//#fff, #000 or long white and black hexes safe words
47
for (let [key, twColors] of Object.entries(colors)) {
5-
for (let [shade, hex] of Object.entries(twColors as [string, string])) {
6-
if (hex === colorInput) {
7-
return `${key}-${shade}`
8+
if((twColors === "#fff" || twColors === "#000") && colord(colorInput).isEqual(twColors)) {
9+
return key
10+
}else {
11+
for (let [shade, hex] of Object.entries(twColors as [string, string])) {
12+
if (hex === colorInput) {
13+
return `${key}-${shade}`
14+
}
815
}
916
}
1017
}

0 commit comments

Comments
 (0)