refactor core into composables & components

This commit is contained in:
riqo 2021-01-30 16:05:59 -06:00 • committed by Enrique Hernandez
commit a84019da0d
37 changed files with 409 additions and 469 deletions

View file

@ -1,57 +0,0 @@
import { ref, computed, onMounted } from "vue";
export default function useScrollView({ targetId }: { targetId: string }) {
const scroll = ref(0);
let scrollTarget: HTMLElement | SVGElement | null;
const targetHeight = ref(0);
let topBound: number;
const scrollStartHeight = 445;
const clampedScroll = computed(() => {
if (targetHeight.value === 0) { return 0; }
topBound = -targetHeight.value + 450;
return Math.max(topBound, Math.min(scroll.value, 0)) as number;
})
const scrollView = computed(() => {
if (clampedScroll.value === 0) {
scroll.value = 0;
} else if (clampedScroll.value === topBound) {
scroll.value = topBound;
}
return `0 ${clampedScroll.value} 350 500`;
});
const getHeight = () => {
if (scrollTarget) {
return scrollTarget.getBoundingClientRect().height as number;
}
}
const handleScrollEvent = (verticalScroll: number) => {
if (scrollTarget) {
const heightResult = getHeight();
if (heightResult) {
// only scroll if renderer scrollStart
if (heightResult > scrollStartHeight) {
targetHeight.value = heightResult;
scroll.value += verticalScroll;
}
}
}
}
onMounted(() => {
scrollTarget = document.getElementById(targetId);
if (scrollTarget) {
window.addEventListener("wheel", (e) => {
const verticalScroll = e.deltaY * 0.25;
handleScrollEvent(verticalScroll);
})
}
});
return {
scrollView
}
}