@dialpad/dialtone
Version:
Dialpad's Dialtone design system monorepo
1 lines • 6.17 kB
Source Map (JSON)
{"version":3,"file":"dom.cjs","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"],"names":[],"mappings":";;AAAA,MAAe,MAAA;AAAA,EACb,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA,IAKP,8BAA+B,KAAK,YAAY,cAAc,YAAY;AACxE,UAAI,IAAI,wBAAwB;AAC9B,aAAK,uBAAuB,KAAK,YAAY,cAAc,UAAU;AAAA,MAC7E,OAAa;AACL,aAAK,eAAe,KAAK,UAAU,OAAO,cAAc,UAAU;AAAA,MACnE;AAAA,IACF;AAAA;AAAA;AAAA;AAAA;AA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