-
-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6211 from hotosm/fix/6209-comment-reset-on-mouse-…
…drag-outside-popup fix/6209 comment reset on mouse drag outside popup
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// https://github.com/yjose/reactjs-popup/issues/174 | ||
import { useEffect, useState } from 'react'; | ||
|
||
/** | ||
* This hook is a workaround for an issue in reactjs-popup | ||
* | ||
* What this hook does is: | ||
* | ||
* - on mouse down inside the popup, set closeOnDocumentClick to false | ||
* - on mouse up, set closeOnDocumentClick to true | ||
* | ||
* Usage: | ||
* | ||
* const closeOnDocumentClick = useCloseOnDocumentClick() | ||
* | ||
* return ( | ||
* <Popup ... closeOnDocumentClick={closeOnDocumentClick} > | ||
* ... | ||
* </Popup> | ||
* ) | ||
*/ | ||
export default function useCloseOnDocumentClick() { | ||
const [closeOnDocumentClick, setCloseOnDocumentClick] = useState(true); | ||
|
||
useEffect(() => { | ||
function insidePopupContents(target: any): boolean { | ||
return target.querySelector('.popup-content') == null; | ||
} | ||
|
||
function handleMouseDown(event: MouseEvent) { | ||
if (insidePopupContents(event.target)) { | ||
setCloseOnDocumentClick(false); | ||
} | ||
} | ||
|
||
function handleMouseUp() { | ||
setTimeout(() => setCloseOnDocumentClick(true)); | ||
} | ||
|
||
window.document.addEventListener('mousedown', handleMouseDown); | ||
window.document.addEventListener('mouseup', handleMouseUp); | ||
|
||
return () => { | ||
window.document.removeEventListener('mousedown', handleMouseDown); | ||
window.document.removeEventListener('mouseup', handleMouseUp); | ||
}; | ||
}, [setCloseOnDocumentClick]); | ||
|
||
return closeOnDocumentClick; | ||
} |