Skip to content
This repository has been archived by the owner on Feb 18, 2022. It is now read-only.

Overriding custom properties not supported #173

Open
lauriii opened this issue Apr 16, 2019 · 4 comments
Open

Overriding custom properties not supported #173

lauriii opened this issue Apr 16, 2019 · 4 comments

Comments

@lauriii
Copy link

lauriii commented Apr 16, 2019

According to W3C reference, custom properties can be overridden by more specific selectors.

Example:

:root {
  --color: black;
}
div {
  --color: red;
  color: var(--color);
}

Expected:

div {
  color: red;
}

Current behavior:

div {
  color: black;
}

I also created a failing test case for this.

@pascalduez
Copy link
Contributor

Hi,

without a DOM, PostCSS can't replicate 100% of the custom properties behaviour, so it's limited to declarations on the :root element. This used to be on the Readme, but was removed apparently.

There used to be a warning (under an option) as well, but it was also removed.

Personally when I use cascading custom properties like in your example, I tend to had a fallback by hand, when it make sense.

@joelpittet
Copy link

Could it be simulated to at least look in the context of the block it's in or the same selector for overrides?

@maddesigns
Copy link

Could we (ab)use the fallback mechanism, like this:

div {
  --color: red;
  color: var(--color, red);
}

?

@theseanl
Copy link

theseanl commented May 6, 2019

I suppose one way to resolve this is to transpile

a { --prop: red; }
b { --prop: blue; }

c { color: var(--prop) }

to, assuming b has higher specificity than a,

c:matches(a *):matches(a) { color: red; }
c:matches(b *):matches(b) { color: blue; }

so if there were m rules for a custom property and n rules that use the custom property, we end up with mn rules. We can calculate the specificity of selectors, and there is another plugin that transpiles :matches (which is imperfect as well though), so this looks doable.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants