|
| 1 | +import { |
| 2 | + autoUpdate, |
| 3 | + computePosition, |
| 4 | + flip, |
| 5 | + offset, |
| 6 | + shift, |
| 7 | +} from '@floating-ui/dom'; |
| 8 | +import type { TemplateResult } from 'lit'; |
| 9 | +import { css, html, LitElement } from 'lit'; |
| 10 | +import { property } from 'lit/decorators.js'; |
| 11 | +import baseStyles from './styles/base'; |
| 12 | + |
| 13 | +/** |
| 14 | + * A simple tooltip component based on Lit and floating-ui. |
| 15 | + * |
| 16 | + * @example |
| 17 | + * ```html |
| 18 | + * <base-tooltip content="This is a tooltip"> |
| 19 | + * <button>Hover me</button> |
| 20 | + * </base-tooltip> |
| 21 | + * ``` |
| 22 | + */ |
| 23 | +export class BaseTooltip extends LitElement { |
| 24 | + /** |
| 25 | + * The content to display in the tooltip. |
| 26 | + */ |
| 27 | + @property({ type: String }) |
| 28 | + content: string | TemplateResult = ''; |
| 29 | + |
| 30 | + private tooltipEl?: HTMLElement; |
| 31 | + private cleanupFn?: () => void; |
| 32 | + |
| 33 | + override connectedCallback(): void { |
| 34 | + super.connectedCallback(); |
| 35 | + this.addEventListener('mouseenter', this._showTooltip); |
| 36 | + this.addEventListener('mouseleave', this._hideTooltip); |
| 37 | + } |
| 38 | + |
| 39 | + override disconnectedCallback(): void { |
| 40 | + super.disconnectedCallback(); |
| 41 | + this.removeEventListener('mouseenter', this._showTooltip); |
| 42 | + this.removeEventListener('mouseleave', this._hideTooltip); |
| 43 | + |
| 44 | + // Clean up positioning if needed |
| 45 | + if (this.cleanupFn) { |
| 46 | + this.cleanupFn(); |
| 47 | + this.cleanupFn = undefined; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + override firstUpdated(): void { |
| 52 | + // Get tooltip element after the component is rendered |
| 53 | + this.tooltipEl = this.shadowRoot?.querySelector('.tooltip') as HTMLElement; |
| 54 | + } |
| 55 | + |
| 56 | + private _showTooltip = (): void => { |
| 57 | + if (!this.tooltipEl) return; |
| 58 | + |
| 59 | + this.tooltipEl.classList.add('show'); |
| 60 | + this._updatePosition(); |
| 61 | + }; |
| 62 | + |
| 63 | + private _hideTooltip = (): void => { |
| 64 | + if (!this.tooltipEl) return; |
| 65 | + |
| 66 | + this.tooltipEl.classList.remove('show'); |
| 67 | + |
| 68 | + // Clean up positioning |
| 69 | + if (this.cleanupFn) { |
| 70 | + this.cleanupFn(); |
| 71 | + this.cleanupFn = undefined; |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + private _updatePosition(): void { |
| 76 | + if (!this.tooltipEl) return; |
| 77 | + |
| 78 | + const floating = this.tooltipEl; |
| 79 | + |
| 80 | + // Clean up previous positioning if any |
| 81 | + if (this.cleanupFn) { |
| 82 | + this.cleanupFn(); |
| 83 | + } |
| 84 | + |
| 85 | + // Use autoUpdate to reposition on scroll/resize |
| 86 | + this.cleanupFn = autoUpdate(this, floating, async () => { |
| 87 | + // Calculate the position |
| 88 | + const middlewares = [ |
| 89 | + offset(8), // 8px distance from trigger |
| 90 | + flip(), // flip to other side if needed |
| 91 | + shift({ padding: 5 }), // shift to keep in viewport |
| 92 | + ]; |
| 93 | + |
| 94 | + // Calculate position |
| 95 | + const { x, y } = await computePosition(this, floating, { |
| 96 | + placement: 'top', // Default placement is top |
| 97 | + middleware: middlewares, |
| 98 | + }); |
| 99 | + |
| 100 | + // Position the tooltip |
| 101 | + Object.assign(floating.style, { |
| 102 | + left: `${x}px`, |
| 103 | + top: `${y}px`, |
| 104 | + }); |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + override render() { |
| 109 | + return html` |
| 110 | + <slot></slot> |
| 111 | + <div class="tooltip"> |
| 112 | + ${this.content} |
| 113 | + </div> |
| 114 | + `; |
| 115 | + } |
| 116 | + |
| 117 | + static override styles = [ |
| 118 | + ...baseStyles, |
| 119 | + css` |
| 120 | + :host { |
| 121 | + display: inline-block; |
| 122 | + } |
| 123 | +
|
| 124 | + ::slotted(*) { |
| 125 | + display: inline-block; |
| 126 | + } |
| 127 | +
|
| 128 | + .tooltip { |
| 129 | + position: absolute; |
| 130 | + background: rgba(0, 0, 0, 0.8); |
| 131 | + color: white; |
| 132 | + padding: 5px 10px; |
| 133 | + border-radius: 4px; |
| 134 | + font-size: 0.875em; |
| 135 | + max-width: 300px; |
| 136 | + width: auto; |
| 137 | + z-index: 10; |
| 138 | + opacity: 0; |
| 139 | + pointer-events: none; |
| 140 | + transition: opacity 0.2s; |
| 141 | + top: 0; |
| 142 | + left: 0; |
| 143 | + white-space: normal; |
| 144 | + word-wrap: break-word; |
| 145 | + } |
| 146 | +
|
| 147 | + .tooltip.show { |
| 148 | + opacity: 1; |
| 149 | + } |
| 150 | + `, |
| 151 | + ]; |
| 152 | +} |
| 153 | + |
| 154 | +customElements.get('base-tooltip') || |
| 155 | + customElements.define('base-tooltip', BaseTooltip); |
| 156 | + |
| 157 | +declare global { |
| 158 | + interface HTMLElementTagNameMap { |
| 159 | + 'base-tooltip': BaseTooltip; |
| 160 | + } |
| 161 | +} |
0 commit comments