Skip to content

Commit

Permalink
fix: Disable text selection when drag-scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
meltyshev committed Nov 22, 2024
1 parent d0a8c8b commit 54a969e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
24 changes: 19 additions & 5 deletions client/src/components/Board/Board.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,16 @@ const Board = React.memo(
}

prevPosition.current = event.clientX;

window.getSelection().removeAllRanges();
document.body.classList.add(globalStyles.dragScrolling);
},
[wrapper],
);

const handleWindowMouseMove = useCallback(
(event) => {
if (!prevPosition.current) {
if (prevPosition.current === null) {
return;
}

Expand All @@ -97,8 +100,13 @@ const Board = React.memo(
[prevPosition],
);

const handleWindowMouseUp = useCallback(() => {
const handleWindowMouseRelease = useCallback(() => {
if (prevPosition.current === null) {
return;
}

prevPosition.current = null;
document.body.classList.remove(globalStyles.dragScrolling);
}, [prevPosition]);

useEffect(() => {
Expand All @@ -116,14 +124,20 @@ const Board = React.memo(
}, [listIds, isListAddOpened]);

useEffect(() => {
window.addEventListener('mouseup', handleWindowMouseUp);
window.addEventListener('mousemove', handleWindowMouseMove);

window.addEventListener('mouseup', handleWindowMouseRelease);
window.addEventListener('blur', handleWindowMouseRelease);
window.addEventListener('contextmenu', handleWindowMouseRelease);

return () => {
window.removeEventListener('mouseup', handleWindowMouseUp);
window.removeEventListener('mousemove', handleWindowMouseMove);

window.removeEventListener('mouseup', handleWindowMouseRelease);
window.removeEventListener('blur', handleWindowMouseRelease);
window.removeEventListener('contextmenu', handleWindowMouseRelease);
};
}, [handleWindowMouseUp, handleWindowMouseMove]);
}, [handleWindowMouseMove, handleWindowMouseRelease]);

return (
<>
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/CardModal/DescriptionEdit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const DescriptionEdit = React.forwardRef(({ children, defaultValue, onUpdate },
);

const handleChildrenClick = useCallback(() => {
if (!getSelection().toString()) {
if (!window.getSelection().toString()) {
open();
}
}, [open]);
Expand Down
5 changes: 5 additions & 0 deletions client/src/styles.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@
pointer-events: none;
}

&.dragScrolling>* {
pointer-events: none;
user-select: none;
}

/* Backgrounds */

.backgroundBerryRed {
Expand Down

0 comments on commit 54a969e

Please sign in to comment.