Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shrinkable mat-toolbar

I used the example provided here to set up a responsive navbar

https://theinfogrid.com/tech/developers/angular/responsive-navbar-angular-flex-layout/

and my code looks pretty similar

<div style="height: 85vh;">

  <mat-toolbar color="primary" mat-scroll-shrink>
    <span>{{title}}</span>
    <span class="example-spacer"></span>
    <div fxShow="true" fxHide.lt-md="true">
      <!-- The following menu items will be hidden on both SM and XS screen sizes -->
      <a href="#" mat-button>Home</a>
      <a href="#" mat-button>About</a>
      <a href="#" mat-button>Services</a>
      <a href="#" mat-button>Portfolio</a>
      <a href="#" mat-button>Start</a>
      <a href="#" mat-button>FAQ</a>
      <a href="#" mat-button>Blog</a>
      <a href="#" mat-button>Contact</a>
    </div>

    <div fxShow="true" fxHide.gt-sm="true">
      <a href="#" (click)="sidenav.open()">Show Side Menu</a>
    </div>
  </mat-toolbar>

  <mat-sidenav-container fxFlexFill class="example-container">
    <mat-sidenav #sidenav fxLayout="column">
      <div fxLayout="column">
        <a (click)="sidenav.close()" href="#" mat-button>Close</a>
        <a href="#" mat-button>Home</a>
        <a href="#" mat-button>About</a>
        <a href="#" mat-button>Services</a>
        <a href="#" mat-button>Portfolio</a>
        <a href="#" mat-button>Start</a>
        <a href="#" mat-button>FAQ</a>
        <a href="#" mat-button>Blog</a>
        <a href="#" mat-button>Contact</a>
      </div>
    </mat-sidenav>
    <mat-sidenav-content fxFlexFill>

      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
      <p>Demoing some content to make this thing scroll</p>
    </mat-sidenav-content>
  </mat-sidenav-container>
</div>

What I would like to have happen is have the mat-toolbar shrink as I scroll down, which is common in a lot of sites, such as this one:

https://www.havealook.com.au/

I won't post the rest of the angular 5 code, just follow the example to re-create - its pretty quick.

I've looked at the materials website here

https://material.angular.io/components/toolbar/overview

but there's not much explanation on how to add to it, and I'm pretty new to this stuff. Does anyone know how I might customise this to make the toolbar shrink, based on scrolling?

like image 512
Michael Coxon Avatar asked Jan 26 '18 01:01

Michael Coxon


People also ask

What is Mat toolbar?

The <mat-toolbar> is an Angular Directive used to create a toolbar to show the title, header, or any button action of buttons. <mat-toolbar>: It represents the main container. <mat-toolbar-row>: It adds a new row at the toolbar. Example of ToolBar: Video Player is loading.

How to set mat toolbar color?

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' .

How do I change the height of my mat toolbar?

The height of the toolbar can be changed by adding the classes to the md-toolbar . Show activity on this post. Show activity on this post.


1 Answers

Update Nov 2018

The ScrollDispatchModule was deprecated with Angular CDK v7. Use the ScrollingModule instead.


I've created a Stackblitz with a toolbar that shrinks when scrolling down.

Main Steps

Use the CdkScrollDispatcher service to react to scroll events

  1. Import the ScrollDispatchModule in your module.
import {ScrollDispatchModule} from '@angular/cdk/scrolling';
  1. Mark the containers of which the scroll events are relevant with the directive cdkScrollable, here it is mat-sidenav-content.
 <mat-sidenav-content fxFlexFill cdkScrollable>
  1. React to scroll events in the ngOnInit of your component, get the scrollTop position and set a flag if it is larger than a certain threshold:
private readonly SHRINK_TOP_SCROLL_POSITION = 50;
shrinkToolbar = false;

constructor(private scrollDispatcher: ScrollDispatcher,
            private ngZone: NgZone) { }

ngOnInit() {
  this.scrollDispatcher.scrolled()
    .pipe(
      map((event: CdkScrollable) => event.getElementRef().nativeElement.scrollTop)
    )
    .subscribe(scrollTop => this.ngZone.run(() => this.shrinkToolbar = scrollTop > this.SHRINK_TOP_SCROLL_POSITION ? true : false));
}

You need to run this with ngZone because scrolled() events of the ScrollDispatcher are run outside of Angular by default. Without it, the ChangeDetection won't run and your templates won't be updated.

Change the toolbar layout on scroll

  1. Add a shrink css class, when the container is scrolled down
<mat-toolbar color="primary" [ngClass]="{'shrink-toolbar': shrinkToolbar}">
  1. Define the css class for the shrinked layout.
.shrink-toolbar {
  height: 32px;
}

Find more information about the scroll service in the official docs.

like image 193
Kim Kern Avatar answered Sep 17 '22 16:09

Kim Kern