bonsai-analyzer
Version:
Trim your dependency tree.
98 lines (77 loc) • 2.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = scrollToAndFocus;
exports.scrollTo = scrollTo;
exports.scrollToElem = scrollToElem;
function scrollToAndFocus(target) {
const elem = scrollTo(target);
if (elem) {
elem.focus();
}
}
function scrollTo(target) {
if (target instanceof HTMLElement) {
scrollToElem(target);
return target;
} else {
const elem = document.getElementById(target);
if (elem) {
scrollToElem(elem);
return elem;
}
}
return null;
}
function getScrollParent(element, includeHidden = false) {
var style = getComputedStyle(element);
var excludeStaticParent = style.position === 'absolute';
var overflowRegex = includeHidden ? /(auto|scroll|hidden)/ : /(auto|scroll)/;
if (style.position === 'fixed') {
return document.body;
} // eslint-disable-next-line no-cond-assign
for (var parent = element; parent = parent.parentElement;) {
style = getComputedStyle(parent);
if (excludeStaticParent && style.position === 'static') {
continue;
}
if (overflowRegex.test(style.overflow + style.overflowY + style.overflowX)) {
return parent instanceof HTMLElement ? parent : null;
}
}
return document.body;
}
function findPos(obj) {
let curtop = 0;
let elem = obj;
if (elem.offsetParent) {
do {
if (elem instanceof HTMLElement) {
curtop += elem.offsetTop;
}
elem = elem.offsetParent;
} while (elem);
return curtop;
}
return elem.offsetTop;
}
function isWithinViewport(viewportTop, viewportHeight, targetTop, targetHeight) {
return viewportTop < targetTop && targetTop < viewportTop + viewportHeight - targetHeight;
}
function scrollToElem(elem) {
const left = 0;
const top = findPos(elem);
const scrollParent = getScrollParent(elem);
if (scrollParent) {
if (scrollParent === document.body) {
if (!isWithinViewport(window.scrollY, window.innerHeight, top, elem.offsetHeight)) {
window.scroll(left, top);
}
} else {
if (!isWithinViewport(scrollParent.scrollTop, scrollParent.offsetHeight, top, elem.offsetHeight)) {
scrollParent.scrollTop = top;
}
}
}
}