Skip to content

Commit

Permalink
fix tooltip positining and closing in mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
pettinarip committed Mar 11, 2024
1 parent 2ce32ca commit f9f3516
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/components/Tooltip/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { ReactNode } from "react"
import React, { ReactNode, useEffect } from "react"
import {
Popover,
PopoverArrow,
PopoverBody,
PopoverContent,
PopoverProps,
PopoverTrigger,
useDisclosure,
} from "@chakra-ui/react"

export interface IProps extends PopoverProps {
Expand All @@ -14,11 +15,35 @@ export interface IProps extends PopoverProps {
}

const Tooltip: React.FC<IProps> = ({ content, children, ...rest }) => {
const { isOpen, onOpen, onClose } = useDisclosure()

// Close the popover when the user scrolls.
// This is useful for mobile devices where the popover is open by clicking the
// trigger, not hovering.
useEffect(() => {
const handleScroll = () => {
if (isOpen) {
onClose()
}
}

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

return () => {
window.removeEventListener("scroll", handleScroll)
}
}, [isOpen, onClose])

return (
<Popover
isOpen={isOpen}
onOpen={onOpen}
onClose={onClose}
placement="top"
trigger="hover"
strategy="fixed"
gutter={8}
{...rest}
>
Expand Down

0 comments on commit f9f3516

Please sign in to comment.