import { BaseComponent } from "./ave-base-component.js"; class AveTooltip extends BaseComponent { static get observedAttributes() { return ['text', 'side', 'align', 'trigger-text']; } constructor() { super(); this._tooltipId = `tooltip-${Math.random().toString(36).substr(2, 9)}`; this._triggerElement = null; this._tooltipElement = null; this._showTimeout = null; this._hideTimeout = null; this._handleMouseEnter = this._handleMouseEnter.bind(this); this._handleMouseLeave = this._handleMouseLeave.bind(this); this._handleFocus = this._handleFocus.bind(this); this._handleBlur = this._handleBlur.bind(this); } // Property getters/setters get text() { return this.getAttribute('text') || 'Tooltip text'; } set text(value) { this.setAttribute('text', value); } get side() { return this.getAttribute('side') || 'top'; } set side(value) { this.setAttribute('side', value); } get align() { return this.getAttribute('align') || 'center'; } set align(value) { this.setAttribute('align', value); } get triggerText() { return this.getAttribute('trigger-text') || 'Hover me'; } set triggerText(value) { this.setAttribute('trigger-text', value); } setupEventListeners() { // Set up native event listeners on the trigger element this._triggerElement = this.shadowRoot.querySelector('.tooltip-trigger'); if (this._triggerElement) { this._triggerElement.addEventListener('mouseenter', this._handleMouseEnter); this._triggerElement.addEventListener('mouseleave', this._handleMouseLeave); this._triggerElement.addEventListener('focus', this._handleFocus); this._triggerElement.addEventListener('blur', this._handleBlur); } } _handleMouseEnter() { this._showTooltip(); } _handleMouseLeave() { this._hideTooltip(); } _handleFocus() { this._showTooltip(); } _handleBlur() { this._hideTooltip(); } _showTooltip() { // Clear any existing hide timeout if (this._hideTimeout) { clearTimeout(this._hideTimeout); this._hideTimeout = null; } // Show with slight delay this._showTimeout = setTimeout(() => { this._createTooltipElement(); this._positionTooltip(); // Dispatch show event this._dispatch('ave-tooltip-show', { text: this.text, side: this.side, align: this.align }); }, 300); } _hideTooltip() { // Clear any existing show timeout if (this._showTimeout) { clearTimeout(this._showTimeout); this._showTimeout = null; } this._hideTimeout = setTimeout(() => { if (this._tooltipElement) { this._tooltipElement.remove(); this._tooltipElement = null; } // Dispatch hide event this._dispatch('ave-tooltip-hide', { text: this.text }); }, 100); } _createTooltipElement() { if (this._tooltipElement) return; const tooltip = document.createElement('div'); tooltip.className = `tooltip tooltip-${this.side} tooltip-${this.align}`; tooltip.textContent = this.text; tooltip.style.cssText = ` position: absolute; z-index: 9999; background: var(--popover); color: var(--popover-foreground); border: 1px solid var(--border); border-radius: 6px; padding: 6px 12px; font-size: 14px; line-height: 1.4; white-space: nowrap; box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1); pointer-events: none; opacity: 0; transform: scale(0.95); transition: opacity 150ms, transform 150ms; `; document.body.appendChild(tooltip); this._tooltipElement = tooltip; // Trigger animation requestAnimationFrame(() => { tooltip.style.opacity = '1'; tooltip.style.transform = 'scale(1)'; }); } render() { const slotContent = this.innerHTML; const hasCustomContent = slotContent.trim().length > 0; this.shadowRoot.innerHTML = `
${hasCustomContent ? slotContent : this._getDefaultContent()}
`; this.setupEventListeners(); } _positionTooltip() { if (!this._tooltipElement || !this._triggerElement) return; const triggerRect = this._triggerElement.getBoundingClientRect(); const tooltipRect = this._tooltipElement.getBoundingClientRect(); let top, left; const offset = 8; // Calculate position based on side switch (this.side) { case 'top': top = triggerRect.top - tooltipRect.height - offset; left = this._calculateHorizontalPosition(triggerRect, tooltipRect, this.align); break; case 'bottom': top = triggerRect.bottom + offset; left = this._calculateHorizontalPosition(triggerRect, tooltipRect, this.align); break; case 'left': top = this._calculateVerticalPosition(triggerRect, tooltipRect, this.align); left = triggerRect.left - tooltipRect.width - offset; break; case 'right': top = this._calculateVerticalPosition(triggerRect, tooltipRect, this.align); left = triggerRect.right + offset; break; default: top = triggerRect.top - tooltipRect.height - offset; left = this._calculateHorizontalPosition(triggerRect, tooltipRect, this.align); } // Adjust for viewport boundaries const viewport = { width: window.innerWidth, height: window.innerHeight }; if (left < 0) left = 8; if (left + tooltipRect.width > viewport.width) left = viewport.width - tooltipRect.width - 8; if (top < 0) top = 8; if (top + tooltipRect.height > viewport.height) top = viewport.height - tooltipRect.height - 8; this._tooltipElement.style.top = `${top + window.scrollY}px`; this._tooltipElement.style.left = `${left + window.scrollX}px`; } _calculateHorizontalPosition(triggerRect, tooltipRect, align) { switch (align) { case 'start': return triggerRect.left; case 'end': return triggerRect.right - tooltipRect.width; case 'center': default: return triggerRect.left + (triggerRect.width - tooltipRect.width) / 2; } } _calculateVerticalPosition(triggerRect, tooltipRect, align) { switch (align) { case 'start': return triggerRect.top; case 'end': return triggerRect.bottom - tooltipRect.height; case 'center': default: return triggerRect.top + (triggerRect.height - tooltipRect.height) / 2; } } _getDefaultContent() { return ` `; } disconnectedCallback() { // Clean up timeouts and tooltip element if (this._showTimeout) { clearTimeout(this._showTimeout); } if (this._hideTimeout) { clearTimeout(this._hideTimeout); } if (this._tooltipElement) { this._tooltipElement.remove(); } // Remove event listeners if (this._triggerElement) { this._triggerElement.removeEventListener('mouseenter', this._handleMouseEnter); this._triggerElement.removeEventListener('mouseleave', this._handleMouseLeave); this._triggerElement.removeEventListener('focus', this._handleFocus); this._triggerElement.removeEventListener('blur', this._handleBlur); } } } customElements.define('ave-tooltip', AveTooltip);