Stateful CSS

bind-through-state-variables

Every change flows through a variable: a property never reads a private that a variant or a state changes, and a variable a variant writes is never written by a state.

Severity
error
Fixable
no
  • A property reading a changing private. A private is changing when a variant or a state writes it, when its value reads a changing private, or when it takes one with inherit. A property that reads it directly, anywhere in its value (calc() and other functions included), is reported. The message names the variant or state that changes the private, the privates on the way for a chain, and the element’s own state variable to bind through (--s-background-color), whose default reads the private. When the private is its owner’s (a part, a slot or a pseudo-element reading its component root’s, or a page element’s pseudo-element reading the element’s), the message also says to take it with inherit first: --_gutter: inherit; --s-padding: var(--_gutter); padding: var(--s-padding);. A part variable on the owner wouldn’t do: its default would read the private, and nothing on the owner would write it, so no-undeclared-state-variables reports it as never changed.
  • A variable a variant writes, which a state writes too. A state variable (--s-*), or a part variable (--p-*) on its owner, written by a variant block and by a state block on the same element is reported on each variant’s declaration. Context states (@media, @container) count as states. The message says to set a private (--_padding-inline) in the variant and give the variable the default that reads it. It runs over every variant, whatever its order: a variant written before the states hides the collision only by order.
  • A variant combined with a state (&[data-emphasis="primary"]:hover) is reported when another state writes the same variable, since the combination outranks it. The message says to set a private in the variant and let the state set the variable from it. A combination with no other state writing the variable isn’t reported, and neither are two combinations alone. A variant written inside :where() adds no specificity, so it doesn’t make a combination, and neither does a value of the state’s own attribute: &[hidden]:where(:not([hidden="until-found" i])) is one semantic state.
  • A block of unknown kind (an extra class, &.is-active) isn’t a variant or a state, so a private written only there isn’t changing, and a variable written there isn’t a collision.
  • A write in :visited changes nothing: the browser ignores custom properties there. A private set only in :visited isn’t changing, and a variable written there isn’t a collision; visited-colors-only tells the author to set the color property directly.
  • A utility, a theme scope and what :deep() or :global() selects are skipped.
  • One finding for each written declaration. A selector list on two elements, or two variants in one selector list, reports once, naming both.

The --s-* list only answers “what can change here?” if every changing property goes through it. A property reading a changing private directly is a change nobody can see from that list. bind-once catches a property declared in a state; this rule catches the change that arrives through a private.

The second check catches the one place a variant writing a state variable breaks: a state that writes the same one. A size variant (0,2,0) beats a container query that writes the same state variable, so the narrow layout never applies to a large button; a combination of a variant and hover beats pressed, so a pressed primary button looks hovered. With the variant writing a private that the state variable’s default reads, only states write the state variable, and their order decides.

A variant changes a private that a property reads directly

Section titled “A variant changes a private that a property reads directly”

Reported:

.button {	--_background-color: var(--t-color-background-raised);	background-color: var(--_background-color);	&[data-emphasis="primary"] {		--_background-color: var(--t-color-background-accent);	}}

Fixed:

.button {	--_background-color: var(--t-color-background-raised);	--s-background-color: var(--_background-color);	background-color: var(--s-background-color);	&[data-emphasis="primary"] {		--_background-color: var(--t-color-background-accent);	}}

A variant and a state write the same state variable

Section titled “A variant and a state write the same state variable”

Reported: in a narrow container, the variant (0,2,0) beats the query.

.button {	--s-padding-inline: var(--t-space-3);	padding-inline: var(--s-padding-inline);	&[data-size="large"] {		--s-padding-inline: var(--t-space-4);	}	@container (inline-size < 12rem) {		--s-padding-inline: var(--t-space-1);	}}

Fixed: the variant writes a private; the state variable’s default reads it.

.button {	--_padding-inline: var(--t-space-3);	--s-padding-inline: var(--_padding-inline);	padding-inline: var(--s-padding-inline);	&[data-size="large"] {		--_padding-inline: var(--t-space-4);	}	@container (inline-size < 12rem) {		--s-padding-inline: var(--t-space-1);	}}

A change that arrives through a chain of privates

Section titled “A change that arrives through a chain of privates”

Reported: margin-block reads --_half, which reads --_gap, which a variant changes.

.card {	--_gap: var(--t-space-4);	--_half: calc(var(--_gap) / 2);	margin-block: var(--_half);	&[data-density="compact"] {		--_gap: var(--t-space-2);	}}

Fixed:

.card {	--_gap: var(--t-space-4);	--s-margin-block: calc(var(--_gap) / 2);	margin-block: var(--s-margin-block);	&[data-density="compact"] {		--_gap: var(--t-space-2);	}}

A variant combined with a state, and another state

Section titled “A variant combined with a state, and another state”

Reported: the combination outranks :active, so a pressed primary button looks hovered.

.button {	--s-background-color: var(--t-color-background-raised);	background-color: var(--s-background-color);	&[data-emphasis="primary"]:hover {		--s-background-color: var(--t-color-background-accent);	}	&:active {		--s-background-color: var(--t-color-background-pressed);	}}

Fixed: the variant sets a private, and the hover reads it.

.button {	--_background-color-hover: var(--t-color-background-raised-strong);	--s-background-color: var(--t-color-background-raised);	background-color: var(--s-background-color);	&[data-emphasis="primary"] {		--_background-color-hover: var(--t-color-background-accent);	}	&:hover {		--s-background-color: var(--_background-color-hover);	}	&:active {		--s-background-color: var(--t-color-background-pressed);	}}
  • .button { --s-gap: var(--t-space-1); gap: var(--s-gap); &[data-size="large"] { --s-gap: var(--t-space-2); } }: only the size variant changes the gap, so it writes the state variable.
  • .card { --_gap: var(--t-space-4); gap: var(--_gap); }: nothing changes --_gap, so it’s static.
  • .card { --_gap: …; --s-padding-inline: var(--_gap); padding-inline: var(--s-padding-inline); &[data-density="compact"] { --_gap: …; } }: the change goes through the state variable.
  • .button { --s-opacity: 1; &:hover { --s-opacity: 0.9; } &:active { --s-opacity: 0.8; } }: two states, and no variant; their order decides.
  • .button { &[data-emphasis="primary"]:hover { --s-background-color: … } } with no other state writing --s-background-color: nothing for the combination to outrank.
  • .panel { --s-display: flex; @container (inline-size < 20rem) { --s-display: block; } &[hidden]:where(:not([hidden="until-found" i])) { --s-display: none; } }: the hidden state that spares until-found is a state like the container query; their order decides.
  • .tab { --_color: …; color: var(--_color); &.is-active { --_color: …; } }: an extra class is a block of unknown kind, not a variant or a state.
  • .button { &[data-size="large"] { --_gap: …; } &:hover { --_gap: …; } }: a variant and a state may both write a private; the property reads it through --s-gap.

None of its own. It reads themeAttribute, to skip theme scopes, and the names of components and their parts from statefulcss.config. It also reads the settings that tell a state from a variant: stateAttributes, blockingStates and interactionStates. With stateAttributes: ["data-state"], &[data-state="open"] is a state, so a variable it writes beside a hover isn’t a variant’s write, and isn’t reported.