Skip to content

Commit 9bdf640

Browse files
authored
fix(material/schematics): Change themeTypes to a single select instead of a multiselect prompt in M3 theme schematic (#28997)
1 parent 9c11d45 commit 9bdf640

File tree

5 files changed

+34
-49
lines changed

5 files changed

+34
-49
lines changed

src/material/schematics/ng-generate/m3-theme/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ mixins.
2525

2626
@include mat.core();
2727

28-
// Apply the light theme by default
29-
@include mat.core-theme(my-theme.$light-theme);
30-
@include mat.button-theme(my-theme.$light-theme);
28+
html {
29+
// Apply the light theme by default
30+
@include mat.core-theme(my-theme.$light-theme);
31+
@include mat.button-theme(my-theme.$light-theme);
32+
}
3133
```
3234

3335
## Options
@@ -48,5 +50,4 @@ tertiary color generated from Material based on the primary.
4850
neutral color generated from Material based on the primary.
4951
* `directory` - Relative path to a directory within the project that the
5052
generated theme file should be created in. Defaults to the project root.
51-
* `themeTypes` - List of theme types (light and dark) to generate themes for.
52-
Defaults to both light and dark.
53+
* `themeTypes` - Theme types ('light', 'dark', or 'both') to generate themes for. Defaults to both.

src/material/schematics/ng-generate/m3-theme/index.spec.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ describe('material-m3-theme-schematic', () => {
224224
try {
225225
await runM3ThemeSchematic(runner, {
226226
primaryColor: '#fffff',
227-
themeTypes: ['light'],
227+
themeTypes: 'light',
228228
});
229229
} catch (e) {
230230
expect((e as Error).message).toBe(
@@ -236,15 +236,15 @@ describe('material-m3-theme-schematic', () => {
236236
it('should generate m3 theme file', async () => {
237237
const tree = await runM3ThemeSchematic(runner, {
238238
primaryColor: '#984061',
239-
themeTypes: ['light'],
239+
themeTypes: 'light',
240240
});
241241
expect(tree.exists('m3-theme.scss')).toBe(true);
242242
});
243243

244244
it('should generate m3 theme file at specified path', async () => {
245245
const tree = await runM3ThemeSchematic(runner, {
246246
primaryColor: '#984061',
247-
themeTypes: ['light'],
247+
themeTypes: 'light',
248248
directory: 'projects/',
249249
});
250250
expect(tree.exists('projects/m3-theme.scss')).toBe(true);
@@ -253,21 +253,21 @@ describe('material-m3-theme-schematic', () => {
253253
it('should generate m3 theme file with correct indentation and formatting', async () => {
254254
const tree = await runM3ThemeSchematic(runner, {
255255
primaryColor: '#984061',
256-
themeTypes: ['light', 'dark'],
256+
themeTypes: 'both',
257257
});
258258
expect(tree.readText('m3-theme.scss')).toEqual(testM3ThemeScss);
259259
});
260260

261261
it('should generate light theme when provided a primary color', async () => {
262262
const tree = await runM3ThemeSchematic(runner, {
263263
primaryColor: '#984061',
264-
themeTypes: ['light'],
264+
themeTypes: 'light',
265265
});
266266

267267
const generatedSCSS = tree.readText('m3-theme.scss');
268268
const testSCSS = generateSCSSTheme(
269269
testM3ThemePalette,
270-
['light'],
270+
'light',
271271
'Color palettes are generated from primary: #984061',
272272
);
273273

@@ -278,13 +278,13 @@ describe('material-m3-theme-schematic', () => {
278278
it('should generate dark theme when provided a primary color', async () => {
279279
const tree = await runM3ThemeSchematic(runner, {
280280
primaryColor: '#984061',
281-
themeTypes: ['dark'],
281+
themeTypes: 'dark',
282282
});
283283

284284
const generatedSCSS = tree.readText('m3-theme.scss');
285285
const testSCSS = generateSCSSTheme(
286286
testM3ThemePalette,
287-
['dark'],
287+
'dark',
288288
'Color palettes are generated from primary: #984061',
289289
);
290290

@@ -295,13 +295,13 @@ describe('material-m3-theme-schematic', () => {
295295
it('should generate light and dark theme when provided a primary color', async () => {
296296
const tree = await runM3ThemeSchematic(runner, {
297297
primaryColor: '#984061',
298-
themeTypes: ['light', 'dark'],
298+
themeTypes: 'both',
299299
});
300300

301301
const generatedSCSS = tree.readText('m3-theme.scss');
302302
const testSCSS = generateSCSSTheme(
303303
testM3ThemePalette,
304-
['light', 'dark'],
304+
'both',
305305
'Color palettes are generated from primary: #984061',
306306
);
307307

@@ -313,7 +313,7 @@ describe('material-m3-theme-schematic', () => {
313313
const tree = await runM3ThemeSchematic(runner, {
314314
primaryColor: '#984061',
315315
secondaryColor: '#984061',
316-
themeTypes: ['light', 'dark'],
316+
themeTypes: 'both',
317317
});
318318

319319
const generatedSCSS = tree.readText('m3-theme.scss');
@@ -325,7 +325,7 @@ describe('material-m3-theme-schematic', () => {
325325

326326
const testSCSS = generateSCSSTheme(
327327
testPalette,
328-
['light', 'dark'],
328+
'both',
329329
'Color palettes are generated from primary: #984061, secondary: #984061',
330330
);
331331

@@ -338,7 +338,7 @@ describe('material-m3-theme-schematic', () => {
338338
primaryColor: '#984061',
339339
secondaryColor: '#984061',
340340
tertiaryColor: '#984061',
341-
themeTypes: ['light', 'dark'],
341+
themeTypes: 'both',
342342
});
343343

344344
const generatedSCSS = tree.readText('m3-theme.scss');
@@ -351,7 +351,7 @@ describe('material-m3-theme-schematic', () => {
351351

352352
const testSCSS = generateSCSSTheme(
353353
testPalette,
354-
['light', 'dark'],
354+
'both',
355355
'Color palettes are generated from primary: #984061, secondary: #984061, tertiary: #984061',
356356
);
357357

@@ -365,7 +365,7 @@ describe('material-m3-theme-schematic', () => {
365365
secondaryColor: '#984061',
366366
tertiaryColor: '#984061',
367367
neutralColor: '#984061',
368-
themeTypes: ['light', 'dark'],
368+
themeTypes: 'both',
369369
});
370370

371371
const generatedSCSS = tree.readText('m3-theme.scss');
@@ -379,7 +379,7 @@ describe('material-m3-theme-schematic', () => {
379379

380380
const testSCSS = generateSCSSTheme(
381381
testPalette,
382-
['light', 'dark'],
382+
'both',
383383
'Color palettes are generated from primary: #984061, secondary: #984061, tertiary: #984061, neutral: #984061',
384384
);
385385

src/material/schematics/ng-generate/m3-theme/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ function getColorPalettesSCSS(colorPalettes: Map<string, Map<number, string>>):
9898
/**
9999
* Gets the generated scss from the provided color palettes and theme types.
100100
* @param colorPalettes Map of colors and their hue tones and values.
101-
* @param themeTypes List of theme types for the theme (ex. ['light', 'dark']).
101+
* @param themeTypes Theme types for the theme (ex. 'light', 'dark', or 'both').
102102
* @param colorComment Comment with original hex colors used to generate palettes.
103103
* @returns String of the generated theme scss.
104104
*/
105105
export function generateSCSSTheme(
106106
colorPalettes: Map<string, Map<number, string>>,
107-
themeTypes: string[],
107+
themeTypes: string,
108108
colorComment: string,
109109
): string {
110110
let scss = [
@@ -128,9 +128,10 @@ export function generateSCSSTheme(
128128
'',
129129
];
130130

131+
let themes = themeTypes === 'both' ? ['light', 'dark'] : [themeTypes];
131132
// Note: Call define-theme function here since creating the color tokens
132133
// from the palettes is a private function
133-
for (const themeType of themeTypes) {
134+
for (const themeType of themes) {
134135
scss = scss.concat([
135136
'$' + themeType + '-theme: mat.define-theme((',
136137
' color: (',
@@ -173,9 +174,9 @@ export default function (options: Schema): Rule {
173174
colorComment += ', neutral: ' + options.neutralColor;
174175
}
175176

176-
if (options.themeTypes.length === 0) {
177+
if (!options.themeTypes) {
177178
context.logger.info('No theme types specified, creating both light and dark themes.');
178-
options.themeTypes = ['light', 'dark'];
179+
options.themeTypes = 'both';
179180
}
180181

181182
const themeScss = generateSCSSTheme(colorPalettes, options.themeTypes, colorComment);

src/material/schematics/ng-generate/m3-theme/schema.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ export interface Schema {
2424
*/
2525
neutralColor?: string;
2626
/**
27-
* Type for theme (ex. 'light' and/or 'dark')
27+
* Type for theme (ex. 'light', 'dark', or 'both').
2828
*/
29-
themeTypes: string[];
30-
/**
29+
themeTypes?: string;
30+
/*
3131
* Workspace-relative path to a directory where the file with the custom M3
3232
* theme will be generated.
3333
*

src/material/schematics/ng-generate/m3-theme/schema.json

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,10 @@
3030
"x-prompt": "What is the directory you want to place the generated theme file in? (Enter the relative path such as 'src/app/styles/' or leave blank to generate at your project root)"
3131
},
3232
"themeTypes": {
33-
"type": "array",
34-
"items": {
35-
"type": "string",
36-
"enum": ["light", "dark"]
37-
},
33+
"type": "string",
34+
"enum": ["light", "dark", "both"],
3835
"description": "The components to migrate.",
39-
"x-prompt": {
40-
"message": "Choose light, dark, or both to generate the corresponding themes",
41-
"type": "list",
42-
"multiselect": true,
43-
"items": [
44-
{
45-
"value": "light",
46-
"label": "light"
47-
},
48-
{
49-
"value": "dark",
50-
"label": "dark"
51-
}
52-
]
53-
}
36+
"x-prompt": "Choose light, dark, or both to generate the corresponding themes"
5437
}
5538
}
5639
}

0 commit comments

Comments
 (0)