UNPKG

@dialpad/dialtone

Version:

Dialpad's Dialtone design system monorepo

1 lines 5.68 kB
{"version":3,"file":"dom.cjs","names":[],"sources":["../../../common/mixins/dom.js"],"sourcesContent":["export default {\n methods: {\n /**\n * Scroll an element into view if it is not fully visible in its nearest scrollable ancestor.\n * @param {Element} ref\n */\n scrollElementIntoViewIfNeeded (ref, opt_center, opt_behavior, opt_parent) {\n if (ref.scrollIntoViewIfNeeded) {\n this.scrollIntoViewIfNeeded(ref, opt_center, opt_behavior, opt_parent);\n } else {\n this.scrollIntoView(ref, 'bottom', false, opt_behavior, opt_parent);\n }\n },\n\n /**\n * Scroll an element to the top or bottom of its scroll ancestor.\n * @param {Element} ref\n */\n scrollElementIntoView (ref, opt_scrollToTop, opt_behavior, opt_parent) {\n if (opt_scrollToTop === 'center') {\n this.scrollIntoView(ref, 'center', false, opt_behavior, opt_parent);\n return;\n }\n\n if (opt_scrollToTop === false) {\n this.scrollIntoView(ref, 'bottom', false, opt_behavior, opt_parent);\n } else {\n this.scrollIntoView(ref, 'top', false, opt_behavior, opt_parent);\n }\n },\n\n scrollIntoViewIfNeeded (ref, opt_center, opt_behavior, opt_parent) {\n const dir = opt_center ? 'center' : undefined;\n this.scrollIntoView(ref, dir, true, opt_behavior, opt_parent);\n },\n\n scrollIntoView (ref, opt_dir, opt_ifNeeded, opt_behavior, opt_parent) {\n if (!ref || !ref.parentElement) {\n return;\n }\n const offsetTop = ref.offsetTop;\n const refParent = opt_parent || ref.parentElement;\n const scrollBounds = this._getScrollBounds(refParent);\n const parentHeight = scrollBounds.bottom - scrollBounds.top;\n const elHeight = this._getElementHeight(ref);\n const offsetBottom = offsetTop + elHeight;\n let scrollTop = -1;\n switch (opt_dir) {\n case 'top':\n scrollTop = offsetTop;\n break;\n case 'center':\n scrollTop = offsetTop + (elHeight - parentHeight) / 2;\n break;\n case 'bottom':\n scrollTop = scrollTop = offsetBottom - parentHeight;\n break;\n default:\n // Go to the closest edge if needed and no direction is provided.\n if (offsetTop - scrollBounds.top <= (parentHeight / 2)) {\n // Lock it to the top edge.\n scrollTop = offsetTop;\n } else {\n scrollTop = offsetBottom - parentHeight;\n }\n break;\n }\n this._setScrollTop(refParent, scrollTop, scrollBounds, offsetTop, offsetBottom, opt_ifNeeded, opt_behavior);\n },\n\n _setScrollTop (el, scrollTop, bounds, offsetTop, offsetBottom, opt_isNeeded, opt_behavior) {\n bounds = bounds || this._getScrollBounds(el);\n const parentHeight = bounds.bottom - bounds.top;\n if (opt_isNeeded && this._inScrollBounds(offsetTop, offsetBottom, bounds)) {\n // Then lock to the top or bottom if it's hanging off the edge.\n if (offsetTop < bounds.top) {\n // Snap it to the top.\n scrollTop = offsetTop;\n } else if (offsetBottom > bounds.bottom) {\n scrollTop = offsetBottom - parentHeight;\n } else {\n return;\n }\n }\n\n if (opt_behavior) {\n el.scrollTo({ top: scrollTop, behavior: opt_behavior });\n } else {\n el.scrollTop = scrollTop;\n }\n },\n\n _getElementHeight (el) {\n return el.getBoundingClientRect().height;\n },\n\n _getScrollBounds (el) {\n const height = this._getElementHeight(el);\n const scrollTop = el.scrollTop;\n return {\n top: scrollTop,\n bottom: scrollTop + height,\n };\n },\n\n _inScrollBounds (top, bottom, bounds) {\n // Since we read from top to bottom, we want more than 3/4 to be visible at the top\n // (bc you're looking at the bottom half) or a quarter visible at the bottom (bc you\n // can read the beginning of it)\n\n const height = bottom - top;\n return (bottom <= bounds.bottom + (3 * height / 4)) && (top >= bounds.top - (height / 4));\n },\n },\n};\n"],"mappings":"+FAAA,IAAA,EAAe,CACb,QAAS,CAKP,8BAA+B,EAAK,EAAY,EAAc,EAAY,CACpE,EAAI,uBACN,KAAK,uBAAuB,EAAK,EAAY,EAAc,EAAW,CAEtE,KAAK,eAAe,EAAK,SAAU,GAAO,EAAc,EAAW,EAQvE,sBAAuB,EAAK,EAAiB,EAAc,EAAY,CACrE,GAAI,IAAoB,SAAU,CAChC,KAAK,eAAe,EAAK,SAAU,GAAO,EAAc,EAAW,CACnE,OAG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