Skip to content

Commit

Permalink
create useEffect to scroll comments to the bottom of the page
Browse files Browse the repository at this point in the history
  • Loading branch information
NoorChasib committed Nov 20, 2023
1 parent 96a74f3 commit 33a0efb
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/frontend/src/components/public/CommentsSidebar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useState } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';
import CommentSidebarProps, { Comment } from './interfaces';
import { getDateTime, stringToDate } from '../../../utils/date';
import { faEllipsisH } from '@fortawesome/free-solid-svg-icons';
Expand Down Expand Up @@ -29,6 +29,7 @@ const CommentSidebar = ({
const [modalParagraph, setModalParagraph] = useState<string>('');
const [modalButtonValue, setModalButtonValue] = useState<string>('');
const [enableComments, setEnableComments] = useState<boolean>(false);
const commentsEndRef = useRef<HTMLDivElement>(null);

useEffect(() => {
if (!pia?.status) return;
Expand Down Expand Up @@ -69,6 +70,16 @@ const CommentSidebar = ({
setComments([]);
}, [pathname]);

useEffect(() => {
if (commentsEndRef.current) {
const scrollHeight = commentsEndRef.current.scrollHeight;
commentsEndRef.current.scrollTo({
top: scrollHeight,
behavior: 'smooth',
});
}
}, [comments]);

const handleModalClose = async (event: any) => {
event.preventDefault();
setShowModal(false);
Expand Down Expand Up @@ -115,7 +126,10 @@ const CommentSidebar = ({
<>
<div className="bg-white comment-sidebar">
<h3 className="ps-3">Comments</h3>
<div className="comment-sidebar__comments-container">
<div
className="comment-sidebar__comments-container"
ref={commentsEndRef}
>
{comments &&
comments?.map((comment) => (
<div className="p-3" key={comment.id}>
Expand Down

0 comments on commit 33a0efb

Please sign in to comment.