w3c / w3c/csswg-drafts

[css-conditional-5] Desugaring @if

Open
#12,909 9 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

css-conditional-5
Dominant language
Bikeshed
Stars
4.9k
Forks
816
PR merge metrics
PR metrics pending

Description

With inline if() and revert-rule now in place, do we indeed[^1] have everything we need to add a useful block-if?

Just like how @container imposes an additional "late" (vs MQs) condition on rules, we could use similar behavior for @if:

@container (width > 800px) {
  .thing { font-size: 30px; }
}

The above effectively behaves as[^2]:

.thing { font-size: if(container(width > 800px):30px; else:revert-rule); }

@if could work the same way:

@if style(--mode:pill) {
  .box { border-radius: 10px; }
}

Desugared:

.box { border-radius: if(style(--mode:pill):10px; else:revert-rule); }

Obviously it would work with nesting, so you can set a bunch of things conditionally in a nice way:

.box {
  @if style(--mode:pill) {
    border-radius: 10px;
    background-color: tomato;
    padding: 4px;
  }
}

This "inlinification" should answer questions about cycles (etc) automatically. I wouldn't expect implementations to literally perform this, of course. It's just a way of explaining the behavior from existing primitives.

Multiple levels of @if would desugar into a compound if-test:

@if style(--mode:pill) {
  @if style(--theme:dark) {
    .box { border-radius: 10px; }
  }
}

Desugared:

.box { border-radius: if(style(--mode:pill) and style(--theme:dark):10px; else:revert-rule); }

There might be a potential trap when using nesting, e.g. authors could reasonably expect the following if-test to branch off of <div class=box>'s variables, when in reality style(--mode:pill) gets evaluated against <div class=inner>'s variables:

.box {
  @if style(--mode:pill) {
    .inner { green: green; }
  }
}

However, this is similar to how different rules within a single @container rule can target totally different containers based on the element being matched.


Thoughts on this? E.g. @tabatkins, @LeaVerou?

[^1]: See Pave the cowpaths for block @if in https://github.com/w3c/csswg-drafts/issues/12806
[^2]: Using the proposed container() test from https://github.com/w3c/csswg-drafts/issues/12069.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the linked discussions on block @if (issue 12806), revert-rule (issue 10443), and container() (issue 12069), then compare their proposed semantics with the examples here. Done means the CSS Working Group has resolved whether block @if can be specified using these primitives and has agreed on its nesting and evaluation behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
css
Domain
frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.