Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Extend support to @container and @supports
  • Loading branch information
philipp-spiess committed Sep 6, 2024
commit 625393228f994c92d9546a9f448de08b23555547
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Support CSS `theme()` functions inside `@custom-media` rules ([#14358])(https://github.com/tailwindlabs/tailwindcss/pull/14358)
- Support CSS `theme()` functions inside other `@custom-media`, `@container`, and `@supports` rules ([#14358])(https://github.com/tailwindlabs/tailwindcss/pull/14358)

### Fixed

Expand Down
107 changes: 62 additions & 45 deletions packages/tailwindcss/src/css-functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,63 +620,80 @@ describe('theme function', () => {
})
})

describe('in @custom-media queries', () => {
test('@custom-media --my-media (min-width:theme(breakpoint.md)) and (max-width: theme(--breakpoint-lg))', async () => {
expect(
await compileCss(css`
@theme {
--breakpoint-md: 48rem;
--breakpoint-lg: 64rem;
}
/* prettier-ignore */
@custom-media --my-media (min-width:theme(breakpoint.md)) and (max-width: theme(--breakpoint-lg));
@media (--my-media) {
.red {
color: red;
}
}
`),
).toMatchInlineSnapshot(`
":root {
test('@custom-media --my-media (min-width: theme(breakpoint.md))', async () => {
expect(
await compileCss(css`
@theme {
--breakpoint-md: 48rem;
--breakpoint-lg: 64rem;
}

@media (width >= 48rem) and (width <= 64rem) {
@custom-media --my-media (min-width: theme(breakpoint.md));
@media (--my-media) {
.red {
color: red;
}
}"
`)
})
}
`),
).toMatchInlineSnapshot(`
":root {
--breakpoint-md: 48rem;
}

test('@custom-media --my-media (width >= theme(breakpoint.md)) and (width<theme(--breakpoint-lg))', async () => {
expect(
await compileCss(css`
@theme {
--breakpoint-md: 48rem;
--breakpoint-lg: 64rem;
}
@custom-media --my-media (width >= theme(breakpoint.md)) and (width<theme(--breakpoint-lg));
@media (--my-media) {
.red {
color: red;
}
}
`),
).toMatchInlineSnapshot(`
":root {
@media (width >= 48rem) {
.red {
color: red;
}
}"
`)
})

test('@container (width > theme(breakpoint.md))', async () => {
expect(
await compileCss(css`
@theme {
--breakpoint-md: 48rem;
--breakpoint-lg: 64rem;
}
@container (width > theme(breakpoint.md)) {
.red {
color: red;
}
}
`),
).toMatchInlineSnapshot(`
":root {
--breakpoint-md: 48rem;
}

@media (width >= 48rem) and (width < 64rem) {
@container (width > 48rem) {
.red {
color: red;
}
}"
`)
})

test('@supports (text-stroke: theme(--font-size-xs))', async () => {
expect(
await compileCss(css`
@theme {
--font-size-xs: 0.75rem;
}
@supports (text-stroke: theme(--font-size-xs)) {
.red {
color: red;
}
}"
`)
})
}
`),
).toMatchInlineSnapshot(`
":root {
--font-size-xs: .75rem;
}

@supports (text-stroke: 0.75rem) {
.red {
color: red;
}
}"
`)
})
})

Expand Down
7 changes: 5 additions & 2 deletions packages/tailwindcss/src/css-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ export function substituteFunctions(ast: AstNode[], pluginApi: PluginAPI) {
return
}

// Find @media rules
// Find at-rules rules
if (node.kind === 'rule') {
if (
node.selector[0] === '@' &&
(node.selector.startsWith('@media ') || node.selector.startsWith('@custom-media ')) &&
(node.selector.startsWith('@media ') ||
node.selector.startsWith('@custom-media ') ||
node.selector.startsWith('@container ') ||
node.selector.startsWith('@supports ')) &&
node.selector.includes(THEME_FUNCTION_INVOCATION)
) {
node.selector = substituteFunctionsInValue(node.selector, pluginApi)
Expand Down