mirror of
https://github.com/Redocly/redoc.git
synced 2025-08-06 05:10:20 +03:00
Break out scrollIntoViewIfNeeded into freely usable function
This commit is contained in:
parent
3d11a80c9c
commit
86cef81e9c
|
@ -26,11 +26,11 @@ export function html2Str(html: string): string {
|
|||
|
||||
// scrollIntoViewIfNeeded polyfill
|
||||
|
||||
if (typeof Element !== 'undefined' && !(Element as any).prototype.scrollIntoViewIfNeeded) {
|
||||
(Element as any).prototype.scrollIntoViewIfNeeded = function (centerIfNeeded) {
|
||||
centerIfNeeded = arguments.length === 0 ? true : !!centerIfNeeded;
|
||||
|
||||
const parent = this.parentNode;
|
||||
export function scrollIntoViewIfNeeded(el: HTMLElement, centerIfNeeded = true) {
|
||||
const parent = el.parentNode as HTMLElement | null;
|
||||
if (!parent) {
|
||||
return;
|
||||
}
|
||||
const parentComputedStyle = window.getComputedStyle(parent, undefined);
|
||||
const parentBorderTopWidth = parseInt(
|
||||
parentComputedStyle.getPropertyValue('border-top-width'),
|
||||
|
@ -40,36 +40,41 @@ if (typeof Element !== 'undefined' && !(Element as any).prototype.scrollIntoView
|
|||
parentComputedStyle.getPropertyValue('border-left-width'),
|
||||
10,
|
||||
);
|
||||
const overTop = this.offsetTop - parent.offsetTop < parent.scrollTop;
|
||||
const overTop = el.offsetTop - parent.offsetTop < parent.scrollTop;
|
||||
const overBottom =
|
||||
this.offsetTop - parent.offsetTop + this.clientHeight - parentBorderTopWidth >
|
||||
el.offsetTop - parent.offsetTop + el.clientHeight - parentBorderTopWidth >
|
||||
parent.scrollTop + parent.clientHeight;
|
||||
const overLeft = this.offsetLeft - parent.offsetLeft < parent.scrollLeft;
|
||||
const overLeft = el.offsetLeft - parent.offsetLeft < parent.scrollLeft;
|
||||
const overRight =
|
||||
this.offsetLeft - parent.offsetLeft + this.clientWidth - parentBorderLeftWidth >
|
||||
el.offsetLeft - parent.offsetLeft + el.clientWidth - parentBorderLeftWidth >
|
||||
parent.scrollLeft + parent.clientWidth;
|
||||
const alignWithTop = overTop && !overBottom;
|
||||
|
||||
if ((overTop || overBottom) && centerIfNeeded) {
|
||||
parent.scrollTop =
|
||||
this.offsetTop -
|
||||
el.offsetTop -
|
||||
parent.offsetTop -
|
||||
parent.clientHeight / 2 -
|
||||
parentBorderTopWidth +
|
||||
this.clientHeight / 2;
|
||||
el.clientHeight / 2;
|
||||
}
|
||||
|
||||
if ((overLeft || overRight) && centerIfNeeded) {
|
||||
parent.scrollLeft =
|
||||
this.offsetLeft -
|
||||
el.offsetLeft -
|
||||
parent.offsetLeft -
|
||||
parent.clientWidth / 2 -
|
||||
parentBorderLeftWidth +
|
||||
this.clientWidth / 2;
|
||||
el.clientWidth / 2;
|
||||
}
|
||||
|
||||
if ((overTop || overBottom || overLeft || overRight) && !centerIfNeeded) {
|
||||
this.scrollIntoView(alignWithTop);
|
||||
el.scrollIntoView(alignWithTop);
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof Element !== 'undefined' && !(Element as any).prototype.scrollIntoViewIfNeeded) {
|
||||
(Element as any).prototype.scrollIntoViewIfNeeded = function (centerIfNeeded = true) {
|
||||
scrollIntoViewIfNeeded(this, centerIfNeeded);
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user