Skip to content

Commit ee35a1d

Browse files
Support CSS theme() functions inside @custom-media rules (#14358)
This PR will now also scan `@custom-media` rules for invocations of the CSS `theme()` function.
1 parent 7b59aac commit ee35a1d

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Support CSS `theme()` functions inside other `@custom-media`, `@container`, and `@supports` rules ([#14358])(https://github.com/tailwindlabs/tailwindcss/pull/14358)
13+
1014
### Fixed
1115

1216
- Ensure there is always CLI feedback on save even when no new classes were found ([#14351](https://github.com/tailwindlabs/tailwindcss/pull/14351))

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,82 @@ describe('theme function', () => {
619619
`)
620620
})
621621
})
622+
623+
test('@custom-media --my-media (min-width: theme(breakpoint.md))', async () => {
624+
expect(
625+
await compileCss(css`
626+
@theme {
627+
--breakpoint-md: 48rem;
628+
}
629+
@custom-media --my-media (min-width: theme(breakpoint.md));
630+
@media (--my-media) {
631+
.red {
632+
color: red;
633+
}
634+
}
635+
`),
636+
).toMatchInlineSnapshot(`
637+
":root {
638+
--breakpoint-md: 48rem;
639+
}
640+
641+
@media (width >= 48rem) {
642+
.red {
643+
color: red;
644+
}
645+
}"
646+
`)
647+
})
648+
649+
test('@container (width > theme(breakpoint.md))', async () => {
650+
expect(
651+
await compileCss(css`
652+
@theme {
653+
--breakpoint-md: 48rem;
654+
}
655+
@container (width > theme(breakpoint.md)) {
656+
.red {
657+
color: red;
658+
}
659+
}
660+
`),
661+
).toMatchInlineSnapshot(`
662+
":root {
663+
--breakpoint-md: 48rem;
664+
}
665+
666+
@container (width > 48rem) {
667+
.red {
668+
color: red;
669+
}
670+
}"
671+
`)
672+
})
673+
674+
test('@supports (text-stroke: theme(--font-size-xs))', async () => {
675+
expect(
676+
await compileCss(css`
677+
@theme {
678+
--font-size-xs: 0.75rem;
679+
}
680+
@supports (text-stroke: theme(--font-size-xs)) {
681+
.red {
682+
color: red;
683+
}
684+
}
685+
`),
686+
).toMatchInlineSnapshot(`
687+
":root {
688+
--font-size-xs: .75rem;
689+
}
690+
691+
@supports (text-stroke: 0.75rem) {
692+
.red {
693+
color: red;
694+
}
695+
}"
696+
`)
697+
})
622698
})
623699

624700
describe('in plugins', () => {

packages/tailwindcss/src/css-functions.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ export function substituteFunctions(ast: AstNode[], pluginApi: PluginAPI) {
1313
return
1414
}
1515

16-
// Find @media rules
16+
// Find at-rules rules
1717
if (node.kind === 'rule') {
1818
if (
1919
node.selector[0] === '@' &&
20-
node.selector.startsWith('@media ') &&
20+
(node.selector.startsWith('@media ') ||
21+
node.selector.startsWith('@custom-media ') ||
22+
node.selector.startsWith('@container ') ||
23+
node.selector.startsWith('@supports ')) &&
2124
node.selector.includes(THEME_FUNCTION_INVOCATION)
2225
) {
2326
node.selector = substituteFunctionsInValue(node.selector, pluginApi)

0 commit comments

Comments
 (0)