Skip to content

Commit

Permalink
close tooltip if the user scrolls more than 80px
Browse files Browse the repository at this point in the history
  • Loading branch information
pettinarip committed Mar 14, 2024
1 parent 816f871 commit 4c6cb92
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/components/Tooltip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
useDisclosure,
} from "@chakra-ui/react"

import { isMobile } from "@/lib/utils/isMobile"

export interface IProps extends PopoverProps {
content: ReactNode
children?: ReactNode
Expand All @@ -21,15 +23,21 @@ const Tooltip: React.FC<IProps> = ({ content, children, ...rest }) => {
// This is useful for mobile devices where the popover is open by clicking the
// trigger, not hovering.
useEffect(() => {
let originalPosition = 0

const handleScroll = () => {
if (isOpen) {
const delta = window.scrollY - originalPosition

// Close the popover if the user scrolls more than 80px
if (isOpen && Math.abs(delta) > 80) {
onClose()
}
}

// Add event listener when the popover is open
if (isOpen) {
window.addEventListener("scroll", handleScroll)
originalPosition = window.scrollY
}

return () => {
Expand All @@ -43,7 +51,7 @@ const Tooltip: React.FC<IProps> = ({ content, children, ...rest }) => {
onOpen={onOpen}
onClose={onClose}
placement="top"
trigger="hover"
trigger={isMobile() ? "click" : "hover"}
gutter={8}
{...rest}
>
Expand Down

0 comments on commit 4c6cb92

Please sign in to comment.