-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathInfiniteLoading.vue
More file actions
127 lines (115 loc) · 3.14 KB
/
Copy pathInfiniteLoading.vue
File metadata and controls
127 lines (115 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<script lang="ts" setup>
import type { Props, Params, State, StateHandler } from "@root/types";
import { computed, onMounted, ref, toRefs, onUnmounted, watch, nextTick } from "vue";
import { startObserver, getParentEl, isVisible } from "@root/utils";
// @ts-ignore
import Spinner from "./Spinner.vue";
const emit = defineEmits<{ infinite: [$state: StateHandler] }>();
const props = withDefaults(defineProps<Props>(), {
top: false,
firstload: true,
distance: 0,
});
defineSlots<{
spinner(props: {}): any;
complete(props: {}): any;
error(props: { retry(): void }): any;
}>();
let observer: IntersectionObserver | null = null;
let prevHeight = 0;
const infiniteLoading = ref(null);
const state = ref<State>("");
const { top, firstload, distance } = props;
const { identifier, target } = toRefs(props);
const params: Params = {
infiniteLoading,
top,
firstload,
distance,
parentEl: null,
emit() {
const parentEl = params.parentEl || document.documentElement;
prevHeight = parentEl.scrollHeight;
stateHandler.loading();
emit("infinite", stateHandler);
},
};
const stateHandler: StateHandler = {
loading() {
state.value = "loading";
},
async loaded() {
params.firstload = false;
state.value = "loaded";
const parentEl = params.parentEl || document.documentElement;
await nextTick();
if (top) parentEl.scrollTop = parentEl.scrollHeight - prevHeight;
if (isVisible(infiniteLoading.value!, params.parentEl)) params.emit();
},
complete() {
state.value = "complete";
observer?.disconnect();
},
error() {
state.value = "error";
},
};
watch(identifier, () => {
observer?.disconnect();
observer = startObserver(params);
});
onMounted(async () => {
params.parentEl = await getParentEl(target!);
observer = startObserver(params);
});
onUnmounted(() => {
observer?.disconnect();
});
const showNoMore = computed(() => state.value == "complete" && !params.firstload);
const showNoResults = computed(() => state.value == "complete" && params.firstload);
const showError = computed(() => state.value === "error");
</script>
<template>
<div ref="infiniteLoading" style="min-height: 1px">
<div v-show="state == 'loading'">
<slot name="spinner">
<Spinner />
</slot>
</div>
<slot v-if="showNoMore" name="complete">
<span> {{ slots?.complete || "No more results!" }} </span>
</slot>
<slot v-if="showNoResults" name="noResults">
<span> {{ slots?.noResults || "No data found!" }} </span>
</slot>
<slot v-if="showError" name="error" :retry="params.emit">
<span class="state-error">
<span>{{ slots?.error || "Oops something went wrong!" }}</span>
<button class="retry" @click="params.emit">retry</button>
</span>
</slot>
</div>
</template>
<style scoped>
.state-error {
display: flex;
flex-direction: column;
align-items: center;
}
.retry {
margin-top: 8px;
padding: 2px 6px 4px 6px;
width: 60px;
color: inherit;
font-size: 14px;
font-family: inherit;
background: transparent;
border: 2px solid currentColor;
border-radius: 5px;
outline: none;
cursor: pointer;
}
.retry:hover {
opacity: 0.8;
}
</style>