I am new to angular2 and I have been trying to create a resizable div (vertically). but I am unable to achieve that. What I have tried using a directive
and this is my directive
import { Directive, HostListener, ElementRef, Input } from '@angular/core';
@Directive({
  selector: '[appNgxResizer]'
})
export class NgxResizerDirective {
  constructor(private el: ElementRef) {
  }
  @HostListener('mousemove', ['$event']) resize(e) {
    this.el.nativeElement.parentNode.style.height = (e.clientY - this.el.nativeElement.parentNode.offsetTop) + 'px';
    event.preventDefault();
  }
  @HostListener('mouseup', ['$event']) stopResize(e) {
    event.preventDefault();
  }
}
Here is the stackblitz for what I have tried https://stackblitz.com/edit/angular-text-resizable-q6ddyy
I want to click grab to resize the div. Something like this https://jsfiddle.net/zv2ep6eo/.
Thanks.
I think you miss the part where you keep the old value of the height, plus check the state on mouseup and also to listen to mousedown. I didn't make it to a directive in the example bellow, but it would be complicated.
Stackblitz example
Typescript:
  height = 150;
  y = 100;
  oldY = 0;
  grabber = false;
  @HostListener('document:mousemove', ['$event'])
  onMouseMove(event: MouseEvent) {
    if (!this.grabber) {
        return;
    }
    this.resizer(event.clientY - this.oldY);
    this.oldY = event.clientY;
  }
  @HostListener('document:mouseup', ['$event'])
  onMouseUp(event: MouseEvent) {
    this.grabber = false;
  }
  resizer(offsetY: number) {
    this.height += offsetY;
  }
  @HostListener('document:mousedown', ['$event'])
  onMouseDown(event: MouseEvent) {
    this.grabber = true;
    this.oldY = event.clientY;
    event.preventDefault();
  }
HTML
<div class="textarea" [style.height.px]='height' contenteditable="true" >
  this is a text area
  <div class="grabber"></div>  
</div>
                        Using @Vega's Answer - a directive.
import { Directive, HostListener, ElementRef, OnInit } from '@angular/core';
@Directive({
  selector: '[resizer]'
})
export class NgxResizerDirective implements OnInit {
  height: number;
  oldY = 0;
  grabber = false;
  constructor(private el: ElementRef) { }
  @HostListener('document:mousemove', ['$event'])
  onMouseMove(event: MouseEvent) {
    if (!this.grabber) {
      return;
    }
    this.resizer(event.clientY - this.oldY);
    this.oldY = event.clientY;
  }
  @HostListener('document:mouseup', ['$event'])
  onMouseUp(event: MouseEvent) {
    this.grabber = false;
  }
  resizer(offsetY: number) {
    this.height += offsetY;
    this.el.nativeElement.parentNode.style.height = this.height + "px";
  }
  @HostListener('mousedown', ['$event']) onResize(event: MouseEvent, resizer?: Function) {
    this.grabber = true;
    this.oldY = event.clientY;
    event.preventDefault();
  }
  ngOnInit() {
    this.height = parseInt(this.el.nativeElement.parentNode.offsetHeight);
  }
}
HTML
<div class="textarea" contenteditable="true">
  this is a text area
  <div class="grabber" resizer contenteditable="false" ></div>
</div>
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With