Stateful CSS

state-variable-names

A state variable is named after the property that reads it and holds that property's whole value. A part variable is named the same way, after its part.

Severity
error
Fixable
no
  • A state variable is --s-{property}, where {property} is the property that reads it: opacity: var(--s-opacity).
  • It holds the property’s whole value. It’s read alone, or as its own longhand’s slot in a shorthand: border: var(--s-border-width) solid var(--s-border-color). A slot’s name is a longhand the shorthand sets, so padding: var(--s-padding-block) … is reported: padding sets the physical sides, not padding-block.
  • A read inside a function (calc(), scaleX()) is reported. The whole value of the property belongs in the variable’s values, so the property reads it alone: transform: translateX(10px) scale(var(--s-factor)) becomes --s-transform: translateX(10px) scale(2), with every other function kept.
  • A part variable is --p-{part}-{property}, checked the same way. {part} is the part; tab-after for a part’s ::after when the root declares it; or after for a pseudo-element its own host declares for it, the root or the part. Which part it names is part-variables’ check: a part variable whose part can’t be told is left to that rule.
  • Only the element the name is for is checked: the element that declares a state variable, and the part or pseudo-element a part variable is named for. A state variable that nothing declares is checked where it’s read (no-undeclared-state-variables reports the missing declaration too). A state variable declared on another element isn’t checked here, and neither are content under a part ([data-part="code"] .line) and its pseudo-elements, which have no name of their own in the variable.
  • A vendor’s property may be read by either name: -webkit-line-clamp: var(--s-line-clamp).
  • A variable’s value reading another variable isn’t a binding, so it isn’t checked: --s-color: contrast-color(var(--s-background-color)).
  • A slot in a property the shorthand table doesn’t list (box-shadow, grid-template-columns) is left alone rather than guessed at.
  • A value core can’t read (SCSS interpolation, v-bind()) is skipped.
  • Privates (--_*) and component variables (--c-*) are named by other rules.
  • One finding for each written declaration and variable: a selector list’s declaration is reported once.
  • There’s no --fix. Renaming touches every write and read of the variable, and the parts of a calculation can’t be named by guessing.

Property names are the only names nobody has to invent. A state variable named after its property says what it changes, and a reader finds every state of padding-inline by searching for --s-padding-inline. Prefixes inside the name (--s-parts-background-color) get complicated fast. A calculation at the binding hides part of the value from the states that change it.

Reported:

[data-component="card"] {	--s-gap: var(--t-space-4);	padding-inline: var(--s-gap);}

Fixed:

[data-component="card"] {	--s-padding-inline: var(--t-space-4);	padding-inline: var(--s-padding-inline);}

The same holds for a name that isn’t a property, such as --s-text-color read by color: it’s --s-color.

Reported:

[data-component="card"] {	--s-margin-block: var(--t-space-4);	margin-block: calc(var(--s-margin-block) / 2);}

Fixed: the calculation lives in the default.

[data-component="card"] {	--s-margin-block: calc(var(--t-space-4) / 2);	margin-block: var(--s-margin-block);}

Reported:

[data-component="toggle"] {	--s-scale: 0;	transform: scaleX(var(--s-scale));}

Fixed:

[data-component="toggle"] {	--s-transform: scaleX(0);	transform: var(--s-transform);}

Reported:

[data-component="card"] {	padding: var(--s-padding-block) var(--s-padding-inline);}

Fixed: logical shorthands.

[data-component="card"] {	padding-block: var(--s-padding-block);	padding-inline: var(--s-padding-inline);}

Reported: --p-icon-size is read by inline-size.

[data-component="button"] {	--p-icon-size: 16px;	&[data-size="small"] {		--p-icon-size: 12px;	}}@scope ([data-component="button"]) to ([data-component]) {	[data-part="icon"] {		inline-size: var(--p-icon-size);	}}

Fixed:

[data-component="button"] {	--p-icon-inline-size: 16px;	&[data-size="small"] {		--p-icon-inline-size: 12px;	}}@scope ([data-component="button"]) to ([data-component]) {	[data-part="icon"] {		inline-size: var(--p-icon-inline-size);	}}
  • Longhands in their own slots: border: var(--s-border-width) solid var(--s-border-color), outline: 2px solid var(--s-outline-color).
  • A state variable reading another in its default: --s-color: contrast-color(var(--s-background-color)).
  • A fallback: color: var(--s-color, black).
  • A slot in a property the table doesn’t list: box-shadow: 0 0 4px var(--s-shadow-color).

None.