Tips for converting a toolbar into a side navigation bar when resizing the window (shrinking)?

Check out this example

When the browser is maximized, it should show the toolbar and its content. However, if the browser window is resized, a new side nav bar should appear. How can I implement this feature? How do I utilize media queries to achieve this?

Full Screen

https://i.sstatic.net/nvrZd.png

Reduced Width

https://i.sstatic.net/iJPuS.png

I am looking to incorporate Angular material design to showcase the toolbar and side nav bar.

Answer №1

To control the visibility of the sidenav button and toolbar content, you can implement a boolean flag with ngIf. This flag can be toggled using the window:resize event.

For example, you can display the sidenav differently for screen sizes below 720px compared to those greater than or equal to 720px.

Here's how it can be achieved:

<md-toolbar [color]="toolbarColor">
    <button *ngIf="openSidenavFlag" md-button color="primary" (click)="sidenav.toggle()">
      <md-icon>menu</md-icon>
    </button>
    <h1>Angular</h1>
    <div *ngIf="!openSidenavFlag" style="margin: 0 auto">
      <button md-button>Blog</button>
      <button md-button>About</button>
      <button md-button>Contact</button>
    </div>
</md-toolbar>

<div fxFlex class="app-content">
  This is the content
</div>

<md-sidenav-container (window:resize)="onResize($event)" style="height: 91vh;background: #FFFFFF">

  <md-sidenav #sidenav mode="side" style="background: orange">
    Sidenav!
  </md-sidenav>

</md-sidenav-container>

<footer style="background: skyblue">This is footer</footer>

ts:

  openSidenavFlag = false;

  constructor() {
  }

  ngOnInit(){
    this.onResize();
  }

  onResize(event) {
    let width;

    if(event != undefined){
      width = event.target.innerWidth;
      // console.log(event.target.innerWidth);
    }
    else{
      // console.log(document.body.clientWidth);
      width = document.body.clientWidth;
    }

    if (width >= 720) {
      this.openSidenavFlag = false;
    } 
    else {
      this.openSidenavFlag = true;
    }

  }

Plunker demo

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular 6 does not automatically include the X-XSRF-TOKEN header in its HTTP requests

Despite thoroughly reading the documentation and searching for answers on various platforms, I am still facing issues with Angular's XSRF mechanism. I cannot seem to get the X-XSRF-TOKEN header automatically appended when making a POST request. My si ...

Angular 9 is throwing an error that specifies that the options provided in the @ViewChild decorator must be in

After successfully upgrading my Angular project from version 8 to 9, I encountered an error when trying to run the project on localhost or build it. The error message states: ERROR in @ViewChild options must be an object literal The @ViewChild syntax that ...

Removing the final BR elements from a div using jQuery

Does anyone know how to remove only the last <BR> tag within a div using jQuery? The code I tried ends up removing all <BR> tags in the div instead of just the last one. HTML Code <div class="topic"> Lorem ipsum dolor sit amet, consect ...

What is the best way to align inline circles created with CSS vertically as the screen size changes?

Looking for resources on how to create circles using CSS? It can be a bit tricky, but here is the code you need: HTML <div id="wrapper"> <ul id="circles"> <li> <div class="circle"><div>K&l ...

Achieving perfect alignment for CSS heading and floated controls in the center of the page

I seem to encounter situations where I need inline-block elements on opposite ends of the same "line" while also needing them to be vertically aligned. Take a look at this example: http://jsfiddle.net/96DJv/4/ (focus on aligning the buttons with the headi ...

How to maintain HTML list items in a single line while overlaying text

I'm seeking assistance with understanding how to solve a particular issue. I have multiple list elements with varying lengths of text content, each flanked by small images/icons on either side. Below is an example of the HTML structure: .truncate ...

Issue with the influx of subscription requests

Currently, I am triggering a method on my component to fetch address information based on the postal code entered in the input field. Initially, the method loads the information into the getAddress$ variable and then subscribes to it to retrieve the data a ...

Tips for appending the id of an active element to a URL

My goal was to include the id value of the active element in a URL and then redirect to that URL with a button click. HTML <div class="icon" tabindex="0" id="company"> <img src="company.png"> </div> <button type="submit" onclick= ...

In order to execute the 'ng serve' command successfully, it is necessary to be within an Angular project. However, the system could

Attempting to set up a nrwl workspace project using nrwl cli, following the instructions provided on GitHub at the link below: https://github.com/onehungrymind/angular-core-workshop Encountering an error as depicted in the image below: https://i.sstatic ...

Troubleshooting the issue with the simple Angular material layout/flex example

I attempted to implement a basic layout/flex example from the Angular material design demo site: https://material.angularjs.org/latest/layout/container However, no matter what I try, the layout does not function as described and simply arranges the four ...

A way to navigate through a JSON result without the need for ngFor in Angular

Upon receiving JSON data from the backend, I am presented with the following result: [ { "status":"RUN", "numberOfTurbines":1 }, { "status":"ERROR", "numberOfTurbines&q ...

Bidirectional binding in Angular 2 Custom Directive

I've been working on a custom directive that automatically selects all options when the user chooses "All" from a dropdown. While I was able to get my custom directive to select all options, it doesn't update the model on the consuming component. ...

What is the best way to simulate the "window.screen.orientation.lock" function with Jest?

Within our hybrid app, we have implemented a cordova plugin to control screen orientation. The AppComponent contains code that manages the screen orientation locking using the window.screen.orientation.lock function. Is there a way to create a mock versi ...

The error code TS2345 indicates that the argument type 'Event' cannot be assigned to a parameter type 'string'

Hello, I'm a newcomer to utilizing Angular and I'm struggling to identify where my mistake lies. Below is the TypeScript code in question: import { Component } from '@angular/core'; @Component({ selector: 'app-root' ...

Inject a <div> element into a table row upon clicking a button using jQuery and PHP

I am faced with an issue regarding displaying pdf reports within specific table rows. The scenario involves a table containing folder paths and buttons in each row. Upon clicking the button, the path is sent to a sequence of PHP files (ajax.php, ajax2.php, ...

Is it possible to ensure uniformity in the appearance of all images with MVC Image Handling?

I'm currently developing an MVC 4 application that allows users to upload images. These uploaded images are then displayed in a different view. Users can upload images up to 10MB in size, which are then resized to 800 x ? pixels using the WebImage (Sy ...

Using Jquery to select and apply functions to specified div elements

As someone who is relatively new to JQuery, I have a question regarding the .on() function. Take this example: $('div').on('click', function() {$(this).toggleClass('show-description');}); This code selects all the 'div& ...

Angular 6.0.0 material ready for deployment

Currently, I am utilizing angular material 5, however, angular material 6 offers new features such as tree functionality. I am wondering if angular material is suitable for production use? ...

Resolving the problem of Turkish uppercase dotted i when using the capitalization pipe in Angular 2

I have created a custom capitalization pipe that successfully capitalizes almost all characters, including converting the Turkish 'ı' character into 'I'. However, I am facing an issue where the 'i' character is also being con ...

Develop a simulated version that does not include all the functionalities of the primary service

Let's imagine a scenario where there is an OriginalService class with various methods class OriginalService { method1() { } method2() { } method3() { } .. } Now, suppose we need to create a mock of OriginalService that will only be used with ...