Skip to content

Commit efa8371

Browse files
author
Dylan Huang
committed
Implement drag detection in TableRowInteractive to prevent click events during text selection
1 parent a101b3f commit efa8371

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

vite-app/src/components/TableContainer.tsx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,43 @@ export function TableRowInteractive({
314314
? "hover:bg-gray-100 cursor-pointer"
315315
: "";
316316

317+
// Track simple drag to prevent triggering click (expand/collapse) when selecting text
318+
const mouseDownPos = React.useRef<{ x: number; y: number } | null>(null);
319+
const didDrag = React.useRef(false);
320+
321+
function handleMouseDown(e: React.MouseEvent<HTMLTableRowElement>) {
322+
mouseDownPos.current = { x: e.clientX, y: e.clientY };
323+
didDrag.current = false;
324+
}
325+
326+
function handleMouseMove(e: React.MouseEvent<HTMLTableRowElement>) {
327+
if (!mouseDownPos.current || e.buttons === 0) return;
328+
const dx = Math.abs(e.clientX - mouseDownPos.current.x);
329+
const dy = Math.abs(e.clientY - mouseDownPos.current.y);
330+
if (dx > 3 || dy > 3) didDrag.current = true;
331+
}
332+
333+
function handleMouseUp() {
334+
mouseDownPos.current = null;
335+
}
336+
337+
function handleClick(e: React.MouseEvent<HTMLTableRowElement>) {
338+
if (didDrag.current) {
339+
e.preventDefault();
340+
e.stopPropagation();
341+
didDrag.current = false;
342+
return;
343+
}
344+
onClick?.();
345+
}
346+
317347
return (
318348
<tr
319349
className={`text-sm ${interactiveClasses} ${className}`}
320-
onClick={onClick}
350+
onMouseDown={handleMouseDown}
351+
onMouseMove={handleMouseMove}
352+
onMouseUp={handleMouseUp}
353+
onClick={handleClick}
321354
>
322355
{children}
323356
</tr>

0 commit comments

Comments
 (0)