Skip to content

Commit d87f63a

Browse files
committed
refactor: Cleaned-up auto-scroll handler
1 parent 5a6c266 commit d87f63a

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

src/components/chat/chat.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ export default class IgcChatComponent extends EventEmitterMixin<
182182
@query(IgcChatInputComponent.tagName)
183183
private readonly _input?: IgcChatInputComponent;
184184

185+
@query('[part="typing-indicator"]')
186+
private readonly _typingIndicator?: HTMLElement;
187+
188+
@query('[part="suggestions-container"]')
189+
private readonly _suggestionsContainer?: HTMLElement;
190+
185191
private _updateContext(): void {
186192
this._context.setValue(this._state, true);
187193
}
@@ -190,12 +196,6 @@ export default class IgcChatComponent extends EventEmitterMixin<
190196
this._userInputContext.setValue(this._state, true);
191197
}
192198

193-
@query('[part="typing-indicator"]')
194-
private typingIndicator?: HTMLElement;
195-
196-
@query('[part="suggestions-container"]')
197-
private suggestionsContainer?: HTMLElement;
198-
199199
/**
200200
* The list of chat messages currently displayed.
201201
* Use this property to set or update the message history.
@@ -317,8 +317,8 @@ export default class IgcChatComponent extends EventEmitterMixin<
317317
protected override updated(properties: PropertyValues<this>): void {
318318
if (
319319
(properties.has('messages') ||
320-
this.typingIndicator ||
321-
this.suggestionsContainer) &&
320+
this._typingIndicator ||
321+
this._suggestionsContainer) &&
322322
!this._state.disableAutoScroll
323323
) {
324324
this._scrollToBottom();
@@ -332,15 +332,11 @@ export default class IgcChatComponent extends EventEmitterMixin<
332332
.querySelectorAll(IgcChatMessageComponent.tagName)
333333
.item(this.messages.length - 1);
334334

335+
const scrollTarget =
336+
this._typingIndicator ?? this._suggestionsContainer ?? lastMessage;
337+
335338
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-
}
339+
scrollTarget?.scrollIntoView({ block: 'end', inline: 'end' });
344340
});
345341
}
346342

0 commit comments

Comments
 (0)