-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Update to scroll behavior and UX tweaks #77
Conversation
const chatboxContainerRef = useRef(null); | ||
|
||
// this use effect is intended to scroll to the bottom of the chat window, in the case | ||
// that a message is larger than the chat window height. | ||
useEffect(() => { | ||
const messageContainer = chatboxContainerRef.current; | ||
|
||
if (messageContainer) { | ||
const { scrollHeight, clientHeight } = messageContainer; | ||
const maxScrollTop = scrollHeight - clientHeight; | ||
const duration = 200; | ||
|
||
if (maxScrollTop > 0) { | ||
const startTime = Date.now(); | ||
const endTime = startTime + duration; | ||
|
||
const scroll = () => { | ||
const currentTime = Date.now(); | ||
const timeFraction = (currentTime - startTime) / duration; | ||
const scrollTop = maxScrollTop * timeFraction; | ||
|
||
messageContainer.scrollTo({ | ||
top: scrollTop, | ||
behavior: 'smooth', | ||
}); | ||
|
||
if (currentTime < endTime) { | ||
requestAnimationFrame(scroll); | ||
} | ||
}; | ||
|
||
requestAnimationFrame(scroll); | ||
} | ||
} | ||
}, [messageList, isOpen, apiError]); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The scrolling behavior was moved to the ChatBox component directly refactored to be much simpler.
a3f8b00
to
c2b0504
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Thank you for making those UI tweaks - makes a big difference in how professional the chat looks.
Ticket: COSMO-597🔒
Description
This PR changes the way the chat scroll behaves. Its default behavior to have a smooth scroll down whenever a new message appears causes a weird experience on opening with an existing chat history to smooth scroll all messages down each time it opens. These changes makes the first render to scroll instantly to the last message on open and then uses smooth scrolling on each new message.
Additionally, it tweaks the UI to make it closer to the original design and added some extra tweaks.
#Screenshots
Old UI
New UI
Note
Notice some UI improvements like using shadows at the top and bottom of the chat scroll list, the shadows now are cast over the messages rather than clipping behind them and updated some sizes and paddings to make it nicer to the eye.