Skip to content

Commit 6bd2775

Browse files
amcdnljelbourn
authored andcommitted
chore(schematics): minor polish for all schematics (#11191)
* Fixed sidenav shadow appearing in "side" mode for nav schematic * Switches table schematic to native `<table>` API * Make dashboard schematic responsive * Expand docs * Fix misc typos and naming * Use first argument as `--name` argument
1 parent 0f17f16 commit 6bd2775

File tree

23 files changed

+113
-73
lines changed

23 files changed

+113
-73
lines changed

src/lib/schematics/README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,29 @@
22
A collection of Schematics for Angular Material.
33

44
## Collection
5-
- [Shell](shell/README.md)
5+
6+
### Install
7+
Adds Angular Material and its depedencies and pre-configures the application.
8+
9+
- Adds Material and CDK to `package.json`
10+
- Adds Material Icons Stylesheet to `index.html`
11+
- Adds Roboto Font to `index.html`
12+
- Ensure `BrowserAnimationsModule` is installed and included in root module
13+
- Adds pre-configured theme to `.angular.json` file OR adds custom theme scaffolding to `styles.scss`
14+
15+
Command: `ng add @angular/material`
16+
17+
### Dashboard
18+
Creates a responive card grid list component.
19+
20+
Command: `ng g @angular/material:dashboard my-dashboard`
21+
22+
### Nav
23+
Creates a navigation component with a responsive sidenav.
24+
25+
Command: `ng g @angular/material:nav my-nav`
26+
27+
### Table
28+
Creates a table component with sorting and paginator.
29+
30+
Command: `ng g @angular/material:table my-table`

src/lib/schematics/collection.json

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,30 @@
55
// Adds Angular Material to an application without changing any templates
66
"ng-add": {
77
"description": "Adds Angular Material to the application without affecting any templates",
8-
"factory": "./shell",
9-
"schema": "./shell/schema.json",
10-
"aliases": ["material-shell"]
8+
"factory": "./install",
9+
"schema": "./install/schema.json",
10+
"aliases": ["material-shell", "materialShell"]
1111
},
12-
1312
// Create a dashboard component
14-
"materialDashboard": {
13+
"dashboard": {
1514
"description": "Create a card-based dashboard component",
1615
"factory": "./dashboard/index",
1716
"schema": "./dashboard/schema.json",
18-
"aliases": [ "material-dashboard" ]
17+
"aliases": ["material-dashboard", "materialDashboard"]
1918
},
2019
// Creates a table component
21-
"materialTable": {
20+
"table": {
2221
"description": "Create a component that displays data with a data-table",
2322
"factory": "./table/index",
2423
"schema": "./table/schema.json",
25-
"aliases": [ "material-table" ]
24+
"aliases": ["material-table", "materialTable"]
2625
},
2726
// Creates toolbar and navigation components
28-
"materialNav": {
27+
"nav": {
2928
"description": "Create a component with a responsive sidenav for navigation",
3029
"factory": "./nav/index",
3130
"schema": "./nav/schema.json",
32-
"aliases": [ "material-nav" ]
31+
"aliases": ["material-nav", "materialNav"]
3332
}
3433
}
3534
}

src/lib/schematics/dashboard/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Component<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%><% if(changeDetection !== 'Default') { %>, ChangeDetectionStrategy<% }%> } from '@angular/core';
2+
import { map } from 'rxjs/operators';
3+
import { Breakpoints, BreakpointState, BreakpointObserver } from '@angular/cdk/layout';
24

35
@Component({
46
selector: '<%= selector %>',<% if(inlineTemplate) { %>
@@ -59,10 +61,26 @@ import { Component<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%><% if(
5961
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>
6062
})
6163
export class <%= classify(name) %>Component {
62-
cards = [
63-
{ title: 'Card 1', cols: 2, rows: 1 },
64-
{ title: 'Card 2', cols: 1, rows: 1 },
65-
{ title: 'Card 3', cols: 1, rows: 2 },
66-
{ title: 'Card 4', cols: 1, rows: 1 }
67-
];
64+
/** Based on the screen size, switch from standard to one column per row */
65+
cards = this.breakpointObserver.observe(Breakpoints.Handset).pipe(
66+
map(({ matches }) => {
67+
if (matches) {
68+
return [
69+
{ title: 'Card 1', cols: 1, rows: 1 },
70+
{ title: 'Card 2', cols: 1, rows: 1 },
71+
{ title: 'Card 3', cols: 1, rows: 1 },
72+
{ title: 'Card 4', cols: 1, rows: 1 }
73+
];
74+
}
75+
76+
return [
77+
{ title: 'Card 1', cols: 2, rows: 1 },
78+
{ title: 'Card 2', cols: 1, rows: 1 },
79+
{ title: 'Card 3', cols: 1, rows: 2 },
80+
{ title: 'Card 4', cols: 1, rows: 1 }
81+
];
82+
})
83+
);
84+
85+
constructor(private breakpointObserver: BreakpointObserver) {}
6886
}

src/lib/schematics/dashboard/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {addModuleImportToModule, findModuleFromOptions} from '../utils/ast';
44
import {buildComponent} from '../utils/devkit-utils/component';
55

66
/**
7-
* Scaffolds a new navigation component.
7+
* Scaffolds a new dashboard component.
88
* Internally it bootstraps the base component schematic
99
*/
1010
export default function(options: Schema): Rule {
@@ -25,6 +25,7 @@ function addNavModulesToModule(options: Schema) {
2525
addModuleImportToModule(host, modulePath, 'MatMenuModule', '@angular/material');
2626
addModuleImportToModule(host, modulePath, 'MatIconModule', '@angular/material');
2727
addModuleImportToModule(host, modulePath, 'MatButtonModule', '@angular/material');
28+
addModuleImportToModule(host, modulePath, 'LayoutModule', '@angular/cdk/layout');
2829
return host;
2930
};
3031
}

src/lib/schematics/dashboard/index_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('material-dashboard-schematic', () => {
2828
});
2929

3030
it('should create dashboard files and add them to module', () => {
31-
const tree = runner.runSchematic('materialDashboard', { ...options }, createTestApp());
31+
const tree = runner.runSchematic('dashboard', { ...options }, createTestApp());
3232
const files = tree.files;
3333

3434
expect(files).toContain('/src/app/foo/foo.component.css');

src/lib/schematics/dashboard/schema.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
"project": {
1414
"type": "string",
1515
"description": "The name of the project.",
16-
"visible": false
16+
"visible": false,
17+
"$default": {
18+
"$source": "projectName"
19+
}
1720
},
1821
"name": {
1922
"type": "string",
@@ -90,7 +93,5 @@
9093
"description": "Specifies if declaring module exports the component."
9194
}
9295
},
93-
"required": [
94-
"name"
95-
]
96+
"required": []
9697
}

src/lib/schematics/shell/index.ts renamed to src/lib/schematics/install/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {addModuleImportToRootModule, getStylesPath} from '../utils/ast';
44
import {InsertChange} from '../utils/devkit-utils/change';
55
import {getProjectFromWorkspace, getWorkspace} from '../utils/devkit-utils/config';
66
import {addHeadLink} from '../utils/html';
7-
import {angularVersion, cdkVersion, materialVersion} from '../utils/lib-versions';
7+
import {angularVersion, materialVersion} from '../utils/lib-versions';
88
import {addPackageToPackageJson} from '../utils/package';
99
import {Schema} from './schema';
1010
import {addThemeToAppStyles} from './theming';
@@ -29,7 +29,7 @@ export default function(options: Schema): Rule {
2929
/** Add material, cdk, annimations to package.json if not already present. */
3030
function addMaterialToPackageJson() {
3131
return (host: Tree, context: SchematicContext) => {
32-
addPackageToPackageJson(host, 'dependencies', '@angular/cdk', cdkVersion);
32+
addPackageToPackageJson(host, 'dependencies', '@angular/cdk', materialVersion);
3333
addPackageToPackageJson(host, 'dependencies', '@angular/material', materialVersion);
3434
addPackageToPackageJson(host, 'dependencies', '@angular/animations', angularVersion);
3535
context.addTask(new NodePackageInstallTask());
@@ -80,7 +80,8 @@ function addBodyMarginToStyles(options: Schema) {
8080
const buffer = host.read(stylesPath);
8181
if (buffer) {
8282
const src = buffer.toString();
83-
const insertion = new InsertChange(stylesPath, src.length, `\nbody { margin: 0; }\n`);
83+
const insertion = new InsertChange(stylesPath, src.length,
84+
`\nhtml, body { height: 100%; }\nbody { margin: 0; font-family: 'Roboto', sans-serif; }\n`);
8485
const recorder = host.beginUpdate(stylesPath);
8586
recorder.insertLeft(insertion.pos, insertion.toAdd);
8687
host.commitUpdate(recorder);

src/lib/schematics/shell/index_spec.ts renamed to src/lib/schematics/install/index_spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ describe('material-shell-schematic', () => {
2020
});
2121

2222
it('should update package.json', () => {
23-
const tree = runner.runSchematic('materialShell', {}, appTree);
23+
const tree = runner.runSchematic('install', {}, appTree);
2424
const packageJson = JSON.parse(getFileContent(tree, '/package.json'));
2525

2626
expect(packageJson.dependencies['@angular/material']).toBeDefined();
2727
expect(packageJson.dependencies['@angular/cdk']).toBeDefined();
2828
});
2929

3030
it('should add default theme', () => {
31-
const tree = runner.runSchematic('materialShell', {}, appTree);
31+
const tree = runner.runSchematic('shell', {}, appTree);
3232
const config: any = getConfig(tree);
3333
config.apps.forEach(app => {
3434
expect(app.styles).toContain(
@@ -37,7 +37,7 @@ describe('material-shell-schematic', () => {
3737
});
3838

3939
it('should add custom theme', () => {
40-
const tree = runner.runSchematic('materialShell', {
40+
const tree = runner.runSchematic('install', {
4141
theme: 'custom'
4242
}, appTree);
4343

@@ -53,7 +53,7 @@ describe('material-shell-schematic', () => {
5353
});
5454

5555
it('should add font links', () => {
56-
const tree = runner.runSchematic('materialShell', {}, appTree);
56+
const tree = runner.runSchematic('install', {}, appTree);
5757
const config: any = getConfig(tree);
5858
const workspace = getWorkspace(tree);
5959
const project = getProjectFromWorkspace(workspace, config.project.name);

src/lib/schematics/shell/schema.json renamed to src/lib/schematics/install/schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "http://json-schema.org/schema",
3-
"id": "SchematicsMaterialShell",
4-
"title": "Material Shell Options Schema",
3+
"id": "SchematicsMaterialInstall",
4+
"title": "Material Install Options Schema",
55
"type": "object",
66
"properties": {
77
"skipPackageJson": {

src/lib/schematics/nav/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.__styleext__

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@
44

55
.sidenav {
66
width: 200px;
7-
box-shadow: 3px 0 6px rgba(0,0,0,.24);
7+
}
8+
9+
.mat-toolbar.mat-primary {
10+
position: sticky;
11+
top: 0;
812
}

src/lib/schematics/nav/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
*ngIf="isHandset$ | async">
2424
<mat-icon aria-label="Side nav toggle icon">menu</mat-icon>
2525
</button>
26-
<span>Application Title</span>
26+
<span><%= project %></span>
2727
</mat-toolbar>
28+
<!-- Add Content Here -->
2829
</mat-sidenav-content>
2930
</mat-sidenav-container>

src/lib/schematics/nav/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ import { map } from 'rxjs/operators';
3131
*ngIf="isHandset$ | async">
3232
<mat-icon aria-label="Side nav toggle icon">menu</mat-icon>
3333
</button>
34-
<span>Application Title</span>
34+
<span><%= project %></span>
3535
</mat-toolbar>
36+
<!-- Add Content Here -->
3637
</mat-sidenav-content>
3738
</mat-sidenav-container>
3839
`,<% } else { %>
@@ -45,7 +46,11 @@ import { map } from 'rxjs/operators';
4546
4647
.sidenav {
4748
width: 200px;
48-
box-shadow: 3px 0 6px rgba(0,0,0,.24);
49+
}
50+
51+
.mat-toolbar.mat-primary {
52+
position: sticky;
53+
top: 0;
4954
}
5055
`
5156
]<% } else { %>

src/lib/schematics/nav/index_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('material-nav-schematic', () => {
2828
});
2929

3030
it('should create nav files and add them to module', () => {
31-
const tree = runner.runSchematic('materialNav', { ...options }, createTestApp());
31+
const tree = runner.runSchematic('nav', { ...options }, createTestApp());
3232
const files = tree.files;
3333

3434
expect(files).toContain('/src/app/foo/foo.component.css');
@@ -42,7 +42,7 @@ describe('material-nav-schematic', () => {
4242
});
4343

4444
it('should add nav imports to module', () => {
45-
const tree = runner.runSchematic('materialNav', { ...options }, createTestApp());
45+
const tree = runner.runSchematic('nav', { ...options }, createTestApp());
4646
const moduleContent = getFileContent(tree, '/src/app/app.module.ts');
4747

4848
expect(moduleContent).toContain('LayoutModule');

src/lib/schematics/nav/schema.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
"project": {
1414
"type": "string",
1515
"description": "The name of the project.",
16-
"visible": false
16+
"visible": false,
17+
"$default": {
18+
"$source": "projectName"
19+
}
1720
},
1821
"name": {
1922
"type": "string",
@@ -90,7 +93,5 @@
9093
"description": "Specifies if declaring module exports the component."
9194
}
9295
},
93-
"required": [
94-
"name"
95-
]
96+
"required": []
9697
}

src/lib/schematics/shell/README.md

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/lib/schematics/table/README.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/lib/schematics/table/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
<div class="mat-elevation-z8">
2-
<mat-table #table [dataSource]="dataSource" matSort aria-label="Elements">
3-
2+
<table mat-table #table [dataSource]="dataSource" matSort aria-label="Elements">
43
<!-- Id Column -->
54
<ng-container matColumnDef="id">
6-
<mat-header-cell *matHeaderCellDef mat-sort-header>Id</mat-header-cell>
7-
<mat-cell *matCellDef="let row">{{row.id}}</mat-cell>
5+
<th mat-header-cell *matHeaderCellDef mat-sort-header>Id</th>
6+
<td mat-cell *matCellDef="let row">{{row.id}}</td>
87
</ng-container>
98

109
<!-- Name Column -->
1110
<ng-container matColumnDef="name">
12-
<mat-header-cell *matHeaderCellDef mat-sort-header>Name</mat-header-cell>
13-
<mat-cell *matCellDef="let row">{{row.name}}</mat-cell>
11+
<th mat-header-cell *matHeaderCellDef mat-sort-header>Name</th>
12+
<td mat-cell *matCellDef="let row">{{row.name}}</td>
1413
</ng-container>
1514

16-
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
17-
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
18-
</mat-table>
15+
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
16+
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
17+
</table>
1918

2019
<mat-paginator #paginator
2120
[length]="dataSource.data.length"

src/lib/schematics/table/index_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('material-table-schematic', () => {
2828
});
2929

3030
it('should create table files and add them to module', () => {
31-
const tree = runner.runSchematic('materialTable', { ...options }, createTestApp());
31+
const tree = runner.runSchematic('table', { ...options }, createTestApp());
3232
const files = tree.files;
3333

3434
expect(files).toContain('/src/app/foo/foo.component.css');
@@ -50,7 +50,7 @@ describe('material-table-schematic', () => {
5050
});
5151

5252
it('should add table imports to module', () => {
53-
const tree = runner.runSchematic('materialTable', { ...options }, createTestApp());
53+
const tree = runner.runSchematic('table', { ...options }, createTestApp());
5454
const moduleContent = getFileContent(tree, '/src/app/app.module.ts');
5555

5656
expect(moduleContent).toContain('MatTableModule');

0 commit comments

Comments
 (0)