Skip to content

Commit 38334d6

Browse files
authored
feat(material/schematics): add option not to include animations module in ng-add (#22559)
Adds a third option to the `ng-add` schematic that allows users to opt out of including any of the animations modules.
1 parent 5fc655b commit 38334d6

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ describe('ng-add schematic', () => {
223223
describe('animations disabled', () => {
224224
it('should add the NoopAnimationsModule to the project module', async () => {
225225
const tree = await runner
226-
.runSchematicAsync('ng-add-setup-project', {animations: false}, appTree)
226+
.runSchematicAsync('ng-add-setup-project', {animations: 'disabled'}, appTree)
227227
.toPromise();
228228
const fileContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
229229

@@ -253,6 +253,20 @@ describe('ng-add schematic', () => {
253253
});
254254
});
255255

256+
describe('animations excluded', () => {
257+
it('should not add any animations code if animations are excluded', async () => {
258+
const tree = await runner
259+
.runSchematicAsync('ng-add-setup-project', {animations: 'excluded'}, appTree)
260+
.toPromise();
261+
const fileContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
262+
263+
expect(fileContent).not.toContain('NoopAnimationsModule');
264+
expect(fileContent).not.toContain('BrowserAnimationsModule');
265+
expect(fileContent).not.toContain('@angular/platform-browser/animations');
266+
expect(fileContent).not.toContain('@angular/animations');
267+
});
268+
});
269+
256270
describe('custom project builders', () => {
257271
/** Overwrites a target builder for the workspace in the given tree */
258272
function overwriteTargetBuilder(tree: Tree, targetName: 'build' | 'test', newBuilder: string) {

src/material/schematics/ng-add/schema.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,18 @@
4646
"x-prompt": "Set up global Angular Material typography styles?"
4747
},
4848
"animations": {
49-
"type": "boolean",
50-
"default": true,
51-
"description": "Whether Angular browser animations should be set up.",
52-
"x-prompt": "Set up browser animations for Angular Material?"
49+
"type": "string",
50+
"default": "enabled",
51+
"description": "Whether Angular browser animations should be included.",
52+
"x-prompt": {
53+
"message": "Include the Angular animations module?",
54+
"type": "list",
55+
"items": [
56+
{ "value": "enabled", "label": "Include and enable animations" },
57+
{ "value": "disabled", "label": "Include, but disable animations" },
58+
{ "value": "excluded", "label": "Do not include" }
59+
]
60+
}
5361
}
5462
},
5563
"required": []

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export interface Schema {
1010
/** Name of the project. */
1111
project: string;
1212

13-
/** Whether Angular browser animations should be set up. */
14-
animations: boolean;
13+
/** Whether the Angular browser animations module should be included and enabled. */
14+
animations: 'enabled' | 'disabled' | 'excluded';
1515

1616
/** Name of pre-built theme to install. */
1717
theme: 'indigo-pink' | 'deeppurple-amber' | 'pink-bluegrey' | 'purple-green' | 'custom';

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function addAnimationsModule(options: Schema) {
6868
const project = getProjectFromWorkspace(workspace, options.project);
6969
const appModulePath = getAppModulePath(host, getProjectMainFile(project));
7070

71-
if (options.animations) {
71+
if (options.animations === 'enabled') {
7272
// In case the project explicitly uses the NoopAnimationsModule, we should print a warning
7373
// message that makes the user aware of the fact that we won't automatically set up
7474
// animations. If we would add the BrowserAnimationsModule while the NoopAnimationsModule
@@ -79,16 +79,18 @@ function addAnimationsModule(options: Schema) {
7979
`because "${noopAnimationsModuleName}" is already imported.`,
8080
);
8181
context.logger.info(`Please manually set up browser animations.`);
82-
return;
82+
} else {
83+
addModuleImportToRootModule(
84+
host,
85+
browserAnimationsModuleName,
86+
'@angular/platform-browser/animations',
87+
project,
88+
);
8389
}
84-
85-
addModuleImportToRootModule(
86-
host,
87-
browserAnimationsModuleName,
88-
'@angular/platform-browser/animations',
89-
project,
90-
);
91-
} else if (!hasNgModuleImport(host, appModulePath, browserAnimationsModuleName)) {
90+
} else if (
91+
options.animations === 'disabled' &&
92+
!hasNgModuleImport(host, appModulePath, browserAnimationsModuleName)
93+
) {
9294
// Do not add the NoopAnimationsModule module if the project already explicitly uses
9395
// the BrowserAnimationsModule.
9496
addModuleImportToRootModule(

0 commit comments

Comments
 (0)