-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinfinite-list.ts
40 lines (34 loc) · 936 Bytes
/
infinite-list.ts
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
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { throttle } from '@ember/runloop';
interface ArgsInterface {
loadMore: () => void;
isLoading: boolean;
itemsShown: number;
itemsAmount: number;
}
export default class InfiniteList extends Component<ArgsInterface> {
@action
scroll(event: Event) {
throttle(this, this._onScroll, event, 500, false);
}
get moreDataToLoad() {
return this.args.itemsShown < this.args.itemsAmount;
}
_onScroll(event: Event) {
const { scrollTop, scrollHeight, clientHeight } =
event.target as HTMLElement;
const scrollTopMax = scrollHeight - clientHeight;
const scrollPercentage = scrollTop / scrollTopMax;
// trigger loadMore when >80% is scrolled
if (scrollPercentage > 0.8) {
this.loadMore();
}
}
@action
loadMore() {
if (this.moreDataToLoad) {
this.args.loadMore();
}
}
}