@@ -9,11 +9,6 @@ import prettier from 'prettier';
99import type { Dictionary , PlatformConfig } from 'style-dictionary' ;
1010import { fileHeader } from 'style-dictionary/utils' ;
1111
12- const prettierConfig = {
13- parser : 'css' ,
14- tabWidth : 2 ,
15- } as const ;
16-
1712export async function generateExtraThemingFiles ( _dictionary : Dictionary , config : PlatformConfig ) : Promise < void > {
1813
1914 const commonSource = await getSourceFromFileWithRootSelector ( config , 'hds' , 'common-tokens.css' ) ;
@@ -25,38 +20,75 @@ export async function generateExtraThemingFiles(_dictionary: Dictionary, config:
2520
2621 const header = await fileHeader ( { } ) ;
2722
28- const methods = [ 'prefers-color-scheme' , 'css-selectors' ] ;
23+ const methods = [ 'prefers-color-scheme' , 'css-selectors' , 'combined-strategies' , 'scss-mixins' ] ;
2924
3025 for ( const method of methods ) {
3126
3227 let outputContent = `${ header } \n\n` ;
3328
34- // CSS files for `prefers-color-scheme` (note: we use `cds-0` for `light` and `
29+ // CSS file for `prefers-color-scheme` (note: we use `cds-0` for `light` and `cds-100` for `dark `
3530 if ( method === 'prefers-color-scheme' ) {
31+ outputContent = `${ header } \n\n` ;
3632 outputContent += `@media (prefers-color-scheme: dark) { ${ cds0ThemedSource } }\n\n` ;
3733 outputContent += `@media (prefers-color-scheme: light) { ${ cds100ThemedSource } }\n\n` ;
3834 // this is the fallback to `light` mode
3935 // commented for now: consumers can always import the `themed-tokens/with-root-selector/cds-0/themed-tokens.css` as extra file if they want to
4036 // outputContent += '\n\n';
4137 // outputContent += `${cds0ThemedSource}\n\n`;
38+ //
39+ // this is the common part
40+ outputContent += `${ commonSource } \n\n` ;
4241 }
4342
44- // CSS files for `.class/[data]` selectors
43+ // CSS file for `.class/[data]` selectors
4544 if ( method === 'css-selectors' ) {
4645 outputContent = `${ header } \n\n` ;
4746 outputContent += `${ hdsThemedSource . replace ( / ^ : r o o t / , '.hds-theme-default, [data-hds-theme="default"]' ) } \n\n` ;
4847 outputContent += `${ cds0ThemedSource . replace ( / ^ : r o o t / , '.hds-theme-cds-0, [data-hds-theme="cds-0"]' ) } \n\n` ;
4948 outputContent += `${ cds10ThemedSource . replace ( / ^ : r o o t / , '.hds-theme-cds-10, [data-hds-theme="cds-10"]' ) } \n\n` ;
5049 outputContent += `${ cds90ThemedSource . replace ( / ^ : r o o t / , '.hds-theme-cds-90, [data-hds-theme="cds-90"]' ) } \n\n` ;
5150 outputContent += `${ cds100ThemedSource . replace ( / ^ : r o o t / , '.hds-theme-cds-100, [data-hds-theme="cds-100"]' ) } \n\n` ;
51+ //
52+ // this is the fallback to the default `hds` mode
53+ outputContent += `${ hdsThemedSource } \n\n` ;
54+ //
55+ // this is the common part
56+ outputContent += `${ commonSource } \n\n` ;
57+ }
58+
59+ // CSS file for combined `prefers-color-scheme` and CSS selectors in the same file
60+ if ( method === 'combined-strategies' ) {
61+ outputContent = `${ header } \n\n` ;
62+ // we will revisit the `[class*=hds-theme-]` selector if we find that is too generic and there are cases where this is picking up other classes
63+ outputContent += `@media (prefers-color-scheme: dark) { ${ cds0ThemedSource . replace ( / ^ : r o o t / , ':root:not([class*=hds-theme-]):not([data-hds-theme])' ) } }\n\n` ;
64+ outputContent += `@media (prefers-color-scheme: light) { ${ cds100ThemedSource . replace ( / ^ : r o o t / , ':root:not([class*=hds-theme-]):not([data-hds-theme])' ) } }\n\n` ;
65+ outputContent += `${ hdsThemedSource . replace ( / ^ : r o o t / , '.hds-theme-default, [data-hds-theme="default"]' ) } \n\n` ;
66+ outputContent += `${ cds0ThemedSource . replace ( / ^ : r o o t / , '.hds-theme-cds-0, [data-hds-theme="cds-0"]' ) } \n\n` ;
67+ outputContent += `${ cds10ThemedSource . replace ( / ^ : r o o t / , '.hds-theme-cds-10, [data-hds-theme="cds-10"]' ) } \n\n` ;
68+ outputContent += `${ cds90ThemedSource . replace ( / ^ : r o o t / , '.hds-theme-cds-90, [data-hds-theme="cds-90"]' ) } \n\n` ;
69+ outputContent += `${ cds100ThemedSource . replace ( / ^ : r o o t / , '.hds-theme-cds-100, [data-hds-theme="cds-100"]' ) } \n\n` ;
70+ //
5271 // this is the fallback to the default `hds` mode
53- outputContent += '\n\n' ;
5472 outputContent += `${ hdsThemedSource } \n\n` ;
73+ //
74+ // this is the common part
75+ outputContent += `${ commonSource } \n\n` ;
5576 }
5677
57- outputContent += `${ commonSource } \n\n` ;
78+ // SCSS file for mixins
79+ if ( method === 'scss-mixins' ) {
80+ outputContent = `${ header } \n\n` ;
81+ outputContent += `@mixin hds-theme-default() { ${ hdsThemedSource } }\n\n` ;
82+ outputContent += `@mixin hds-theme-cds0() { ${ cds0ThemedSource } }\n\n` ;
83+ outputContent += `@mixin hds-theme-cds10() { ${ cds10ThemedSource } }\n\n` ;
84+ outputContent += `@mixin hds-theme-cds90() { ${ cds90ThemedSource } }\n\n` ;
85+ outputContent += `@mixin hds-theme-cds100() { ${ cds100ThemedSource } }\n\n` ;
86+ //
87+ // this is the mixin that needs to be used to include the common tokens, shared across themes
88+ outputContent += `@mixin hds-theme-common() { ${ commonSource } }\n\n` ;
89+ }
5890
59- const outputTokensCss = await prettier . format ( outputContent , prettierConfig ) ;
91+ const outputTokensCss = await prettier . format ( outputContent , { parser : 'css' , tabWidth : 2 , } ) ;
6092
6193 const outputFolder = `${ config . buildPath } themed-tokens/with-${ method } /` ;
6294 await fs . ensureDir ( outputFolder ) ;
0 commit comments