2024-03-16 18:11:08 +00:00
|
|
|
import { Ref } from 'vue'
|
|
|
|
import { useInfiniteScroll as baseUseInfiniteScroll } from '@vueuse/core'
|
2022-07-07 18:05:46 +00:00
|
|
|
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
|
2022-05-16 07:34:18 +00:00
|
|
|
|
|
|
|
if (!container) {
|
2022-06-10 10:47:46 +00:00
|
|
|
window.setTimeout(() => makeScrollable(), 200)
|
2022-05-16 07:34:18 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|