UNPKG

@hitachivantara/uikit-react-core

Version:
57 lines (56 loc) 2.17 kB
//#region src/utils/scroll.ts var getScrollTop = (c = window) => { if (c === null) return 0; if (c === window) return window.scrollY || window.pageYOffset || document.documentElement?.scrollTop || document.body.scrollTop; return c.scrollTop; }; var verticalScrollOffset = (t, c = window) => { if (c === window) return (t?.getBoundingClientRect?.().top || 0) + (window.scrollY || window.pageYOffset); if (getComputedStyle(c).position !== "static") return t.offsetTop; return t.offsetTop - c.offsetTop; }; var horizontalScrollOffset = (t, c = window) => { if (c === window) return (t?.getBoundingClientRect?.().left || 0) + (window.scrollX || window.pageXOffset); if (getComputedStyle(c).position !== "static") return t.offsetLeft; return t.offsetLeft - c.offsetLeft; }; var scrollElement = (element, container, offset = 0, direction) => { if (container === null) return; if (direction === "row") { const elemLeft = horizontalScrollOffset(element, container); container?.scrollTo?.({ left: elemLeft - offset, behavior: "smooth" }); } else { const elemTop = verticalScrollOffset(element, container); container?.scrollTo?.({ top: elemTop - offset, behavior: "smooth" }); } element.focus({ preventScroll: true }); }; var isScrolledToTheBottom = (container) => { if (container === null) return false; const containerScrollTop = getScrollTop(container); if (container === window) { const scrollHeight = document.documentElement?.scrollHeight || document.body.scrollHeight; return containerScrollTop + window.innerHeight >= scrollHeight; } return containerScrollTop + container.offsetHeight >= container.scrollHeight; }; var findFirstVisibleElement = (container, options, offset) => { if (container === null) return -1; const boundsTop = verticalScrollOffset(container); let i = 0; for (; i < options.length; i += 1) { const ele = document.getElementById(options[i].value); if (ele) { if (verticalScrollOffset(ele) - (options[i].offset || offset) > boundsTop) break; } } return i - 1; }; //#endregion export { findFirstVisibleElement, getScrollTop, isScrolledToTheBottom, scrollElement, verticalScrollOffset };