Skip to content

Commit 29c4137

Browse files
committed
ensure --theme(…) can only be used with CSS variables
Let's not allow old dot-notation, `--theme(colors.red.500)` should error. Even though we can make it works, we want to nudge people in using the new/modern syntax.
1 parent c620fa9 commit 29c4137

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

packages/tailwindcss/src/css-functions.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ describe('--spacing(…)', () => {
146146
})
147147

148148
describe('--theme(…)', () => {
149-
test('theme(--color-red-500)', async () => {
149+
test('--theme(--color-red-500)', async () => {
150150
expect(
151151
await compileCss(css`
152152
@theme {
@@ -166,6 +166,19 @@ describe('--theme(…)', () => {
166166
}"
167167
`)
168168
})
169+
170+
test('--theme(…) can only be used with CSS variables from your theme', async () => {
171+
expect(() =>
172+
compileCss(css`
173+
@theme {
174+
--color-red-500: #f00;
175+
}
176+
.red {
177+
color: --theme(colors.red.500);
178+
}
179+
`),
180+
).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: The --theme(…) function can only be used with CSS variables from your theme.]`)
181+
})
169182
})
170183

171184
describe('theme(…)', () => {

packages/tailwindcss/src/css-functions.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const functions: Record<string, (designSystem: DesignSystem, ...args: string[])
99
'--alpha': alpha,
1010
'--spacing': spacing,
1111
'--theme': theme,
12-
theme,
12+
theme: legacyTheme,
1313
}
1414

1515
function alpha(_designSystem: DesignSystem, value: string, alpha: string, ...rest: string[]) {
@@ -50,6 +50,14 @@ function spacing(designSystem: DesignSystem, value: string, ...rest: string[]) {
5050
}
5151

5252
function theme(designSystem: DesignSystem, path: string, ...fallback: string[]) {
53+
if (!path.startsWith('--')) {
54+
throw new Error(`The --theme(…) function can only be used with CSS variables from your theme.`)
55+
}
56+
57+
return legacyTheme(designSystem, path, ...fallback)
58+
}
59+
60+
function legacyTheme(designSystem: DesignSystem, path: string, ...fallback: string[]) {
5361
path = eventuallyUnquote(path)
5462

5563
let resolvedValue = designSystem.resolveThemeValue(path)

0 commit comments

Comments
 (0)