diff --git a/components/tooltip.tsx b/components/tooltip.tsx index c31252eb3..453ef0a4d 100644 --- a/components/tooltip.tsx +++ b/components/tooltip.tsx @@ -3,12 +3,12 @@ import React, { useEffect, useRef, useState } from "react"; const TooltipComponent = ({ children }: { children: React.ReactNode }) => { const [tooltip, setTooltip] = useState(false); const [boundary, setBoundary] = useState(false); + const tooltipRef = useRef(null); - const showTooltip = () => { - setTooltip(true); + const toggleTooltip = () => { + setTooltip((prev) => !prev); }; - const tooltipRef = useRef(null); useEffect(() => { const handleScroll = () => { if (tooltipRef.current) { @@ -18,19 +18,27 @@ const TooltipComponent = ({ children }: { children: React.ReactNode }) => { setBoundary(distanceToBottom <= touchThreshold); } }; + + const handleClickOutside = (event: MouseEvent) => { + if ( + tooltipRef.current && + !tooltipRef.current.contains(event.target as Node) + ) { + setTooltip(false); + } + }; window.addEventListener("scroll", handleScroll); - handleScroll(); + window.addEventListener("scroll", handleClickOutside); + document.addEventListener("mousedown", handleClickOutside); return () => { window.removeEventListener("scroll", handleScroll); + document.removeEventListener("mousedown", handleClickOutside); }; }, []); - const hideTooltip = () => { - setTooltip(false); - }; return ( -
+
{ role="link" aria-label="tooltip" className="nx-focus:outline-none nx-focus:ring-gray-300 nx-rounded-full nx-focus:ring-offset-2 nx-focus:ring-2 nx-focus:bg-gray-200 nx-relative nx-mt-20 md:nx-mt-0" - onMouseOver={showTooltip} - onFocus={showTooltip} - onMouseOut={hideTooltip} + onClick={toggleTooltip} > -
+
{ id="tooltip" role="tooltip" className={`nx-z-20 ${ - boundary && "nx-bottom-10" + boundary ? "nx-bottom-10" : "" } nx-w-64 nx-absolute nx-transition nx-duration-150 nx-ease-in-out nx-left-0 nx-ml-8 nx-shadow-lg nx-bg-white dark:nx-bg-dark-mode-white-2 nx-p-4 nx-rounded`} > - - - - - - - - - - - {children}
)}