@dialpad/dialtone
Version:
Dialpad's Dialtone design system monorepo
78 lines (77 loc) • 2.23 kB
JavaScript
const I = {
methods: {
/**
* Scroll an element into view if it is not fully visible in its nearest scrollable ancestor.
* @param {Element} ref
*/
scrollElementIntoViewIfNeeded(e, l, t, o) {
e.scrollIntoViewIfNeeded ? this.scrollIntoViewIfNeeded(e, l, t, o) : this.scrollIntoView(e, "bottom", !1, t, o);
},
/**
* Scroll an element to the top or bottom of its scroll ancestor.
* @param {Element} ref
*/
scrollElementIntoView(e, l, t, o) {
if (l === "center") {
this.scrollIntoView(e, "center", !1, t, o);
return;
}
l === !1 ? this.scrollIntoView(e, "bottom", !1, t, o) : this.scrollIntoView(e, "top", !1, t, o);
},
scrollIntoViewIfNeeded(e, l, t, o) {
const n = l ? "center" : void 0;
this.scrollIntoView(e, n, !0, t, o);
},
scrollIntoView(e, l, t, o, n) {
if (!e || !e.parentElement)
return;
const s = e.offsetTop, r = n || e.parentElement, c = this._getScrollBounds(r), h = c.bottom - c.top, m = this._getElementHeight(e), g = s + m;
let i = -1;
switch (l) {
case "top":
i = s;
break;
case "center":
i = s + (m - h) / 2;
break;
case "bottom":
i = i = g - h;
break;
default:
s - c.top <= h / 2 ? i = s : i = g - h;
break;
}
this._setScrollTop(r, i, c, s, g, t, o);
},
_setScrollTop(e, l, t, o, n, s, r) {
t = t || this._getScrollBounds(e);
const c = t.bottom - t.top;
if (s && this._inScrollBounds(o, n, t))
if (o < t.top)
l = o;
else if (n > t.bottom)
l = n - c;
else
return;
r ? e.scrollTo({ top: l, behavior: r }) : e.scrollTop = l;
},
_getElementHeight(e) {
return e.getBoundingClientRect().height;
},
_getScrollBounds(e) {
const l = this._getElementHeight(e), t = e.scrollTop;
return {
top: t,
bottom: t + l
};
},
_inScrollBounds(e, l, t) {
const o = l - e;
return l <= t.bottom + 3 * o / 4 && e >= t.top - o / 4;
}
}
};
export {
I as default
};
//# sourceMappingURL=dom.js.map