Hello,
I have a fully functional Tooltip directive that is implemented with CDK Portal and Overlay.
Once I hover on an element the tooltip appears, but I want to delay the attach a bit. If I try to attach the portal inside a setTimeout it will give the error: Host already has a portal attached.
There is even an extra validation if a portal was attach to prevent the issue happening, but it still gets attached multiple times after a while.
private _overlayRef: OverlayRef | undefined;
private _tooltipRef: ComponentRef<TooltipContainerComponent> | undefined;
private _tooltipPortal: ComponentPortal<TooltipContainerComponent> = new ComponentPortal(TooltipContainerComponent);
@HostListener('mouseenter')
show(): void {
if (!this._overlayRef) {
return;
}
// Check if it is already attached
if (!this._tooltipPortal.isAttached) {
setTimeout(() => {
this._tooltipRef = this._overlayRef?.attach(this._tooltipPortal);
}, 500);
}
}
@HostListener('mouseleave')
hide(): void {
if (this._overlayRef) {
this._overlayRef.detach();
}
}
ngOnDestroy(): void {
if (this._overlayRef) {
this._overlayRef.dispose();
}
}