Stateful CSS

component-variables-read-only

A component never writes its own component variables.

Severity
error
Fixable
no
  • A component’s own component variables are the --c-{name}-* named after it: its @component tag, else its data-component value, else its class in kebab-case. It never writes one it also reads, on its root, in a variant or a state, or on anything in its scope: a part, content, or a pseudo-element of one of them.
  • Writing another component’s variable is fine: that’s how the outside customizes a component. A component placed in a slot is another component, so the owner may set its variables from the placement path.
  • When the file styles two components whose names both start a variable’s name, the longer name owns it: --c-card-header-padding is the Card Header’s, not the Card’s.
  • A component’s own variable that it never reads isn’t reported. It may be for a nested component styled in another file whose name starts the same way (--c-card-header-gap on a Card), and a write nothing in the component reads can’t overwrite an outside value the component uses.
  • A global element (:root, main) setting a component’s variable isn’t a component writing its own.
  • One finding for each written declaration: a selector list’s declaration is reported once.
  • There’s no --fix. The fallback has to come from the written value, and a write in a variant or state needs a state variable.

Component variables are how the outside customizes a component. If the component writes its own, an outside value is overwritten on the element itself, and the customization silently stops working.

Reported:

/** * Button * @component button */[data-component="button"] {	--c-button-border-radius: var(--t-radius-medium);	border-radius: var(--c-button-border-radius);}

Fixed: read it with a fallback. Nothing changes it, so no state variable.

/** * Button * @component button */[data-component="button"] {	border-radius: var(--c-button-border-radius, var(--t-radius-medium));}

If a variant or a state changes it, a state variable takes the component variable as its default:

[data-component="button"] {	--s-border-radius: var(--c-button-border-radius, var(--t-radius-medium));	border-radius: var(--s-border-radius);	&[data-shape="pill"] {		--s-border-radius: 999px;	}}
  • Another component setting it: [data-component="dialog"] { --c-button-border-radius: var(--t-radius-large); }, and in a variant, [data-component="toolbar"][data-density="compact"] { --c-button-border-radius: var(--t-radius-small); }.
  • An owner setting a variable for the component in its slot: [data-component="card"] { & > [data-slot="action"] { --c-card-action-gap: 0; } }.
  • A page-wide setting: :root { --c-button-border-radius: 0; }.

None.