Skip to content

Commit 0aa935b

Browse files
Show light color swatch from light-dark() functions (#1199)
Fixes #940 We've decided to only show one swatch for consistency purposes with completions as they only support a single color swatch.
1 parent 0427a76 commit 0aa935b

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

packages/tailwindcss-language-server/tests/colors/colors.test.js

+35
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,38 @@ defineTest({
353353
])
354354
},
355355
})
356+
357+
defineTest({
358+
name: 'colors that use light-dark() resolve to their light color',
359+
fs: {
360+
'app.css': css`
361+
@import 'tailwindcss';
362+
@theme {
363+
--color-primary: light-dark(#ff0000, #0000ff);
364+
}
365+
`,
366+
},
367+
prepare: async ({ root }) => ({ c: await init(root) }),
368+
handle: async ({ c }) => {
369+
let textDocument = await c.openDocument({
370+
lang: 'html',
371+
text: '<div class="bg-primary">',
372+
})
373+
374+
expect(c.project).toMatchObject({
375+
tailwind: {
376+
version: '4.0.0',
377+
isDefaultVersion: true,
378+
},
379+
})
380+
381+
let colors = await c.sendRequest(DocumentColorRequest.type, {
382+
textDocument,
383+
})
384+
385+
expect(colors).toEqual([
386+
//
387+
{ range: range(0, 12, 0, 22), color: color(1, 0, 0, 1) },
388+
])
389+
},
390+
})

packages/tailwindcss-language-service/src/util/color.ts

+7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ function getColorsInString(state: State, str: string): (culori.Color | KeywordCo
6666

6767
str = replaceCssVarsWithFallbacks(state, str)
6868
str = removeColorMixWherePossible(str)
69+
str = resolveLightDark(str)
6970

7071
let possibleColors = str.matchAll(colorRegex)
7172

@@ -280,3 +281,9 @@ function removeColorMixWherePossible(str: string) {
280281
return culori.formatRgb({ ...parsed, alpha })
281282
})
282283
}
284+
285+
const LIGHT_DARK_REGEX = /light-dark\(\s*(.*?)\s*,\s*.*?\s*\)/g
286+
287+
function resolveLightDark(str: string) {
288+
return str.replace(LIGHT_DARK_REGEX, (_, lightColor) => lightColor)
289+
}

packages/vscode-tailwindcss/CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Prerelease
44

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

77
## 0.14.4
88

0 commit comments

Comments
 (0)