Skip to content

Show light color swatch from light-dark() functions #1199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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