GithubHelp home page GithubHelp logo

yuvalsuede / sidenav-example Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 2.0 910 KB

Angular4 sidebar example using Component interaction & Animations

TypeScript 91.85% JavaScript 5.61% HTML 1.12% CSS 1.42%

sidenav-example's Introduction

Sidebar Navigation

An Angular4 Sidebar Navigation using Angular Animations & Service[]: Reference-style:

alt text

Components

First define AppComponent

@Component({
  selector: 'app-root',
  template: `
    <div class="page-container">
      <div class="app-ui">
        <sidebar>
        </sidebar>
        <div id="app-main">
          <top-bar></top-bar>
        </div>

      </div>
    </div>
  `
})
export class AppComponent {

}

Now lets define a menu management service that manages our toggle state using Rx Observable

@Injectable()
export class MenuService {

  private open: boolean = false;

  private subject: Subject<boolean> = new Subject<boolean>();
  constructor() { }

  setToggleMenu(): void {
    this.open = !this.open;
    this.subject.next(this.open);
  }

  getToggleMenu(): Observable<any> {
    return this.subject.asObservable();
  }
}

Create a SideBar Component with Angular animations and state management, using @HostBinding

@Component({
    selector: 'sidebar',
    host: {

    },
    template: `
      <div class="sidebar-inner"></div>
    `
    ...
    
  animations: [
    trigger('toggleMenu', [
      state('open', style({
        'margin-left': '0',
      })),
      state('close', style({
        'margin-left': '-244px',
      })),
      transition('open <=> close', animate('250ms ease-out')),
    ]),
  ]
})
export class SidebarComponent implements OnInit {
    constructor(private ms: MenuService) { }
    state: string = 'close';

    @HostBinding('@toggleMenu') get toggleState() {
      return this.state;
    }

    ngOnInit() {
      this.ms.getToggleMenu().subscribe((open: boolean) => {

        if (open) this.state = 'open';
        if (!open) this.state = 'close';

      });
    }


}

Create Top Bar Component and inject our Menu Service, on button click use toggle() for changing state.

@Component({
    selector: 'top-bar',
    template: `
      <button type="button" class="btn btn-default" aria-label="Left Align" (click)="toggle()">
        <span class="glyphicon glyphicon-th" aria-hidden="true"></span>
      </button>

    `
})
export class TopbarComponent implements OnInit {

  constructor(private ms:MenuService) { }

  ngOnInit() { }

  toggle() {

    this.ms.setToggleMenu();

  }

}

Development server

Run ng serve for a dev server. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.

Build

Run ng build to build the project. The build artifacts will be stored in the dist/ directory. Use the -prod flag for a production build.

sidenav-example's People

Contributors

yuvalsuede avatar angular-cli avatar

Stargazers

MATT avatar Misikir Adane avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.