Skip to content

Commit

Permalink
keep tooltip open on hover and click links
Browse files Browse the repository at this point in the history
  • Loading branch information
groninge01 committed Sep 20, 2024
1 parent a748fc6 commit 6b630b7
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions lib/shared/components/marketing/EcosystemActivityChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
VStack,
} from '@chakra-ui/react'
import ButtonGroup from '@/lib/shared/components/btns/button-group/ButtonGroup'
import { FC, PropsWithChildren } from 'react'
import { FC, PropsWithChildren, useState, useRef, useCallback, useEffect } from 'react'
import { motion } from 'framer-motion'
import { EcosystemChainSelect } from './EcosystemChainSelect'
import { getChainShortName } from '@/lib/config/app.config'
Expand Down Expand Up @@ -62,6 +62,39 @@ export function EcosystemActivityChart() {
}
})

const [isTooltipVisible, setIsTooltipVisible] = useState(false)
const tooltipRef = useRef<HTMLDivElement>(null)
const timeoutRef = useRef<NodeJS.Timeout | null>(null)

const showTooltip = useCallback(
(params: any) => {
onEvents.mousemove(params)
setIsTooltipVisible(true)
if (timeoutRef.current) {
clearTimeout(timeoutRef.current)
}
},
[onEvents]
)

const hideTooltip = useCallback(() => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current)
}
timeoutRef.current = setTimeout(() => {
onEvents.mouseout()
setIsTooltipVisible(false)
}, 100)
}, [onEvents])

useEffect(() => {
return () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current)
}
}
}, [])

return (
<Card>
<Box position="relative">
Expand Down Expand Up @@ -108,18 +141,27 @@ export function EcosystemActivityChart() {
style={{ height: `${chartHeight}px` }}
option={chartOption}
ref={eChartsRef}
onEvents={onEvents}
onEvents={{
...onEvents,
mousemove: showTooltip,
mouseout: hideTooltip,
}}
/>
{/* No idea how to get this to work with <Portal> */}
{createPortal(
<div
ref={tooltipRef}
style={{
position: 'fixed',
left: `${tooltipPosition.x + bounds.left}px`,
top: `${tooltipPosition.y + bounds.top}px`,
pointerEvents: 'none',
pointerEvents: isTooltipVisible ? 'auto' : 'none',
zIndex: 9999,
opacity: isTooltipVisible ? 1 : 0,
transition: 'opacity 0.2s',
}}
onMouseEnter={showTooltip}
onMouseLeave={hideTooltip}
>
{tooltipContent}
</div>,
Expand Down

0 comments on commit 6b630b7

Please sign in to comment.