Skip to content

Commit 5a6c266

Browse files
authored
fix(*): Scroll typing indicator/suggestions into view. (#1848)
* fix(*): Scroll typing indicator/suggestions into view. * refactor(*); Cleaned up scroll to bottom logic
1 parent 2838753 commit 5a6c266

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

src/components/chat/chat.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ export default class IgcChatComponent extends EventEmitterMixin<
190190
this._userInputContext.setValue(this._state, true);
191191
}
192192

193+
@query('[part="typing-indicator"]')
194+
private typingIndicator?: HTMLElement;
195+
196+
@query('[part="suggestions-container"]')
197+
private suggestionsContainer?: HTMLElement;
198+
193199
/**
194200
* The list of chat messages currently displayed.
195201
* Use this property to set or update the message history.
@@ -309,21 +315,33 @@ export default class IgcChatComponent extends EventEmitterMixin<
309315
}
310316

311317
protected override updated(properties: PropertyValues<this>): void {
312-
if (properties.has('messages') && !this._state.disableAutoScroll) {
318+
if (
319+
(properties.has('messages') ||
320+
this.typingIndicator ||
321+
this.suggestionsContainer) &&
322+
!this._state.disableAutoScroll
323+
) {
313324
this._scrollToBottom();
314325
}
315326
}
316327

317-
private _scrollToBottom(): void {
318-
if (!isEmpty(this.messages)) {
319-
const lastMessage = this.renderRoot
320-
.querySelectorAll(IgcChatMessageComponent.tagName)
321-
.item(this.messages.length - 1);
322-
323-
requestAnimationFrame(() =>
324-
lastMessage.scrollIntoView({ block: 'end', inline: 'end' })
325-
);
326-
}
328+
private _scrollToBottom() {
329+
if (isEmpty(this.messages)) return;
330+
331+
const lastMessage = this.renderRoot
332+
.querySelectorAll(IgcChatMessageComponent.tagName)
333+
.item(this.messages.length - 1);
334+
335+
requestAnimationFrame(() => {
336+
if (this.typingIndicator || this.suggestionsContainer) {
337+
(this.typingIndicator
338+
? this.typingIndicator
339+
: this.suggestionsContainer
340+
)?.scrollIntoView({ block: 'end', inline: 'end' });
341+
} else {
342+
lastMessage.scrollIntoView({ block: 'end', inline: 'end' });
343+
}
344+
});
327345
}
328346

329347
private _renderHeader() {
@@ -370,20 +388,20 @@ export default class IgcChatComponent extends EventEmitterMixin<
370388
}
371389
)}
372390
${this._state.options?.isTyping
373-
? this._getRenderer('typingIndicator')(ctx)
391+
? html`<div part="typing-indicator">
392+
${this._getRenderer('typingIndicator')(ctx)}
393+
</div>`
374394
: nothing}
375395
</div>
376396
`;
377397
}
378398

379399
private _renderLoadingTemplate() {
380400
return html`
381-
<div part="typing-indicator">
382-
<div part="typing-dot"></div>
383-
<div part="typing-dot"></div>
384-
<div part="typing-dot"></div>
385-
<div part="typing-dot"></div>
386-
</div>
401+
<div part="typing-dot"></div>
402+
<div part="typing-dot"></div>
403+
<div part="typing-dot"></div>
404+
<div part="typing-dot"></div>
387405
`;
388406
}
389407

0 commit comments

Comments
 (0)