Compare commits

...

1 Commits

Author SHA1 Message Date
mustafa-rezae
8cc66757a9 Add custom useInfiniteScroll
Update haditha
2025-05-15 16:21:53 +03:30
3 changed files with 91 additions and 22 deletions

View File

@ -1,18 +1,58 @@
// composables/useInfiniteScroll.ts // composables/useInfiniteScroll.ts
import { ref, onMounted, onBeforeUnmount } from "vue";
export default function useInfiniteScroll( export default function useInfiniteScroll(
callback: () => void, callback: () => Promise<void>,
elementId: string elementId?: string
) { ) {
const route = useRoute();
const isFetching = ref(false); const isFetching = ref(false);
const infiniteScroll = ref<HTMLElement | null>(null); const infiniteScroll = ref<HTMLElement | Window | null>(null);
const handleScroll = () => { // Throttle function to limit scroll event frequency
if (isFetching.value) return; const throttle = (func: (...args: any[]) => void, limit: number) => {
const scrollPosition = let lastFunc: ReturnType<typeof setTimeout>;
infiniteScroll.value.scrollTop + infiniteScroll.value.clientHeight; let lastRan: number;
const threshold = infiniteScroll.value.scrollHeight - 100; return function (this: any, ...args: any[]) {
const context = this;
if (!lastRan) {
func.apply(context, args);
lastRan = Date.now();
} else {
clearTimeout(lastFunc);
lastFunc = setTimeout(() => {
if (Date.now() - lastRan >= limit) {
func.apply(context, args);
lastRan = Date.now();
}
}, limit - (Date.now() - lastRan));
}
};
};
if (scrollPosition >= threshold) { const checkScrollPosition = () => {
if (isFetching.value || !infiniteScroll.value) return;
let scrollPosition: number;
let threshold: number;
let clientHeight: number;
let scrollHeight: number;
if (infiniteScroll.value === window) {
scrollPosition = window.scrollY || window.pageYOffset;
clientHeight = document.documentElement.clientHeight;
scrollHeight = document.documentElement.scrollHeight;
} else {
const el = infiniteScroll.value as HTMLElement;
scrollPosition = el.scrollTop;
clientHeight = el.clientHeight;
scrollHeight = el.scrollHeight;
}
threshold = scrollHeight - 100;
const currentPosition = scrollPosition + clientHeight;
if (currentPosition >= threshold) {
isFetching.value = true; isFetching.value = true;
callback().finally(() => { callback().finally(() => {
isFetching.value = false; isFetching.value = false;
@ -20,28 +60,57 @@ export default function useInfiniteScroll(
} }
}; };
// Throttled version of scroll handler
const throttledScrollHandler = throttle(checkScrollPosition, 200);
const handleTouchEnd = () => { const handleTouchEnd = () => {
// Add a slight delay to ensure scroll position is updated setTimeout(throttledScrollHandler, 100);
setTimeout(handleScroll, 100);
}; };
onMounted(() => { onMounted(() => {
const targetElement = document.getElementById(elementId); console.info(route.name)
if (route.name == "hadithaSearch" || route.name == "hadithaLibrary") {
const targetElement = elementId
? document.getElementById(elementId)
: window;
if (!targetElement) {
console.warn(
`Element ${elementId || "window"} not found for infinite scroll`
);
return;
}
infiniteScroll.value = targetElement; infiniteScroll.value = targetElement;
if (targetElement) { if (targetElement === window) {
targetElement.addEventListener("scroll", handleScroll); window.addEventListener("scroll", throttledScrollHandler);
window.addEventListener("touchend", handleTouchEnd);
} else {
targetElement.addEventListener("scroll", throttledScrollHandler);
targetElement.addEventListener("touchend", handleTouchEnd); targetElement.addEventListener("touchend", handleTouchEnd);
} }
}
}); });
onBeforeUnmount(() => { onBeforeUnmount(() => {
const targetElement = document.getElementById(elementId); if (route.name == "hadithaSearch" || route.name == "hadithaLibrary") {
infiniteScroll.value = targetElement; if (!infiniteScroll.value) return;
if (targetElement) { if (infiniteScroll.value === window) {
targetElement.removeEventListener("scroll", handleScroll); window.removeEventListener("scroll", throttledScrollHandler);
targetElement.removeEventListener("touchend", handleTouchEnd); window.removeEventListener("touchend", handleTouchEnd);
} else {
(infiniteScroll.value as HTMLElement).removeEventListener(
"scroll",
throttledScrollHandler
);
(infiniteScroll.value as HTMLElement).removeEventListener(
"touchend",
handleTouchEnd
);
}
} }
}); });

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

@ -1 +1 @@
Subproject commit be9e992c68be3240d197244ff87e5ae910dc88c1 Subproject commit 1df5c5d35659e965e51faf69d32a525534421d15