1
0
mirror of https://github.com/amir20/dozzle.git synced 2025-12-24 06:28:42 +01:00
Files
dozzle/assets/components/InfiniteLoader.vue
2020-06-15 16:44:20 -07:00

40 lines
1.1 KiB
Vue

<template>
<div ref="observer" class="control" :class="{ 'is-loading': isLoading }"></div>
</template>
<script>
export default {
name: "InfiniteLoader",
data() {
return {
isLoading: false,
};
},
props: {
onLoadMore: Function,
enabled: Boolean,
},
mounted() {
const intersectionObserver = new IntersectionObserver(
async (entries) => {
if (entries[0].intersectionRatio <= 0) return;
if (this.onLoadMore && this.enabled) {
const scrollingParent = this.$el.closest("[data-scrolling]") || document.documentElement;
const previousHeight = scrollingParent.scrollHeight;
this.isLoading = true;
await this.onLoadMore();
this.isLoading = false;
this.$nextTick(() => (scrollingParent.scrollTop += scrollingParent.scrollHeight - previousHeight));
}
},
{ threshholds: 1 }
);
intersectionObserver.observe(this.$refs.observer);
this.$once("hook:beforeDestroy", () => intersectionObserver.disconnect());
},
};
</script>
<style scoped lang="scss"></style>