Created
April 17, 2023 10:05
-
-
Save anthonyec/65c15de1b5d0d40acfb6d0428b759a24 to your computer and use it in GitHub Desktop.
Tooltip in Salad Room
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useRef, useState } from 'react'; | |
import { CSSTransition } from 'react-transition-group'; | |
import TooltipBubble from './tooltip_bubble'; | |
export default function Tooltip({ message, children }) { | |
const [showTooltip, setShowTooltip] = useState(false); | |
const childRef = useRef(null); | |
const child = React.Children.only(children); | |
const handleMouseEnter = () => { | |
child.onMouseEnter && child.onMouseEnter(); | |
setShowTooltip(true); | |
}; | |
const handleMouseLeave = () => { | |
child.onMouseLeave && child.onMouseLeave(); | |
setShowTooltip(false); | |
}; | |
const clonedChild = React.cloneElement(child, { | |
onMouseOver: handleMouseEnter, | |
onMouseLeave: handleMouseLeave, | |
ref: childRef | |
}); | |
return ( | |
<> | |
{clonedChild} | |
<CSSTransition | |
in={showTooltip} | |
classNames="tooltip-bubble--transition" | |
timeout={300} | |
unmountOnExit | |
> | |
<TooltipBubble targetRef={childRef}>{message}</TooltipBubble> | |
</CSSTransition> | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment