Skip to content
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

fix: scroll to last message #110

Merged
merged 14 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions packages/chat-component/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export class ChatComponent extends LitElement {
@query('#question-input')
questionInput!: HTMLInputElement;

@query('#chat-list-footer')
chatFooter!: HTMLElement;

// Default prompts to display in the chat
@property({ type: Boolean })
isDisabled = false;
Expand Down Expand Up @@ -105,8 +108,17 @@ export class ChatComponent extends LitElement {

static override styles = [mainStyle];

// debounce dispatching must-scroll event
debounceScrollIntoView(): void {
let timeout: any = 0;
clearTimeout(timeout);
timeout = setTimeout(() => {
if (this.chatFooter) {
this.chatFooter.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
}, 500);
}
// Send the question to the Open AI API and render the answer in the chat

// Add a message to the chat, when the user or the API sends a message
async processApiResponse({ message, isUserMessage }: { message: string; isUserMessage: boolean }) {
const citations: Citation[] = [];
Expand Down Expand Up @@ -355,7 +367,8 @@ export class ChatComponent extends LitElement {
</ol>`,
);
}

// scroll to the bottom of the chat
this.debounceScrollIntoView();
return entries;
}

Expand All @@ -381,7 +394,6 @@ export class ChatComponent extends LitElement {
</ol>
`;
}

return '';
}

Expand All @@ -408,7 +420,6 @@ export class ChatComponent extends LitElement {
</div>
`;
}

return '';
}

Expand All @@ -431,7 +442,7 @@ export class ChatComponent extends LitElement {
${unsafeSVG(iconDelete)}
</button>
</div>
<ul class="chat__list" aria-live="assertive">
<ul class="chat__list" id="chat-list" aria-live="assertive">
${this.chatThread.map(
(message) => html`
<li class="chat__listItem ${message.isUserMessage ? 'user-message' : ''}">
Expand Down Expand Up @@ -485,6 +496,7 @@ export class ChatComponent extends LitElement {
`
: ''}
</ul>
<div class="chat__footer" id="chat-list-footer"></div>
`
: ''}
${this.isAwaitingResponse && !this.hasAPIError
Expand Down
14 changes: 9 additions & 5 deletions packages/chat-component/src/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export const mainStyle = css`
--accent-lighter: rgba(140, 222, 242, 0.4);
--error-color: #8a0000;
}
html {
scroll-behavior: smooth;
}
ul {
margin-block-start: 0;
margin-block-end: 0;
Expand Down Expand Up @@ -118,7 +121,6 @@ export const mainStyle = css`
}
.chat__container {
min-width: 100%;
min-height: 400px;
transition: width 0.3s ease-in-out;
max-height: 100vh;
}
Expand Down Expand Up @@ -170,8 +172,8 @@ export const mainStyle = css`
background: linear-gradient(
0deg,
rgba(245, 245, 245, 1) 0%,
rgba(245, 245, 245, 0.6) 35%,
rgba(245, 245, 245, 0.2) 100%
rgba(245, 245, 245, 0.8) 75%,
rgba(245, 245, 245, 0.5) 100%
);
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
padding: 15px 10px 50px;
Expand Down Expand Up @@ -318,8 +320,10 @@ export const mainStyle = css`
color: var(--text-color);
display: flex;
flex-direction: column;
padding: 0;
margin-bottom: 50px;
}
.chat__footer {
width: 100%;
height: 70px;
}
.chat__listItem {
max-width: 80%;
Expand Down