Skip to content

Commit 4c6cb92

Browse files
committed
close tooltip if the user scrolls more than 80px
1 parent 816f871 commit 4c6cb92

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/components/Tooltip/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
useDisclosure,
1010
} from "@chakra-ui/react"
1111

12+
import { isMobile } from "@/lib/utils/isMobile"
13+
1214
export interface IProps extends PopoverProps {
1315
content: ReactNode
1416
children?: ReactNode
@@ -21,15 +23,21 @@ const Tooltip: React.FC<IProps> = ({ content, children, ...rest }) => {
2123
// This is useful for mobile devices where the popover is open by clicking the
2224
// trigger, not hovering.
2325
useEffect(() => {
26+
let originalPosition = 0
27+
2428
const handleScroll = () => {
25-
if (isOpen) {
29+
const delta = window.scrollY - originalPosition
30+
31+
// Close the popover if the user scrolls more than 80px
32+
if (isOpen && Math.abs(delta) > 80) {
2633
onClose()
2734
}
2835
}
2936

3037
// Add event listener when the popover is open
3138
if (isOpen) {
3239
window.addEventListener("scroll", handleScroll)
40+
originalPosition = window.scrollY
3341
}
3442

3543
return () => {
@@ -43,7 +51,7 @@ const Tooltip: React.FC<IProps> = ({ content, children, ...rest }) => {
4351
onOpen={onOpen}
4452
onClose={onClose}
4553
placement="top"
46-
trigger="hover"
54+
trigger={isMobile() ? "click" : "hover"}
4755
gutter={8}
4856
{...rest}
4957
>

0 commit comments

Comments
 (0)