Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions src/components/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export function Tooltip({
className = '',
}: TooltipProps) {
const [isOpen, setIsOpen] = React.useState(false)
const [isMounted, setIsMounted] = React.useState(false)
const portalRoot = React.useRef<HTMLElement | null>(null)

const { refs, floatingStyles, context } = useFloating({
open: isOpen,
Expand All @@ -37,6 +39,21 @@ export function Tooltip({

const { getReferenceProps, getFloatingProps } = useInteractions([hover])

// Ensure DOM is ready before rendering the portal
React.useEffect(() => {
// Only set portalRoot if we're in a browser environment
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
portalRoot.current = document.body
setIsMounted(true)
}

return () => {
// Clean up on unmount to prevent memory leaks
setIsMounted(false)
portalRoot.current = null
}
}, [])

return (
<>
{/* eslint-disable-next-line react-hooks/refs */}
Expand All @@ -45,18 +62,20 @@ export function Tooltip({
ref: refs.setReference,
...getReferenceProps(),
} as any)}
<FloatingPortal>
{isOpen && (
<div
ref={refs.setFloating /* eslint-disable-line react-hooks/refs */}
style={floatingStyles}
{...getFloatingProps()}
className={`z-50 rounded-md bg-gray-900 px-3 py-1.5 text-sm text-white shadow-lg dark:bg-gray-800 ${className}`}
>
{content}
</div>
)}
</FloatingPortal>
{isMounted && portalRoot.current && (
<FloatingPortal root={portalRoot.current}>
{isOpen && (
<div
ref={refs.setFloating /* eslint-disable-line react-hooks/refs */}
style={floatingStyles}
{...getFloatingProps()}
className={`z-50 rounded-md bg-gray-900 px-3 py-1.5 text-sm text-white shadow-lg dark:bg-gray-800 ${className}`}
>
{content}
</div>
)}
</FloatingPortal>
)}
</>
)
}
Loading