import { createSignal, onMount, onCleanup, Show, splitProps, mergeProps } from 'solid-js'; import { Portal } from 'solid-js/web'; export default function Tooltip(props) { const merged = mergeProps( { placement: 'bottom', maxWidth: '400px', minWidth: '280px', }, props ); const [local, others] = splitProps(merged, [ 'placement', 'maxWidth', 'minWidth', 'children', 'content', 'class', ]); let iconRef; let tooltipContentRef; const [isVisible, setIsVisible] = createSignal(false); const [position, setPosition] = createSignal({ top: 0, left: 0, ready: false }); const [isMobile, setIsMobile] = createSignal(window.innerWidth < 640); let timeoutId = null; const checkMobile = () => { setIsMobile(window.innerWidth < 640); }; const updatePosition = () => { if (!iconRef || !isVisible() || isMobile()) return; // Wait for tooltip to render requestAnimationFrame(() => { if (!iconRef || !tooltipContentRef) return; const iconRect = iconRef.getBoundingClientRect(); const tooltipRect = tooltipContentRef.getBoundingClientRect(); if (tooltipRect.width === 0 || tooltipRect.height === 0) { setTimeout(updatePosition, 10); return; } const viewportWidth = window.innerWidth; const viewportHeight = window.innerHeight; const padding = 12; // Position below icon, centered horizontally relative to icon const iconCenterX = iconRect.left + iconRect.width / 2; let top = iconRect.bottom + 8; let left = iconCenterX - tooltipRect.width / 2; // Flip to top if not enough space below if (top + tooltipRect.height > viewportHeight - padding) { top = iconRect.top - tooltipRect.height - 8; } // Keep within viewport horizontally if (left < padding) { left = padding; } else if (left + tooltipRect.width > viewportWidth - padding) { left = viewportWidth - tooltipRect.width - padding; } // Keep within viewport vertically if (top < padding) { top = padding; } setPosition({ top, left, ready: true }); }); }; const showTooltip = () => { clearTimeout(timeoutId); timeoutId = setTimeout(() => { setIsVisible(true); if (!isMobile()) { setPosition({ top: 0, left: 0, ready: false }); updatePosition(); } }, isMobile() ? 0 : 300); }; const hideTooltip = () => { clearTimeout(timeoutId); timeoutId = setTimeout(() => { setIsVisible(false); setPosition({ top: 0, left: 0, ready: false }); }, isMobile() ? 0 : 200); }; const toggleTooltip = (e) => { e.stopPropagation(); if (isVisible()) { hideTooltip(); } else { showTooltip(); } }; const handleTooltipMouseEnter = () => { if (!isMobile()) clearTimeout(timeoutId); }; const handleTooltipMouseLeave = () => { if (!isMobile()) hideTooltip(); }; onMount(() => { if (iconRef) { iconRef.addEventListener('mouseenter', () => !isMobile() && showTooltip()); iconRef.addEventListener('mouseleave', () => !isMobile() && hideTooltip()); iconRef.addEventListener('click', toggleTooltip); } window.addEventListener('scroll', updatePosition, true); window.addEventListener('resize', () => { checkMobile(); updatePosition(); }); }); onCleanup(() => { if (iconRef) { iconRef.removeEventListener('mouseenter', showTooltip); iconRef.removeEventListener('mouseleave', hideTooltip); iconRef.removeEventListener('click', toggleTooltip); } window.removeEventListener('scroll', updatePosition, true); window.removeEventListener('resize', checkMobile); clearTimeout(timeoutId); }); return ( } > {local.content} {/* Mobile: Bottom Drawer */} {/* Backdrop */}
{/* Drawer */}
{/* Handle */}
{/* Header */}
Info
{/* Content */}
{local.children}
{/* Desktop: Floating Tooltip */}
{local.children}
); }