Download ave-tooltip.js from Avenislo/ave: direct link, hf CLI and curl.
- Browser
- Download file 7.97 kB
-
https://huggingface.co/spaces/Avenislo/ave/resolve/main/ave-tooltip.js
- Command line
-
hf download hf://spaces/Avenislo/ave/ave-tooltip.js
-
curl -L -o ave-tooltip.js https://huggingface.co/spaces/Avenislo/ave/resolve/main/ave-tooltip.js
7.97 kB
| 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 = ` | |
| <style> | |
| :host { | |
| display: inline-block; | |
| } | |
| .tooltip-trigger { | |
| cursor: pointer; | |
| } | |
| </style> | |
| <div class="tooltip-trigger" id="${this._tooltipId}"> | |
| ${hasCustomContent ? slotContent : this._getDefaultContent()} | |
| </div> | |
| `; | |
| 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 ` | |
| <button | |
| class="inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 border border-input bg-background hover:bg-accent hover:text-accent-foreground h-10 px-4 py-2" | |
| > | |
| ${this.triggerText} | |
| </button> | |
| `; | |
| } | |
| 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); | |