Skip to content

Commit

Permalink
fix: handle scrollYOffset in ScrollService
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Jan 11, 2018
1 parent 39d1ac5 commit dcab770
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/services/AppStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
10 changes: 7 additions & 3 deletions src/services/ScrollService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { debounce, bind } from 'decko';
import { EventEmitter } from 'eventemitter3';

import { querySelector, isBrowser } from '../utils';
import { RedocNormalizedOptions } from './RedocNormalizedOptions';

const EVENT = 'scroll';

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();
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down

0 comments on commit dcab770

Please sign in to comment.