Skip to content

Commit

Permalink
fix: auto scroll chat
Browse files Browse the repository at this point in the history
  • Loading branch information
emcelroy committed Dec 3, 2024
1 parent cd9b2f0 commit de304c9
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/components/chat-transcript.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

.chat-transcript {
margin: 0;
max-height: 250px;
overflow-y: auto;

.chat-transcript__messages {
Expand Down
1 change: 0 additions & 1 deletion src/components/chat-transcript.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe("test chat transcript component", () => {
expect(message).toHaveAttribute("aria-label", labelContent);

const speaker = within(message).getByTestId("chat-message-speaker");
expect(speaker).toHaveAttribute("aria-label", "speaker");
expect(speaker).toHaveTextContent(chatTranscript.messages[index].speaker);

const content = within(message).getByTestId("chat-message-content");
Expand Down
6 changes: 3 additions & 3 deletions src/components/chat-transcript.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ export const ChatTranscriptComponent = observer(({chatTranscript}: IProps) => {
const chatTranscriptRef = useRef<HTMLElement>(null);

useEffect(() => {
// Always scroll to the bottom of the chat transcript.
// Autoscroll to the top of the latest message in the transcript.
const chatTranscriptContainer = chatTranscriptRef.current;
if (chatTranscriptContainer) {
const lastMessage = chatTranscriptContainer.querySelector(".chat-transcript__message:last-of-type");
lastMessage?.scrollIntoView({behavior: "smooth"});
}
}, [chatTranscript]);
}, [chatTranscript.messages.length]);

return (
<section ref={chatTranscriptRef} id="chat-transcript" className="chat-transcript" data-testid="chat-transcript" role="group">
Expand All @@ -44,7 +44,7 @@ export const ChatTranscriptComponent = observer(({chatTranscript}: IProps) => {
key={message.timestamp}
role="listitem"
>
<h3 aria-label="speaker" data-testid="chat-message-speaker">
<h3 data-testid="chat-message-speaker">
{message.speaker}
</h3>
<div
Expand Down

0 comments on commit de304c9

Please sign in to comment.