postcss-dark-theme-class

CSS solution for light/dark/auto theme switcher for websites

Usage no npm install needed!

<script type="module">
  import postcssDarkThemeClass from 'https://cdn.skypack.dev/postcss-dark-theme-class';
</script>

README

PostCSS Dark Theme Class

CSS solution for light/dark/auto theme switcher for websites.

  • It doesn’t have FART flash of light theme during JS initialization.
  • Pure CSS solution. You need JS only to set HTML class, when user.
  • Automatic theme provide better UX for users with theme switching by subset/sunrise (all operating systems now have theme switching schedule).

PostCSS plugin to make switcher to force dark or light theme by copying styles from media query to special class.

/* Input CSS */

@media (prefers-color-scheme: dark) {
  html {
    --text-color: white
  }
  body {
    background: black
  }
}
/* Output CSS */

@media (prefers-color-scheme: dark) {
  html:not(.is-light) {
    --text-color: white
  }
  html:not(.is-light) body {
    background: black
  }
}
html.is-dark {
  --text-color: white
}
html.is-dark body {
  background: black
}

By default (without classes on html), website will use browser dark/light theme. If user want to use dark theme, you set html.is-dark class. If user want to force light theme, you use html.is-light.

Sponsored by Evil Martians

Docs

Read full docs on GitHub.