|
| 1 | +import { Directive, ElementRef, HostListener, Input, OnDestroy, Renderer2 } from '@angular/core'; |
| 2 | + |
| 3 | +@Directive({ |
| 4 | + selector: '[appTooltip]' |
| 5 | +}) |
| 6 | +export class TooltipDirective implements OnDestroy { |
| 7 | + @Input('appTooltip') tooltipText: string; |
| 8 | + @Input() position: 'top' | 'bottom' | 'left' | 'right' = 'bottom'; |
| 9 | + @Input() tooltipClass = ''; |
| 10 | + |
| 11 | + private tooltip: HTMLElement | null = null; |
| 12 | + private arrow: HTMLElement | null = null; |
| 13 | + private hasBeenShown = false; |
| 14 | + |
| 15 | + constructor(private el: ElementRef, private renderer: Renderer2) {} |
| 16 | + |
| 17 | + @HostListener('mouseenter') onMouseEnter(): void { |
| 18 | + this.show(); |
| 19 | + } |
| 20 | + |
| 21 | + @HostListener('mouseleave') onMouseLeave(): void { |
| 22 | + this.hide(); |
| 23 | + } |
| 24 | + |
| 25 | + @HostListener('focus') onFocus(): void { |
| 26 | + this.show(); |
| 27 | + } |
| 28 | + |
| 29 | + @HostListener('blur') onBlur(): void { |
| 30 | + this.hide(); |
| 31 | + } |
| 32 | + |
| 33 | + private show(): void { |
| 34 | + if (this.tooltip || !this.tooltipText) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + // create tooltip element |
| 39 | + this.tooltip = this.renderer.createElement('div'); |
| 40 | + this.renderer.addClass(this.tooltip, 'app-tooltip'); |
| 41 | + if (this.tooltipClass) { |
| 42 | + this.tooltipClass.split(' ').forEach(cls => { |
| 43 | + if (cls) { |
| 44 | + this.renderer.addClass(this.tooltip, cls); |
| 45 | + } |
| 46 | + }); |
| 47 | + } |
| 48 | + this.renderer.setProperty(this.tooltip, 'innerHTML', this.tooltipText); |
| 49 | + |
| 50 | + // create arrow element |
| 51 | + this.arrow = this.renderer.createElement('div'); |
| 52 | + this.renderer.addClass(this.arrow, 'app-tooltip-arrow'); |
| 53 | + |
| 54 | + // append to body (not to component) |
| 55 | + this.renderer.appendChild(this.tooltip, this.arrow); |
| 56 | + this.renderer.appendChild(document.body, this.tooltip); |
| 57 | + |
| 58 | + // position after a slight delay to ensure proper rendering |
| 59 | + setTimeout(() => { |
| 60 | + this.setPosition(); |
| 61 | + this.renderer.addClass(this.tooltip, 'app-tooltip-visible'); |
| 62 | + this.hasBeenShown = true; |
| 63 | + }, 20); |
| 64 | + } |
| 65 | + |
| 66 | + private hide(): void { |
| 67 | + if (!this.tooltip) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + this.renderer.removeClass(this.tooltip, 'app-tooltip-visible'); |
| 72 | + |
| 73 | + // remove after transition completes |
| 74 | + setTimeout(() => { |
| 75 | + if (this.tooltip && this.tooltip.parentNode) { |
| 76 | + this.renderer.removeChild(document.body, this.tooltip); |
| 77 | + this.tooltip = null; |
| 78 | + this.arrow = null; |
| 79 | + } |
| 80 | + }, 300); |
| 81 | + } |
| 82 | + |
| 83 | + private setPosition(): void { |
| 84 | + if (!this.tooltip) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + const hostRect = this.el.nativeElement.getBoundingClientRect(); |
| 89 | + const tooltipRect = this.tooltip.getBoundingClientRect(); |
| 90 | + |
| 91 | + let top = 0; |
| 92 | + let left = 0; |
| 93 | + |
| 94 | + switch (this.position) { |
| 95 | + case 'top': |
| 96 | + top = hostRect.top - tooltipRect.height - 10; |
| 97 | + left = hostRect.left + (hostRect.width / 2) - (tooltipRect.width / 2); |
| 98 | + this.renderer.addClass(this.arrow, 'app-tooltip-arrow-bottom'); |
| 99 | + break; |
| 100 | + case 'bottom': |
| 101 | + top = hostRect.bottom + 10; |
| 102 | + left = hostRect.left + (hostRect.width / 2) - (tooltipRect.width / 2); |
| 103 | + this.renderer.addClass(this.arrow, 'app-tooltip-arrow-top'); |
| 104 | + break; |
| 105 | + case 'left': |
| 106 | + top = hostRect.top + (hostRect.height / 2) - (tooltipRect.height / 2); |
| 107 | + left = hostRect.left - tooltipRect.width - 10; |
| 108 | + this.renderer.addClass(this.arrow, 'app-tooltip-arrow-right'); |
| 109 | + break; |
| 110 | + case 'right': |
| 111 | + top = hostRect.top + (hostRect.height / 2) - (tooltipRect.height / 2); |
| 112 | + left = hostRect.right + 10; |
| 113 | + this.renderer.addClass(this.arrow, 'app-tooltip-arrow-left'); |
| 114 | + break; |
| 115 | + } |
| 116 | + |
| 117 | + // ensure tooltip is within viewport |
| 118 | + if (top < 0) { |
| 119 | + top = hostRect.bottom + 10; |
| 120 | + this.renderer.removeClass(this.arrow, 'app-tooltip-arrow-bottom'); |
| 121 | + this.renderer.addClass(this.arrow, 'app-tooltip-arrow-top'); |
| 122 | + } |
| 123 | + |
| 124 | + if (left < 0) { |
| 125 | + left = 10; |
| 126 | + } |
| 127 | + |
| 128 | + if (left + tooltipRect.width > window.innerWidth) { |
| 129 | + left = window.innerWidth - tooltipRect.width - 10; |
| 130 | + } |
| 131 | + |
| 132 | + // set arrow position based on host element |
| 133 | + const arrowLeft = hostRect.left - left + (hostRect.width / 2) - 6; |
| 134 | + this.renderer.setStyle(this.arrow, 'left', `${arrowLeft}px`); |
| 135 | + |
| 136 | + // set tooltip position dynamically |
| 137 | + this.renderer.setStyle(this.tooltip, 'top', `${top}px`); |
| 138 | + this.renderer.setStyle(this.tooltip, 'left', `${left}px`); |
| 139 | + } |
| 140 | + |
| 141 | + ngOnDestroy(): void { |
| 142 | + if (this.tooltip && this.tooltip.parentNode) { |
| 143 | + this.renderer.removeChild(document.body, this.tooltip); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments