koel/resources/assets/js/composables/useInfiniteScroll.ts

31 lines
746 B
TypeScript
Raw Normal View History

2024-03-16 18:11:08 +00:00
import { Ref } from 'vue'
import { useInfiniteScroll as baseUseInfiniteScroll } from '@vueuse/core'
import ToTopButton from '@/components/ui/BtnScrollToTop.vue'
2022-04-15 14:24:30 +00:00
2024-03-16 18:11:08 +00:00
export const useInfiniteScroll = (el: Ref<HTMLElement | null>, loadMore: Closure) => {
baseUseInfiniteScroll(el, loadMore, { distance: 32 })
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
let tries = 0
const MAX_TRIES = 5
const makeScrollable = async () => {
2024-03-16 18:11:08 +00:00
const container = el.value
if (!container) {
2022-06-10 10:47:46 +00:00
window.setTimeout(() => makeScrollable(), 200)
return
}
2022-06-10 10:47:46 +00:00
if (container.scrollHeight <= container.clientHeight && tries < MAX_TRIES) {
tries++
await loadMore()
window.setTimeout(() => makeScrollable(), 200)
2022-04-15 14:24:30 +00:00
}
}
return {
ToTopButton,
makeScrollable
}
}