(() => { const selector = '[data-be-preview]'; const margin = 10; let preview; let tile; let hideTimer; function hide() { window.clearTimeout(hideTimer); if (!tile) return; tile.removeAttribute('aria-describedby'); tile = null; preview.hidden = true; } function position() { if (!tile) return; if (!tile.isConnected) { hide(); return; } const viewport = document.documentElement; const anchor = tile.getBoundingClientRect(); const grid = tile.closest('.be-grid-scroll').getBoundingClientRect(); if (anchor.bottom <= 0 || anchor.top >= viewport.clientHeight || anchor.right <= grid.left || anchor.left >= grid.right) { hide(); return; } const {width, height} = preview.getBoundingClientRect(); const below = anchor.bottom + margin; const above = anchor.top - height - margin; const maxTop = viewport.clientHeight - height - margin; const top = below <= maxTop || above < margin ? below : above; preview.style.left = Math.max(margin, Math.min(anchor.left, viewport.clientWidth - width - margin)) + 'px'; preview.style.top = Math.max(margin, Math.min(top, maxTop)) + 'px'; } function show(target) { window.clearTimeout(hideTimer); if (target === tile) return; const content = document.getElementById(target.dataset.bePreview); if (!content?.content) return; if (!preview) { preview = document.createElement('div'); preview.className = 'be-grid-preview'; preview.id = 'be-calendar-event-tooltip'; preview.setAttribute('role', 'tooltip'); preview.hidden = true; document.body.append(preview); } hide(); tile = target; preview.replaceChildren(content.content.cloneNode(true)); preview.hidden = false; tile.setAttribute('aria-describedby', preview.id); position(); } function scheduleHide() { window.clearTimeout(hideTimer); hideTimer = window.setTimeout(() => { if (tile && document.activeElement !== tile && !tile.matches(':hover') && !preview.matches(':hover')) hide(); }, 150); } document.addEventListener('pointerover', event => { if (event.pointerType === 'touch') return; const target = event.target.closest?.(selector); if (target && !target.contains(event.relatedTarget)) show(target); }); document.addEventListener('pointerout', event => { if (event.pointerType !== 'touch' && tile && (tile.contains(event.target) || preview.contains(event.target))) { scheduleHide(); } }); document.addEventListener('focusin', event => { const target = event.target.closest?.(selector); if (target) show(target); }); document.addEventListener('focusout', scheduleHide); document.addEventListener('keydown', event => { if (event.key === 'Escape' && tile) { hide(); event.preventDefault(); } }); window.addEventListener('resize', position); document.addEventListener('scroll', position, {capture: true, passive: true}); })();