Saad Bashir
Saad Bashir

Reputation: 4519

Angular - Sidebar Navigation

Instead of the conventional bootstrap slide down navigation on collapse, I am trying to create a slide in from left navigation.

HTML for toggle button

<button id="collapseBtn" class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" (click)="toggleSideBar()" >
Toggle</button>

HTML for sidebar

<div class="sidebar">
  <ul>
      <li>S-Dashboard</li>
      <li>S-Voucher</li>
  </ul>
</div>

CSS for sidebar

.sidebar {
   background: #1a2580;
   color: white;
   height: 100%;
   width: 0;
   position: fixed; 
   z-index: 1;
   top: 0;
   left: 0;
   overflow-x: hidden;
   padding-top: 60px;
   transition: 0.5s; 
}
.showsidebar {
   width: 250px;
}
.hidesidebar {
   width: 0px;
}

ToggleSideBar function in component file

toggleSideBar() {
   document.getElementsByClassName('sidebar')[0].classList.add('showsidebar');
   this.sideBarOpen = true;
}

Clicking the toggle button slides out the navigation as wanted however it doesn't push the content to the right. And as a result the content gets hidden underneath the sidebar.

How to push the content to the right?

Secondly, I tried using HostListener to close the sidebar when clicked anywhere on the page but that doesn't seem to work.

StackBlitz Link

Upvotes: 1

Views: 14083

Answers (1)

Dimitar Spassov
Dimitar Spassov

Reputation: 2733

You can toggle a class on the body element which should be responsible for moving (pushing) the whole page content when you toggle the side menu.

styles.css

body { 
  position: relative;
  overflow: hidden;
  transition: left 0.5s ease; /* Animation styles are just for demo */
  left:0;
}

body.push {
  left:250px;
}

and in your component:

import { Component, OnInit, HostListener, ElementRef } from '@angular/core';


@Component({
  selector: 'app-topnav',
  templateUrl: './topnav.component.html',
  styleUrls: ['./topnav.component.css']
})
export class TopnavComponent implements OnInit {

  isMenuSmall:boolean = true;
  sideBarOpen: boolean = false;
  constructor(private el:ElementRef) { }

 // Your initial click listener on the host element
 @HostListener('click', ['$event'])onClick(event) {
      event.stopPropagation();
   if (event.target.id == "collapseBtn") {
      document.getElementsByClassName('sidebar')[0].classList.add('showsidebar');
      document.body.classList.add('push');
      this.sideBarOpen = true;
   } else {
    if (this.sideBarOpen) {
       document.getElementsByClassName('sidebar')[0].classList.remove('showsidebar');
       document.body.classList.remove('push');
       this.sideBarOpen = false;
    }
   }
 }

  // Click listener on the window object to handle clicks anywhere on 
  // the screen.
  @HostListener('window:click', ['$event']) onOutsideClick(event){
    if(this.sideBarOpen && !this.el.nativeElement.contains(event.target)){
      this.sideBarOpen=false;
      document.getElementsByClassName('sidebar')[0].classList.remove('showsidebar');
      document.body.classList.remove('push');
    }
  }

  ngOnInit() {
  }
  toggleSideBar() {
  }

}

Of course, the code above can be enhanced additionally, but I hope it shows you an approach for what you are trying to achieve.

Upvotes: 2

Related Questions