mirror of
https://github.com/Redocly/redoc.git
synced 2025-06-06 14:03:13 +03:00
fix: handle scrollYOffset in ScrollService
This commit is contained in:
parent
39d1ac5f22
commit
34a8fe4906
|
@ -36,7 +36,7 @@ export class AppStore {
|
||||||
constructor(spec: OpenAPISpec, specUrl?: string, options: RedocRawOptions = {}) {
|
constructor(spec: OpenAPISpec, specUrl?: string, options: RedocRawOptions = {}) {
|
||||||
this.rawOptions = options;
|
this.rawOptions = options;
|
||||||
this.options = new RedocNormalizedOptions(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.spec = new SpecStore(spec, specUrl, this.options);
|
||||||
this.menu = new MenuStore(this.spec, this.scroll);
|
this.menu = new MenuStore(this.spec, this.scroll);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ import { debounce, bind } from 'decko';
|
||||||
import { EventEmitter } from 'eventemitter3';
|
import { EventEmitter } from 'eventemitter3';
|
||||||
|
|
||||||
import { querySelector, isBrowser } from '../utils';
|
import { querySelector, isBrowser } from '../utils';
|
||||||
|
import { RedocNormalizedOptions } from './RedocNormalizedOptions';
|
||||||
|
|
||||||
const EVENT = 'scroll';
|
const EVENT = 'scroll';
|
||||||
|
|
||||||
|
@ -9,7 +10,7 @@ export class ScrollService {
|
||||||
private _scrollParent: Window | HTMLElement | undefined;
|
private _scrollParent: Window | HTMLElement | undefined;
|
||||||
private _emiter: EventEmitter;
|
private _emiter: EventEmitter;
|
||||||
private _prevOffsetY: number = 0;
|
private _prevOffsetY: number = 0;
|
||||||
constructor() {
|
constructor(private options: RedocNormalizedOptions) {
|
||||||
this._scrollParent = isBrowser ? window : undefined;
|
this._scrollParent = isBrowser ? window : undefined;
|
||||||
this._emiter = new EventEmitter();
|
this._emiter = new EventEmitter();
|
||||||
this.bind();
|
this.bind();
|
||||||
|
@ -37,12 +38,12 @@ export class ScrollService {
|
||||||
|
|
||||||
isElementBellow(el: Element | null) {
|
isElementBellow(el: Element | null) {
|
||||||
if (el === null) return;
|
if (el === null) return;
|
||||||
return el.getBoundingClientRect().top > 0;
|
return el.getBoundingClientRect().top > this.options.scrollYOffset();
|
||||||
}
|
}
|
||||||
|
|
||||||
isElementAbove(el: Element | null) {
|
isElementAbove(el: Element | null) {
|
||||||
if (el === null) return;
|
if (el === null) return;
|
||||||
return Math.trunc(el.getBoundingClientRect().top) <= 0;
|
return Math.trunc(el.getBoundingClientRect().top) <= this.options.scrollYOffset();
|
||||||
}
|
}
|
||||||
|
|
||||||
subscribe(cb): () => void {
|
subscribe(cb): () => void {
|
||||||
|
@ -55,6 +56,9 @@ export class ScrollService {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
element.scrollIntoView();
|
element.scrollIntoView();
|
||||||
|
this._scrollParent &&
|
||||||
|
this._scrollParent.scrollBy &&
|
||||||
|
(this._scrollParent.scrollBy as any)(0, -this.options.scrollYOffset());
|
||||||
}
|
}
|
||||||
|
|
||||||
scrollIntoViewBySelector(selector: string) {
|
scrollIntoViewBySelector(selector: string) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user