Skip to content

Commit

Permalink
feat: add perfect-scrollbar for side menu
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Apr 23, 2017
1 parent 8a49fb3 commit cdeee67
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/components/Redoc/redoc.scss
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
}

side-menu {
overflow-y: auto;
overflow: hidden;
}

[sticky-sidebar] {
Expand Down
2 changes: 1 addition & 1 deletion lib/components/SideMenu/side-menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<side-menu-items [items]="menuItems" (activate)="activateAndScroll($event)"></side-menu-items>
</ng-template>

<div #desktop id="resources-nav">
<div #desktop id="resources-nav" perfect-scrollbar>
<ul class="menu-root">
<div *ngIf="itemsTemplate; else default">
<ng-container *ngTemplateOutlet="itemsTemplate; context: this"></ng-container>
Expand Down
10 changes: 9 additions & 1 deletion lib/components/SideMenu/side-menu.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
$mobile-menu-compact-breakpoint: 550px;

:host {
display: block;
display: flex;
box-sizing: border-box;
}

#resources-nav {
position: relative;
}

ul.menu-root {
margin: 0;
padding: 0;
Expand Down Expand Up @@ -35,6 +39,10 @@ ul.menu-root {
}

@media (max-width: $side-menu-mobile-breakpoint) {
:host {
display: block;
}

.mobile-nav {
display: block;
}
Expand Down
14 changes: 11 additions & 3 deletions lib/components/SideMenu/side-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { Component,
Output,
ElementRef,
ChangeDetectorRef,
ViewChild,
OnInit,
OnDestroy
} from '@angular/core';

import { trigger, state, animate, transition, style } from '@angular/core';
import { ScrollService, MenuService, OptionsService, MenuItem } from '../../services/';
import { PerfectScrollbar } from '../../shared/components';
import { BrowserDomAdapter as DOM } from '../../utils/browser-adapter';

const global = window;
Expand Down Expand Up @@ -51,6 +53,7 @@ export class SideMenu implements OnInit, OnDestroy {
activeItemCaption: string;
menuItems: Array<MenuItem>;
@Input() itemsTemplate;
@ViewChild(PerfectScrollbar) PS:PerfectScrollbar;

private options: any;
private $element: any;
Expand All @@ -77,7 +80,7 @@ export class SideMenu implements OnInit, OnDestroy {

this.changedActiveSubscription = this.menuService.changedActiveItem.subscribe((evt) => this.changed(evt));
this.changedSubscription = this.menuService.changed.subscribe((evt) => {
this.detectorRef.detectChanges()
this.update();
});
}

Expand All @@ -95,11 +98,16 @@ export class SideMenu implements OnInit, OnDestroy {
this.activeItemCaption = '';
}

//safari doesn't update bindings if not run changeDetector manually :(
this.detectorRef.detectChanges();
// safari doesn't update bindings if not run changeDetector manually :(
this.update();
this.scrollActiveIntoView();
}

update() {
this.detectorRef.detectChanges();
this.PS && this.PS.update();
}

scrollActiveIntoView() {
let $item = this.$element.querySelector('li.active, label.active');
if ($item) $item.scrollIntoViewIfNeeded();
Expand Down
37 changes: 37 additions & 0 deletions lib/shared/components/PerfectScrollbar/perfect-scrollbar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

import 'perfect-scrollbar/dist/css/perfect-scrollbar.css';

import { Directive, ElementRef, Input, OnInit, OnDestroy } from '@angular/core';
import { BrowserDomAdapter as DOM } from '../../../utils/browser-adapter';

import * as PS from 'perfect-scrollbar';

@Directive({
selector: '[perfect-scrollbar]'
})
export class PerfectScrollbar implements OnInit, OnDestroy {
$element: any;
subscription: any;

constructor(elementRef:ElementRef) {
this.$element = elementRef.nativeElement;
}

update() {
PS.update(this.$element);
}

ngOnInit() {
requestAnimationFrame(() => PS.initialize(this.$element, {
wheelSpeed: 2,
wheelPropagation: false,
minScrollbarLength: 20,
suppressScrollX: true
}));
}

ngOnDestroy() {
PS.destroy(this.$element);
}
}
5 changes: 3 additions & 2 deletions lib/shared/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { CopyButton } from './CopyButton/copy-button.directive';
import { SelectOnClick } from './SelectOnClick/select-on-click.directive';
import { DynamicNg2Viewer, DynamicNg2Wrapper } from './DynamicNg2Viewer/dynamic-ng2-viewer.component';
import { LazyFor, LazyTasksService, LazyTasksServiceSync } from './LazyFor/lazy-for';
import { PerfectScrollbar } from './PerfectScrollbar/perfect-scrollbar';

export const REDOC_COMMON_DIRECTIVES = [
DropDown, StickySidebar, Tabs, Tab, Zippy, CopyButton, SelectOnClick, DynamicNg2Viewer, DynamicNg2Wrapper, LazyFor
PerfectScrollbar, DropDown, StickySidebar, Tabs, Tab, Zippy, CopyButton, SelectOnClick, DynamicNg2Viewer, DynamicNg2Wrapper, LazyFor
];

export { DropDown, StickySidebar, Tabs, Tab, Zippy, CopyButton, SelectOnClick, DynamicNg2Viewer, DynamicNg2Wrapper, LazyFor }
export { LazyTasksService, LazyTasksServiceSync }
export { LazyTasksService, LazyTasksServiceSync, PerfectScrollbar }

0 comments on commit cdeee67

Please sign in to comment.