I have successfully created a tooltip using Angular 5.2.0 and ngrx. The tooltip, among other things, receives an ElementRef to the target Element when the state updates, allowing me to position it absolutely:
let rect = state.tooltip.target.nativeElement.getBoundingClientRect();
if (rect) {
this.position.left = rect.left;
this.position.top = rect.top;
}
The state.tooltip.target
is of type ElementRef and is obtained by the element triggering the tooltip through @ViewChild:
@ViewChild('linkWithTooltip') tooltipTarget: ElementRef;
openTooltip() {
if (this.tooltipOpen) {
this.tooltipAction.closeTooltip();
} else {
this.tooltipAction.openTooltip({
text: 'foo',
target: this.tooltipTarget
});
}
this.tooltipOpen = !this.tooltipOpen;
}
This is referenced in the template as:
<a #linkWithTooltip href="">Lorem</a>
As explained here and in other sources, I am able to position the tooltip properly. However, to accurately position the tooltip, I need to know its dimensions after rendering, such as for centering it. I require an ElementRef of the Tooltip itself rather than a ViewChild.
How can I obtain the dimensions of the current component? Can I retrieve them using the Component's ElementRef? If so, how can I access the ElementRef?