Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/6209 comment reset on mouse drag outside popup #6211

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions frontend/src/components/taskSelection/taskList.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { LockIcon, ListIcon, ZoomPlusIcon, CloseIcon, InternalLinkIcon } from '.
import { PaginatorLine, howManyPages } from '../paginator';
import { Dropdown } from '../dropdown';
import { TextField } from '../formInputs';
import useCloseOnDocumentClick from '../../hooks/UseCloseOnDocumentClick';

export function TaskStatus({ status, lockHolder }: Object) {
const isReadyOrLockedForMapping = ['READY', 'LOCKED_FOR_MAPPING'].includes(status);
Expand Down Expand Up @@ -63,6 +64,8 @@ export function TaskItem({
const location = useLocation();
const { value, unit } = selectUnit(new Date(data.actionDate));

const closeOnDocumentClick = useCloseOnDocumentClick();

const handleCopyToClipboard = () =>
navigator.clipboard
.writeText(`${window.location.origin}${location.pathname}?search=${data.taskId}`)
Expand Down Expand Up @@ -117,6 +120,7 @@ export function TaskItem({
<ListIcon width="18px" height="18px" className="pointer hover-blue-grey" />
</div>
}
closeOnDocumentClick={closeOnDocumentClick}
>
{(close) => (
<TaskActivityDetail
Expand Down
50 changes: 50 additions & 0 deletions frontend/src/hooks/UseCloseOnDocumentClick.js
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;
}
Loading