prefer-data-theme-over-prefers-color-scheme
Prefer Docusaurus data-theme selector scopes over prefers-color-scheme media queries when styling Docusaurus theme tokens or global theme surfaces.
Targeted pattern scopeβ
This rule is intentionally narrow.
It reports @media (prefers-color-scheme: dark) and @media (prefers-color-scheme: light) blocks only when they style Docusaurus-specific surfaces such as:
- Docusaurus or Infima theme tokens like
--ifm-*and--docsearch-* - documented runtime theme surfaces such as
.navbar,.footer,.DocSearch, or stabletheme-*classes
It does not report unrelated local component media queries such as .card or .heroBanner dark-mode styling when no Docusaurus theme surface is involved.
What this rule reportsβ
This rule reports Docusaurus theme overrides that rely on prefers-color-scheme instead of the siteβs real color-mode contract.
Why this rule existsβ
Docusaurus exposes color mode through root data-theme selectors on the HTML element.
That matters because the site can be configured to force a mode, remember a user choice, or differ from the raw operating-system preference. Styling Docusaurus theme tokens and theme surfaces through prefers-color-scheme can therefore drift away from the actual site mode.
β Incorrectβ
@media (prefers-color-scheme: dark) {
:root {
--ifm-color-primary: #4e89e8;
}
}
@media (prefers-color-scheme: dark) {
.navbar {
box-shadow: none;
}
}
β Correctβ
[data-theme="dark"] {
--ifm-color-primary: #4e89e8;
}
[data-theme="dark"] .navbar {
box-shadow: none;
}
Behavior and migration notesβ
- This rule is report-only.
- It does not autofix because converting an
@mediablock into[data-theme]selectors can require restructuring nested rules and preserving surrounding media conditions. - It complements
prefer-data-theme-color-mode, which rewrites legacy.theme-dark/.theme-lightselectors after you are already using selector-based color-mode scopes.
Additional examplesβ
β Correct β local component media query outside the Docusaurus theme surfaceβ
@media (prefers-color-scheme: dark) {
.card {
color: plum;
}
}
This rule intentionally leaves this pattern alone because it is not directly overriding a Docusaurus theme token or curated global theme surface.
Stylelint config exampleβ
import { docusaurusPluginConfigs } from "stylelint-plugin-docusaurus";
export default {
...docusaurusPluginConfigs["docusaurus-recommended"],
rules: {
...docusaurusPluginConfigs["docusaurus-recommended"].rules,
"docusaurus/prefer-data-theme-over-prefers-color-scheme": true,
},
};
When not to use itβ
Disable this rule if your project intentionally styles Docusaurus theme surfaces from raw operating-system color-scheme media queries and that divergence from Docusaurus runtime color mode is acceptable.
Package documentationβ
Docusaurus package documentation:
Rule catalog ID: R022