Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
<label class="form-label d-none" [for]="id">{{ label() | translate }}</label>
<textarea
#textInput
cdkTextareaAutosize
cdkAutosizeMinRows="1"
cdkAutosizeMaxRows="8"
class="chat-textarea w-100 border-0"
rows="1"
[id]="id"
Expand All @@ -27,7 +30,6 @@
[maxlength]="maxLength() || null"
[(ngModel)]="value"
(keydown)="onKeyDown($event)"
(input)="adjustTextareaHeight($event)"
></textarea>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
.chat-textarea {
// Prevent layout shift on first input
min-block-size: 1lh;
max-block-size: 30vh;
font-family: inherit;
padding-block: 0;
display: block;
outline: none;
resize: none;
overflow-y: auto;
Comment thread
chintankavathia marked this conversation as resolved.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is needed

background-color: transparent;

&::placeholder {
Expand Down
41 changes: 7 additions & 34 deletions projects/element-ng/chat-messages/si-chat-input.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: MIT
*/
import { CdkMenuTrigger } from '@angular/cdk/menu';
import { CdkTextareaAutosize } from '@angular/cdk/text-field';
import {
AfterViewInit,
booleanAttribute,
Expand Down Expand Up @@ -80,6 +81,7 @@ export interface ChatInputAttachment extends Attachment {
selector: 'si-chat-input',
imports: [
CdkMenuTrigger,
CdkTextareaAutosize,
FormsModule,
SiIconComponent,
SiTranslatePipe,
Expand Down Expand Up @@ -436,23 +438,14 @@ export class SiChatInputComponent implements AfterViewInit {

ngAfterViewInit(): void {
const textarea = this.textInput();
if (textarea?.nativeElement) {
this.setTextareaHeight(textarea.nativeElement);

if (this.autoFocus()) {
// Use setTimeout to ensure the element is fully rendered
setTimeout(() => {
textarea.nativeElement.focus();
}, 0);
}
if (textarea?.nativeElement && this.autoFocus()) {
// Use setTimeout to ensure the element is fully rendered
setTimeout(() => {
textarea.nativeElement.focus();
}, 0);
}
}

protected adjustTextareaHeight(event: Event): void {
const textarea = event.target as HTMLTextAreaElement;
this.setTextareaHeight(textarea);
}

/**
* Focus the textarea input
*/
Expand Down Expand Up @@ -487,24 +480,4 @@ export class SiChatInputComponent implements AfterViewInit {
event.stopPropagation();
this.dragOver = true;
}

private setTextareaHeight(textarea: HTMLTextAreaElement): void {
textarea.style.blockSize = 'auto';

const computedStyle = window.getComputedStyle(textarea);
const lineHeight =
parseInt(computedStyle.lineHeight, 10) || parseInt(computedStyle.fontSize, 10) * 1.2;
const paddingTop = parseInt(computedStyle.paddingBlockStart, 10) || 0;
const paddingBottom = parseInt(computedStyle.paddingBlockEnd, 10) || 0;
const minHeight = lineHeight + paddingTop + paddingBottom;

const viewportHeight = window.innerHeight;
const maxViewportHeight = viewportHeight * 0.3;
const maxLinesHeight = lineHeight * 8;
const maxHeight = Math.min(maxViewportHeight, maxLinesHeight) + paddingTop + paddingBottom;

const scrollHeight = textarea.scrollHeight;
const finalHeight = Math.max(Math.min(scrollHeight, maxHeight), minHeight);
textarea.style.height = finalHeight + 'px';
}
}
Loading