Skip to content

Commit fa2c83e

Browse files
authored
Improve --theme() CSS function to only accept modern syntax (#15580)
This PR makes sure that the `--theme(…)` CSS function can only be used with the modern syntax. For backwards compatibility, the `theme(…)` function must be used with the older dot notation.
1 parent 82589eb commit fa2c83e

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
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: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { withAlpha } from './utilities'
55
import { segment } from './utils/segment'
66
import * as ValueParser from './value-parser'
77

8-
const functions: Record<string, (designSystem: DesignSystem, ...args: string[]) => any> = {
8+
const functions: Record<string, (designSystem: DesignSystem, ...args: string[]) => 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)