Skip to content

Commit

Permalink
do not auto-scroll if user has changed scroll location
Browse files Browse the repository at this point in the history
  • Loading branch information
sysread committed Jul 19, 2024
1 parent abb1794 commit 91b963d
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ import Storage from './storage.js';

const conversation = [];

let scrollMessagePaneLocked = false;

const scrollMessagePaneIntvl = window.setInterval(() => {
if (scrollMessagePaneLocked) {
return;
}

const messagePane = document.getElementById('chat-messages');

if (messagePane) {
messagePane.scrollTop = messagePane.scrollHeight;
}
}, 100);

function buildMessageCard(role, content) {
const card = document.createElement('div');
card.classList.add('card');
Expand Down Expand Up @@ -163,18 +177,12 @@ document.addEventListener('DOMContentLoaded', () => {
sendMessage('user', text);
});

// On load, immediately focus the user input field
userInput.focus();

// Scroll to the bottom of chat messages on focus
userInput.addEventListener('focus', () => {
window.setTimeout(() => {
messagePane.scrollTop = messagePane.scrollHeight;
}, 300);
// Lock and unlock scroll based on user action
messagePane.addEventListener('scroll', () => {
const atBottom = messagePane.scrollHeight - messagePane.scrollTop === messagePane.clientHeight;
scrollMessagePaneLocked = !atBottom;
});

// Ensure the message pane is scrolled to the bottom on window resize
window.addEventListener('resize', () => {
messagePane.scrollTop = messagePane.scrollHeight;
});
// On load, immediately focus the user input field
userInput.focus();
});

0 comments on commit 91b963d

Please sign in to comment.