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 8 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
34 changes: 28 additions & 6 deletions packages/chat-component/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { chatHttpOptions, globalConfig, requestOptions } from './config/global-c
import { getAPIResponse } from './core/http/index.js';
import { parseStreamedMessages } from './core/parser/index.js';
import { mainStyle } from './style.js';
import { getTimestamp, processText } from './utils/index.js';
import { getTimestamp, processText, mustScrollEvent, scrollToFooter } from './utils/index.js';
import { unsafeSVG } from 'lit/directives/unsafe-svg.js';

// TODO: allow host applications to customize these icons
Expand Down Expand Up @@ -105,8 +105,30 @@ export class ChatComponent extends LitElement {

static override styles = [mainStyle];

// Send the question to the Open AI API and render the answer in the chat
override connectedCallback(): void {
super.connectedCallback();

// add event listener to know when to scroll chat list footer into view
this.addEventListener('must-scroll', () => {
sinedied marked this conversation as resolved.
Show resolved Hide resolved
this.handleOnMustScroll();
});
}
// debounce dispatching must-scroll event
debounceDispatchMustScrollEvent(): void {
let timeout: any = 0;
clearTimeout(timeout);
timeout = setTimeout(() => {
this.dispatchEvent(mustScrollEvent);
}, 500);
}
// handle must scroll event
handleOnMustScroll(): void {
const footer = this.shadowRoot?.querySelector('#chat-list-footer') as HTMLElement;
sinedied marked this conversation as resolved.
Show resolved Hide resolved
if (footer) {
scrollToFooter(footer);
}
}
// 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 @@ -158,6 +180,7 @@ export class ChatComponent extends LitElement {
isUserMessage,
},
];
// scroll to the bottom of the chat
return true;
};

Expand Down Expand Up @@ -355,7 +378,7 @@ export class ChatComponent extends LitElement {
</ol>`,
);
}

this.debounceDispatchMustScrollEvent();
return entries;
}

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

return '';
}

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

return '';
}

Expand All @@ -431,7 +452,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 +506,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
11 changes: 11 additions & 0 deletions packages/chat-component/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,14 @@ export function getTimestamp() {
hour12: true,
});
}

// Define must scroll event
export const mustScrollEvent = new CustomEvent('must-scroll', {
bubbles: true,
composed: true,
});

// Scroll to last message
export function scrollToFooter(element: HTMLElement): void {
element.scrollIntoView({ behavior: 'smooth', block: 'center' });
}