What
is Angular Material in Angular ?
Angular
Material
is a UI component library that provides a set of pre-built, reusable
components that follow Google's Material Design principles. It helps
developers build beautiful, consistent, and responsive user interfaces
(UIs) without having to start from scratch.
🎨 Key Features of Angular Material
- Pre-built
UI Components
Angular Material provides over 30 pre-built components like: - Buttons
- Forms
- Navigation
(e.g., toolbars, sidenavs)
- Dialogs,
menus, and tooltips
- Cards,
tables, and lists
- Datepickers,
sliders, and checkboxes
- Material
Design
The components follow Material Design guidelines, offering a consistent visual language and ensuring a cohesive user experience across platforms. - Responsive
Components are responsive by default, adapting smoothly to different screen sizes and devices. - Customizable
You can customize the look and feel of Angular Material components by overriding styles or using Angular's theming system. - Accessibility
Angular Material prioritizes accessibility by providing components that are keyboard-navigable and screen reader-friendly. - Integration
with Angular
Angular Material is designed to integrate seamlessly with Angular, leveraging Angular's features like reactive forms, animations, and change detection.
🔧 Setting Up Angular Material
To start using
Angular Material in your project, follow these steps:
- Install
Angular Material, CDK, and Animations
ng add
@angular/material
- This
command installs Angular Material and automatically configures some
dependencies, including Angular CDK (Component Dev Kit) and Angular
Animations.
- Choose
a Theme
You’ll be prompted to
choose a theme (e.g., indigo-pink, deep-purple-amber), or you can
create your own custom theme.
- Import
Angular Material Modules
In app.module.ts,
import the Material modules for the components you want to use. For example, if
you want to use buttons and input fields:
import { NgModule }
from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { MatButtonModule } from '@angular/material/button';
import { MatInputModule } from '@angular/material/input';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
MatButtonModule,
MatInputModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
📚 Example: Using Material Components
1.
Material Button and Input
<mat-form-field>
<input matInput
placeholder="Enter your name" [(ngModel)]="name">
</mat-form-field>
<button
mat-raised-button color="primary" (click)="submit()">Submit</button>
- mat-form-field:
Wraps form inputs for consistent styling.
- matInput:
Applies Material Design styles to <input> elements.
- mat-raised-button:
Creates a raised button with Material Design styling.
2.
Material Dialog (Popup)
- Import
Material Dialog Module:
import {
MatDialogModule } from '@angular/material/dialog';
- Open
a Dialog:
import { MatDialog }
from '@angular/material/dialog';
@Component({
selector: 'app-my-component',
template: '<button mat-raised-button
(click)="openDialog()">Open Dialog</button>'
})
export class MyComponent {
constructor(private
dialog: MatDialog) {}
openDialog() {
this.dialog.open(MyDialogComponent);
}
}
- Dialog
Component:
@Component({
selector: 'app-my-dialog',
template: `<h1>Dialog
Content</h1>`
})
export class MyDialogComponent {}
🎨 Theming and Customization
Angular Material
allows easy theming. You can customize the default colors, typography,
and density.
Example: Creating a
Custom Theme
- Define
a theme in styles.scss:
@import
'~@angular/material/theming';
$custom-primary:
mat-palette($mat-indigo);
$custom-accent: mat-palette($mat-pink, A200, A100, A400);
$custom-theme:
mat-light-theme($custom-primary, $custom-accent);
@include
angular-material-theme($custom-theme);
- Add
global styles
(like button, input styles) by customizing Angular Material's default
style.
🧠Summary
Feature |
Description |
UI
Components |
Pre-built
Material Design components |
Theming |
Built-in
theming system to customize colors |
Responsive |
Automatically
adapts to screen size |
Accessibility |
Focus
on accessible and user-friendly components |
Integration |
Tight
integration with Angular’s architecture |
No comments:
Post a Comment