Skip to content

Commit dcab770

Browse files
committed
fix: handle scrollYOffset in ScrollService
1 parent 39d1ac5 commit dcab770

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/services/AppStore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class AppStore {
3636
constructor(spec: OpenAPISpec, specUrl?: string, options: RedocRawOptions = {}) {
3737
this.rawOptions = options;
3838
this.options = new RedocNormalizedOptions(options);
39-
this.scroll = new ScrollService();
39+
this.scroll = new ScrollService(this.options);
4040
this.spec = new SpecStore(spec, specUrl, this.options);
4141
this.menu = new MenuStore(this.spec, this.scroll);
4242
}

src/services/ScrollService.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import { debounce, bind } from 'decko';
22
import { EventEmitter } from 'eventemitter3';
33

44
import { querySelector, isBrowser } from '../utils';
5+
import { RedocNormalizedOptions } from './RedocNormalizedOptions';
56

67
const EVENT = 'scroll';
78

89
export class ScrollService {
910
private _scrollParent: Window | HTMLElement | undefined;
1011
private _emiter: EventEmitter;
1112
private _prevOffsetY: number = 0;
12-
constructor() {
13+
constructor(private options: RedocNormalizedOptions) {
1314
this._scrollParent = isBrowser ? window : undefined;
1415
this._emiter = new EventEmitter();
1516
this.bind();
@@ -37,12 +38,12 @@ export class ScrollService {
3738

3839
isElementBellow(el: Element | null) {
3940
if (el === null) return;
40-
return el.getBoundingClientRect().top > 0;
41+
return el.getBoundingClientRect().top > this.options.scrollYOffset();
4142
}
4243

4344
isElementAbove(el: Element | null) {
4445
if (el === null) return;
45-
return Math.trunc(el.getBoundingClientRect().top) <= 0;
46+
return Math.trunc(el.getBoundingClientRect().top) <= this.options.scrollYOffset();
4647
}
4748

4849
subscribe(cb): () => void {
@@ -55,6 +56,9 @@ export class ScrollService {
5556
return;
5657
}
5758
element.scrollIntoView();
59+
this._scrollParent &&
60+
this._scrollParent.scrollBy &&
61+
this._scrollParent.scrollBy({ top: -this.options.scrollYOffset() });
5862
}
5963

6064
scrollIntoViewBySelector(selector: string) {

0 commit comments

Comments
 (0)