Skip to content

Commit f4f9c80

Browse files
committed
feat(select-is-html): add support for :root
1 parent 341b8a7 commit f4f9c80

4 files changed

Lines changed: 38 additions & 6 deletions

File tree

.changeset/tiny-boats-beam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'postcss-color-scheme': patch
3+
---
4+
5+
add support for `:root` selector; i.e `:root { @dark { /* ... */ } }`

lib/color-scheme.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ function findRootOrMediaNode(node) {
1616
return findRootOrMediaNode(parent);
1717
}
1818

19+
const HTML_SELECTOR_PREFIXES = ['html', ':root'];
20+
const GLOBAL_HTML_SELECTOR_PREFIXES = HTML_SELECTOR_PREFIXES.map((selector) => `:global(${selector}`);
1921
/**
2022
* @param {import('postcss').Helpers} helpers
2123
* @param {import('postcss').Container} parent
@@ -29,12 +31,23 @@ function constructSelector(helpers, parent, additionalChunk, global) {
2931
let selectors = [];
3032
for (const selector of parentSelectors) {
3133
let joinedSelector = '';
32-
if (selector.startsWith(':global(html')) {
33-
joinedSelector = `:global(html${additionalChunk}${selector.substring(':global(html'.length)}`;
34-
} else if (selector.startsWith('html')) {
35-
joinedSelector = `html${additionalChunk}${selector.substring('html'.length)}`;
36-
if (global) joinedSelector = `:global(${joinedSelector})`;
37-
} else {
34+
35+
for (const prefix of GLOBAL_HTML_SELECTOR_PREFIXES) {
36+
if (selector.startsWith(prefix)) {
37+
joinedSelector = `${prefix}${additionalChunk}${selector.substring(prefix.length)}`;
38+
break;
39+
}
40+
}
41+
42+
for (const prefix of HTML_SELECTOR_PREFIXES) {
43+
if (selector.startsWith(prefix)) {
44+
joinedSelector = `${prefix}${additionalChunk}${selector.substring(prefix.length)}`;
45+
if (global) joinedSelector = `:global(${joinedSelector})`;
46+
break;
47+
}
48+
}
49+
50+
if (!joinedSelector) {
3851
let chunkToAdd = `html${additionalChunk}`;
3952
if (global) chunkToAdd = `:global(${chunkToAdd})`;
4053
joinedSelector = `${chunkToAdd} ${selector}`;

lib/tests/selector-is-html.input.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,9 @@ html[lang="en"] {
4343
color: blue;
4444
}
4545
}
46+
47+
:root {
48+
@dark {
49+
color: violet;
50+
}
51+
}

lib/tests/selector-is-html.output.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ html[data-color-scheme="light"][lang="en"] {
3434
color: gray;
3535
}
3636

37+
:root[data-color-scheme="dark"] {
38+
color: violet;
39+
}
40+
3741
@media (prefers-color-scheme: dark) {
3842
html:not([data-color-scheme="light"]) {
3943
color: black;
@@ -50,6 +54,10 @@ html[data-color-scheme="light"][lang="en"] {
5054
:global(html:not([data-color-scheme="light"])) {
5155
color: brown;
5256
}
57+
58+
:root:not([data-color-scheme="light"]) {
59+
color: violet;
60+
}
5361
}
5462

5563
@media (prefers-color-scheme: light) {

0 commit comments

Comments
 (0)