Skip to content

Instantly share code, notes, and snippets.

@maxt3r
Created November 28, 2016 17:39
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save maxt3r/2485356e91a1969bdb6cf54902e61165 to your computer and use it in GitHub Desktop.
Save maxt3r/2485356e91a1969bdb6cf54902e61165 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 } from "@angular/core";
@Directive({
selector: "ion-textarea[autoresize]" // Attribute selector
})
export class Autoresize {
@HostListener("input", ["$event.target"])
onInput(textArea: HTMLTextAreaElement): void {
this.adjust();
}
constructor(public element: ElementRef) {
}
ngOnInit(): void {
this.adjust();
}
adjust(): void {
let ta = this.element.nativeElement.querySelector("textarea");
ta.style.overflow = "hidden";
ta.style.height = "auto";
ta.style.height = ta.scrollHeight + "px";
}
}
@yolier
Copy link

yolier commented Dec 4, 2016

Thanks a lot for putting this up. Exactly what I needed right now and it works like a charm :-).

@yolier
Copy link

yolier commented Dec 4, 2016

Ok one small thing: you might wanna check if ta is null in the adjust() method.
I got a Uncaught TypeError: Cannot read property 'style' of null in some cases after leaving the screen that contained the <ion-textarea>. Checking ta fixed that for me.

@kikill95
Copy link

I have used if (ta) { ... } in typescript@2.0.9, @angular/core@2.2.1
It didn't work in the initial state without this for me

@Eetezadi
Copy link

Following the previous commentators, I just did this:

https://gist.github.com/Eetezadi/368658ca4364354cfca768f24e2ac810

@stefan-beyer
Copy link

stefan-beyer commented Apr 11, 2017

I think there should be a little timeout before
ta.style.height = ta.scrollHeight + "px";
to give it a little time to recalculate the scrollHeight.
For me, the ScrollHeight was not correct at initial run.

I did this:

setTimeout(() => {
    ta.style.height = ta.scrollHeight + "px";
}, 100);

@salmanyaqoob
Copy link

how to use autoresize.ts file under my ionic project?

@captaincole
Copy link

Thanks for this. For something small like this I would rather have an in-house impl than another module I have to import.

@kuldeepsmaurya
Copy link

Where to put the file autoresize.ts and how to use it?

@maartenvandillen
Copy link

import the file and add it to

@NgModule({
  declarations: [
    Autoresize
  ]
})

@bruno-campos
Copy link

bruno-campos commented Jul 27, 2017

Thanks for this :) Made some changes in here:
https://gist.github.com/bruno-campos/14a0977743ebda598936a246a55fabb3

Basically I wanted it to work having a maximum height and also don't override my initial css height value

Usage:

<ion-textarea autoresize="100"></ion-textarea>

For a 100px maximum height

@alquifran
Copy link

Thanks to all, this works beautifully.

@kinoli
Copy link

kinoli commented Sep 13, 2017

I've updated @bruno-campos addition to allow you to optionally add a maximum height for the textarea.
https://gist.github.com/kinoli/da7aee28934ec5728fb28d1ac3548230

<ion-textarea autoresize [(ngModel)]="body"></ion-textarea>
<ion-textarea autoresize="100" [(ngModel)]="body"></ion-textarea>

Enjoy!

@IgorSamer
Copy link

Amazing! Thanks! Working perfectly with Ionic 3

@rdethurens
Copy link

rdethurens commented Feb 15, 2018

Hi everyone,

I'm using this tricks for months but a user report me a bug. When you put lots of new lines, the apps seems to be moving to the top : my header is going under Status bar and a blank space appears in the bottom.

2018-02-15 11 49 27

Has anyone this kind of bug ? If not could you send me your Status bar config. Here is mine :

<plugin name="cordova-plugin-statusbar" spec="^2.4.1" />
<preference name="StatusBarBackgroundColor" value="#000000" />
    <preference name="StatusBarOverlaysWebView" value="false" />
    <preference name="StatusBarStyle" value="lightcontent" />
    <preference name="StatusBarDefaultScrollToTop" value="false" />

Thanks in advance

@orengbasha
Copy link

@kinoli Thaank you sir its working fine in ionic 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment