Skip to content

Commit 906d3cf

Browse files
committed
Fix color parsing when alpha is 0 (tailwindlabs#177)
1 parent 7ef7794 commit 906d3cf

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

src/lsp/providers/completionProvider.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ function completionsFromClassList(
8989
sortText = '-' + sortText // move to top
9090
} else {
9191
const color = getColor(state, [className])
92-
if (color) {
92+
if (color !== null) {
9393
kind = CompletionItemKind.Color
94-
documentation = color.documentation
94+
if (typeof color !== 'string' && color.a !== 0) {
95+
documentation = color.toRgbString()
96+
}
9597
}
9698
}
9799

src/lsp/providers/documentColorProvider.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ export function registerDocumentColorProvider(state: State) {
2727
let parts = getClassNameParts(state, className.className)
2828
if (!parts) return
2929
let color = getColor(state, parts)
30-
if (!color) return
31-
colors.push({ range: className.range, color: color.documentation })
30+
if (color === null || typeof color === 'string' || color.a === 0) {
31+
return
32+
}
33+
colors.push({ range: className.range, color: color.toRgbString() })
3234
})
3335
})
3436

src/lsp/util/color.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ const COLOR_PROPS = [
2121
'text-decoration-color',
2222
]
2323

24+
function isKeyword(value: string): boolean {
25+
return ['transparent', 'currentcolor'].includes(value.toLowerCase())
26+
}
27+
2428
export function getColor(
2529
state: State,
2630
keys: string[]
27-
): { documentation?: string } {
31+
): TinyColor | string | null {
2832
const item = dlv(state.classNames.classNames, keys)
2933
if (!item.__rule) return null
3034
const props = Object.keys(removeMeta(item))
@@ -48,40 +52,36 @@ export function getColor(
4852
)
4953

5054
// check that all of the values are valid colors
51-
if (colors.some((color) => color !== 'transparent' && !color.isValid)) {
55+
if (colors.some((color) => typeof color !== 'string' && !color.isValid)) {
5256
return null
5357
}
5458

5559
// check that all of the values are the same color, ignoring alpha
5660
const colorStrings = dedupe(
5761
colors.map((color) =>
58-
color === 'transparent'
59-
? 'transparent'
60-
: `${color.r}-${color.g}-${color.b}`
62+
typeof color === 'string' ? color : `${color.r}-${color.g}-${color.b}`
6163
)
6264
)
6365
if (colorStrings.length !== 1) {
6466
return null
6567
}
6668

67-
if (colorStrings[0] === 'transparent') {
68-
return {
69-
documentation: 'rgba(0, 0, 0, 0.01)',
70-
}
69+
if (isKeyword(colorStrings[0])) {
70+
return colorStrings[0]
7171
}
7272

73-
const nonTransparentColors = colors.filter(
74-
(color): color is TinyColor => color !== 'transparent'
73+
const nonKeywordColors = colors.filter(
74+
(color): color is TinyColor => typeof color !== 'string'
7575
)
7676

77-
const alphas = dedupe(nonTransparentColors.map((color) => color.a))
77+
const alphas = dedupe(nonKeywordColors.map((color) => color.a))
78+
79+
if (alphas.length === 1) {
80+
return nonKeywordColors[0]
81+
}
7882

79-
if (alphas.length === 1 || (alphas.length === 2 && alphas.includes(0))) {
80-
return {
81-
documentation: nonTransparentColors
82-
.find((color) => color.a !== 0)
83-
.toRgbString(),
84-
}
83+
if (alphas.length === 2 && alphas.includes(0)) {
84+
return nonKeywordColors.find((color) => color.a !== 0)
8585
}
8686

8787
return null
@@ -99,9 +99,9 @@ export function getColorFromValue(value: unknown): string {
9999
return null
100100
}
101101

102-
function createColor(str: string): TinyColor | 'transparent' {
103-
if (str === 'transparent') {
104-
return 'transparent'
102+
function createColor(str: string): TinyColor | string {
103+
if (isKeyword(str)) {
104+
return str
105105
}
106106

107107
// matches: rgba(<r>, <g>, <b>, var(--bg-opacity))

0 commit comments

Comments
 (0)