From dcab77029f7070b7a72ba77ed500d5e22521b4a9 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Thu, 11 Jan 2018 18:08:33 +0200 Subject: [PATCH] fix: handle scrollYOffset in ScrollService --- src/services/AppStore.ts | 2 +- src/services/ScrollService.ts | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/services/AppStore.ts b/src/services/AppStore.ts index 9cc10e0df3..5d112f6599 100644 --- a/src/services/AppStore.ts +++ b/src/services/AppStore.ts @@ -36,7 +36,7 @@ export class AppStore { constructor(spec: OpenAPISpec, specUrl?: string, options: RedocRawOptions = {}) { this.rawOptions = options; this.options = new RedocNormalizedOptions(options); - this.scroll = new ScrollService(); + this.scroll = new ScrollService(this.options); this.spec = new SpecStore(spec, specUrl, this.options); this.menu = new MenuStore(this.spec, this.scroll); } diff --git a/src/services/ScrollService.ts b/src/services/ScrollService.ts index e9e5dc2c27..7e17a7aaf8 100644 --- a/src/services/ScrollService.ts +++ b/src/services/ScrollService.ts @@ -2,6 +2,7 @@ import { debounce, bind } from 'decko'; import { EventEmitter } from 'eventemitter3'; import { querySelector, isBrowser } from '../utils'; +import { RedocNormalizedOptions } from './RedocNormalizedOptions'; const EVENT = 'scroll'; @@ -9,7 +10,7 @@ export class ScrollService { private _scrollParent: Window | HTMLElement | undefined; private _emiter: EventEmitter; private _prevOffsetY: number = 0; - constructor() { + constructor(private options: RedocNormalizedOptions) { this._scrollParent = isBrowser ? window : undefined; this._emiter = new EventEmitter(); this.bind(); @@ -37,12 +38,12 @@ export class ScrollService { isElementBellow(el: Element | null) { if (el === null) return; - return el.getBoundingClientRect().top > 0; + return el.getBoundingClientRect().top > this.options.scrollYOffset(); } isElementAbove(el: Element | null) { if (el === null) return; - return Math.trunc(el.getBoundingClientRect().top) <= 0; + return Math.trunc(el.getBoundingClientRect().top) <= this.options.scrollYOffset(); } subscribe(cb): () => void { @@ -55,6 +56,9 @@ export class ScrollService { return; } element.scrollIntoView(); + this._scrollParent && + this._scrollParent.scrollBy && + this._scrollParent.scrollBy({ top: -this.options.scrollYOffset() }); } scrollIntoViewBySelector(selector: string) {