Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed height calculations when using padding #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 9 additions & 4 deletions src/elastic.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import { Observable, Subscription } from 'rxjs/Rx';
export class ElasticDirective implements OnInit, OnDestroy, AfterViewInit {
private modelSub: Subscription;
private textareaEl: HTMLTextAreaElement;
private totalVerticalPadding = 0;

constructor(
private element: ElementRef,
private ngZone: NgZone,
@Optional() private model: NgModel
) {}
) { }
Copy link
Owner

Choose a reason for hiding this comment

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

Please could you recommit without the extra whitespace?


ngOnInit() {
if(!this.model) {
if (!this.model) {
Copy link
Owner

Choose a reason for hiding this comment

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

As above

return;
}

Expand All @@ -30,7 +31,7 @@ export class ElasticDirective implements OnInit, OnDestroy, AfterViewInit {
}

ngOnDestroy() {
if(this.modelSub) {
if (this.modelSub) {
Copy link
Owner

Choose a reason for hiding this comment

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

As above

this.modelSub.unsubscribe();
}
}
Expand Down Expand Up @@ -64,6 +65,10 @@ export class ElasticDirective implements OnInit, OnDestroy, AfterViewInit {
private setupTextarea(textareaEl: HTMLTextAreaElement) {
this.textareaEl = textareaEl;

const paddingTop = window.getComputedStyle(this.textareaEl).getPropertyValue('padding-top').replace('px', '');
const paddingBottom = window.getComputedStyle(this.textareaEl).getPropertyValue('padding-bottom').replace('px', '');
this.totalVerticalPadding = +paddingTop + +paddingBottom;

Copy link
Owner

Choose a reason for hiding this comment

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

I'd prefer to make a few changes here.

  • Let's cache the result of getComputedStyle in a variable so it isn't calculated twice.
  • Let's use parseInt() instead of the unary + operator, as this is consistent with elsewhere in the file.

// Set some necessary styles
const style = this.textareaEl.style;
style.overflow = 'hidden';
Expand All @@ -87,6 +92,6 @@ export class ElasticDirective implements OnInit, OnDestroy, AfterViewInit {
}

this.textareaEl.style.height = 'auto';
this.textareaEl.style.height = this.textareaEl.scrollHeight + "px";
this.textareaEl.style.height = this.textareaEl.scrollHeight - this.totalVerticalPadding + "px";
Copy link
Owner

Choose a reason for hiding this comment

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

The most recent commit uses a backtick string so let's do the same here.

}
}