blob: f8c855c16aa1289274eeb930aa07f7da062ba9f1 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
// Thanks: https://github.com/evseevdev/ngx-textarea-autosize
import { AfterViewInit, Directive, ElementRef, HostBinding, HostListener } from '@angular/core'
@Directive({
selector: 'textarea[myAutoResize]'
})
export class TextareaAutoResizeDirective implements AfterViewInit {
@HostBinding('attr.rows') rows = '1'
@HostBinding('style.overflow') overflow = 'hidden'
constructor (private elem: ElementRef) { }
public ngAfterViewInit () {
this.resize()
}
@HostListener('input')
resize () {
const textarea = this.elem.nativeElement as HTMLTextAreaElement
// Reset textarea height to auto that correctly calculate the new height
textarea.style.height = 'auto'
// Set new height
textarea.style.height = `${textarea.scrollHeight}px`
}
}
|