Skip to content

Postcss-light-dark-function Breaks With Cascade Layers #1348

Closed
@Jothsa

Description

@Jothsa

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

https://preset-env.cssdb.org/playground/#JTdCJTIyc291cmNlJTIyJTNBJTIyJTQwbGF5ZXIlMjB0aGVtZSUyMCU3QiU1Q24lMjAlMjAlMjAlMjAlMjAlM0Fyb290JTIwJTdCJTVDbiUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGNvbG9yLXNjaGVtZSUzQSUyMGxpZ2h0JTNCJTVDbiUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMC0tYmclM0ElMjBsaWdodC1kYXJrKHdoaXRlJTJDJTIwYmxhY2spJTNCJTVDbiU1Q24lMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjYlN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CUyMCUyMCUyMCUyMCUyMCU3RCU1Q24lMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjYlNUJkYXRhLXRoZW1lLW1vZGUlM0QlNUMlMjJkYXJrJTVDJTIyJTVEJTIwJTdCJTVDbiUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGNvbG9yLXNjaGVtZSUzQSUyMGRhcmslM0IlNUNuJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTdEJTVDbiUyMCUyMCUyMCUyMCU3RCU1Q24lN0QlMjIlMkMlMjJjb25maWclMjIlM0ElN0IlMjJicm93c2VycyUyMiUzQSU1QiUyMiUzRSUyMDElMjUlMjIlNUQlMkMlMjJtaW5pbXVtVmVuZG9ySW1wbGVtZW50YXRpb25zJTIyJTNBMCUyQyUyMnN0YWdlJTIyJTNBMiUyQyUyMmxvZ2ljYWwlMjIlM0ElN0IlMjJpbmxpbmVEaXJlY3Rpb24lMjIlM0ElMjJsZWZ0LXRvLXJpZ2h0JTIyJTJDJTIyYmxvY2tEaXJlY3Rpb24lMjIlM0ElMjJ0b3AtdG8tYm90dG9tJTIyJTdEJTJDJTIyZmVhdHVyZXMlMjIlM0ElN0IlMjJuZXN0aW5nLXJ1bGVzJTIyJTNBJTVCJTIyYXV0byUyMiUyQyU3QiUyMmVkaXRpb24lMjIlM0ElMjIyMDI0LTAyJTIyJTdEJTVEJTdEJTdEJTdE

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions