Skip to content

Commit

Permalink
Set column px width on user column resize
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowbas authored and bas080 committed Feb 6, 2025
1 parent 7628984 commit cb46f0b
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/app/resizable-button/resizable-button.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
// ---------- END RUNBOX LICENSE ----------

import { Component, ElementRef, EventEmitter, Output, AfterViewInit, Input, HostListener } from '@angular/core';
import { Subject } from 'rxjs';
import { take } from 'rxjs/operators';

const userResize = new Subject()

@Component({
selector: 'app-resizable-button',
Expand All @@ -39,31 +43,34 @@ export class ResizableButtonComponent implements AfterViewInit {
private onMouseUpListener: () => void;
private initialWidth: string | null = null;

constructor(private elementRef: ElementRef) {}
constructor(private elementRef: ElementRef) {
// Only set absolute value when the user does a resize.
userResize.pipe(take(1)).subscribe(() => {
this.setAbsoluteWidth();
});
}

ngAfterViewInit() {
this.initialWidth = this.parentElement.style.width
console.log(this.initialWidth)
this.setAbsoluteWidth()
}

get parentElement() {
return this.elementRef.nativeElement.parentElement;
}

setAbsoluteWidth() {
if (!this.parentElement) return
setTimeout(() => {
if (!this.parentElement) return

this.widthChange.emit(this.parentElement.offsetWidth);
this.changeWidth(this.parentElement.offsetWidth);
}, 0)
}

resetWidth() {
const parentElement = this.elementRef.nativeElement.parentElement;

parentElement.style.width = this.initialWidth;
setTimeout(() => {
this.setAbsoluteWidth()
}, 10)
this.setAbsoluteWidth()
}

@HostListener('window:resize')
Expand Down Expand Up @@ -98,7 +105,7 @@ export class ResizableButtonComponent implements AfterViewInit {
if (parentElement) {
const diff = event.clientX - this.startX;
const newWidth = this.startWidth + diff;
this.widthChange.emit(newWidth);
this.changeWidth(newWidth);
}
}

Expand All @@ -120,12 +127,17 @@ export class ResizableButtonComponent implements AfterViewInit {
const currentWidth = parentElement.offsetWidth;

if (event.key === 'ArrowRight') {
this.widthChange.emit(currentWidth + step);
this.changeWidth(currentWidth + step);
} else if (event.key === 'ArrowLeft') {
this.widthChange.emit(currentWidth - step);
this.changeWidth(currentWidth - step);
}
}

changeWidth(pixels: number) {
this.widthChange.emit(pixels)
userResize.next(pixels)
}

@HostListener('window:blur')
@HostListener('window:focus')
onWindowFocus(): void {
Expand Down

0 comments on commit cb46f0b

Please sign in to comment.