Fix missing event listener removals

This commit is contained in:
Alexander Zinchuk 2022-02-02 22:48:44 +01:00
parent 9415e0da19
commit 52e70c2f0e
3 changed files with 35 additions and 25 deletions

View File

@ -20,23 +20,23 @@ export const useResize = (
elementRef.current.style.width = `${initialWidth}px`; elementRef.current.style.width = `${initialWidth}px`;
}, [elementRef, initialWidth]); }, [elementRef, initialWidth]);
const handleMouseUp = () => { function handleMouseUp() {
document.body.classList.remove('no-selection', 'cursor-ew-resize'); document.body.classList.remove('no-selection', 'cursor-ew-resize');
}; }
const initResize = (event: React.MouseEvent<HTMLElement, MouseEvent>) => { function initResize(event: React.MouseEvent<HTMLElement, MouseEvent>) {
document.body.classList.add('no-selection', 'cursor-ew-resize'); document.body.classList.add('no-selection', 'cursor-ew-resize');
setInitialMouseX(event.clientX); setInitialMouseX(event.clientX);
setInitialElementWidth(elementRef.current!.offsetWidth); setInitialElementWidth(elementRef.current!.offsetWidth);
markIsActive(); markIsActive();
}; }
const resetResize = (event: React.MouseEvent<HTMLElement, MouseEvent>) => { function resetResize(event: React.MouseEvent<HTMLElement, MouseEvent>) {
event.preventDefault(); event.preventDefault();
elementRef.current!.style.width = ''; elementRef.current!.style.width = '';
onReset(); onReset();
}; }
useEffect(() => { useEffect(() => {
if (!isActive) return; if (!isActive) return;
@ -46,18 +46,24 @@ export const useResize = (
elementRef.current!.style.width = `${newWidth}px`; elementRef.current!.style.width = `${newWidth}px`;
}; };
const stopDrag = () => { function stopDrag() {
cleanup();
onResize(elementRef.current!.offsetWidth);
}
function cleanup() {
handleMouseUp(); handleMouseUp();
document.removeEventListener('mousemove', handleMouseMove, false); document.removeEventListener('mousemove', handleMouseMove, false);
document.removeEventListener('mouseup', stopDrag, false); document.removeEventListener('mouseup', stopDrag, false);
document.removeEventListener('blur', stopDrag, false); document.removeEventListener('blur', stopDrag, false);
onResize(elementRef.current!.offsetWidth);
unmarkIsActive(); unmarkIsActive();
}; }
document.addEventListener('mousemove', handleMouseMove, false); document.addEventListener('mousemove', handleMouseMove, false);
document.addEventListener('mouseup', stopDrag, false); document.addEventListener('mouseup', stopDrag, false);
document.addEventListener('blur', stopDrag, false); document.addEventListener('blur', stopDrag, false);
return cleanup;
}, [initialElementWidth, initialMouseX, elementRef, onResize, isActive, unmarkIsActive]); }, [initialElementWidth, initialMouseX, elementRef, onResize, isActive, unmarkIsActive]);
return { initResize, resetResize, handleMouseUp }; return { initResize, resetResize, handleMouseUp };

View File

@ -18,7 +18,7 @@ const useSendWithEnter = (
useEffect(() => { useEffect(() => {
window.addEventListener('keydown', handleKeyDown, false); window.addEventListener('keydown', handleKeyDown, false);
return () => window.removeEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown, false);
}, [handleKeyDown]); }, [handleKeyDown]);
return buttonRef; return buttonRef;

View File

@ -130,7 +130,7 @@ export function captureEvents(element: HTMLElement, options: CaptureOptions) {
} }
} }
function onRelease(e: MouseEvent | TouchEvent) { function onRelease(e?: MouseEvent | TouchEvent) {
if (captureEvent) { if (captureEvent) {
if (options.withCursor) { if (options.withCursor) {
document.body.classList.remove('cursor-grabbing'); document.body.classList.remove('cursor-grabbing');
@ -149,20 +149,22 @@ export function captureEvents(element: HTMLElement, options: CaptureOptions) {
}); });
} }
if (hasMoved) { if (e) {
if (options.onRelease) { if (hasMoved) {
options.onRelease(e); if (options.onRelease) {
options.onRelease(e);
}
} else if (e.type === 'mouseup') {
if (options.onDoubleClick && Date.now() - lastClickTime < 300) {
options.onDoubleClick(e, {
centerX: captureEvent!.pageX!,
centerY: captureEvent!.pageY!,
});
} else if (options.onClick && (!('button' in e) || e.button === 0)) {
options.onClick(e);
}
lastClickTime = Date.now();
} }
} else if (e.type === 'mouseup') {
if (options.onDoubleClick && Date.now() - lastClickTime < 300) {
options.onDoubleClick(e, {
centerX: captureEvent!.pageX!,
centerY: captureEvent!.pageY!,
});
} else if (options.onClick && (!('button' in e) || e.button === 0)) {
options.onClick(e);
}
lastClickTime = Date.now();
} }
} }
@ -276,8 +278,10 @@ export function captureEvents(element: HTMLElement, options: CaptureOptions) {
element.addEventListener('touchstart', onCapture, { passive: !options.isNotPassive }); element.addEventListener('touchstart', onCapture, { passive: !options.isNotPassive });
return () => { return () => {
element.removeEventListener('mousedown', onCapture); onRelease();
element.removeEventListener('touchstart', onCapture); element.removeEventListener('touchstart', onCapture);
element.removeEventListener('mousedown', onCapture);
}; };
} }