You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Negate combined conditions as a whole in not-* variants - #20664
not in a media, supports or container query only negates the condition right after it. When a variant's condition combines several conditions with and or or, not-* puts not in front of the first one only, which gives an invalid query:
@custom-variant tablet (@media (min-width: 768px) and (max-width: 1023px));
not-tablet:flex generates:
@media not (min-width: 768px) and (max-width: 1023px) {
.not-tablet\:flex {
display: flex;
}
}
not (a) and (b) doesn't match the grammar, so browsers ignore the rule, and Lightning CSS drops it from an optimized build. The same happens with the built-in supports-* variant, e.g. not-supports-[(display:grid)_and_(gap:1rem)]:flex, with or, and with @container queries (named or not). @media only screen and (…) has a similar problem: not only screen and (…) is invalid too.
This PR wraps a combined condition in parentheses before negating it, and drops only from a negated media query:
Variant condition
Before
After
@media (a) and (b)
@media not (a) and (b)
@media not ((a) and (b))
@media (a) or (b)
@media not (a) or (b)
@media not ((a) or (b))
@media only screen and (a)
@media not only screen and (a)
@media not screen and (a)
@supports (a) and (b)
@supports not (a) and (b)
@supports not ((a) and (b))
@container (a) and (b)
@container not (a) and (b)
@container not ((a) and (b))
@container name (a) and (b)
@container name not (a) and (b)
@container name not ((a) and (b))
A single condition, a media type like screen and (a), and conditions that already start with not are negated as before.
Test plan
Added a case to the not test in variants.test.ts with custom variants that use and/or in @media and @container (with and without a name), only screen, and not-supports-[(display:grid)_and_(gap:1rem)].
With the fix reverted, the new case fails: every rule is dropped and the output is "". With the fix, it passes.
The reason will be displayed to describe this comment to others. Learn more.
Tab-separated queries lose styles
wrapCombinedConditions splits only on literal spaces, but the CSS parser preserves tabs. A custom variant with (width > 40rem)\tand\t(height > 30rem) still produces not (width > 40rem)\tand\t(height > 30rem), which browsers ignore. Mixed whitespace can also break previously valid queries: screen\tand (width > 40rem) now gets wrapped inside parentheses.
Read top-level CSS whitespace rather than only spaces, and add tests for these forms.
The reason will be displayed to describe this comment to others. Learn more.
Mixed-case queries lose styles
The new only check is case-sensitive, although CSS keywords are not. For @custom-variant tablet (@media ONLY screen and (width > 40rem)), ONLY falls through to the media-type branch and produces the invalid query not ONLY screen and (width > 40rem). Browsers ignore the resulting styles.
Compare the keyword without case and add a mixed-case test so the only fix covers valid CSS spellings.
The not-* variant logic now wraps combined conditions when negating container, media, and other conditional-rule conditions. Media negation also handles only queries and leading media types. Tests cover combined media and supports conditions, named and unnamed container conditions, and only screen. A changelog entry describes the change.
Merge Risk: Low · up to 90a76
The not-* variants now negate combined conditions as a whole, which fixes the dropped queries the PR describes. Unusual spellings, such as uppercase ONLY or escaped media-type names in custom variants, may still produce invalid queries. These are rare and can be handled as follow-ups.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title states the main change: combined and/or conditions are now negated as a whole in not-* variants. It matches the changeset, which wraps combined media, supports, and container condition…
Description check
The description explains the bug: not-* variants put not only before the first condition in combined and/or queries, producing invalid CSS. It lists the affected media, supports, and container…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title states the main change: combined and/or conditions are now negated as a whole in not-* variants. It matches the changeset, which wraps combined media, supports, and container conditions in parentheses before negating them.
Full details: Description check
Explanation
The description explains the bug: not-* variants put not only before the first condition in combined and/or queries, producing invalid CSS. It lists the affected media, supports, and container cases and the fix, and it describes the added test. This matches the changeset.
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: a3b50bf2-bea4-4817-ac9e-4c6439b75277
Commits
Reviewing files that changed from the base of the PR and between 2626ae6 and 1a3c9f5.
Files selected for processing: 3
.changelog/not-variant-combined-conditions.md
packages/tailwindcss/src/variants.test.ts
packages/tailwindcss/src/variants.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Use quote- and escape-aware top-level whitespace handling in both condition paths.
The current space-only segmentation misses tab- and newline-separated conditions. This leaves combined conditions unwrapped and can make the media-type check wrap screen\tand (a) as a parenthesized media type.
Do not use a depth-only whitespace scanner. Reuse or extend the state machine in packages/tailwindcss/src/utils/segment.ts, which already handles quotes, escapes, and bracket nesting. Apply the same top-level whitespace logic to the media-type check and wrapCombinedConditions. Preserve whitespace inside quoted or nested CSS values.
`not` only negates the condition right after it. For a condition that
combines several conditions with `and` or `or`, `not-*` produced an
invalid query, e.g. `@media not (min-width: 768px) and (max-width: 1023px)`,
which browsers and Lightning CSS drop. Wrap such conditions in
parentheses before negating them, and drop `only` from a negated media
query.
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: babbc2b5-d3d1-492a-b0a6-cb7d32390cb5
Commits
Reviewing files that changed from the base of the PR and between 1a3c9f5 and 90a7618.
Files selected for processing: 2
packages/tailwindcss/src/variants.test.ts
packages/tailwindcss/src/variants.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
Minor · Functional Correctness · Quick win
Match the only modifier without case sensitivity.
For @custom-variant device (@media ONLY screen and (color)), not-device:flex produces not ONLY screen and (color). The comparison fails, then the media-type branch accepts ONLY. This query is invalid, so the utility does not apply. Media-query keywords are ASCII case-insensitive. Compare a normalized keyword and add an uppercase regression case. (drafts.csswg.org)
The reason will be displayed to describe this comment to others. Learn more.
Minor · Functional Correctness · Quick win
Recognize escaped media-type identifiers before wrapping.
For @custom-variant device (@media \screen and (color)), this regex rejects the escaped spelling of screen. The fallback produces not (\screen and (color)). A media type cannot appear inside a media condition; this expression evaluates as unknown and never matches, instead of negating screen and (color). Recognize CSS identifier tokens, including escapes, before choosing the wrapping path. Add this case beside the plain media-type coverage. (w3.org)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
notin a media, supports or container query only negates the condition right after it. When a variant's condition combines several conditions withandoror,not-*putsnotin front of the first one only, which gives an invalid query:not-tablet:flexgenerates:not (a) and (b)doesn't match the grammar, so browsers ignore the rule, and Lightning CSS drops it from an optimized build. The same happens with the built-insupports-*variant, e.g.not-supports-[(display:grid)_and_(gap:1rem)]:flex, withor, and with@containerqueries (named or not).@media only screen and (…)has a similar problem:not only screen and (…)is invalid too.This PR wraps a combined condition in parentheses before negating it, and drops
onlyfrom a negated media query:@media (a) and (b)@media not (a) and (b)@media not ((a) and (b))@media (a) or (b)@media not (a) or (b)@media not ((a) or (b))@media only screen and (a)@media not only screen and (a)@media not screen and (a)@supports (a) and (b)@supports not (a) and (b)@supports not ((a) and (b))@container (a) and (b)@container not (a) and (b)@container not ((a) and (b))@container name (a) and (b)@container name not (a) and (b)@container name not ((a) and (b))A single condition, a media type like
screen and (a), and conditions that already start withnotare negated as before.Test plan
nottest invariants.test.tswith custom variants that useand/orin@mediaand@container(with and without a name),only screen, andnot-supports-[(display:grid)_and_(gap:1rem)]."". With the fix, it passes.vitest run --project tailwindcss: 45 files, 5189 tests passed, 1 skipped.prettier --checkon the changed files andpnpm changelog checkpass.