Documents v0.9.0 · previews run the real runtime · experimental features are marked exp

Conditions

concept

when() picks one of two values from a condition, and :when() matches whole rules. Boolean conditions compile to native if() where the browser supports it.

On this page · 7 sections+

when()

syntax.css
property: when(<condition>, <if-true>, <if-false>?);

The second value is optional. When the condition is unknown, when() gives no value at all (the property is unset).

.billing {  --annual: state(false);}.billing [role="switch"]:on(click) {  --annual: toggle();}.billing [role="switch"] {  --attr-aria-checked: var(--annual);}.price::after {  content: when(--annual, "$96 / year", "$10 / month");}.saving {  display: none;}.saving:when(--annual) {  display: inline;}
state()Function · hover any red tokenMakes each matching element the owner of an independent state with an initial value.Reference →
Resultvisual only
waiting for interaction

Writing conditions

Conditions use ==, !=, >, <, >=, <=, and, or, not and parentheses.

compare.cssreactive
.cart .badge {  display: when(--items > 0, inline-flex, none);}.page {  color-scheme: when(--theme == dark, dark, light);}.panel:when(--open and not --pinned) {  box-shadow: 0 8px 24px rgb(0 0 0 / 0.12);}
when()Function · hover any red tokenPicks one of two values from a condition. Boolean conditions compile to native if() where supported.Reference →

Operands

States, previous(--x), direction(--x), numbers, dimensions, true/false, keywords and quoted strings. Strings compare exactly and keywords case-insensitively. Units convert where CSS can convert them, so 10px == 7.5pt is true.

Bare states must be booleans

when(--open, …) needs --open to be a boolean. For numbers, write the comparison: when(--count > 0, …).

Unknown, not false

A missing state or an impossible comparison makes the condition unknown rather than false.

ConditionResult
--count > 10px on a number stateUnknown: incompatible types
--theme == dark with no owner of --themeUnknown: missing state
--count > 3 with --count: state(5)True

An unknown when() gives no value and an unknown :when() doesn’t match. Both log a warning in ReactiveCSS.diagnostics.

:when()

Use :when() when several declarations change together, or when you need more than two outcomes.

:root {  --theme: state(light);}.theme-button:on(click) {  --theme: cycle(light, dark, sepia);}.page:when(--theme == dark) {  background: #1c1917;  color: #f5f5f4;}.page:when(--theme == sepia) {  background: #f3e7d0;  color: #3f2d1c;}
state()Function · hover any red tokenMakes each matching element the owner of an independent state with an initial value.Reference →
Resultnative button
waiting for interaction

Native if()

When a condition is made only of boolean states with and, or and not, and the values are static, when() compiles to native if() in browsers that support it (Chromium and WebKit are tested). The result is identical, with less runtime work.

Caution · Native path differences

With native if(), a missing state logs no warning, and a number state of exactly 1 or 0 is read as a boolean. Set window.ReactiveCSSConfig = { native: false } to force the runtime path while debugging.

Fallbacks

Declare a static value first. Without the runtime, the browser drops the when() declaration and keeps the one before it.

fallback.cssreactive
.price::after {  content: "$10 / month";  content: when(--annual, "$96 / year", "$10 / month");}
when()Function · hover any red tokenPicks one of two values from a condition. Boolean conditions compile to native if() where supported.Reference →
Experimental · syntax may change between minor versions