|
| 1 | +import type { OnDestroy } from '@angular/core'; |
| 2 | +import { |
| 3 | + Directive, |
| 4 | + effect, |
| 5 | + ElementRef, |
| 6 | + HostListener, |
| 7 | + inject, |
| 8 | + input, |
| 9 | + SecurityContext, |
| 10 | +} from '@angular/core'; |
| 11 | +import { DomSanitizer } from '@angular/platform-browser'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Directive to create a tooltip on hover. |
| 15 | + * |
| 16 | + * @example |
| 17 | + * ````html |
| 18 | + * <h3 |
| 19 | + * libTooltip="Hello, friend. <br/> Nice to meet you" |
| 20 | + * [tooltipClass]="'bg-blue-500 text-white'" |
| 21 | + * > |
| 22 | + * Hover me and you'll see |
| 23 | + * </h3> |
| 24 | + * ```` |
| 25 | + * |
| 26 | + * @author louiiuol |
| 27 | + * @version 0.0.1 |
| 28 | + */ |
| 29 | +@Directive({ |
| 30 | + selector: '[libTooltip]', |
| 31 | + standalone: true, |
| 32 | +}) |
| 33 | +export class TooltipDirective implements OnDestroy { |
| 34 | + /** |
| 35 | + * Required input for the tooltip content. |
| 36 | + */ |
| 37 | + libTooltip = input.required<string>(); |
| 38 | + |
| 39 | + /** |
| 40 | + * Optional input for the Tailwind CSS classes of the tooltip. |
| 41 | + */ |
| 42 | + tooltipClass = input<string>(); |
| 43 | + |
| 44 | + private tooltipElement!: HTMLElement; |
| 45 | + private readonly elementRef = inject(ElementRef); |
| 46 | + private readonly sanitizer = inject(DomSanitizer); |
| 47 | + |
| 48 | + constructor() { |
| 49 | + // Create the tooltip element |
| 50 | + this.createTooltipElement(); |
| 51 | + |
| 52 | + // Set up effects to react to changes in content and class inputs |
| 53 | + this.setupContentEffect(); |
| 54 | + this.setupClassEffect(); |
| 55 | + } |
| 56 | + |
| 57 | + private createTooltipElement() { |
| 58 | + this.tooltipElement = document.createElement('div'); |
| 59 | + // Add default Tailwind CSS classes for styling |
| 60 | + this.tooltipElement.classList.add( |
| 61 | + 'absolute', |
| 62 | + 'bg-gray-800', |
| 63 | + 'text-white', |
| 64 | + 'text-sm', |
| 65 | + 'py-1', |
| 66 | + 'px-2', |
| 67 | + 'rounded', |
| 68 | + 'opacity-0', |
| 69 | + 'transition-opacity', |
| 70 | + 'duration-200', |
| 71 | + 'pointer-events-none', |
| 72 | + 'z-50', |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + private setupContentEffect() { |
| 77 | + effect(() => { |
| 78 | + const content = this.libTooltip(); |
| 79 | + |
| 80 | + // Sanitize the HTML content |
| 81 | + const sanitizedContent = |
| 82 | + this.sanitizer.sanitize(SecurityContext.HTML, content) ?? ''; |
| 83 | + |
| 84 | + // Set the sanitized HTML content |
| 85 | + this.tooltipElement.innerHTML = sanitizedContent; |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + private setupClassEffect() { |
| 90 | + effect(() => { |
| 91 | + const customClasses = this.tooltipClass(); |
| 92 | + // Remove all classes except the default ones |
| 93 | + const defaultClasses = [ |
| 94 | + 'absolute', |
| 95 | + 'opacity-0', |
| 96 | + 'transition-opacity', |
| 97 | + 'duration-200', |
| 98 | + 'pointer-events-none', |
| 99 | + 'z-50', |
| 100 | + 'bg-gray-800', |
| 101 | + 'text-white', |
| 102 | + 'text-sm', |
| 103 | + 'py-1', |
| 104 | + 'px-2', |
| 105 | + 'rounded', |
| 106 | + ]; |
| 107 | + this.tooltipElement.className = ''; |
| 108 | + this.tooltipElement.classList.add(...defaultClasses); |
| 109 | + |
| 110 | + // Add custom Tailwind CSS classes if provided |
| 111 | + if (customClasses) { |
| 112 | + this.tooltipElement.classList.add(...customClasses.split(' ')); |
| 113 | + } |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + @HostListener('mouseenter') |
| 118 | + onMouseEnter() { |
| 119 | + document.body.appendChild(this.tooltipElement); |
| 120 | + this.updateTooltipPosition(); |
| 121 | + this.tooltipElement.classList.remove('opacity-0'); |
| 122 | + this.tooltipElement.classList.add('opacity-100'); |
| 123 | + } |
| 124 | + |
| 125 | + @HostListener('mouseleave') |
| 126 | + onMouseLeave() { |
| 127 | + this.tooltipElement.classList.remove('opacity-100'); |
| 128 | + this.tooltipElement.classList.add('opacity-0'); |
| 129 | + setTimeout(() => { |
| 130 | + if (this.tooltipElement.parentNode) { |
| 131 | + this.tooltipElement.parentNode.removeChild(this.tooltipElement); |
| 132 | + } |
| 133 | + }, 200); // Match the transition duration |
| 134 | + } |
| 135 | + |
| 136 | + @HostListener('mousemove') |
| 137 | + onMouseMove() { |
| 138 | + this.updateTooltipPosition(); |
| 139 | + } |
| 140 | + |
| 141 | + private updateTooltipPosition() { |
| 142 | + const hostPos = this.elementRef.nativeElement.getBoundingClientRect(); |
| 143 | + const tooltipPos = this.tooltipElement.getBoundingClientRect(); |
| 144 | + |
| 145 | + // Calculate the position of the tooltip |
| 146 | + const top = hostPos.top - tooltipPos.height - 8 + window.scrollY; |
| 147 | + const left = |
| 148 | + hostPos.left + (hostPos.width - tooltipPos.width) / 2 + window.scrollX; |
| 149 | + |
| 150 | + // Set the position |
| 151 | + this.tooltipElement.style.top = `${top}px`; |
| 152 | + this.tooltipElement.style.left = `${left}px`; |
| 153 | + } |
| 154 | + |
| 155 | + ngOnDestroy() { |
| 156 | + if (this.tooltipElement.parentNode) { |
| 157 | + this.tooltipElement.parentNode.removeChild(this.tooltipElement); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments