Description
Bug description
Postcss-light-dark-function does not work with css cascade layers. I have variables and the rules to switch the color-scheme
based on an attribute on the :root
element defined in a css cascade layer. However, postcss-light-dark-function adds media queries to set the values of --csstools-color-scheme--light
and --csstools-color-scheme--dark
based on the value of prefers-color-scheme
outside of any cascade layer. This causes those rules to have a higher priority than the rules which change the variables and color schemes based on an html attribute. This conflict results in the color-scheme attribute changing to reflect the attribute on the :root
element, but the values of --csstools-color-scheme--light
and --csstools-color-scheme--dark
remaining set based on the value of prefers-color-scheme
.
Source CSS
@layer theme {
:root {
color-scheme: light;
--bg: light-dark(white, black);
&[data-theme-mode="auto"], &:not([data-theme-mode="light"], [data-theme-mode="dark"]) {
@media (prefers-color-scheme: dark) {
color-scheme: dark;
}
@media (prefers-color-scheme: light) {
color-scheme: light;
}
}
&[data-theme-mode="light"] {
color-scheme: light;
}
&[data-theme-mode="dark"] {
color-scheme: dark;
}
}
}
Expected CSS
@layer theme {
:root {
--csstools-color-scheme--light: initial;
--csstools-color-scheme--dark: ;
color-scheme: light;
--bg: var(--csstools-color-scheme--light, white)
var(--csstools-color-scheme--dark, black);
&[data-theme-mode='auto'],
&:not([data-theme-mode='light'], [data-theme-mode='dark']) {
@media (prefers-color-scheme: dark) {
--csstools-color-scheme--light: ;
--csstools-color-scheme--dark: initial;
color-scheme: dark;
}
@media (prefers-color-scheme: light) {
--csstools-color-scheme--light: initial;
--csstools-color-scheme--dark: ;
color-scheme: light;
}
}
&[data-theme-mode='light'] {
--csstools-color-scheme--light: initial;
--csstools-color-scheme--dark: ;
color-scheme: light;
}
&[data-theme-mode='dark'] {
--csstools-color-scheme--light: ;
--csstools-color-scheme--dark: initial;
color-scheme: dark;
}
& * {
--bg: var(--csstools-color-scheme--light, white)
var(--csstools-color-scheme--dark, black);
}
}
@supports (color: light-dark(red, red)) {
:root {
--bg: light-dark(white, black);
}
}
}
Actual CSS
@layer theme {
:root {
--csstools-color-scheme--light: initial;
--csstools-color-scheme--dark: ;
color-scheme: light;
--bg: var(--csstools-color-scheme--light, white)
var(--csstools-color-scheme--dark, black);
&[data-theme-mode='auto'],
&:not([data-theme-mode='light'], [data-theme-mode='dark']) {
@media (prefers-color-scheme: dark) {
--csstools-color-scheme--light: ;
--csstools-color-scheme--dark: initial;
color-scheme: dark;
}
@media (prefers-color-scheme: light) {
--csstools-color-scheme--light: initial;
--csstools-color-scheme--dark: ;
color-scheme: light;
}
}
&[data-theme-mode='light'] {
--csstools-color-scheme--light: initial;
--csstools-color-scheme--dark: ;
color-scheme: light;
}
&[data-theme-mode='dark'] {
--csstools-color-scheme--light: ;
--csstools-color-scheme--dark: initial;
color-scheme: dark;
}
& * {
--bg: var(--csstools-color-scheme--light, white)
var(--csstools-color-scheme--dark, black);
}
}
@supports (color: light-dark(red, red)) {
:root {
--bg: light-dark(white, black);
}
}
}
:root {
--csstools-color-scheme--light: initial;
--csstools-color-scheme--dark: ;
}
@media (prefers-color-scheme: dark) {
:root {
--csstools-color-scheme--light: ;
--csstools-color-scheme--dark: initial;
}
}
Playgound example
Does it happen with npx @csstools/csstools-cli <plugin-name> minimal-example.css
?
Yes
Debug output
No response
Extra config
No response
What plugin are you experiencing this issue on?
PostCSS Light Dark Function
Plugin version
1.0.2
What OS are you experiencing this on?
macOS
Node Version
20.10.0
Validations
- Follow our Code of Conduct
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Would you like to open a PR for this bug?
- I'm willing to open a PR