Day 9 โ Angular Material & UI Mastery
๐ Day 9 โ Angular Material & UI Mastery
Zero to Hero โ Hands-on Angular Tutorial
Today you will learn:
- โ๏ธ Angular Material Setup
- โ๏ธ Buttons, Cards, Dialogs, Forms
- โ๏ธ Responsive Layout (
MatSidenav,MatToolbar) - โ๏ธ Data Tables with Pagination & Sorting
- โ๏ธ Custom Theming
- โ๏ธ Building a professional Dashboard UI
Angular Material is Googleโs official UI component library. It makes your app look professional instantly. ๐จ
๐ฆ 1. Setup Angular Material
Run this command to install everything:
ng add @angular/material
It will ask:
- Choose a theme: (e.g., Indigo/Pink)
- Typography: Yes
- Animations: Yes
Now your app is ready to use Material components.
๐ฉ 2. Buttons, Icons & Cards
In Standalone components, you must import the specific Material modules you need.
app.component.ts:
import { Component } from '@angular/core';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatCardModule } from '@angular/material/card';
@Component({
selector: 'app-root',
standalone: true,
imports: [MatButtonModule, MatIconModule, MatCardModule],
templateUrl: './app.component.html',
styleUrl: './app.component.scss'
})
export class AppComponent {}
app.component.html:
<mat-card>
<mat-card-header>
<mat-card-title>Angular Material is Awesome</mat-card-title>
<mat-card-subtitle>UI Components</mat-card-subtitle>
</mat-card-header>
<mat-card-content>
<p>This is a material card. It looks customized and clean.</p>
</mat-card-content>
<mat-card-actions>
<button mat-raised-button color="primary">Action</button>
<button mat-icon-button>
<mat-icon>favorite</mat-icon>
</button>
</mat-card-actions>
</mat-card>
- โ๏ธ
mat-raised-button= Button with shadow - โ๏ธ
mat-icon= Google Material Icons - โ๏ธ
mat-card= Container for content
๐ง 3. Material Forms (Clean & Validated)
Material forms allow floating labels and automatic error messages.
Import: MatInputModule and MatFormFieldModule.
app.component.html:
<mat-form-field appearance="outline">
<mat-label>Email Address</mat-label>
<input matInput placeholder="user@example.com">
<mat-icon matSuffix>email</mat-icon>
<mat-hint>We'll never share your email.</mat-hint>
</mat-form-field>
- โ๏ธ Floating Label: Moves up when you type.
- โ๏ธ Suffix Icon: Adds an icon inside the input.
- โ๏ธ Appearance: โfillโ or โoutlineโ.
๐ฅ 4. Dialogs (Modals)
Letโs create a popup dialog.
Step 1: Create ConfirmDialogComponent.
ng g c confirm-dialog --standalone
Step 2: Open it from AppComponent.
app.component.ts:
import { MatDialog, MatDialogModule } from '@angular/material/dialog';
import { ConfirmDialogComponent } from './confirm-dialog/confirm-dialog.component';
export class AppComponent {
constructor(private dialog: MatDialog) {}
openDialog() {
this.dialog.open(ConfirmDialogComponent, {
width: '300px',
data: { message: 'Are you sure?' }
});
}
}
app.component.html:
<button mat-flat-button color="warn" (click)="openDialog()">Delete Item</button>
- โ๏ธ Use
MatDialogservice to open components as modals.
๐ซ 5. Material Data Table (Professional Grids)
The most powerful component in Angular Material.
Import: MatTableModule, MatPaginatorModule, MatSortModule.
user-table.component.html:
<table mat-table [dataSource]="users" class="mat-elevation-z8">
<!-- ID Column -->
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef> ID </th>
<td mat-cell *matCellDef="let user"> {{user.id}} </td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let user"> {{user.name}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="['id', 'name']"></tr>
<tr mat-row *matRowDef="let row; columns: ['id', 'name'];"></tr>
</table>
user-table.component.ts:
users = [
{id: 1, name: 'Alice'},
{id: 2, name: 'Bob'},
{id: 3, name: 'Charlie'}
];
- โ๏ธ Scalable table
- โ๏ธ Sortable & Paginatable (with extra setup)
๐ฆ 6. Building a Dashboard Layout (Sidenav + Toolbar)
A standard app layout usually has a top bar and a side menu.
dashboard.component.html:
<mat-toolbar color="primary">
<button mat-icon-button (click)="sidenav.toggle()">
<mat-icon>menu</mat-icon>
</button>
<span>My App Dashboard</span>
</mat-toolbar>
<mat-sidenav-container>
<mat-sidenav #sidenav mode="side" opened>
<mat-nav-list>
<a mat-list-item href="#">Dashboard</a>
<a mat-list-item href="#">Profile</a>
<a mat-list-item href="#">Settings</a>
</mat-nav-list>
</mat-sidenav>
<mat-sidenav-content>
<h1>Welcome to the Dashboard!</h1>
<!-- Main content goes here -->
</mat-sidenav-content>
</mat-sidenav-container>
Import: MatSidenavModule, MatToolbarModule, MatListModule.
๐จ 7. Custom Theming
Angular Material uses SCSS customization.
In styles.scss, you can override colors:
@use '@angular/material' as mat;
$my-primary: mat.define-palette(mat.$indigo-palette);
$my-accent: mat.define-palette(mat.$pink-palette, A200, A100, A400);
// Use the theme
$my-theme: mat.define-light-theme((
color: (
primary: $my-primary,
accent: $my-accent,
)
));
@include mat.all-component-themes($my-theme);
This changes the entire color scheme of your app instantly.
๐ End of Day 9 โ What You Learned
Today you transformed your app from plain HTML to a professional UI:
- โ๏ธ Installed Angular Material
- โ๏ธ Built Cards, Buttons, and Layouts
- โ๏ธ Created Modals (
MatDialog) - โ๏ธ Built Data Tables
- โ๏ธ Implemented a Dashboard Sidenav
- โ๏ธ Learned Custom Theming
Your app now looks ready for production. ๐
๐งช Day 9 Challenge
Build a โTask Manager Dashboardโ.
Requirements:
- Use
MatSidenavfor navigation (Tasks, Completed, Deleted). - Use
MatCardto display tasks in a grid. - Add a
Floating Action Button (FAB)to add a new task. - Clicking the FAB opens a
MatDialogform. - Use
MatSnackBarto show a notification when a task is saved.