no-subtree-data-theme-selectors
Disallow subtree-scoped data-theme selectors that do not start from the Docusaurus root color-mode attribute.
Targeted pattern scopeโ
This rule targets selectors that use [data-theme='dark'] or [data-theme='light'] somewhere in the middle of the selector instead of starting from the root html color-mode surface.
What this rule reportsโ
This rule reports selectors such as:
.widget [data-theme='dark'] .navbar.widget[data-theme='dark']body [data-theme='light'] .footer
Those patterns imply that data-theme is present on some descendant subtree, but Docusaurus sets it on the root document element.
Why this rule existsโ
Docusaurus color mode is a document-level concern.
When CSS treats data-theme like an arbitrary local attribute, the selector usually does not match what the runtime actually renders. That leads to dead overrides and confusing dark-mode bugs.
โ Incorrectโ
.widget [data-theme="dark"] .navbar {
color: white;
}
โ Correctโ
[data-theme="dark"] .navbar {
color: white;
}
Behavior and migration notesโ
- This rule complements
prefer-data-theme-color-mode. That rule migrates legacy.theme-darkand.theme-lightclasses; this rule checks that yourdata-themeselectors are rooted correctly. - It is report-only because automatically rewriting subtree selectors can change meaning.
Additional examplesโ
โ Correct โ explicit root element formโ
html[data-theme="light"] .footer {
color: black;
}
โ Incorrect โ attribute placed on a local elementโ
.widget[data-theme="dark"] {
color: white;
}
Stylelint config exampleโ
import { docusaurusPluginConfigs } from "stylelint-plugin-docusaurus";
export default {
...docusaurusPluginConfigs["docusaurus-recommended"],
rules: {
...docusaurusPluginConfigs["docusaurus-recommended"].rules,
"docusaurus/no-subtree-data-theme-selectors": true,
},
};
When not to use itโ
Disable this rule if your application deliberately adds data-theme to non-Docusaurus subtree elements and you want this repository to treat that pattern as intentional.
Package documentationโ
Docusaurus package documentation:
Rule catalog ID: R012