I have a container with header and main block inside, like this
<div #container class="container">
<div #header class="header">...</div>
<div class="main">
<lp-chart [chartHeight]="chartHeight"></lp-chart>
</div>
</div>
And my goal is to detect #header height changes and set height to #main component.
So you can imagine,
the whole #container is - 100%,
#header, for example, 50px,
and el inside #main block should be `100% - 50px`
And everything works almost fine, except, on the left part of the page I have sidebar which can be opened or collapsed, so window is not resized, but header will change it's height depends on that,
but my MutationObserver doesn't detect it.
This detailsHeader change it's height in css as max-height: 150px;,(so while sidebar collapsed header height 50px, if opened - expands to 150) and I think thats the reason, but maybe I miss something.
Here's some part of my code:
service
setupHeightMutationObserver(el: ElementRef): Observable<HeightAndWidth> {
const observerable$ = new Observable<HeightAndWidth>(observer => {
const callback = () => observer.next(this.getHeightAndWidthObject(el));
const elementObserver = new MutationObserver(callback);
const config = { attributes: true, childList: true, subtree: true };
elementObserver.observe(el.nativeElement, config);
});
return observerable$
.pipe(
debounceTime(50),
distinctUntilChanged()
);
}
private getHeightAndWidthObject(el: ElementRef): HeightAndWidth {
const newValues = new HeightAndWidth();
newValues.height = el.nativeElement.getBoundingClientRect().height;
newValues.width = el.nativeElement.offsetWidth;
return newValues;
}
component
@ViewChild('details') private details: ElementRef;
@ViewChild('detailsHeader') private detailsHeader: ElementRef;
ngAfterViewInit(): void {
this.subscription$ = this.elementResizeService.setupHeightMutationObserver(this.detailsHeader)
.subscribe((newValues: HeightAndWidth) => this.calculateChartSize(newValues));
}
calculateChartSize(newValues: HeightAndWidth): void {
const detailsHeader = this.elementResizeService.doDivHeightChange(newValues);
const detailsHeight = this.details.nativeElement.getBoundingClientRect().height;
const height = detailsHeight - detailsHeader.height - 10;
this.chartHeight = height;
}
ngOnDestroy(): void {
this.subscription$.unsubscribe();
}
