Skip to content

Commit 8af1f3b

Browse files
committed
Merge branch 'main' into fix/chat-scroll-parent
2 parents 19dc823 + e779c84 commit 8af1f3b

File tree

5 files changed

+70
-41
lines changed

5 files changed

+70
-41
lines changed

js/chat/chat.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ interface ChatInputSetInputOptions {
131131
}
132132

133133
class ChatInput extends LightElement {
134-
private _disabled = false;
135-
136134
@property() placeholder = "Enter a message...";
137135
// disabled is reflected manually because `reflect: true` doesn't work with LightElement
138136
@property({ type: Boolean })
@@ -155,6 +153,27 @@ class ChatInput extends LightElement {
155153
this.#onInput();
156154
}
157155

156+
private _disabled = false;
157+
inputVisibleObserver?: IntersectionObserver;
158+
159+
connectedCallback(): void {
160+
super.connectedCallback();
161+
162+
this.inputVisibleObserver = new IntersectionObserver((entries) => {
163+
entries.forEach((entry) => {
164+
if (entry.isIntersecting) this.#updateHeight();
165+
});
166+
});
167+
168+
this.inputVisibleObserver.observe(this);
169+
}
170+
171+
disconnectedCallback(): void {
172+
super.disconnectedCallback();
173+
this.inputVisibleObserver?.disconnect();
174+
this.inputVisibleObserver = undefined;
175+
}
176+
158177
attributeChangedCallback(
159178
name: string,
160179
_old: string | null,
@@ -189,7 +208,7 @@ class ChatInput extends LightElement {
189208
return html`
190209
<textarea
191210
id="${this.id}"
192-
class="form-control textarea-autoresize"
211+
class="form-control"
193212
rows="1"
194213
placeholder="${this.placeholder}"
195214
@keydown=${this.#onKeyDown}
@@ -217,6 +236,7 @@ class ChatInput extends LightElement {
217236
}
218237

219238
#onInput(): void {
239+
this.#updateHeight();
220240
this.button.disabled = this.disabled
221241
? true
222242
: this.value.trim().length === 0;
@@ -247,6 +267,15 @@ class ChatInput extends LightElement {
247267
if (focus) this.textarea.focus();
248268
}
249269

270+
#updateHeight(): void {
271+
const el = this.textarea;
272+
if (el.scrollHeight == 0) {
273+
return;
274+
}
275+
el.style.height = "auto";
276+
el.style.height = `${el.scrollHeight}px`;
277+
}
278+
250279
setInputValue(
251280
value: string,
252281
{ submit = false, focus = false }: ChatInputSetInputOptions = {}

0 commit comments

Comments
 (0)