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

32 lines
752 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'
2024-04-04 22:20:42 +00:00
import ToTopButton from '@/components/ui/BtnScrollToTop.vue'
2022-04-15 14:24:30 +00:00
2024-04-04 22:20:42 +00:00
export const useInfiniteScroll = (el: Ref<HTMLElement | undefined>, loadMore: Closure) => {
2024-03-16 18:11:08 +00:00
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
}
}