@@ -12,6 +12,32 @@ function findRootOrMediaNode(node) {
1212 return findRootOrMediaNode ( parent ) ;
1313}
1414
15+ /**
16+ * @param {import('postcss').Helpers } helpers
17+ * @param {import('postcss').Container } parent
18+ * @param {string } additionalChunk
19+ * @param {boolean } global
20+ * @returns {string }
21+ */
22+ function constructSelector ( helpers , parent , additionalChunk , global ) {
23+ const parentSelectors = helpers . list . comma ( /** @type {import('postcss').Rule } */ ( parent ) . selector ) ;
24+ /** @type {string[] } */
25+ let selectors = [ ] ;
26+ for ( const selector of parentSelectors ) {
27+ let joinedSelector = '' ;
28+ if ( selector . startsWith ( 'html' ) ) {
29+ joinedSelector = `html${ additionalChunk } ${ selector . substring ( 'html' . length ) } ` ;
30+ if ( global ) joinedSelector = `:global(${ joinedSelector } )` ;
31+ } else {
32+ let chunkToAdd = `html${ additionalChunk } ` ;
33+ if ( global ) chunkToAdd = `:global(${ chunkToAdd } )` ;
34+ joinedSelector = `${ chunkToAdd } ${ selector } ` ;
35+ }
36+ selectors . push ( joinedSelector ) ;
37+ }
38+ return selectors . join ( ', ' ) ;
39+ }
40+
1541/**
1642 * @param {import('postcss').Helpers } helpers
1743 * @param {import('postcss').AtRule } atRule
@@ -30,15 +56,9 @@ function transform(helpers, atRule, theme, config = {}) {
3056 }
3157 const container = findRootOrMediaNode ( parent ) ;
3258
33- let htmlSelectorChunk = `html:not([data-color-scheme="${ theme === 'dark' ? 'light' : 'dark' } "])` ;
34- if ( global ) {
35- htmlSelectorChunk = `:global(${ htmlSelectorChunk } )` ;
36- }
59+ let selectorColorSchemeChunk = `:not([data-color-scheme="${ theme === 'dark' ? 'light' : 'dark' } "])` ;
3760 const implicitRule = new helpers . Rule ( {
38- selector : helpers . list
39- . comma ( /** @type {import('postcss').Rule } */ ( parent ) . selector )
40- . map ( ( s ) => `${ htmlSelectorChunk } ${ s } ` )
41- . join ( ', ' ) ,
61+ selector : constructSelector ( helpers , parent , selectorColorSchemeChunk , global ) ,
4262 nodes : atRule . nodes ,
4363 } ) ;
4464 const implicitAtRule = new helpers . AtRule ( {
@@ -56,15 +76,9 @@ function transform(helpers, atRule, theme, config = {}) {
5676 container . append ( implicitAtRule ) ;
5777 }
5878
59- htmlSelectorChunk = `html[data-color-scheme="${ theme } "]` ;
60- if ( global ) {
61- htmlSelectorChunk = `:global(${ htmlSelectorChunk } )` ;
62- }
79+ selectorColorSchemeChunk = `[data-color-scheme="${ theme } "]` ;
6380 const explicitRule = new helpers . Rule ( {
64- selector : helpers . list
65- . comma ( /** @type {import('postcss').Rule } */ ( parent ) . selector )
66- . map ( ( s ) => `${ htmlSelectorChunk } ${ s } ` )
67- . join ( ', ' ) ,
81+ selector : constructSelector ( helpers , parent , selectorColorSchemeChunk , global ) ,
6882 source : atRule . source ,
6983 nodes : atRule . nodes ,
7084 } ) ;
0 commit comments