@@ -31,9 +31,13 @@ interface ColorVariants {
3131
3232export interface WithCondition {
3333 /**
34- * CSS condition to wrap variables in (e.g., ".MyClass", "@media (prefers-color-scheme: dark)")
34+ * CSS selector to wrap variables in (e.g., ".MyClass"). This will be extracted out of the root.
3535 */
36- condition ?: string ;
36+ selector ?: string ;
37+ /**
38+ * CSS at-rule to wrap variables in (e.g., "@supports (display: grid)")
39+ */
40+ atRule ?: string ;
3741}
3842/**
3943 * Settings for palette colors, including optional conditions like media queries.
@@ -219,41 +223,65 @@ function colorValueToOklch(value: ColorValueOrString): string {
219223 * ```
220224 */
221225export function processColors ( colors : ColorConfig ) : Output {
222- const cssOutput : string [ ] = [ ] ;
226+ const rootOutput : string [ ] = [ ] ;
227+ const outsideOutput : string [ ] = [ ] ;
223228 const resolveMap : ResolveMap = new Map ( ) ;
224- cssOutput . push ( `/* Palette */` ) ;
229+ rootOutput . push ( `/* Palette */` ) ;
225230 const moduleKey = "palette" ;
226231
227232 function conditionalBuilder (
228233 settings : WithCondition | undefined ,
229234 initialComment : string ,
230235 ) {
231- const leadingComments : string [ ] = [ ] ;
232236 const innerComments : string [ ] = [ ] ;
233237 const vars : string [ ] = [ ] ;
234238
235- if ( settings ?. condition ) {
236- leadingComments . push ( initialComment ) ;
237- } else {
238- cssOutput . push ( initialComment ) ;
239- }
239+ const hasSelector = Boolean ( settings ?. selector ) ;
240+ const hasAtRule = Boolean ( settings ?. atRule ) ;
241+
242+ // If no settings provided, emit comment immediately into root output
243+ if ( ! hasSelector && ! hasAtRule ) rootOutput . push ( initialComment ) ;
240244
241245 return {
242246 addComment ( c : string ) {
243- if ( settings ?. condition ) innerComments . push ( c ) ;
244- else cssOutput . push ( c ) ;
247+ if ( hasSelector || hasAtRule ) innerComments . push ( c ) ;
248+ else rootOutput . push ( c ) ;
245249 } ,
246250 pushVariable ( v : string ) {
247- if ( settings ?. condition ) vars . push ( v ) ;
248- else cssOutput . push ( v ) ;
251+ if ( hasSelector || hasAtRule ) vars . push ( v ) ;
252+ else rootOutput . push ( v ) ;
249253 } ,
250254 finalize ( ) {
251- if ( settings ?. condition && vars . length > 0 ) {
252- cssOutput . push ( ...leadingComments ) ;
253- cssOutput . push ( `${ settings . condition } {` ) ;
254- cssOutput . push ( ...innerComments . map ( ( c ) => ` ${ c } ` ) ) ;
255- cssOutput . push ( ...vars . map ( ( v ) => ` ${ v } ` ) ) ;
256- cssOutput . push ( `}` ) ;
255+ if ( ! hasSelector && ! hasAtRule ) return ;
256+ if ( vars . length === 0 && innerComments . length === 0 ) return ;
257+ if ( ! settings ) return ;
258+ if ( hasSelector && hasAtRule ) {
259+ outsideOutput . push ( initialComment ) ;
260+ outsideOutput . push ( `${ settings . atRule } {` ) ;
261+ outsideOutput . push ( ` ${ settings . selector } {` ) ;
262+ outsideOutput . push ( ...innerComments . map ( ( c ) => ` ${ c } ` ) ) ;
263+ outsideOutput . push ( ...vars . map ( ( v ) => ` ${ v } ` ) ) ;
264+ outsideOutput . push ( ` }` ) ;
265+ outsideOutput . push ( `}` ) ;
266+ return ;
267+ }
268+
269+ if ( hasSelector ) {
270+ outsideOutput . push ( initialComment ) ;
271+ outsideOutput . push ( `${ settings . selector } {` ) ;
272+ outsideOutput . push ( ...innerComments . map ( ( c ) => ` ${ c } ` ) ) ;
273+ outsideOutput . push ( ...vars . map ( ( v ) => ` ${ v } ` ) ) ;
274+ outsideOutput . push ( `}` ) ;
275+ return ;
276+ }
277+
278+ if ( hasAtRule ) {
279+ rootOutput . push ( initialComment ) ;
280+ rootOutput . push ( `${ settings . atRule } {` ) ;
281+ rootOutput . push ( ...innerComments . map ( ( c ) => ` ${ c } ` ) ) ;
282+ rootOutput . push ( ...vars . map ( ( v ) => ` ${ v } ` ) ) ;
283+ rootOutput . push ( `}` ) ;
284+ return ;
257285 }
258286 } ,
259287 } ;
@@ -289,9 +317,12 @@ export function processColors(colors: ColorConfig): Output {
289317 }
290318
291319 if ( colors . gradients ) {
292- cssOutput . push ( `/* Gradients */` ) ;
320+ rootOutput . push ( `/* Gradients */` ) ;
293321 const moduleKey = "gradients" ;
294- const palette = { css : cssOutput . join ( "\n" ) , resolveMap } ;
322+ const palette = {
323+ css : { root : rootOutput . join ( "\n" ) , outside : outsideOutput . join ( "\n" ) } ,
324+ resolveMap,
325+ } ;
295326
296327 for ( const [ gradientName , gradient ] of Object . entries ( colors . gradients . value ) ) {
297328 validateName ( gradientName ) ;
@@ -335,9 +366,12 @@ export function processColors(colors: ColorConfig): Output {
335366 }
336367
337368 if ( colors . theme ) {
338- cssOutput . push ( `/* Themes */` ) ;
369+ rootOutput . push ( `/* Themes */` ) ;
339370 const moduleKey = "theme" ;
340- const palette = { css : cssOutput . join ( "\n" ) , resolveMap } ;
371+ const palette = {
372+ css : { root : rootOutput . join ( "\n" ) , outside : outsideOutput . join ( "\n" ) } ,
373+ resolveMap,
374+ } ;
341375
342376 for ( const [ themeName , themeConfig ] of Object . entries ( colors . theme ) ) {
343377 validateName ( themeName ) ;
@@ -387,7 +421,9 @@ export function processColors(colors: ColorConfig): Output {
387421 }
388422 }
389423
390- const output = { css : cssOutput . join ( "\n" ) , resolveMap } ;
391- // console.log(output);
424+ const output = {
425+ css : { root : rootOutput . join ( "\n" ) , outside : outsideOutput . join ( "\n" ) } ,
426+ resolveMap,
427+ } ;
392428 return output ;
393429}
0 commit comments