Skip to content
  • iImagine
  • Register
  • Log In

Web Development School

Learning made easy.

  • Books
    • Beginning Web Development with ASP.Net Core & Client-Side Technologies
      • TOC
      • Part 1
        • Chapter 1: Static HTML – Designing the landing page
      • Part 2
        • Chapter 2: ASP.Net Core – Let’s talk Dynamic
        • Chapter 3: Introduction to ASP.Net Core MVC
          [ASP.Net Core v9]
      • Part 4
        • Chapter 7: Using Server Side & Client Side technologies together
          [ASP.Net Core v7 & Angular 15]
  • Environment Setup
    • Installing Angular
    • Installing Visual Studio 2022
    • Installing SQL Server 2022 Express
    • Installing Postman
    • Installing Git for Windows
  • Blog
  • iImagine WebSolutions
  • Events
  • Learning Videos
  • Toggle search form

Our first Angular Material Component: MatToolbar

In the last module we installed the Angular Material Component Library. Now that we have that taken care of we can start to improve the appearance of the FredsCars web application starting with the Navigation Header.

To start we are going to use a MatToolBar Component for the top navigation bar. And we will replace the plain navigation anchor links with MatIcon and MatButton.

The first thing we need to do is register the Angular Material Modules in either the root module or a custom feature module. It’s probably a good idea to break out the management of Angular Material Modules into its own configuration to keep the root module clean just like we did for AppRoutingModule and, because we may add a lot more Angular Material features along the way. So let’s create an AngularMaterial Module next.

Note: At this point I am copying the project from the Module12 folder to a Module13 folder. The pathway I am using for development here is:
C:\Development\FredsCars\FullStack\Module13.
Table Of Contents
  1. Create the AngularMaterial Module
  2. Register the AngularMaterial Module
  3. Using MatToobar
    • Update the NavMenuComponent HTML
  4. MatToolbar
    • Default Neutral Color
    • Primary Color
    • Accent Color
    • Warn Color
  5. Angular Material Buttons
  6. MatIconButton
    • MatIcon
  7. MatFlatButton
  8. What's Next

Create the AngularMaterial Module

Create a file called angular-material.module.ts in the FredsCars/src/app folder with the following contents.

import { NgModule } from '@angular/core';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatIconModule } from '@angular/material/icon';
import { MatButtonModule } from '@angular/material/button';

@NgModule({
  imports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule
  ],
  exports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule
  ]
})
export class AngularMaterialModule { }

In the above code we are importing the Angular Material Modules we will need; MatToolbarModule, MatIconModule, and MatButtonModule. The next thing we need to do is register our custom Angular Module in the root module.

Register the AngularMaterial Module

Open the app.module.ts file in the FredsCars/src/app folder and modify it with the code below.

import { HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';

import { AppComponent } from './app.component';
import { VehiclesComponent } from './vehicles/vehicles.component';
import { WelcomeComponent } from './welcome/welcome.component';
import { NavMenuComponent } from './nav-menu/nav-menu.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AngularMaterialModule } from './angular-material.module';

@NgModule({
  declarations: [
    AppComponent,
    VehiclesComponent,
    WelcomeComponent,
    NavMenuComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    AngularMaterialModule

  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

In the code above we simply registered our new custom module in the root module.

Using MatToobar

Now that we have our AngularMaterialModule configured, we should be able to use the MatToolbar component in our HTML. So let’s give it a go.

Update the NavMenuComponent HTML

Open the nav-menu.component.html file in the FredsCars/src/app/nav-menu folder and modify its contents with the code below.

<header>
  <mat-toolbar color="primary">
    <a mat-icon-button [routerLink]="['/']">
      <mat-icon>
        home
      </mat-icon>
    </a>
    <a mat-flat-button [routerLink]="['/vehicles']">
      Vehicles
    </a>
  </mat-toolbar>
</header>

Restart the application and run in debug mode and you should see results similar to the following.

As you can see we have replaced our lackluster anchor links and nav section with a nice spiffy toolbar. Let’s talk about what we just did in the NavComponent’s HTML above.

MatToolbar

The documentation for the MatToolbar component can be found at: https://material.angular.io/components/toolbar/overview. It says that <mat-toolbar> is a container for headers, titles, or actions. It also makes a nice container for our navigation buttons as well.

The first thing we did above was replace the <nav> element with the <mat-toolbar> element where the MatToolBar component now sits. We also set the color attribute to primary.

<mat-toolbar color="primary">

The theming section of the MatToolbar documentation states:

The color of a <mat-toolbar> can be changed by using the color property. By default, toolbars use a neutral background color based on the current theme (light or dark). This can be changed to ‘primary’, ‘accent’, or ‘warn’.

Theming

The following are code for and screenshots of the default MatToolbar neutral background, and the three supported colors.

Default Neutral Color

<!-- no color specified -->
<mat-toolbar>

Primary Color

<!-- This is the color we are using in our application -->
<mat-toolbar color="primary">

Accent Color

<!-- accent -->
<mat-toolbar color="accent">

Warn Color

<!-- warn-->
<mat-toolbar color="warn">

The next thing we did was add a couple of button directives to our plain text <a> elements giving them the appearance of being buttons.

We changed the Home link to our welcome screen from this:

<a [routerLink]="['/']">Home</a>

to this:

<a mat-icon-button [routerLink]="['/']">
    <mat-icon>
        home
    </mat-icon>
</a>

which renders like this:

And we changed our vehicles <a> element link from this:

<a [routerLink]="['/vehicles']">Vehicles</a>

to this:

<a mat-flat-button [routerLink]="['/vehicles']">
    Vehicles
</a>

which renders like this:

But how does all this magic happen?

It’s worth taking a pit stop here and spending a few minutes to talk about Angular Material Buttons.

Angular Material Buttons

The documentation for the Angular Material Buttons can be found at: https://material.angular.io/components/button/overview. It says that Angular Material buttons are native <button> or <a> elements enhanced with Material Design styling and ink ripples.

This means that all the mat-button directive variations (mat-raised-button, mat-flat-button and so on) can be used on both <button> and <a> elements. However buttons should be used for actions and anchor elements should be used for navigation. So we have left our <a> elements instead of changing them to <button> elements and just applied some mat-button directives.

Angular Material has a button and color system somewhat similar to Bootstrap.

The following is a screenshot laying out examples of the seven styles of button and six colors available.

The seven styles that can be used are:

  • Basic (default)
  • Raised (mat-raised-button)
  • Stroked (mat-stroked-button)
  • Flat (mat-flat-button)
  • Icon (mat-icon-button)
  • FAB (mat-fab)
  • Mini FAB (mat-mini-fab)

The six colors available are:

  • Basic (the default)
  • Primary
  • Accent
  • Warn
  • Disabled
  • Link

From the available colors listed above, Primary, Accent, and Warn target the three theming colors.

MatIconButton

Let’s take another look at our Home (Welcome) button code nested in the MatToolbar.

<a mat-icon-button [routerLink]="['/']">
    <mat-icon>
        home
    </mat-icon>
</a>

In the highlighted code above we added a mat-icon-button directive to an <a> element specifying it will contain an icon button.

MatIcon

Nested inside of the <a> element dressed with the mat-icon-button attribute is a <mat-icon> element where a MatIcon component can sit.

The MatIcon element is a container for the name of a Material Symbol icon, in this case home which again renders like this.

MatFlatButton

Let’s also take another look at the Vehicles button code in the toolbar.

<a mat-flat-button [routerLink]="['/vehicles']">
    Vehicles
</a>

Here we simply added the mat-flat-button directive to an anchor element which selects the following look, a flat style button with the Basic (default) color.

And again in our case for the Vehicles button in the toolbar, renders like this.

What’s Next

In this module we set up an AngularMaterial Module to cleanly manage our Angular Material Dependency registrations. Next we used Angular Material to improve the appearance of our anchor navigation links with Angular Material Toolbar and Button components. In the next couple of modules we will add header and footer components to our application and learn a new technology to make it easier to work with CSS called Saas.

< Prev
Next >

Leave a ReplyCancel reply

Chapter 1: Static HTML – Designing the landing page.

  • Static HTML – Designing the landing page.
  • Let’s get started!
  • Mock your site with HTML
  • Make CSS easy with Bootstrap
  • Mock your content
  • Introducing JavaScript
  • JavaScript Code Improvements
  • Results Data
  • Images and the HTML Image Element.
  • Revisiting Reusability for CSS and JavaScript
  • Reuse for HTML: PART 1
  • Reuse for HTML: PART 2
  • Details Page – Using a Bootstrap Component
  • Creating Links
  • Chapter One Conclusion

Chapter 2: ASP.Net Core – Let’s talk Dynamic

  • Introduction to ASP.Net Core
  • What is .Net?
  • What is ASP.Net
  • Introduction to Entity Framework Core

Chapter 3: ASP.Net MVC Core – Models, Views, and Controllers [ASP.Net Core v9]

  • Introduction to ASP.Net Core MVC
  • Create the project: ASP.Net Core MVC
  • Explore the ASP.Net Core Empty Web Project Template
  • Configure the Application for MVC
  • Create a Controller: Home Controller
  • Create a View: Index View for the Home Controller
  • Install Bootstrap using Libman
  • Create the Layout template
  • Create the Model
  • Install EF Core & Create the Database
  • Seed the Database: Loading test data
  • DI (Dependency Injection): Display a List of Vehicles
  • Repository Pattern: The Vehicles Repo
  • Unit Test 1: Home Controller Can Use Vehicle Repository
  • Unit Test 2: Vehicle Repository Can Return List
  • Add the ImagePath Migration and Thumbnail images to results
  • Pagination: Create a Custom Tag Helper
  • Sorting
  • Category Filter
  • Partial View: Break out the vehicle results
  • View Component: Create dynamic category buttons
  • Create the Details page
  • Create the Create Page

Chapter 7: Using Server Side & Client Side technologies together. [ASP.Net Core v7 & Angular v15]

  • Intro to Full Stack Development
  • Fred’s Cars – Full Stack Development
  • Prepare the environment
  • Create the Visual Studio Solution
  • Add the ASP.Net Core Web API project
  • Add the Angular Project
  • Wire it up!
  • WeatherForecast: Understanding the basics
  • Vehicles API Controller: Mock Data
  • Vehicles Angular Component: Consuming Data
  • Routing and Navigation
  • Using a Component Library: Angular Material
  • Our first Angular Material Component: MatToolbar
  • Configuring for Saas: CSS with superpowers
  • Create the Header & Footer components
  • Displaying Results with MatTable
  • Loading: Using a Progress Spinner
  • MatTable: Client-Side Paging and Sorting
  • MatSidenav: Create a Search Sidebar
  • MatCheckbox: Category Search UI
  • Adding an image to the welcome page
  • Create the database with Entity Framework Core migrations
  • MatPaginator & PageEvent: Custom Server-Side Paging
  • Unit Testing: Custom Server-Side Paging
  • Repository Pattern: VehicleRepository
  • Unit Test: Paging in the Vehicles controller
  • Server-Side Sorting
  • Unit Tests: Sorting
  • Filter (Quick Search)
  • Unit Tests: Filter feature
  • Advanced Search: Categories
  • Unit Tests: Search by category
  • Progress Spinner: Final Fix

TOC

  • What were WebForms?
  • Enter MVC
    • Understanding MVC
    • Advantages of MVC
  • ASP.Net Core MVC – A total rewrite
  • ASP.Net Core 2 MVC – Here come Razor Pages
    • Understanding Razor Pages
  • ASP.Net Core 3 – Dropping the MVC reference
    • Understanding Blazor
  • Dropping the MVC reference
  • Hello .Net 5!
  • What’s Next? – Here comes .Net 6.

Recent Posts

  • Angular Commands Cheat Sheet
  • Installing Git for Windows
  • Installing Postman
  • Installing SQL Server 2022 Express
  • Installing Visual Studio 2022

Recent Comments

No comments to show.

Archives

  • November 2023
  • October 2023
  • June 2023
  • October 2021

Categories

  • Angular
  • ASP.Net
  • Environment Setup
  • See All
  • SQL Server
  • Visual Studio
  • Web API & Rest Services

WordPress Theme Editor

Copyright © 2025 Web Development School.

Powered by PressBook Blog WordPress theme