Skip to content

Commit

Permalink
feat: add native-scrollbars option to workaround scrolling perf issues
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Sep 21, 2017
1 parent d8b6e02 commit f2ed92c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ ReDoc makes use of the following [vendor extensions](http://swagger.io/specifica
* `no-auto-auth` - do not inject Authentication section automatically
* `path-in-middle-panel` - show path link and HTTP verb in the middle panel instead of the right one
* `hide-loading` - do not show loading animation. Useful for small docs
* `native-scrollbars` - use native scrollbar for sidemenu instead of perfect-scroll (scrolling performance optimization for big specs)

## Advanced usage
Instead of adding `spec-url` attribute to the `<redoc>` element you can initialize ReDoc via globally exposed `Redoc` object:
Expand Down
1 change: 1 addition & 0 deletions lib/components/SideMenu/side-menu.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ $mobile-menu-compact-breakpoint: 550px;
#resources-nav {
position: relative;
width: 100%;
overflow: scroll;
}

ul.menu-root {
Expand Down
6 changes: 5 additions & 1 deletion lib/services/options.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const OPTION_NAMES = new Set([
'untrustedSpec',
'hideLoading',
'ignoredHeaderParameters',
'nativeScrollbars',
]);

export interface Options {
Expand All @@ -40,6 +41,7 @@ export interface Options {
hideLoading?: boolean;
spec?: any;
ignoredHeaderParameters?: string[];
nativeScrollbars?: boolean;
}

@Injectable()
Expand Down Expand Up @@ -79,7 +81,7 @@ export class OptionsService {
this._normalizeOptions();
}

_normalizeOptions():void {
_normalizeOptions(): void {
// modify scrollYOffset to always be a function
if (!isFunction(this._options.scrollYOffset)) {
if (isFinite(this._options.scrollYOffset)) {
Expand Down Expand Up @@ -109,6 +111,8 @@ export class OptionsService {
if (isString(this._options.pathInMiddlePanel)) this._options.pathInMiddlePanel = true;
if (isString(this._options.untrustedSpec)) this._options.untrustedSpec = true;
if (isString(this._options.hideLoading)) this._options.hideLoading = true;
if (isString(this._options.nativeScrollbars))
this._options.nativeScrollbars = true;
if (isString(this._options.expandResponses)) {
let str = this._options.expandResponses as string;
if (str === 'all') return;
Expand Down
38 changes: 25 additions & 13 deletions lib/shared/components/PerfectScrollbar/perfect-scrollbar.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
'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 { Directive, ElementRef, OnDestroy, OnInit } from '@angular/core';
import * as PS from 'perfect-scrollbar';

import { OptionsService } from '../../../services/options.service';

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

constructor(elementRef:ElementRef) {
constructor(elementRef: ElementRef, optionsService: OptionsService) {
this.$element = elementRef.nativeElement;
this.enabled = !optionsService.options.nativeScrollbars;
}

update() {
if (!this.enabled) return;
PS.update(this.$element);
}

ngOnInit() {
requestAnimationFrame(() => PS.initialize(this.$element, {
wheelSpeed: 2,
wheelPropagation: false,
minScrollbarLength: 20,
suppressScrollX: true
}));
if (!this.enabled) return;
requestAnimationFrame(() =>
PS.initialize(this.$element, {
wheelSpeed: 2,
handlers: [
'click-rail',
'drag-scrollbar',
'keyboard',
'wheel',
'touch',
],
wheelPropagation: true,
minScrollbarLength: 20,
suppressScrollX: true,
} as any),
);
}

ngOnDestroy() {
if (!this.enabled) return;
PS.destroy(this.$element);
}
}

0 comments on commit f2ed92c

Please sign in to comment.