Skip to content

Commit b034b11

Browse files
committed
fix(material/schematics): combine style resets
Prior to #31522 we had two places with style resets, because the user could either use a custom theme or one of the pre-built ones. After the change all themes are "custom" so we don't need the extra branch in the logic. These changes combine the style resets into the theme and remove the wording around "custom" from the code. Fixes #32495.
1 parent 52720a3 commit b034b11

File tree

4 files changed

+31
-93
lines changed

4 files changed

+31
-93
lines changed

src/material/schematics/ng-add/index.spec.ts

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {SchematicTestRunner} from '@angular-devkit/schematics/testing';
44
import {
55
getProjectFromWorkspace,
66
getProjectIndexFiles,
7-
getProjectStyleFile,
87
getProjectTargetOptions,
98
} from '@angular/cdk/schematics';
109
import {createTestApp, createTestLibrary, getFileContent} from '../../../cdk/schematics/testing';
@@ -87,7 +86,7 @@ describe('ng-add schematic', () => {
8786
const workspace = await readWorkspace(tree);
8887
const project = getProjectFromWorkspace(workspace, baseOptions.project);
8988

90-
expectProjectStyleFile(project, 'projects/material/src/custom-theme.scss');
89+
expectProjectStyleFile(project, 'projects/material/src/material-theme.scss');
9190
});
9291

9392
it('should support adding a custom theme', async () => {
@@ -121,12 +120,12 @@ describe('ng-add schematic', () => {
121120
);
122121
const workspace = await readWorkspace(tree);
123122
const project = getProjectFromWorkspace(workspace, baseOptions.project);
124-
const expectedStylesPath = normalize(`/${project.root}/src/custom-theme.scss`);
123+
const expectedStylesPath = normalize(`/${project.root}/src/material-theme.scss`);
125124

126125
expect(tree.files)
127126
.withContext('Expected a custom theme file to be created')
128127
.toContain(expectedStylesPath);
129-
expectProjectStyleFile(project, 'projects/material/src/custom-theme.scss');
128+
expectProjectStyleFile(project, 'projects/material/src/material-theme.scss');
130129
});
131130

132131
it('should add font links', async () => {
@@ -160,20 +159,6 @@ describe('ng-add schematic', () => {
160159
});
161160
});
162161

163-
it('should add material app styles', async () => {
164-
const tree = await runner.runSchematic('ng-add-setup-project', baseOptions, appTree);
165-
const workspace = await readWorkspace(tree);
166-
const project = getProjectFromWorkspace(workspace, baseOptions.project);
167-
168-
const defaultStylesPath = getProjectStyleFile(project)!;
169-
const htmlContent = tree.read(defaultStylesPath)!.toString();
170-
171-
expect(htmlContent).toContain('html, body { height: 100%; }');
172-
expect(htmlContent).toContain(
173-
'body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; }',
174-
);
175-
});
176-
177162
describe('custom project builders', () => {
178163
/** Overwrites a target builder for the workspace in the given tree */
179164
function overwriteTargetBuilder(tree: Tree, targetName: 'build' | 'test', newBuilder: string) {
@@ -223,15 +208,15 @@ describe('ng-add schematic', () => {
223208

224209
describe('theme files', () => {
225210
it('should not overwrite existing custom theme files', async () => {
226-
appTree.create('/projects/material/custom-theme.scss', 'custom-theme');
211+
appTree.create('/projects/material/material-theme.scss', 'material-theme');
227212
const tree = await runner.runSchematic(
228213
'ng-add-setup-project',
229214
{...baseOptions, theme: 'azure-blue'},
230215
appTree,
231216
);
232-
expect(tree.readContent('/projects/material/custom-theme.scss'))
217+
expect(tree.readContent('/projects/material/material-theme.scss'))
233218
.withContext('Expected the old custom theme content to be unchanged.')
234-
.toBe('custom-theme');
219+
.toBe('material-theme');
235220
});
236221
});
237222

@@ -277,7 +262,7 @@ describe('ng-add schematic', () => {
277262
const workspace = await readWorkspace(tree);
278263
const project = getProjectFromWorkspace(workspace, baseOptions.project);
279264

280-
expectProjectStyleFile(project, 'projects/material/src/custom-theme.scss');
265+
expectProjectStyleFile(project, 'projects/material/src/material-theme.scss');
281266
});
282267
});
283268

@@ -323,7 +308,7 @@ describe('ng-add schematic', () => {
323308
const workspace = await readWorkspace(tree);
324309
const project = getProjectFromWorkspace(workspace, baseOptions.project);
325310

326-
expectProjectStyleFile(project, 'projects/material/src/custom-theme.scss');
311+
expectProjectStyleFile(project, 'projects/material/src/material-theme.scss');
327312
});
328313
});
329314

@@ -360,7 +345,7 @@ describe('ng-add schematic', () => {
360345
const workspace = await readWorkspace(tree);
361346
const project = getProjectFromWorkspace(workspace, baseOptions.project);
362347

363-
expectProjectStyleFile(project, 'projects/material/src/custom-theme.scss');
348+
expectProjectStyleFile(project, 'projects/material/src/material-theme.scss');
364349
});
365350
});
366351
});

src/material/schematics/ng-add/setup-project.ts

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {chain, Rule, SchematicContext, Tree} from '@angular-devkit/schematics';
10-
import {getProjectFromWorkspace, getProjectStyleFile} from '@angular/cdk/schematics';
10+
import {getProjectFromWorkspace} from '@angular/cdk/schematics';
1111
import {readWorkspace} from '@schematics/angular/utility';
1212
import {ProjectType} from '@schematics/angular/utility/workspace-models';
1313
import {addFontsToIndex} from './fonts/material-fonts';
@@ -25,11 +25,7 @@ export default function (options: Schema): Rule {
2525
const project = getProjectFromWorkspace(workspace, options.project);
2626

2727
if (project.extensions['projectType'] === ProjectType.Application) {
28-
return chain([
29-
addThemeToAppStyles(options),
30-
addFontsToIndex(options),
31-
addMaterialAppStyles(options),
32-
]);
28+
return chain([addThemeToAppStyles(options), addFontsToIndex(options)]);
3329
}
3430
context.logger.warn(
3531
'Angular Material has been set up in your workspace. There is no additional setup ' +
@@ -40,48 +36,3 @@ export default function (options: Schema): Rule {
4036
return;
4137
};
4238
}
43-
44-
/**
45-
* Adds custom Material styles to the project style file. The custom CSS sets up the Roboto font
46-
* and reset the default browser body margin.
47-
*/
48-
function addMaterialAppStyles(options: Schema) {
49-
return async (host: Tree, context: SchematicContext) => {
50-
const workspace = await readWorkspace(host);
51-
const project = getProjectFromWorkspace(workspace, options.project);
52-
const styleFilePath = getProjectStyleFile(project);
53-
const logger = context.logger;
54-
55-
if (!styleFilePath) {
56-
logger.error(`Could not find the default style file for this project.`);
57-
logger.info(`Consider manually adding the Roboto font to your CSS.`);
58-
logger.info(`More information at https://fonts.google.com/specimen/Roboto`);
59-
return;
60-
}
61-
62-
const buffer = host.read(styleFilePath);
63-
64-
if (!buffer) {
65-
logger.error(
66-
`Could not read the default style file within the project ` + `(${styleFilePath})`,
67-
);
68-
logger.info(`Please consider manually setting up the Roboto font.`);
69-
return;
70-
}
71-
72-
const htmlContent = buffer.toString();
73-
const insertion =
74-
'\n' +
75-
`html, body { height: 100%; }\n` +
76-
`body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; }\n`;
77-
78-
if (htmlContent.includes(insertion)) {
79-
return;
80-
}
81-
82-
const recorder = host.beginUpdate(styleFilePath);
83-
84-
recorder.insertLeft(htmlContent.length, insertion);
85-
host.commitUpdate(recorder);
86-
};
87-
}

src/material/schematics/ng-add/theming/create-custom-theme.ts renamed to src/material/schematics/ng-add/theming/create-theme.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
/** Create custom theme for the given application configuration. */
10-
export function createCustomTheme(userPaletteChoice: string): string {
11-
const colorPalettes = new Map<string, {primary: string; tertiary: string}>([
9+
/** Create theme for the given application configuration. */
10+
export function createTheme(userPaletteChoice: string): string {
11+
const colorPalettes = new Map([
1212
['azure-blue', {primary: 'azure', tertiary: 'blue'}],
1313
['rose-red', {primary: 'rose', tertiary: 'red'}],
1414
['magenta-violet', {primary: 'magenta', tertiary: 'violet'}],
@@ -23,6 +23,7 @@ export function createCustomTheme(userPaletteChoice: string): string {
2323
@use '@angular/material' as mat;
2424
2525
html {
26+
height: 100%;
2627
@include mat.theme((
2728
color: (
2829
primary: mat.$${colorPalettes.get(userPaletteChoice)!.primary}-palette,
@@ -48,6 +49,7 @@ body {
4849
4950
// Reset the user agent margin.
5051
margin: 0;
52+
height: 100%;
5153
}
5254
`;
5355
}

src/material/schematics/ng-add/theming/theming.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ import {InsertChange} from '@schematics/angular/utility/change';
1919
import {ProjectDefinition, readWorkspace, updateWorkspace} from '@schematics/angular/utility';
2020
import {join} from 'path';
2121
import {Schema} from '../schema';
22-
import {createCustomTheme} from './create-custom-theme';
22+
import {createTheme} from './create-theme';
2323

2424
/** Path segment that can be found in paths that refer to a prebuilt theme. */
2525
const prebuiltThemePathSegment = '@angular/material/prebuilt-themes';
2626

2727
/** Default file name of the custom theme that can be generated. */
28-
const defaultCustomThemeFilename = 'custom-theme.scss';
28+
const defaultThemeFilename = 'material-theme.scss';
2929

3030
/** Add pre-built styles to the main project style file. */
3131
export function addThemeToAppStyles(options: Schema): Rule {
@@ -40,15 +40,15 @@ export function addThemeToAppStyles(options: Schema): Rule {
4040
palettes = 'azure-blue';
4141
}
4242

43-
return insertCustomTheme(palettes, options.project, host, context.logger);
43+
return insertTheme(palettes, options.project, host, context.logger);
4444
};
4545
}
4646

4747
/**
48-
* Insert an Angular Material theme to project style file. If no valid style file could be found, a new
49-
* Scss file for the custom theme will be created.
48+
* Insert an Angular Material theme to project style file. If no valid style file could be found,
49+
* a new Sass file for the theme will be created.
5050
*/
51-
async function insertCustomTheme(
51+
async function insertTheme(
5252
palettes: string,
5353
projectName: string,
5454
host: Tree,
@@ -57,7 +57,7 @@ async function insertCustomTheme(
5757
const workspace = await readWorkspace(host);
5858
const project = getProjectFromWorkspace(workspace, projectName);
5959
const stylesPath = getProjectStyleFile(project, 'scss');
60-
const themeContent = createCustomTheme(palettes);
60+
const themeContent = createTheme(palettes);
6161

6262
if (!stylesPath) {
6363
if (!project.sourceRoot) {
@@ -69,16 +69,16 @@ async function insertCustomTheme(
6969

7070
// Normalize the path through the devkit utilities because we want to avoid having
7171
// unnecessary path segments and windows backslash delimiters.
72-
const customThemePath = normalize(join(project.sourceRoot, defaultCustomThemeFilename));
72+
const themePath = normalize(join(project.sourceRoot, defaultThemeFilename));
7373

74-
if (host.exists(customThemePath)) {
75-
logger.warn(`Cannot create a custom Angular Material theme because
76-
${customThemePath} already exists. Skipping theme generation.`);
74+
if (host.exists(themePath)) {
75+
logger.warn(`Cannot create an Angular Material theme because
76+
${themePath} already exists. Skipping theme generation.`);
7777
return noop();
7878
}
7979

80-
host.create(customThemePath, themeContent);
81-
return addThemeStyleToTarget(projectName, 'build', customThemePath, logger);
80+
host.create(themePath, themeContent);
81+
return addThemeStyleToTarget(projectName, 'build', themePath, logger);
8282
}
8383

8484
const insertion = new InsertChange(stylesPath, 0, themeContent);
@@ -122,10 +122,10 @@ function addThemeStyleToTarget(
122122
// theme file. If a custom theme is set up, we are not able to safely replace the custom
123123
// theme because these files can contain custom styles, while prebuilt themes are
124124
// always packaged and considered replaceable.
125-
if (stylePath.includes(defaultCustomThemeFilename)) {
125+
if (stylePath.includes(defaultThemeFilename)) {
126126
logger.error(
127127
`Could not add the selected theme to the CLI project ` +
128-
`configuration because there is already a custom theme file referenced.`,
128+
`configuration because there is already a theme file referenced.`,
129129
);
130130
logger.info(`Please manually add the following style file to your configuration:`);
131131
logger.info(` ${assetPath}`);

0 commit comments

Comments
 (0)