UNPKG

@hitachivantara/uikit-react-core

Version:

Core React components for the NEXT Design System.

83 lines (82 loc) 2.61 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const 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; }; const 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; }; const 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; }; const 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 }); }; const 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; }; const 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) { const elemTop = verticalScrollOffset(ele) - (options[i].offset || offset); if (elemTop > boundsTop) { break; } } } return i - 1; }; exports.findFirstVisibleElement = findFirstVisibleElement; exports.getScrollTop = getScrollTop; exports.horizontalScrollOffset = horizontalScrollOffset; exports.isScrolledToTheBottom = isScrolledToTheBottom; exports.scrollElement = scrollElement; exports.verticalScrollOffset = verticalScrollOffset;