Create a Responsive Toolbar using Angular Material

前端 未结 4 725
日久生厌
日久生厌 2020-12-13 15:14

I have created a toolbar using Angular Material. However, it is not responsive. How can I make the toolbar responsive?

Code for toolbar:



        
相关标签:
4条回答
  • 2020-12-13 15:39

    My crude implementation attempt with flex-layout and angular-material 5.0.2

    .mat-sidenav-container {
      background: rgba(0, 0, 0, 0.08);
    }
    
    .blank-grow {
      flex: 1 1 auto;
    }
    
    <mat-sidenav-container fullscreen>
      <mat-sidenav #sidenav>
        <mat-nav-list>
          <a mat-list-item>
            <mat-icon mat-list-icon>home</mat-icon>
            <span mat-line>home</span>
          </a>
    
          <a mat-list-item>
            <mat-icon mat-list-icon>backup</mat-icon>
            <span mat-line>Backup</span>
          </a>
        </mat-nav-list>
      </mat-sidenav>
      <mat-toolbar color="primary">
        <button mat-icon-button (click)="sidenav.open()" fxHide="false" fxHide.gt-sm>
          <mat-icon>menu</mat-icon>
        </button>
        <span> Big Header</span>
        <span class="blank-grow"></span>
        <div fxLayout="row" fxShow="false" fxShow.gt-sm>
          <a>
            <mat-icon mat-list-icon>home</mat-icon>
            <span mat-line>home</span>
          </a>
    
          <a>
            <mat-icon mat-list-icon>backup</mat-icon>
            <span mat-line>Backup</span>
          </a>
        </div>
      </mat-toolbar>
    </mat-sidenav-container>
    
    0 讨论(0)
  • 2020-12-13 15:47

    Use angular/flex-layout for responsiveness.

    • flex-layout wiki
    • flex-layout Demo
    0 讨论(0)
  • 2020-12-13 15:52

    Problem

    You need a way to make your navigation bar responsive.

    Solution

    You can use Material Angular with ToolBar and Flex-Layout.

    Example

    npm install @angular/flex-layout --save
    

    Example Angular Material Toolbar with Flex Layout. Here is a great tutorial here.

    <div style="height: 100vh;">
    
      <mat-toolbar color="primary">
    
        <span>Responsive Navigation</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>Menu Item 1</a>
    
          <a href="#" mat-button>Menu Item 2</a>
    
          <a href="#" mat-button>Menu Item 3</a>
    
          <a href="#" mat-button>Menu Item 4</a>
    
          <a href="#" mat-button>Menu Item 5</a>
    
          <a href="#" mat-button>Menu Item 6</a>
    
        </div>
    
        <div fxShow="true" fxHide.gt-sm="true">
    
          <a href="#" (click)="sidenav.toggle()">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.toggle()" href="#" mat-button>Close</a>
    
            <a href="#" mat-button>Menu Item 1</a>
    
            <a href="#" mat-button>Menu Item 2</a>
    
            <a href="#" mat-button>Menu Item 3</a>
    
            <a href="#" mat-button>Menu Item 4</a>
    
            <a href="#" mat-button>Menu Item 5</a>
    
            <a href="#" mat-button>Menu Item 6</a>
    
          </div>
    
        </mat-sidenav>
    
        <mat-sidenav-content fxFlexFill>Main content</mat-sidenav-content>
    
      </mat-sidenav-container>
    
    </div>
    
    0 讨论(0)
  • 2020-12-13 16:02

    Here is my favorite way of creating a responsive Navigation Bar in Angular. If you use Angular 6, make sure you use a version 6.1+
    Working example on Stackblitz: https://stackblitz.com/edit/angular-v6xwte

    Example on a smaller screen:

    Example on a larger screen:

    Here are precise steps how to do it:

    1) Install necessary packages. Type in your terminal:

    npm install --save @angular/material @angular/cdk @angular/animations
    
    npm install @angular/flex-layout --save
    

    2) Import necessary modules in your app.module.ts

    import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
    import { FlexLayoutModule } from '@angular/flex-layout';
    import {
      MatIconModule, MatButtonModule, MatSidenavModule, MatToolbarModule
    } from '@angular/material';
    

    Remember to add these modules to the imports array below.

    3) Add Material Icons link to your index.html
    The link must go before any Angular content.

    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    

    4) In your styles.css add an Angular theme and set margins to 0%

    @import "~@angular/material/prebuilt-themes/indigo-pink.css";
    body{
        margin: 0%;
    }
    

    5) Add toolbar HTML code in your app.component.html

    <div> 
      <mat-toolbar color="primary">
        <div fxShow="true" fxHide.gt-sm="true">
          <button mat-icon-button (click)="sidenav.toggle()">
            <mat-icon>menu</mat-icon>
          </button>
        </div>
    
        <a mat-button class="companyName" routerLink="/">
          <span>Site name</span>
        </a>
        <span class="example-spacer"></span>
        <div fxShow="true" fxHide.lt-md="true">
          <a mat-button routerLink="/about-us">About us</a>
          <a mat-button routerLink="/prices">Prices</a>
          <a mat-button routerLink="/start-page">Start page</a>
          <a mat-button routerLink="/offer">Offer</a>
          <a mat-button routerLink="/contact">Contact</a>
        </div>
    
      </mat-toolbar>
      <mat-sidenav-container fxFlexFill class="example-container">
    
        <mat-sidenav color="primary" #sidenav fxLayout="column" mode="over"  opened="false" fxHide.gt-sm="true">
          <div fxLayout="column">
            <a mat-button routerLink="/about-us">About us</a>
            <a mat-button routerLink="/prices">Prices</a>
            <a mat-button routerLink="/start-page">Start page</a>
            <a mat-button routerLink="/offer">Offer</a>
            <a mat-button routerLink="/contact">Contact</a>
          </div>
        </mat-sidenav>
        <mat-sidenav-content fxFlexFill>
          Awesome content
        </mat-sidenav-content>
      </mat-sidenav-container>
    </div>
    

    6) Style the toolbar in your app.component.css

    .companyName{
        font-size: 150%;
    }
    
    .mat-toolbar{
      height: 7vh;
    }
    
    div {
        overflow: inherit;
    }
    
    .mat-sidenav-container{
      background-color: lightskyblue;
      min-height: 93vh !important;
    }
    
    a{
        text-decoration: none;
        font-size: 110%;
        white-space: normal;
    }
    
    button{
        font-size: 110%;
        min-width: min-content;
    }
    
    .example-icon {
        padding: 0 14px;
      }
    
      .example-spacer {
        flex: 1 1 auto;
      }
    
      .mat-sidenav-content{
          font-size: 200%;
          text-align: center;
      }
    
    0 讨论(0)
提交回复
热议问题