set()
actionsince 0.2requires runtimeSets a state to a value. In an :on() rule it is a mutation of the state on the left, --x: set(5), with typed expressions.
On this page · 9 sections+−
Syntax
--<state>: set(<expr>);registry syntax: set(--state, value | attr(name) | pick(...) | sibling-index() | sibling-count() [, closest(selector)])
Parameters
- exprtyped expressionrequired
- A literal,
var(--name, fallback?), typedcalc(),min(),max(),clamp(),round()andabs(), the operators+ - * /,event(), and one-timemouse(),element(),viewport(),random(),pick(),attr(),sibling-index()andsibling-count().
Effect
No value. Subscribers of the state re-evaluate in the urgent lane.
Example
level.cssreactive
.dial { --level: state(5); }.dial .reset:on(click) { --level: set(5); }.dial .max:on(click) { --level: set(10); }.dial .half:on(click) { --level: set(round(var(--level) / 2)); }.dial .value { content: var(--level); }state()Function · hover any red tokenMakes each matching element the owner of an independent state with an initial value.Reference →
Behavior
- A mutation of the state on the left. In an
:on()rule,--x: set(<expr>)changes the nearest--xowner of the element the rule matched.set(),toggle(),increment(),decrement()andcycle()share one model: one winner per state through the cascade, and one transaction per event, read against the values from before it. - Typed values. Booleans count as 1 or 0 in arithmetic, dimensions convert within a unit family (
pxandcm,degandturn,sandms), and percentages are their own unit. As in CSS,+and-need spaces around them. Mixing incompatible units (10px + 5%), dividing by zero or comparing strings with numbers is an event-time error: one diagnostic and no write. - At the event.
mouse(),element(),viewport(),random(),pick()andattr()are read once, when the event fires; they are snapshots, not subscriptions. - A value that the state’s type cannot hold is reported once and ignored. A unitless
0can be stored in a dimension state, and0and1in a boolean state. - A write that keeps the same value is a no-op: no subscribers run and
:changed()doesn’t match.
Supported contexts
- As a mutation in an
:on()rule.
Limitations
- Not a general-purpose language: no comparisons, no
when()and no string functions in the expression. - Units that need layout (
em,vw) are not resolved: useelement()orviewport(). attr()reads the attribute of the element the rule matched, at event time; seeattr().
Examples
- Click coordinates · API examples, runs the real runtime
- Persistent preference · API examples, runs the real runtime
Related
Experimental · syntax may change between minor versions