Stateful CSS

no-undeclared-state-variables

Every state variable and private an element reads or changes is declared on that element, except what its owner declares for it.

Severity
error, and a warning for a state variable nothing reads or a variable no part reads
Fixable
no
  • An element reads only the state variables (--s-*) and privates (--_*) it declares itself. Each element has its own set: .link and .link::after are two elements.
  • The exceptions:
    • a part reads, without declaring them, the --p-{part}-* and --_{part}-* its owner declares for it. A component root owns its parts, its slots and its own pseudo-elements; a part owns its pseudo-elements. A part’s pseudo-element may read the private named for its part;
    • a part takes a shared private its component declares with inherit (--_accent: inherit;). Never a state variable or a part variable. A private taken with inherit that no owner in the component declares is an error: its value would come from an outer component.
  • The owner is chosen from the reader and the name, before anything else. A --p-{part}-* read on a part, or on its pseudo-elements and the content under it, is the root’s. A --p-after-* read on a part’s ::after is that part’s, and the root feeds that pseudo-element with --p-{part}-after-*. The declaration and the unknown syntax are checked on that one owner, not on any element above the reader.
  • The other side:
    • reading a --p-* its owner doesn’t declare is an error: the browser would take it from an outer component that has a part of the same name. A part’s own declaration doesn’t count: the message says the value comes from it, and asks the owner to declare it or the part to use a plain value;
    • a part never reads its component’s own state variables;
    • a component declares a part’s variables only when one of its own variants, states or queries writes them. A value that reads a changing variable isn’t such a write;
    • a variable changed in a variant, a state or a query has its default in a block with no variant or state, so a nested instance never inherits a variant’s value. A part that changes its own --p-* in a state is told to change its own state variable instead, or to have its owner write the part variable, since a part never declares one.
  • A state variable nothing reads (warning). A --s-* declared on an element that no property there reads, directly or through another variable’s value (--s-color: contrast-color(var(--s-background-color)), or a part variable a part reads). It does nothing, often after a property was removed or renamed. An unknown on the element, or a value core can’t read there, may read it, so it isn’t reported. A variable with no default, or taken with inherit, is already an error above, so it gets no warning too. A read on another element that reaches it is that element’s error, so it counts as read here.
  • Renaming a part (warning). A --p-{part}-* on a component root, or a --_{part}-* named for one of its parts, that no part reads. What counts as reading it: any part or slot, a pseudo-element of either, or content under a part, reading it or taking it with inherit. A part reading a part variable named for another part is part-variables’ error, not a renamed part. An inherit passes a value through, so only the element that supplies the value counts as read: when a part declares its own value and its ::after takes it with inherit, the root’s is unread. A private also counts when the root reads it into a part variable that a part reads (--p-icon-color: var(--_icon-color)). A root read that reaches no part doesn’t count, and neither does a plain element in the scope that belongs to no part. A slot placed through a framework’s escape from the root block (& > :global([data-slot="action"]), :slotted()) counts as reading what it reads there; the same read inside the scope doesn’t, since the scope’s limit leaves the slotted component out. It isn’t fixed: the right rename can’t be guessed.
  • Syntax core can’t read. An @include or another unknown excuses a missing declaration only on the owner that would declare it: the element itself for a state variable or a private, the owner chosen above for a part variable or a private named for the reader. An unknown inside a part excuses nothing about its owner’s part variables. An unknown under a variant, state or query excuses only a change there: it may keep a part variable from being called unchanging, but it doesn’t supply a default, which must be in the element’s own block. An unknown anywhere in the component turns the rename warning off.
  • One file holds the whole component. A component’s parts are styled only in the file that holds its root block, so every check reads the root and its parts together. A scope with no root block in its file is checked like any other, and parts-in-scope reports it. A root block whose file styles no part gets the rename warning for each part variable. A file’s blocks of one kind of CSS are read together, so a Vue file’s two <style scoped> blocks are checked as one. A component split between a file’s scoped and global CSS is checked as written, and parts-in-scope reports the split. One case is skipped, because the root block or the parts may be where the rule can’t see them: a file with syntax the rule can’t read outside any block (a top-level @include).
  • One finding for each written read and reason. A selector list whose branches share a reason (.card, .button { color: var(--s-missing); }) gets one finding that names every element it holds for. Two reasons at one read stay two findings.
  • Foreign elements (:deep(), :global()) and utilities are skipped, and so is a variable written in a block core can’t classify.

Custom properties inherit. An element that reads --s-display or --_padding without a declaration, its own or its owner’s for it, silently gets whatever an ancestor set. It looks right until something above changes it. A component and its parts are one unit: what the component declares for a part is visible, and the names say whose it is.

A part reads its component’s own state variable

Section titled “A part reads its component’s own state variable”

Reported:

.button {	--s-display: inline-flex;	display: var(--s-display);}@scope (.button) to ([data-component]) {	[data-part="spinner"] {		display: var(--s-display);	}}

Fixed: the Button prepares the spinner’s part variable, and its state writes it.

.button {	--s-display: inline-flex;	--p-spinner-display: none;	display: var(--s-display);	&[data-loading] {		--p-spinner-display: block;	}}@scope (.button) to ([data-component]) {	[data-part="spinner"] {		display: var(--p-spinner-display);	}}

A part reads a part variable its component doesn’t declare

Section titled “A part reads a part variable its component doesn’t declare”

Inside a Card, it would take the Card’s.

Reported:

@scope (.note) to ([data-component]) {	[data-part="title"] {		color: var(--p-title-color);	}}

Fixed: the Note declares it, and its variant writes it.

.note {	--p-title-color: var(--t-color-text);	&[data-tone="muted"] {		--p-title-color: var(--t-color-text-muted);	}}@scope (.note) to ([data-component]) {	[data-part="title"] {		color: var(--p-title-color);	}}

Reported, as a warning:

[data-component="card"] {	--s-justify-content: start;	&[data-align="end"] {		--s-justify-content: end;	}}

Fixed: a property reads it.

[data-component="card"] {	--s-justify-content: start;	justify-content: var(--s-justify-content);	&[data-align="end"] {		--s-justify-content: end;	}}

A state changes a state variable nobody declared

Section titled “A state changes a state variable nobody declared”

Reported:

.button {	&:hover {		--s-opacity: 0.9;	}}

Fixed:

.button {	--s-opacity: 1;	opacity: var(--s-opacity);	&:hover {		--s-opacity: 0.9;	}}

The component declares something it never changes

Section titled “The component declares something it never changes”

Reported:

.card {	--p-title-font-weight: 600;}@scope (.card) to ([data-component]) {	[data-part="title"] {		font-weight: var(--p-title-font-weight);	}}

Fixed: the title declares it.

.card {	display: grid;}@scope (.card) to ([data-component]) {	[data-part="title"] {		font-weight: 600;	}}

A default Card inside a large Card would get 2rem.

Reported:

.card {	padding: var(--_padding, 1rem);	&[data-size="large"] {		--_padding: 2rem;	}}

Fixed: declared in the own block. No state changes it, so the variant writes the state variable.

.card {	--s-padding: 1rem;	padding: var(--s-padding);	&[data-size="large"] {		--s-padding: 2rem;	}}

A pseudo-element reads its component’s state variable

Section titled “A pseudo-element reads its component’s state variable”

::after is a part, and reads the link’s own --s-transform.

Reported:

.link {	--s-transform: scaleX(0);	&::after {		transform: var(--s-transform);	}}

Fixed: the link prepares a part variable for its pseudo-element.

.link {	--p-after-transform: scaleX(0);	&::after {		transform: var(--p-after-transform);	}	&[aria-current="page"] {		--p-after-transform: scaleX(1);	}}

A part’s pseudo-element reads what only the root declares under that name

Section titled “A part’s pseudo-element reads what only the root declares under that name”

The tab’s ::after takes --p-after-* from the tab, its owner, not from the Card.

Reported:

.card {	--p-after-color: red;	&:hover {		--p-after-color: blue;	}	&::after {		color: var(--p-after-color);	}}@scope (.card) to ([data-component]) {	[data-part="tab"]::after {		color: var(--p-after-color);	}}

Fixed: the tab declares it for its own pseudo-element, and the Card feeds that pseudo-element with --p-tab-after-*.

.card {	--p-tab-after-color: red;	&:hover {		--p-tab-after-color: blue;	}}@scope (.card) to ([data-component]) {	[data-part="tab"] {		--p-after-color: red;		&[aria-selected="true"] {			--p-after-color: blue;		}		&::after {			color: var(--p-after-color);			background: var(--p-tab-after-color);		}	}}

A private taken with inherit that nothing in the component declares

Section titled “A private taken with inherit that nothing in the component declares”

Reported:

.card {	color: red;}@scope (.card) to ([data-component]) {	[data-part="badge"] {		--_accent: inherit;		color: var(--_accent);	}}

Fixed: the Card declares the private the badge takes.

.card {	--_accent: red;	color: var(--_accent);}@scope (.card) to ([data-component]) {	[data-part="badge"] {		--_accent: inherit;		color: var(--_accent);	}}

The icon became the glyph: the glyph reads what nothing declares, and nothing reads --p-icon-color.

Reported:

.button {	--p-icon-color: var(--t-color-neutral-600);	&:hover {		--p-icon-color: var(--t-color-neutral-800);	}}@scope (.button) to ([data-component]) {	[data-part="glyph"] {		color: var(--p-glyph-color);	}}

Fixed: the Button’s writes are renamed for the glyph.

.button {	--p-glyph-color: var(--t-color-neutral-600);	&:hover {		--p-glyph-color: var(--t-color-neutral-800);	}}@scope (.button) to ([data-component]) {	[data-part="glyph"] {		color: var(--p-glyph-color);	}}
  • A part reading the part variables its component declares for it: .button { --p-icon-color: var(--_icon-color); &:hover { --p-icon-color: var(--_icon-color-hover); } } with [data-part="icon"] { color: var(--p-icon-color); } in its scope.
  • A part with a state of its own, declaring its own --s-* from what the component prepared: [data-part="tab"] { --s-color: var(--p-tab-color); color: var(--s-color); &[aria-selected="true"] { --s-color: … } }.
  • A part feeding its own pseudo-element: [data-part="tab"] { --p-after-block-size: 0; &[aria-selected="true"] { --p-after-block-size: 2px; } &::after { block-size: var(--p-after-block-size); } }.
  • A part taking a shared private with inherit, when its component declares it: .card { --_accent: red; } with [data-part="badge"] { --_accent: inherit; color: var(--_accent); } in its scope.
  • A part’s pseudo-element reading the root’s private while its part has an @include, which may declare that private for it: [data-part="tab"] { @include palette; &::after { color: var(--_after-color); } }.
  • A part forwarding its root’s private to its pseudo-element with inherit, which is the same as not declaring it: .card { --_icon-color: red; } with [data-part="icon"] { --_icon-color: inherit; &::after { color: var(--_icon-color); } } in its scope. A value of the part’s own there (--_icon-color: blue) is reported, even when the root has an @include.
  • A part taking a private named for another part: [data-part="badge"] { --_accent-ink: inherit; color: var(--_accent-ink); } beside an accent part.
  • A part variable the root declares, which a state’s @include may change: .card { --p-title-color: red; &:hover { @include paint; } }.
  • A Vue file whose first <style scoped> holds the root block and whose second holds the scope, with parts reading the part variables the root declares.
  • Tokens and component variables, which come from outside: color: var(--c-card-color, var(--t-color-text)).

None of its own. It reads three statefulcss.config settings, for the names of components and their parts: componentAttribute, partAttribute and slotAttribute (data-component, data-part and data-slot by default).