Skip to content

Commit e6aef71

Browse files
authored
Chore: Ensure comment list auto scrolls to bottom on post (#339)
1 parent 865c5f2 commit e6aef71

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

src/components/CommentList/CommentList.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,44 @@ type Props = {
1313
onDelete: Function
1414
};
1515

16-
const CommentList = ({ comments, onDelete }: Props) => (
17-
<ul className={CLASS_COMMENT_LIST}>
18-
{comments.map(({ id, ...rest }) => (
19-
<li className={CLASS_COMMENT_LIST_ITEM} key={`annotation_${id}`}>
20-
<Comment id={id} onDelete={onDelete} {...rest} />
21-
</li>
22-
))}
23-
</ul>
24-
);
16+
class CommentList extends React.Component<Props> {
17+
commentsContainer: null | HTMLElement;
18+
19+
componentDidMount() {
20+
this.scrollToBottom();
21+
}
22+
23+
componentDidUpdate() {
24+
this.scrollToBottom();
25+
}
26+
27+
/**
28+
* Scrolls the container to the bottom
29+
* @return {void}
30+
*/
31+
scrollToBottom = () => {
32+
if (this.commentsContainer) {
33+
this.commentsContainer.scrollTop = this.commentsContainer.scrollHeight;
34+
}
35+
};
36+
37+
render() {
38+
const { comments, onDelete } = this.props;
39+
return (
40+
<ul
41+
className={CLASS_COMMENT_LIST}
42+
ref={(ref) => {
43+
this.commentsContainer = ref;
44+
}}
45+
>
46+
{comments.map(({ id, ...rest }) => (
47+
<li className={CLASS_COMMENT_LIST_ITEM} key={`annotation_${id}`}>
48+
<Comment id={id} onDelete={onDelete} {...rest} />
49+
</li>
50+
))}
51+
</ul>
52+
);
53+
}
54+
}
2555

2656
export default CommentList;

0 commit comments

Comments
 (0)