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 = `