Skip to content

Instantly share code, notes, and snippets.

@anthonyec
Created April 17, 2023 10:05
Show Gist options
  • Save anthonyec/65c15de1b5d0d40acfb6d0428b759a24 to your computer and use it in GitHub Desktop.
Save anthonyec/65c15de1b5d0d40acfb6d0428b759a24 to your computer and use it in GitHub Desktop.
Tooltip in Salad Room
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