Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix flattening of `@scope` at-rules ([#20369](https://github.com/tailwindlabs/tailwindcss/pull/20369))
- Fix standalone declarations in `@scope`, wrap them in `:where(:scope)` ([#20369](https://github.com/tailwindlabs/tailwindcss/pull/20369))
- Always emit a space for empty fallback values in CSS variables (e.g. `var(--tw-blur,)` → `var(--tw-blur, )`) ([#20373](https://github.com/tailwindlabs/tailwindcss/pull/20373))
- Canonicalization: convert arbitrary breakpoint and container query variants to named equivalents (e.g. `max-[64rem]` → `max-lg`) ([#20380](https://github.com/tailwindlabs/tailwindcss/pull/20380))

## [4.3.3] - 2026-07-16

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export function migrateArbitraryVariants(
rawCandidate: string,
): string {
let designSystem = prepareDesignSystemStorage(baseDesignSystem)
let signatures = designSystem.storage[VARIANT_SIGNATURE_KEY]
let variants = designSystem.storage[PRE_COMPUTED_VARIANTS_KEY]
let signatures = designSystem.storage[VARIANT_SIGNATURE_KEY].get(null)
let variants = designSystem.storage[PRE_COMPUTED_VARIANTS_KEY].get(null)

for (let readonlyCandidate of designSystem.parseCandidate(rawCandidate)) {
// We are only interested in the variants
Expand All @@ -35,7 +35,11 @@ export function migrateArbitraryVariants(
if (typeof targetSignature !== 'string') continue

let foundVariants = variants.get(targetSignature)
if (foundVariants.length !== 1) continue
if (foundVariants.length === 0) continue

// The variant is already in a canonical form, e.g.: `min-lg` and `lg`
// produce the same CSS, but both are valid so we keep them as-is.
if (foundVariants.includes(targetString)) continue

let foundVariant = foundVariants[0]
let parsedVariant = designSystem.parseVariant(foundVariant)
Expand Down
98 changes: 95 additions & 3 deletions packages/tailwindcss/src/canonicalize-candidates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,11 @@ describe.each([['default'], ['with-variant'], ['important'], ['prefix']])('%s',
// This should still migrate the `theme(…)` syntax to the modern syntax.
['bg-[theme(colors.red.500/50%)]/50', 'bg-[--theme(--color-red-500/50%)]/50'],

// Variants, we can't use `var(…)` especially inside of `@media(…)`. We can
// still upgrade the `theme(…)` to the modern syntax.
['max-[theme(screens.lg)]:flex', 'max-[--theme(--breakpoint-lg)]:flex'],
// Variants, we can't use `var(…)` especially inside of `@media(…)`. But
// since the value matches the `lg` breakpoint exactly, we can migrate to
// the `max-lg` variant.
['max-[theme(screens.lg)]:flex', 'max-lg:flex'],
['min-[theme(screens.lg)]:flex', 'lg:flex'], // We could also map to `min-lg:flex`
// There are no variables for `--spacing` multiples, so we can't convert this
['max-[theme(spacing.4)]:flex', 'max-[theme(spacing.4)]:flex'],

Expand Down Expand Up @@ -719,6 +721,7 @@ describe.each([['default'], ['with-variant'], ['important'], ['prefix']])('%s',
@import 'tailwindcss';
@theme {
--*: initial;
--breakpoint-lg: 64rem;
}
`

Expand All @@ -735,6 +738,11 @@ describe.each([['default'], ['with-variant'], ['important'], ['prefix']])('%s',
['[@media(scripting:_none)]:flex', 'noscript:flex'],
['[@media_(scripting:_none)]:flex', 'noscript:flex'],

// Arbitrary variants with media queries cannot use `var(…)`, but if an
// equivalent variant exists we can use that one instead.
['[@media(width>=theme(screens.lg))]:flex', 'lg:flex'], // `min-lg:flex` would also be valid
['[@media(width<theme(screens.lg))]:flex', 'max-lg:flex'],

// With compound variants
['has-[&:focus]:flex', 'has-focus:flex'],
['not-[&:focus]:flex', 'not-focus:flex'],
Expand Down Expand Up @@ -951,6 +959,67 @@ describe.each([['default'], ['with-variant'], ['important'], ['prefix']])('%s',
})
})

// https://github.com/tailwindlabs/tailwindcss/issues/20365
describe('media query and container query variants', () => {
test.each([
// Breakpoints (`--breakpoint-lg: 64rem`, `--breakpoint-md: 48rem`)
['min-[64rem]:flex', 'lg:flex'],
['max-[64rem]:flex', 'max-lg:flex'],
['min-[48rem]:flex', 'md:flex'],
['max-[48rem]:flex', 'max-md:flex'],

// With the `rem` option set, equivalent `px` values should also be
// canonicalized to the breakpoint variants.
['min-[1024px]:flex', 'lg:flex'],
['max-[1024px]:flex', 'max-lg:flex'],

// Container queries (`--container-md: 28rem`)
['@min-[28rem]:flex', '@md:flex'],
['@max-[28rem]:flex', '@max-md:flex'],
['@min-[448px]:flex', '@md:flex'],
['@max-[448px]:flex', '@max-md:flex'],

// Values that don't map to a known breakpoint or container size should
// stay as-is.
['min-[123px]:flex', 'min-[123px]:flex'],
['max-[123px]:flex', 'max-[123px]:flex'],
['@min-[123px]:flex', '@min-[123px]:flex'],
['@max-[123px]:flex', '@max-[123px]:flex'],

// Named variants that produce the same CSS should be kept as-is
['min-lg:flex', 'min-lg:flex'],
['@min-md:flex', '@min-md:flex'],
])(testName, { timeout }, async (candidate, expected) => {
let input = css`
@import 'tailwindcss';
`

await expectCanonicalization(input, candidate, expected)
})

test.each([
// Without the `rem` option, `px` values cannot be safely converted to
// `rem` based breakpoints.
['min-[1024px]:flex', 'min-[1024px]:flex'],
['@min-[448px]:flex', '@min-[448px]:flex'],

// Same unit still works without the `rem` option
['min-[64rem]:flex', 'lg:flex'],
['max-[64rem]:flex', 'max-lg:flex'],
['@min-[28rem]:flex', '@md:flex'],
['@max-[28rem]:flex', '@max-md:flex'],
])(`${testName} (no rem option)`, { timeout }, async (candidate, expected) => {
let input = css`
@import 'tailwindcss';
`

await expectCanonicalization(input, candidate, expected, {
collapse: true,
logicalToPhysical: true,
})
})
})

describe('modernize arbitrary variants', () => {
test.each([
// Arbitrary variants
Expand Down Expand Up @@ -1546,4 +1615,27 @@ describe('regressions', () => {
'bg-brand-purple',
])
})

// https://github.com/tailwindlabs/tailwindcss/issues/20365
test('canonicalizes min/max media query and container query variants', async () => {
let designSystem = await __unstable__loadDesignSystem(
css`
@tailwind utilities;
@theme {
--breakpoint-lg: 64rem;
--container-lg: 32rem;
--spacing: 0.25rem;
}
`,
{ base: __dirname },
)

expect(designSystem.canonicalizeCandidates(['min-[64rem]:flex'])).toEqual(['lg:flex'])
expect(designSystem.canonicalizeCandidates(['max-[64rem]:flex'])).toEqual(['max-lg:flex'])
expect(designSystem.canonicalizeCandidates(['@min-[32rem]:flex'])).toEqual(['@lg:flex'])
expect(designSystem.canonicalizeCandidates(['@max-[32rem]:flex'])).toEqual(['@max-lg:flex'])
expect(designSystem.canonicalizeCandidates(['min-[1024px]:flex'], { rem: 16 })).toEqual([
'lg:flex',
])
})
})
Loading