Computed custom properties
declarationsince 0.10experimentalrequires runtime--clean: trim(--value): a custom property whose whole value is one value function is also a read-only state, computed from other states.
On this page · 8 sections+−
Syntax
--<name>: <value function>(...);--<name>: live(<value function>(...) [, <interval>]);registry syntax: --name: value-function(…) | live(value-function(…) [, interval])
Parameters
- value function
length,includes,lower,trim,format,timezone;timeinsidelive()required - The call must be the whole value. It may read only states, literals and other value functions.
Effect
A read-only state of that name on each matched element, and the same value as a CSS custom property.
Example
chain.cssreactive
.form { --value: state(""); --clean: trim(--value); --chars: length(--clean); }.form textarea:on(input) { --value: set(event(value)); }.form .count { text: var(--chars); }.form:when(--chars > 140) .count { color: red; }state()Function · hover any red tokenMakes each matching element the owner of an independent state with an initial value.Reference →
Behavior
- Updates. Recomputed synchronously when a state it reads changes, inputs before what reads them, before the change returns. An unchanged result notifies nobody.
previous()never sees an intermediate value. - Owner. Each matched element owns one, while it matches. Consumers use the nearest owner, as for
state(). - Read-only.
set(),toggle()and other mutations, legacyon:actions andbind:input change nothing, with a diagnostic. - Errors. An evaluation error keeps the last value, with a diagnostic. A missing input gives no value, reported after a second.
Supported contexts
- An unconditional style rule.
@layerand nesting in an unconditional parent are allowed.
Limitations
- Not a general CSS cascade. The value is written inline on each owner. CSS shows the state on the owner unless
!importantor@propertyoverrides it, and inside the owner unless another declaration of the name applies there. - A declaration of the same name in a processed stylesheet (one with reactive syntax), including
state()and@property, turns the name off, with a diagnostic. In a stylesheet without reactive syntax, and for inline styles inside an owner, only development builds report it. Cross-origin stylesheets and shadow roots cannot be checked. - In
:when(), state pseudo-class, pseudo-element,@media,@supportsor@containerrules, and with!important, the declaration stays CSS-only, with a diagnostic. So does one that readsattr(),event(),element(),random()ortime()outsidelive(). - Names that read each other are rejected.
Related
Experimental · syntax may change between minor versions