Skip to content

Commit

Permalink
fix: pointer always pops up when sending and receiving messages
Browse files Browse the repository at this point in the history
The "New Messages" pointer pops up both when sending messages continuously and receiving messages.
#77 (comment)
  • Loading branch information
hfdem committed Aug 9, 2024
1 parent 358ea92 commit 2040092
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions client/src/components/Messaging/ScrollWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,51 @@ type ScrollWrapperProps = {

export const ScrollWrapper = ({ children, messageCount }: ScrollWrapperProps) => {
const wrapperRef = useRef<HTMLDivElement>(null);
const [isAtBottom, setIsAtBottom] = useState(true);
const [showPopup, setShowPopup] = useState(false);
const [scrollAtBottom, setScrollAtBottom] = useState(true);

useEffect(() => {
if (wrapperRef.current) {
const handleScroll = () => {
const wrapper = wrapperRef.current;
const isScrolledToBottom = wrapper.scrollHeight - wrapper.scrollTop === wrapper.clientHeight;
setScrollAtBottom(isScrolledToBottom);
if (isScrolledToBottom) {
wrapper.scrollTop = wrapper.scrollHeight;
} else {
setShowPopup(true);
if (!wrapper) return;
setIsAtBottom(wrapper.scrollTop + wrapper.clientHeight >= wrapper.scrollHeight);
};

const wrapperElement = wrapperRef.current;
if (wrapperElement) {
wrapperElement.addEventListener('scroll', handleScroll);
}

return () => {
if (wrapperElement) {
wrapperElement.removeEventListener('scroll', handleScroll);
}
};
}, []);

useEffect(() => {
if (isAtBottom && messageCount > 0) {
scrollToBottom();
}
}, [messageCount]);
}, [messageCount, isAtBottom]);

useEffect(() => {
if (scrollAtBottom && wrapperRef.current) {
wrapperRef.current.scrollTop = wrapperRef.current.scrollHeight;
if (!isAtBottom && messageCount > 0) {
setShowPopup(true);
}
}, [scrollAtBottom, messageCount]);
}, [messageCount]);

const scrollToBottom = () => {
if (wrapperRef.current) {
wrapperRef.current.scrollTop = wrapperRef.current.scrollHeight;
setShowPopup(false);
}
};

const handleScroll = () => {
if (wrapperRef.current) {
const wrapper = wrapperRef.current;
const isScrolledToBottom = wrapper.scrollHeight - wrapper.scrollTop === wrapper.clientHeight;
setScrollAtBottom(isScrolledToBottom);
setShowPopup(false);
}
setShowPopup(false);
};

return (
<div className={styles.scrollWrapper} ref={wrapperRef} onScroll={handleScroll}>
<div className={styles.scrollWrapper} ref={wrapperRef}>
{children}
{showPopup && !scrollAtBottom && (
{showPopup && (
<div className={styles.popup} onClick={scrollToBottom}>
New Messages
</div>
Expand Down

0 comments on commit 2040092

Please sign in to comment.