π¨ Angular Material Theming - A Step-by-Step Guide
Hey there, coding wizards! π§ββοΈ Ready to add some serious style to your Angular app? Letβs explore Angular Material Theming step by step and make your UI stunning! β¨
π Start Here: Learn the Basics
Before we begin, check out these essential resources on Angular Material:
1οΈβ£ Components Overview π Material Components
2οΈβ£ Theming Basics π Material Theming
3οΈβ£ In-Depth Theming Guide π Complete Theming Guide
βοΈ Hands-on: Create an Angular Project with Material
Follow these quick steps to set up your Angular app with Material theming! π
π οΈ Step 1: Create a New Angular App
Run this command in your terminal:
ng new my-app --style=scss --defaults
π This will set up an Angular app with SCSS as the default styling.
π¨ Step 2: Add Angular Material
Install Angular Material with this command:
ng add @angular/material
βοΈ Select CUSTOM for the theme.
βοΈ Choose Yes (Y) for typography.
βοΈ Choose Yes (Y) for animations.
π Step 3: Generate a Custom Theme
π‘ Create a custom Material theme with:
ng g @angular/material:theme-color
βοΈ Choose colors:
- Primary color:
#00796B
(A cool teal shade πΏ). - Secondary, Tertiary, Neutral colors: Pick anything you like!
βοΈ Set the directory:src/app/theme/greentheme
βοΈ Skip SCSS file: We want a CSS file instead.
π― Step 4: Apply Your Theme
Modify styles.scss
1οΈβ£ Comment out the default Material theme import
// @use "@angular/material" as mat;
2οΈβ£ Add your custom theme import:
@import "./app/theme/greentheme.css";
π Step 5: Update Your UI with Themed Components
Modify app.component.html
Replace the contents of app.component.html
with:
<h1>Angular Material Color Theme Demo</h1>
<section>
<div class="example-label">Basic</div>
<div class="example-button-row">
<button mat-button>Basic</button>
<button mat-button disabled>Disabled</button>
<a mat-button href="https://www.google.com/" target="_blank">Link</a>
</div>
</section>
<section>
<div class="example-label">Raised</div>
<div class="example-button-row">
<button mat-raised-button>Basic</button>
<button mat-raised-button disabled>Disabled</button>
<a mat-raised-button href="https://www.google.com/" target="_blank">Link</a>
</div>
</section>
<section>
<div class="example-label">Stroked</div>
<div class="example-button-row">
<button mat-stroked-button>Basic</button>
<button mat-stroked-button disabled>Disabled</button>
<a mat-stroked-button href="https://www.google.com/" target="_blank">Link</a>
</div>
</section>
π₯ Now, your app will have styled buttons using your custom theme!
β‘ Step 6: Update app.component.ts
Modify the contents of app.component.ts
to import the required Material modules:
import { RouterOutlet } from '@angular/router';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { Component } from '@angular/core';
import { MatDividerModule } from '@angular/material/divider';
@Component({
selector: 'app-root',
imports: [RouterOutlet, MatButtonModule, MatIconModule, MatDividerModule],
templateUrl: './app.component.html',
styleUrl: './app.component.scss'
})
export class AppComponent {
title = 'my-app';
}
π Step 7: Run and Enjoy Your Themed App!
Once everything is set, launch your app:
ng serve -o
π₯ This opens the app in your browser with your custom Material theme applied! π¨
π Wrapping Up
Congrats! π Youβve successfully:
β
Created an Angular app with Material.
β
Installed Material and customized a theme.
β
Applied the theme to buttons and UI elements.
π¨βπ¨ Next Steps?
πΉ Try changing the theme colors.
πΉ Explore Material Typography and Density.
πΉ Add more Material components (cards, toolbars, forms).
π Go ahead, make your app look amazing! β¨ Happy coding! π¨π₯
No comments:
Post a Comment