Skip to content
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
35 changes: 32 additions & 3 deletions js/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ interface ChatInputSetInputOptions {
}

class ChatInput extends LightElement {
private _disabled = false;

@property() placeholder = "Enter a message...";
// disabled is reflected manually because `reflect: true` doesn't work with LightElement
@property({ type: Boolean })
Expand All @@ -155,6 +153,27 @@ class ChatInput extends LightElement {
this.#onInput();
}

private _disabled = false;
inputVisibleObserver?: IntersectionObserver;

connectedCallback(): void {
super.connectedCallback();

this.inputVisibleObserver = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) this.#updateHeight();
});
});

this.inputVisibleObserver.observe(this);
}

disconnectedCallback(): void {
super.disconnectedCallback();
this.inputVisibleObserver?.disconnect();
this.inputVisibleObserver = undefined;
}

attributeChangedCallback(
name: string,
_old: string | null,
Expand Down Expand Up @@ -189,7 +208,7 @@ class ChatInput extends LightElement {
return html`
<textarea
id="${this.id}"
class="form-control textarea-autoresize"
class="form-control"
rows="1"
placeholder="${this.placeholder}"
@keydown=${this.#onKeyDown}
Expand Down Expand Up @@ -217,6 +236,7 @@ class ChatInput extends LightElement {
}

#onInput(): void {
this.#updateHeight();
this.button.disabled = this.disabled
? true
: this.value.trim().length === 0;
Expand Down Expand Up @@ -247,6 +267,15 @@ class ChatInput extends LightElement {
if (focus) this.textarea.focus();
}

#updateHeight(): void {
const el = this.textarea;
if (el.scrollHeight == 0) {
return;
}
el.style.height = "auto";
el.style.height = `${el.scrollHeight}px`;
}

setInputValue(
value: string,
{ submit = false, focus = false }: ChatInputSetInputOptions = {}
Expand Down
Loading
Loading