Skip to content

Commit cdeee67

Browse files
committed
feat: add perfect-scrollbar for side menu
1 parent 8a49fb3 commit cdeee67

File tree

6 files changed

+62
-8
lines changed

6 files changed

+62
-8
lines changed

lib/components/Redoc/redoc.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
}
4646

4747
side-menu {
48-
overflow-y: auto;
48+
overflow: hidden;
4949
}
5050

5151
[sticky-sidebar] {

lib/components/SideMenu/side-menu.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<side-menu-items [items]="menuItems" (activate)="activateAndScroll($event)"></side-menu-items>
99
</ng-template>
1010

11-
<div #desktop id="resources-nav">
11+
<div #desktop id="resources-nav" perfect-scrollbar>
1212
<ul class="menu-root">
1313
<div *ngIf="itemsTemplate; else default">
1414
<ng-container *ngTemplateOutlet="itemsTemplate; context: this"></ng-container>

lib/components/SideMenu/side-menu.scss

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
$mobile-menu-compact-breakpoint: 550px;
33

44
:host {
5-
display: block;
5+
display: flex;
66
box-sizing: border-box;
77
}
88

9+
#resources-nav {
10+
position: relative;
11+
}
12+
913
ul.menu-root {
1014
margin: 0;
1115
padding: 0;
@@ -35,6 +39,10 @@ ul.menu-root {
3539
}
3640

3741
@media (max-width: $side-menu-mobile-breakpoint) {
42+
:host {
43+
display: block;
44+
}
45+
3846
.mobile-nav {
3947
display: block;
4048
}

lib/components/SideMenu/side-menu.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import { Component,
66
Output,
77
ElementRef,
88
ChangeDetectorRef,
9+
ViewChild,
910
OnInit,
1011
OnDestroy
1112
} from '@angular/core';
1213

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

1719
const global = window;
@@ -51,6 +53,7 @@ export class SideMenu implements OnInit, OnDestroy {
5153
activeItemCaption: string;
5254
menuItems: Array<MenuItem>;
5355
@Input() itemsTemplate;
56+
@ViewChild(PerfectScrollbar) PS:PerfectScrollbar;
5457

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

7881
this.changedActiveSubscription = this.menuService.changedActiveItem.subscribe((evt) => this.changed(evt));
7982
this.changedSubscription = this.menuService.changed.subscribe((evt) => {
80-
this.detectorRef.detectChanges()
83+
this.update();
8184
});
8285
}
8386

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

98-
//safari doesn't update bindings if not run changeDetector manually :(
99-
this.detectorRef.detectChanges();
101+
// safari doesn't update bindings if not run changeDetector manually :(
102+
this.update();
100103
this.scrollActiveIntoView();
101104
}
102105

106+
update() {
107+
this.detectorRef.detectChanges();
108+
this.PS && this.PS.update();
109+
}
110+
103111
scrollActiveIntoView() {
104112
let $item = this.$element.querySelector('li.active, label.active');
105113
if ($item) $item.scrollIntoViewIfNeeded();
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
import 'perfect-scrollbar/dist/css/perfect-scrollbar.css';
4+
5+
import { Directive, ElementRef, Input, OnInit, OnDestroy } from '@angular/core';
6+
import { BrowserDomAdapter as DOM } from '../../../utils/browser-adapter';
7+
8+
import * as PS from 'perfect-scrollbar';
9+
10+
@Directive({
11+
selector: '[perfect-scrollbar]'
12+
})
13+
export class PerfectScrollbar implements OnInit, OnDestroy {
14+
$element: any;
15+
subscription: any;
16+
17+
constructor(elementRef:ElementRef) {
18+
this.$element = elementRef.nativeElement;
19+
}
20+
21+
update() {
22+
PS.update(this.$element);
23+
}
24+
25+
ngOnInit() {
26+
requestAnimationFrame(() => PS.initialize(this.$element, {
27+
wheelSpeed: 2,
28+
wheelPropagation: false,
29+
minScrollbarLength: 20,
30+
suppressScrollX: true
31+
}));
32+
}
33+
34+
ngOnDestroy() {
35+
PS.destroy(this.$element);
36+
}
37+
}

lib/shared/components/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import { CopyButton } from './CopyButton/copy-button.directive';
77
import { SelectOnClick } from './SelectOnClick/select-on-click.directive';
88
import { DynamicNg2Viewer, DynamicNg2Wrapper } from './DynamicNg2Viewer/dynamic-ng2-viewer.component';
99
import { LazyFor, LazyTasksService, LazyTasksServiceSync } from './LazyFor/lazy-for';
10+
import { PerfectScrollbar } from './PerfectScrollbar/perfect-scrollbar';
1011

1112
export const REDOC_COMMON_DIRECTIVES = [
12-
DropDown, StickySidebar, Tabs, Tab, Zippy, CopyButton, SelectOnClick, DynamicNg2Viewer, DynamicNg2Wrapper, LazyFor
13+
PerfectScrollbar, DropDown, StickySidebar, Tabs, Tab, Zippy, CopyButton, SelectOnClick, DynamicNg2Viewer, DynamicNg2Wrapper, LazyFor
1314
];
1415

1516
export { DropDown, StickySidebar, Tabs, Tab, Zippy, CopyButton, SelectOnClick, DynamicNg2Viewer, DynamicNg2Wrapper, LazyFor }
16-
export { LazyTasksService, LazyTasksServiceSync }
17+
export { LazyTasksService, LazyTasksServiceSync, PerfectScrollbar }

0 commit comments

Comments
 (0)