Skip to content

Commit

Permalink
Show light color swatch from light-dark() functions (#1199)
Browse files Browse the repository at this point in the history
Fixes #940

We've decided to only show one swatch for consistency purposes with
completions as they only support a single color swatch.
  • Loading branch information
thecrypticace authored Feb 12, 2025
1 parent 0427a76 commit 0aa935b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
35 changes: 35 additions & 0 deletions packages/tailwindcss-language-server/tests/colors/colors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,38 @@ defineTest({
])
},
})

defineTest({
name: 'colors that use light-dark() resolve to their light color',
fs: {
'app.css': css`
@import 'tailwindcss';
@theme {
--color-primary: light-dark(#ff0000, #0000ff);
}
`,
},
prepare: async ({ root }) => ({ c: await init(root) }),
handle: async ({ c }) => {
let textDocument = await c.openDocument({
lang: 'html',
text: '<div class="bg-primary">',
})

expect(c.project).toMatchObject({
tailwind: {
version: '4.0.0',
isDefaultVersion: true,
},
})

let colors = await c.sendRequest(DocumentColorRequest.type, {
textDocument,
})

expect(colors).toEqual([
//
{ range: range(0, 12, 0, 22), color: color(1, 0, 0, 1) },
])
},
})
7 changes: 7 additions & 0 deletions packages/tailwindcss-language-service/src/util/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function getColorsInString(state: State, str: string): (culori.Color | KeywordCo

str = replaceCssVarsWithFallbacks(state, str)
str = removeColorMixWherePossible(str)
str = resolveLightDark(str)

let possibleColors = str.matchAll(colorRegex)

Expand Down Expand Up @@ -280,3 +281,9 @@ function removeColorMixWherePossible(str: string) {
return culori.formatRgb({ ...parsed, alpha })
})
}

const LIGHT_DARK_REGEX = /light-dark\(\s*(.*?)\s*,\s*.*?\s*\)/g

function resolveLightDark(str: string) {
return str.replace(LIGHT_DARK_REGEX, (_, lightColor) => lightColor)
}
2 changes: 1 addition & 1 deletion packages/vscode-tailwindcss/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Prerelease

- Nothing yet!
- Show light color swatch from light-dark() functions ([#1199](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1199))

## 0.14.4

Expand Down

0 comments on commit 0aa935b

Please sign in to comment.