Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
postcss-color-scheme
Advanced tools
Postcss plugin for handling prefers-color-scheme
, plus tailwind support
Input
.my-class {
color: black;
@dark {
color: white;
}
}
Output
.my-class {
color: black;
}
html[data-color-scheme="dark"] .my-class {
color: white;
}
@media (prefers-color-scheme: dark) {
html:not([data-color-scheme="light"]) .my-class {
color: white;
}
}
npm install --save-dev postcss postcss-color-scheme
Add to your postcss config
module.exports = {
plugins: [
+ require('postcss-color-scheme'),
],
};
You might have noticed a couple of opinionated code at the top of this document. These are extracted from my work, and currently serve my use cases very well. Should you have concerns, suggestions for improvements, or solution for making this more generic, feel free to open an issue. Thanks!
Rely on data-color-scheme
for explicit theme settings. This requires setting data-color-scheme
on the root html element.
Provide fallback to when user has not explicitly select a theme. Let's refer to the demo above, with rules enumerated:
/* (1) */
.my-class {
color: black;
}
/* (2) */
html[data-color-scheme="dark"] .my-class {
color: white;
}
/* (3) */
@media (prefers-color-scheme: dark) {
html:not([data-color-scheme="light"]) .my-class {
color: white;
}
}
Imagine your system provides 3 options: dark
, light
, and system
(default). There are 4 possible scenarios.
User has not explicitly selected a theme (theme = system
), and the system prefers light
(prefers-color-scheme
= light
):
--> (1) applies.
User has not explicitly selected a theme (theme = system
), and the system prefers dark
(prefers-color-scheme
= dark
):
--> (1) & (3) applies, (3) takes precedence because of its higher specificity.
User selected dark
(data-color-theme
set to dark
on root html) :
--> (1) & (2) applies, (2) takes precedence because of its higher specificity.
User selected light
(data-color-theme
set to light
on root html) :
--> (1) applies.
At Rule | Description |
---|---|
@dark | apply rules for dark color scheme |
@light | apply rules for light color scheme |
postcss-color-scheme
supports the :global
syntax, often seen in css modules and similar systems.
Input
.my-class {
@dark global {
color: white;
}
}
:global(html[data-color-scheme="dark"]) .my-class {
color: white;
}
@media (prefers-color-scheme: dark) {
:global(html:not([data-color-scheme="light"])) .my-class {
color: white;
}
}
The following table lists test cases covered by this plugin, please refer to tests for details and to test input css as examples
Test Case | Description | Input | Output |
---|---|---|---|
nest in other media queries | @media (min-width: 768px) { .my-class { @dark { color: white } } } | input | output |
with combined selector | .my-class, .others { @dark { color: white } } | input | output |
with postcss-nesting | .my-class { & .nested { @dark { color: white } } } | input | output |
with postcss-nested | .my-class { .nested { @dark { color: white } } } | input | output |
Make sure you have installed and configured tailwindcss
.
npm install --save-dev tailwindcss
Add postcss-color-scheme
to your tailwind config as a plugin, and turn off the default darkMode
handling.
/** @type {import("tailwindcss").Config } */
module.exports = {
// your config ...
darkMode: '',
plugins: [require('postcss-color-scheme/tailwind')],
};
Now, the following is available:
<input class="text-white dark:text-black light:border-gray-500">
0.2.0
d5a6ae9
Thanks @vnphanquang! - provide tailwind plugin as first party support, import at postcss-color-scheme/tailwind
22b2839
Thanks @vnphanquang! - reorganize src -> lib folder
22b2839
Thanks @vnphanquang! - include tailwind.js in npm publish
FAQs
postcss plugin for handling prefers-color-scheme more gracefully
The npm package postcss-color-scheme receives a total of 10 weekly downloads. As such, postcss-color-scheme popularity was classified as not popular.
We found that postcss-color-scheme demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.