Skip to content

Commit

Permalink
FIX : dialog document attach event timeout (#300)
Browse files Browse the repository at this point in the history
* feat: attach click event to container

* test: update background click case

* Revert "feat: attach click event to container"

This reverts commit 545da4b.

* chore: clear timeout on unmount

* chore: add prefix to set timeout
  • Loading branch information
FiodorGherasimenco authored Dec 21, 2023
1 parent 60d1c9a commit f4c3e8c
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/ebay-dialog-base/components/dialogBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const DialogBase: FC<DialogBaseProps<HTMLElement>> = ({
}, [])

useEffect(() => {
let timeout: number
const handleBackgroundClick = (e: React.MouseEvent) => {
if (drawerBaseEl.current && !drawerBaseEl.current.contains(e.target)) {
onBackgroundClick(e)
Expand All @@ -106,11 +107,14 @@ export const DialogBase: FC<DialogBaseProps<HTMLElement>> = ({
// causing the event listener to be attached to the document at the same time that the dialog
// opens. Adding a timeout so the event is attached after the click event that opened the modal
// is finished.
setTimeout(() => {
timeout = window.setTimeout(() => {
document.addEventListener('click', handleBackgroundClick as any, false)
})
}
return () => document.removeEventListener('click', handleBackgroundClick as any, false)
return () => {
clearTimeout(timeout)
document.removeEventListener('click', handleBackgroundClick as any, false)
}
}, [onBackgroundClick, open])

useEffect(() => {
Expand Down

0 comments on commit f4c3e8c

Please sign in to comment.