This tutorial is adapted from Web Age course Comprehensive Angular 8 Programming.
Material Design is design language developed by Google. It was released in 2014 and incorporated into the Android OS. It is also available for other platforms (Angular, Web, iOS, Flutter, …). Developers can achieve uniformity in their applications across platforms by using Material Design. It uses paper and Ink paradigm. It uses shadows and lighting to create depth and edges. It incorporates movement that reinforces choice and navigation.
Material Design Specification can be found at https://material.io/design/.
Angular Material contains components that implement Material Design UI standards and can be used with Angular. Angular Material components are Built in and for Angular, Reactive – they work across web, mobile and desktop, Fully tested, tuned and optimized, Themeable and Accessible, Support internationalization (i18n) .Using Angular Material components speeds up development.
The main page for Angular Material can be found at https://material.angular.io/.
AM speeds up development by solving the issues listed above which allows teams to spend more time developing application-centric code as opposed to designing and creating basic components
To use Angular Material it needs to be added to an existing Angular project
For full setup instructions see the Angular Material – Getting Started page here:
https://material.angular.io/guide/getting-started
The –defaults flag on the ‘ng new’ command creates an Angular project without routing support and with plain CSS for styling. If desired you can set these differently by leaving off –defaults and following prompts or you can add parameters to the command as follows:
ng new <app-name> --routing true --style
The Angular project can be started as usual using:
ng serve (or npm start)
Once up and running the app displays the following default component (app.component.ts) which does not use Angular Material:
To start development replace the contents of app.component.html with your own template code.
Angular Material’s ‘add’ schematic lets you add Angular Material to an Angular project:
ng add @angular/material
Schematics for creating components are also available:
address-form – Creates an address form component
navigation – Creates a component pre-set with sidenav and toolbar
dashboard – Creates a component with cards in a grid layout
table – Creates a table component with sorting and pagination
tree – Creates a tree structure component
Component schematics are used like this:
ng generate @angular/material:dashboard <comp-name>
Installing one of the above components will also import any Angular Material modules required by the component.
More information on Angular Material schematics is available here:
https://material.angular.io/guide/schematics
Angular Material consists of various Modules :MatSidenavModule – Side Navigation, MatToolbarModule – ToolBar, MatIconModule – Icons, MatListModule – Lists, MatCardModule – Cards, MatButtonModule – Buttons, MatTableModule – Tables, MatDialogModule – Dialogs, MatInputModule – Form Input Fields, MatSelectModule – Dropdown Input Field. Each module must be imported separately before it’s features can be used
Modules must be added to your application before you can use them. The initial install of Angular Material into your project does not install any Angular Material modules. Modules can be added in one of two ways-Modules can be added directly to the app.module.ts file, An additional module can be created just to handle importing of the Angular Material modules (e.g. material.module.ts). Using a separate module reduces clutter in the app.module.ts file. The material.module.ts file is then included in the app.module.ts file as a single import and added to the imports array.
material.module.ts file example
This file goes in the same directory as the app.module.ts file
import {NgModule} from '@angular/core';
import { MatIconModule, MatCardModule } from '@angular/material';
@NgModule({
imports: ,
exports:
})
export class MaterialModule {}
This example imports just a few modules. In practice you can expect to import a half dozen or more.
Keep in mind that when using one of the Angular Material component schematics it will add module imports directly to the app.module.ts file. In such a case you may want to move those imports out of the app.module.ts file and add them instead to your material.module.ts file.
material.module.ts is added to the app.module.ts file
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { MaterialModule } from './material.module'
@NgModule({
declarations: ,
imports: [ BrowserModule, MaterialModule ],
bootstrap:
})
export class AppModule { }
A theme is a set of colors that can be applied to Angular Material components.
Primary Palette – Most widely used colors
Accent Palette – Used for floating or interactive elements
Warm Palette – Used to denote errors
Foreground Palette – For text and icons
Background Palette – For backgrounds
Built-in themes include DeepPurple-Amber, Indigo-Pink, Pink-BlueGrey, Purple-Green. Themes are included by adding a line like this to the styles.css file
@import '@angular/material/prebuilt-themes/deeppurple-amber.css';
Theme colors are mapped by default to some components (e.g. Toolbar). Not all components have themed colors (e.g. Card)
Animations include ripple effects when buttons are clicked and slide and fade in-out component transitions during navigation. Animations are enabled during the addition of Angular Material to a project when the BrowserAnimationsModule is imported and added to the imports array in the appmodule.ts file. Angular Material Animations will not run unless this module is imported.
import {BrowserAnimationsModule}
from '@angular/platform-browser/animations';
...
@NgModule({
declarations: ,
imports: [ BrowserModule,
MaterialModule,
BrowserAnimationsModule ],
bootstrap:
})
export class AppModule { }
Angular Material uses the HammerJS package to support gestures. HammerJS will be installed for you if you choose Yes when asked when the angular/material schematic runs.
If you chose No you can still install it later by:
npm install --save hammerjs
import 'hammerjs';
Gestures supported by this package include: Pan, Pinch, Press, Rotate, Swipe and Tap.
Components that make use of gestures include: mat-slide-toggle, mat-slider, matTooltip,…
1.14 Angular Material Icons
Material Design includes open source sets of icons that can be used in applications.
Icons are displayed using the mat-icon component:
<mat-icon
aria-hidden="false"
aria-label="Example home icon"
>home</mat-icon>
The above HTML displays the ‘home’ icon.
Many other icons exist including, alarm, bookmark, delete, done, event, face, list, print, today. Several categories of Icons are available, Action, Alert, Av, Communication, Content, Device, Editor, File, Hardware, Image, Maps, Navigation, Places, Social, Toggle.
Available icons can be found here:
https://material.io/resources/icons/?style=baseline
https://material.angular.io/components/categories
Angular Material provides three containers for navigational items
Menu – A floating panel of items
Sidenav – A container that slides out from the side of the screen
Toolbar – A container for title information and screen controls
<button mat-button =”menu”>Menu</button>
<mat-menu #menu=”matMenu”>
<button mat-menu-item>Item 1</button>
<button mat-menu-item>Item 2</button>
</mat-menu>
More information on navigational components can be found here:
https://material.angular.io/components/categories/nav
Angular Material supports several layout related components
List – Displays list of items
Tree – Displays hierarchical content
Card – Container for itemized content
Tabs – Displays one view out of multiple possible views
Expansion Panel – Container that can be expanded
GridList – Flexible grid based container for items
Stepper – Wizard type interface
Divider – Vertical or horizontal divider
Notes
For more information on Angular Material layouts see:
https://material.angular.io/components/categories/layout
<mat-card>Basic card</mat-card>
<mat-list role="list">
<mat-list-item role="listitem">Maple</mat-list-item>
<mat-list-item role="listitem">Oak</mat-list-item>
<mat-list-item role="listitem">Birch</mat-list-item>
</mat-list>
The official documentation includes examples of all layout types:
https://material.angular.io/components/categories/layout
For more information on cards see:
https://material.angular.io/components/card/overview
For more information on Lists see:
https://material.angular.io/components/list/overview
Angular Material includes support for the following form input controls
Autocomplete – User types and it suggests selections
Checkbox – For boolean input
Datepicker – A directive for use with <input> fields
Input – A directive for use with <input> elements
Radio button – Supports mutually exclusive selections
Select – Drop down selector
Slider – Selects value from a range
Slide Toggle – On/Off toggle
Controls exist as either an Angular Material component (e.g. mat-slider) or as a directive used with the standard HTML input element
More information on form components can be found here:
https://material.angular.io/components/categories/forms
<mat-form-field>
<mat-label>Favorite Car</mat-label>
<mat-select>
<mat-option *ngFor="let car of cars"
="car.value">
{{car.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field class="example-full-width">
<input matInput placeholder="enter comment">
</mat-form-field>
For more on Angular Material form input fields see:
https://material.angular.io/components/categories/forms