ionic4-reactive-textarea
Version:
Angular auto-resize text-area directive
41 lines (35 loc) • 1.21 kB
text/typescript
import { ElementRef, Directive, OnInit, OnDestroy } from '@angular/core';
({
selector: 'ion-textarea[reactive]',
})
export class ReactiveTextAreaDirective implements OnInit, OnDestroy {
mutationObserver: MutationObserver;
constructor(public element: ElementRef) {
const resize = () => {
setTimeout(() => {
const shadowRoot: ShadowRoot = this.element.nativeElement.shadowRoot;
const textArea = shadowRoot.children.item(1) as HTMLTextAreaElement;
const resizeHeight = this.element.nativeElement.getAttribute(
'reactive',
);
textArea.style.overflow = 'hidden';
textArea.style.height = 'auto';
textArea.style.height =
+textArea.scrollHeight > +resizeHeight
? resizeHeight + 'px'
: textArea.scrollHeight + 'px';
});
};
this.mutationObserver = new MutationObserver(resize);
}
ngOnInit(): void {
this.mutationObserver.observe(this.element.nativeElement.shadowRoot, {
characterData: true,
childList: true,
subtree: true,
});
}
ngOnDestroy(): void {
this.mutationObserver.disconnect();
}
}