-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
index.tsx
63 lines (59 loc) · 1.54 KB
/
index.tsx
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
import React, { useState, useRef, useEffect } from 'react';
import Scrollbar from 'smooth-scrollbar';
import {
ScrollContent as Content,
ScrollWrapper,
ScrollBarCSS,
} from './elements';
type ScrollableContentProps = {
children: React.ReactNode;
onBottomReached?: () => void;
keepCalling?: boolean;
};
export const ScrollableContent = ({
children,
onBottomReached,
keepCalling,
}: ScrollableContentProps) => {
const [scrolled, setScrolled] = useState(false);
const scrollableElRef = useRef(null);
const [scrollbar, setScrollbar] = useState(null);
const [lastCalled, setLastCalled] = useState(null);
useEffect(() => {
setScrollbar(
Scrollbar.init(scrollableElRef.current, {
alwaysShowTracks: true,
continuousScrolling: true,
})
);
}, []);
useEffect(() => {
if (scrollbar && keepCalling) {
scrollbar.addListener(status => {
setScrolled(status.offset.y > 10);
if (status.offset.y === status.limit.y) {
if (lastCalled !== status.offset.y) {
onBottomReached();
setLastCalled(status.offset.y);
}
}
});
}
}, [keepCalling, lastCalled, onBottomReached, scrollableElRef, scrollbar]);
return (
<>
<ScrollBarCSS />
<ScrollWrapper scrolled={scrolled}>
<Content>
<div
data-scrollbar
style={{ height: '100%', overflow: 'hidden' }}
ref={scrollableElRef}
>
{children}
</div>
</Content>
</ScrollWrapper>
</>
);
};