UNPKG

vuestic-ui

Version:
32 lines (31 loc) 1.15 kB
import { shallowRef, ref, onMounted, onBeforeUnmount } from "vue"; function getTargetElement(target) { if (!target) { throw new Error("No target was provided for `useScroll` hook!"); } return typeof target === "string" ? document.querySelector(target) : target; } function setupScroll(fixed, target) { const scrollRoot = shallowRef(); let targetElement; const isScrolledDown = ref(false); const prevScrollPosition = ref(0); const onScroll = (e) => { const target2 = e.target; const scrollValue = e.target instanceof Window ? target2.scrollY : target2.scrollTop; isScrolledDown.value = prevScrollPosition.value < scrollValue; prevScrollPosition.value = scrollValue; }; onMounted(() => { targetElement = fixed ? window : getTargetElement(target || scrollRoot.value); targetElement == null ? void 0 : targetElement.addEventListener("scroll", onScroll, fixed); }); onBeforeUnmount(() => { targetElement == null ? void 0 : targetElement.removeEventListener("scroll", onScroll); }); return { scrollRoot, isScrolledDown }; } export { setupScroll as s }; //# sourceMappingURL=useScroll.mjs.map