|
| 1 | +// deck.gl |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | +// Copyright (c) vis.gl contributors |
| 4 | + |
| 5 | +const TOOLTIP_ATTR = 'data-deck-widget-tooltip'; |
| 6 | +const GENERATED_ATTR = 'data-deck-widget-tooltip-generated'; |
| 7 | +const OFFSET = 8; |
| 8 | + |
| 9 | +export class WidgetTooltip { |
| 10 | + private anchor: HTMLElement | null = null; |
| 11 | + private element: HTMLDivElement | null = null; |
| 12 | + private listenerRoot: HTMLElement | null = null; |
| 13 | + private prepared = false; |
| 14 | + |
| 15 | + prepare(root: HTMLElement | null | undefined): void { |
| 16 | + this.hide(); |
| 17 | + this.prepared = Boolean(root); |
| 18 | + if (!root) return; |
| 19 | + |
| 20 | + // Preact does not reapply unchanged title props after they are removed below. |
| 21 | + for (const target of root.querySelectorAll<HTMLElement>(`[${GENERATED_ATTR}]`)) { |
| 22 | + const tooltip = target.getAttribute(TOOLTIP_ATTR); |
| 23 | + if (tooltip) target.setAttribute('title', tooltip); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + update(root: HTMLElement | null | undefined, shortcut?: string | HTMLElement): void { |
| 28 | + this.hide(); |
| 29 | + if (!root) return; |
| 30 | + |
| 31 | + this.addListeners(root); |
| 32 | + this.syncTargets(root, this.prepared); |
| 33 | + this.prepared = false; |
| 34 | + this.shortcut = shortcut; |
| 35 | + } |
| 36 | + |
| 37 | + remove(): void { |
| 38 | + this.hide(); |
| 39 | + this.removeListeners(); |
| 40 | + } |
| 41 | + |
| 42 | + private shortcut?: string | HTMLElement; |
| 43 | + |
| 44 | + private hide(): void { |
| 45 | + this.element?.remove(); |
| 46 | + this.element = null; |
| 47 | + this.anchor = null; |
| 48 | + } |
| 49 | + |
| 50 | + private addListeners(root: HTMLElement): void { |
| 51 | + if (this.listenerRoot === root) return; |
| 52 | + |
| 53 | + this.removeListeners(); |
| 54 | + this.listenerRoot = root; |
| 55 | + root.addEventListener('pointerover', this.onPointerOver); |
| 56 | + root.addEventListener('pointerout', this.onPointerOut); |
| 57 | + root.addEventListener('focusin', this.onFocusIn); |
| 58 | + root.addEventListener('focusout', this.onFocusOut); |
| 59 | + root.addEventListener('keydown', this.onKeyDown); |
| 60 | + } |
| 61 | + |
| 62 | + private removeListeners(): void { |
| 63 | + if (!this.listenerRoot) return; |
| 64 | + |
| 65 | + this.listenerRoot.removeEventListener('pointerover', this.onPointerOver); |
| 66 | + this.listenerRoot.removeEventListener('pointerout', this.onPointerOut); |
| 67 | + this.listenerRoot.removeEventListener('focusin', this.onFocusIn); |
| 68 | + this.listenerRoot.removeEventListener('focusout', this.onFocusOut); |
| 69 | + this.listenerRoot.removeEventListener('keydown', this.onKeyDown); |
| 70 | + this.listenerRoot = null; |
| 71 | + } |
| 72 | + |
| 73 | + private syncTargets(root: HTMLElement, prepared: boolean): void { |
| 74 | + if (prepared) { |
| 75 | + for (const target of root.querySelectorAll<HTMLElement>(`[${GENERATED_ATTR}]:not([title])`)) { |
| 76 | + this.clearGeneratedTarget(target); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + for (const target of root.querySelectorAll<HTMLElement>('[title]')) { |
| 81 | + const title = target.getAttribute('title'); |
| 82 | + const wasGenerated = target.hasAttribute(GENERATED_ATTR); |
| 83 | + if (title) { |
| 84 | + target.setAttribute(TOOLTIP_ATTR, title); |
| 85 | + target.setAttribute(GENERATED_ATTR, ''); |
| 86 | + if (!target.hasAttribute('aria-label') || wasGenerated) { |
| 87 | + target.setAttribute('aria-label', title); |
| 88 | + } |
| 89 | + } else if (wasGenerated) { |
| 90 | + this.clearGeneratedTarget(target); |
| 91 | + } |
| 92 | + target.removeAttribute('title'); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private clearGeneratedTarget(target: HTMLElement): void { |
| 97 | + const tooltip = target.getAttribute(TOOLTIP_ATTR); |
| 98 | + if (target.getAttribute('aria-label') === tooltip) target.removeAttribute('aria-label'); |
| 99 | + target.removeAttribute(TOOLTIP_ATTR); |
| 100 | + target.removeAttribute(GENERATED_ATTR); |
| 101 | + } |
| 102 | + |
| 103 | + private onPointerOver = (event: PointerEvent): void => { |
| 104 | + const target = this.getTarget(event.target); |
| 105 | + if (target) this.show(target); |
| 106 | + }; |
| 107 | + |
| 108 | + private onPointerOut = (event: PointerEvent): void => { |
| 109 | + if ( |
| 110 | + this.anchor && |
| 111 | + !(event.relatedTarget instanceof Node && this.anchor.contains(event.relatedTarget)) |
| 112 | + ) { |
| 113 | + this.hide(); |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + private onFocusIn = (event: FocusEvent): void => { |
| 118 | + const target = this.getTarget(event.target); |
| 119 | + if (target?.matches(':focus-visible')) this.show(target); |
| 120 | + }; |
| 121 | + |
| 122 | + private onFocusOut = (): void => this.hide(); |
| 123 | + |
| 124 | + private onKeyDown = (event: KeyboardEvent): void => { |
| 125 | + if (event.key === 'Escape') this.hide(); |
| 126 | + }; |
| 127 | + |
| 128 | + private getTarget(target: EventTarget | null): HTMLElement | null { |
| 129 | + const anchor = target instanceof Element ? target.closest(`[${TOOLTIP_ATTR}]`) : null; |
| 130 | + return anchor instanceof HTMLElement ? anchor : null; |
| 131 | + } |
| 132 | + |
| 133 | + private show(anchor: HTMLElement): void { |
| 134 | + const text = anchor.getAttribute(TOOLTIP_ATTR); |
| 135 | + const root = this.listenerRoot; |
| 136 | + if (!text || !root) return; |
| 137 | + |
| 138 | + this.hide(); |
| 139 | + this.anchor = anchor; |
| 140 | + |
| 141 | + const tooltip = document.createElement('div'); |
| 142 | + tooltip.className = 'deck-widget-tooltip'; |
| 143 | + tooltip.setAttribute('role', 'tooltip'); |
| 144 | + tooltip.append(document.createTextNode(text)); |
| 145 | + if (this.shortcut) { |
| 146 | + tooltip.append( |
| 147 | + typeof this.shortcut === 'string' |
| 148 | + ? document.createTextNode(this.shortcut) |
| 149 | + : this.shortcut.cloneNode(true) |
| 150 | + ); |
| 151 | + } |
| 152 | + |
| 153 | + const container = getContainer(root); |
| 154 | + container.append(tooltip); |
| 155 | + this.element = tooltip; |
| 156 | + this.position(anchor, tooltip, container); |
| 157 | + } |
| 158 | + |
| 159 | + private position(anchor: HTMLElement, tooltip: HTMLDivElement, container: HTMLElement): void { |
| 160 | + const anchorRect = anchor.getBoundingClientRect(); |
| 161 | + const containerRect = container.getBoundingClientRect(); |
| 162 | + const tooltipRect = tooltip.getBoundingClientRect(); |
| 163 | + let left = anchorRect.right - containerRect.left + OFFSET; |
| 164 | + const top = anchorRect.top - containerRect.top + (anchorRect.height - tooltipRect.height) / 2; |
| 165 | + |
| 166 | + if (left + tooltipRect.width > containerRect.width) { |
| 167 | + left = anchorRect.left - containerRect.left - tooltipRect.width - OFFSET; |
| 168 | + } |
| 169 | + tooltip.style.left = `${clamp(left, 0, containerRect.width - tooltipRect.width)}px`; |
| 170 | + tooltip.style.top = `${clamp(top, 0, containerRect.height - tooltipRect.height)}px`; |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +function getContainer(root: HTMLElement): HTMLElement { |
| 175 | + const parent = root.parentElement; |
| 176 | + const grandparent = parent?.parentElement; |
| 177 | + return grandparent?.parentElement?.classList.contains('deck-widget-container') |
| 178 | + ? grandparent |
| 179 | + : parent || root; |
| 180 | +} |
| 181 | + |
| 182 | +function clamp(value: number, min: number, max: number): number { |
| 183 | + return Math.min(Math.max(value, min), Math.max(min, max)); |
| 184 | +} |
0 commit comments