Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/louiiuol/ngx-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
louiiuol committed Oct 28, 2024
2 parents 15c1006 + 37c7eee commit 1fbdf71
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/app/directives/tooltip.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Directive, ElementRef, HostListener, Input, Renderer2 } from '@angular/core';

@Directive({
selector: '[libTooltip]',
standalone: true
})
export class TooltipDirective {
@Input('libTooltip') tooltipContent: string = '';
private tooltipElement: HTMLElement | null = null;

constructor(private el: ElementRef, private renderer: Renderer2) {}

@HostListener('mouseenter') onMouseEnter() {
if (!this.tooltipElement) {
this.showTooltip();
}
}

@HostListener('mouseleave') onMouseLeave() {
this.hideTooltip();
}

private showTooltip() {
this.tooltipElement = this.renderer.createElement('div');
this.renderer.appendChild(
this.tooltipElement,
this.renderer.createText(this.tooltipContent)
);

// Add HTML support
this.tooltipElement.innerHTML = this.tooltipContent;

this.renderer.appendChild(document.body, this.tooltipElement);
this.renderer.addClass(this.tooltipElement, 'tooltip');

const hostPos = this.el.nativeElement.getBoundingClientRect();
const tooltipPos = this.tooltipElement.getBoundingClientRect();

const top = hostPos.top - tooltipPos.height - 8;
const left = hostPos.left + (hostPos.width - tooltipPos.width) / 2;

this.renderer.setStyle(this.tooltipElement, 'position', 'absolute');
this.renderer.setStyle(this.tooltipElement, 'top', `${top}px`);
this.renderer.setStyle(this.tooltipElement, 'left', `${left}px`);
this.renderer.setStyle(this.tooltipElement, 'z-index', '1000');
}

private hideTooltip() {
if (this.tooltipElement) {
this.renderer.removeChild(document.body, this.tooltipElement);
this.tooltipElement = null;
}
}
}

0 comments on commit 1fbdf71

Please sign in to comment.