-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.tsx
154 lines (133 loc) · 5.81 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import * as React from 'react';
import { CSSProperties } from 'react';
export type ScrollableFeedProps = {
forceScroll?: boolean;
animateScroll?: (element: HTMLElement, offset: number) => void;
onScrollComplete?: () => void;
changeDetectionFilter?: (previousProps: ScrollableFeedComponentProps, newProps: ScrollableFeedComponentProps) => boolean;
viewableDetectionEpsilon?: number;
className?: string;
onScroll?: (isAtBottom: boolean) => void;
debug?: boolean
}
type ScrollableFeedComponentProps = React.PropsWithChildren<ScrollableFeedProps>;
class ScrollableFeed extends React.Component<React.PropsWithChildren<ScrollableFeedProps>> {
private readonly wrapperRef: React.RefObject<HTMLDivElement>;
private readonly bottomRef: React.RefObject<HTMLDivElement>;
constructor(props: ScrollableFeedProps) {
super(props);
this.bottomRef = React.createRef();
this.wrapperRef = React.createRef();
this.handleScroll = this.handleScroll.bind(this);
if (this.props.debug) console.log("Component cstr");
}
static defaultProps: ScrollableFeedProps = {
forceScroll: false,
animateScroll: (element: HTMLElement, offset: number): void => {
if (element.scrollBy) {
element.scrollBy({ top: offset });
}
else {
element.scrollTop = offset;
}
},
onScrollComplete: () => {},
changeDetectionFilter: () => true,
viewableDetectionEpsilon: 2,
onScroll: () => {},
};
getSnapshotBeforeUpdate(): boolean {
if (this.props.debug) console.log("Component ", this.getSnapshotBeforeUpdate.name);
if (this.wrapperRef.current && this.bottomRef.current) {
const { viewableDetectionEpsilon } = this.props;
return ScrollableFeed.isViewable(this.wrapperRef.current, this.bottomRef.current, viewableDetectionEpsilon!); //This argument is passed down to componentDidUpdate as 3rd parameter
}
return false;
}
componentDidUpdate(previousProps: ScrollableFeedComponentProps, _previousState: any, snapshot: boolean): void {
if (this.props.debug) console.log("Component ", this.componentDidUpdate.name);
const { forceScroll, changeDetectionFilter } = this.props;
const isValidChange = changeDetectionFilter!(previousProps, this.props);
if (isValidChange && (forceScroll || snapshot) && this.bottomRef.current && this.wrapperRef.current) {
this.scrollParentToChild(this.wrapperRef.current, this.bottomRef.current);
}
}
componentDidMount(): void {
if (this.props.debug) console.log("Component ", this.componentDidMount.name);
//Scroll to bottom from the start
if (this.bottomRef.current && this.wrapperRef.current) {
this.scrollParentToChild(this.wrapperRef.current, this.bottomRef.current);
}
}
/**
* Scrolls a parent element such that the child element will be in view
* @param parent
* @param child
*/
protected scrollParentToChild(parent: HTMLElement, child: HTMLElement): void {
const { viewableDetectionEpsilon } = this.props;
if (!ScrollableFeed.isViewable(parent, child, viewableDetectionEpsilon!)) {
//Source: https://stackoverflow.com/a/45411081/6316091
const parentRect = parent.getBoundingClientRect();
const childRect = child.getBoundingClientRect();
//Scroll by offset relative to parent
const scrollOffset = (childRect.top + parent.scrollTop) - parentRect.top;
const { animateScroll, onScrollComplete } = this.props;
if (animateScroll) {
animateScroll(parent, scrollOffset);
onScrollComplete!();
}
}
}
/**
* Returns whether a child element is visible within a parent element
*
* @param parent
* @param child
* @param epsilon
*/
private static isViewable(parent: HTMLElement, child: HTMLElement, epsilon: number): boolean {
epsilon = epsilon || 0;
//Source: https://stackoverflow.com/a/45411081/6316091
const parentRect = parent.getBoundingClientRect();
const childRect = child.getBoundingClientRect();
const childTopIsViewable = (childRect.top >= parentRect.top);
const childOffsetToParentBottom = parentRect.top + parent.clientHeight - childRect.top;
const childBottomIsViewable = childOffsetToParentBottom + epsilon >= 0;
return childTopIsViewable && childBottomIsViewable;
}
/**
* Fires the onScroll event, sending isAtBottom boolean as its first parameter
*/
protected handleScroll(): void {
const { viewableDetectionEpsilon, onScroll } = this.props;
if (onScroll && this.bottomRef.current && this.wrapperRef.current) {
const isAtBottom = ScrollableFeed.isViewable(this.wrapperRef.current, this.bottomRef.current, viewableDetectionEpsilon!);
onScroll(isAtBottom);
}
}
/**
* Scroll to the bottom
*/
public scrollToBottom(): void {
if (this.bottomRef.current && this.wrapperRef.current) {
this.scrollParentToChild(this.wrapperRef.current, this.bottomRef.current);
}
}
render(): React.ReactNode {
if (this.props.debug) console.log("Component ", this.render.name);
const style: CSSProperties = {
maxHeight: "inherit",
height: "inherit",
overflowY: "auto",
};
const { children, className } = this.props;
return (
<div className={className} style={style} ref={this.wrapperRef} onScroll={this.handleScroll}>
{children}
<div ref={this.bottomRef}></div>
</div>
);
}
}
export default ScrollableFeed;