Skip to content

Instantly share code, notes, and snippets.

@bruno-campos
Forked from maxt3r/autoresize.ts
Last active August 22, 2018 14:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bruno-campos/14a0977743ebda598936a246a55fabb3 to your computer and use it in GitHub Desktop.
Save bruno-campos/14a0977743ebda598936a246a55fabb3 to your computer and use it in GitHub Desktop.
Ionic 2 ion-textarea autoresize
// An autoresize directive that works with ion-textarea in Ionic 2
// Usage example: <ion-textarea autoresize [(ngModel)]="body"></ion-textarea>
// Based on https://www.npmjs.com/package/angular2-autosize
import { Directive, HostListener, ElementRef, Input } from '@angular/core';
@Directive({
selector: 'ion-textarea[autoresize]'
})
export class AutoresizeDirective {
@HostListener('input', ['$event.target'])
onInput(textArea: HTMLTextAreaElement): void {
this.adjust();
}
@Input('autoresize') maxHeight: number;
constructor(public element: ElementRef) {
}
ngOnInit(): void {
this.adjust();
}
adjust(): void {
let ta = this.element.nativeElement.querySelector("textarea");
if(ta) {
ta.style.overflow = "hidden";
ta.style.height = null;
ta.style.height = Math.min(ta.scrollHeight, this.maxHeight) + "px";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment