Angular Material - Toolbar



The <mat-toolbar>, an Angular Directive, is used to create a toolbar to show title, header or any action button.

  • <mat-toolbar> - Represents the main container.

  • <mat-toolbar-row> - Add a new row.

In this chapter, we will showcase the configuration required to draw a toolbar control using Angular Material.

Create Angular Application

Follow the following steps to update the Angular application we created in Angular Material - First Application chapter −

Step Description
1 Create a project with a name material-app as explained in the Angular Material - First Application chapter.
2 Modify app.ts and app.html as explained below. Keep rest of the files unchanged.
3 Compile and run the application to verify the result of the implemented logic.

app.ts

Following is the content of the modified app.ts.

import { Component, signal } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatIconModule } from '@angular/material/icon';
import { MatToolbarModule } from '@angular/material/toolbar';

@Component({
   selector: 'app-root',
   imports: [
      FormsModule,
      MatFormFieldModule,
      MatToolbarModule,
      MatIconModule, 
      MatButtonModule, 
      MatCheckboxModule,
      ReactiveFormsModule,
   ],
   templateUrl: './app.html',
   styleUrl: './app.css'
})
export class App {
   protected readonly title = signal('material-app');
}

app.html

Following is the content of the modified HTML host file app.html.

<mat-toolbar>
   <button matIconButton aria-label="menu icon">
      <mat-icon>menu</mat-icon>
  </button>
  <span>File</span>
  <span class="tp-spacer"></span>
  <button matIconButton aria-label="heart icon">
      <mat-icon>favorite</mat-icon>
  </button>
  <button matIconButton aria-label="share icon">
      <mat-icon>share</mat-icon>
  </button>
</mat-toolbar>

app.css

Following is the content of the modified CSS file app.css.

.tp-spacer {
  flex: 1 1 auto;
}

Result

Verify the result.

Toolbar

Details

  • As first, we've created a toolbar spanning the complete page.
  • Then labels are added.
Advertisements