koel/resources/assets/js/composables/useInfiniteScroll.ts
2024-10-14 00:37:01 +07:00

31 lines
758 B
TypeScript

import type { Ref } from 'vue'
import { useInfiniteScroll as baseUseInfiniteScroll } from '@vueuse/core'
import ToTopButton from '@/components/ui/BtnScrollToTop.vue'
export const useInfiniteScroll = (el: Ref<HTMLElement | undefined>, loadMore: Closure) => {
baseUseInfiniteScroll(el, loadMore, { distance: 32 })
let tries = 0
const MAX_TRIES = 5
const makeScrollable = async () => {
const container = el.value
if (!container) {
window.setTimeout(() => makeScrollable(), 200)
return
}
if (container.scrollHeight <= container.clientHeight && tries < MAX_TRIES) {
tries++
await loadMore()
window.setTimeout(() => makeScrollable(), 200)
}
}
return {
ToTopButton,
makeScrollable,
}
}