Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

fix(web): judge a click that follows no press on its own target (#300)

Ahmad Othman Ammar Adi94b5f7712ed8
diff --git a/apps/web/src/features/issue/hooks/useExitOnClickOutside.test.tsx b/apps/web/src/features/issue/hooks/useExitOnClickOutside.test.tsx
index 0d609f48..11a87506 100644
--- a/apps/web/src/features/issue/hooks/useExitOnClickOutside.test.tsx
+++ b/apps/web/src/features/issue/hooks/useExitOnClickOutside.test.tsx
@@ -164,6 +164,16 @@ describe('useExitOnClickOutside', () => {
assert.equal(exits, 1);
});
+ // A keyboard activation and a synthetic click raise no press, so the press a
+ // previous gesture recorded must not decide them.
+ it('exits on a click that follows no press of its own', () => {
+ render();
+ dispatch(element('inside'), 'pointerdown');
+ dispatch(element('page'), 'click');
+ dispatch(element('page'), 'click');
+ assert.equal(exits, 1);
+ });
+
it('removes its document listeners when the surface unmounts', () => {
render();
assert.equal(documentListeners, 2);
diff --git a/apps/web/src/features/issue/hooks/useExitOnClickOutside.ts b/apps/web/src/features/issue/hooks/useExitOnClickOutside.ts
index bf6a8740..46439a67 100644
--- a/apps/web/src/features/issue/hooks/useExitOnClickOutside.ts
+++ b/apps/web/src/features/issue/hooks/useExitOnClickOutside.ts
@@ -1,23 +1,18 @@
import { useEffect, useRef, type RefObject } from 'react';
-// Closes a surface when a click lands outside it. The listener sits on the document
-// because the surface does not cover the viewport: the page behind it stays scrollable
-// and clickable, so there is no backdrop element to take the click.
+// Closes a surface when a click lands outside it, read from the document because the
+// surface has no backdrop element of its own to take the click.
//
-// The layer check keeps the surface open under its own overlays. A popover, dropdown,
-// select, dialog or toast is its own child of the body, so none of them have to be named
-// here. That holds while the app renders into one child of the body: an element wrapping
-// the providers would put those overlays in the surface's own layer and a click in one
-// would close it.
+// The layer check keeps the surface open under its own overlays: a popover, dropdown,
+// select, dialog or toast is its own child of the body. That holds while the app renders
+// into one child of the body, so an element wrapping the providers would put those
+// overlays in the surface's layer and a click in one would close it.
//
-// Click, not pointerdown: a field that saves on blur commits before the surface closes,
-// and a scrollbar drag or a right-click raises no click at all.
-//
-// A click reports the common ancestor of press and release, so selecting text in the
-// surface and releasing over the page reports an element outside it. The press decides:
-// a gesture that started inside the surface never exits, which is what keeps a selection
-// dragged out of it from closing it. The handler is kept in a ref so the listeners are
-// registered once and always call the latest onExit.
+// The listener is on click so a field saving on blur commits first, and so a scrollbar
+// drag or a right press raises none. A click reports the common ancestor of press and
+// release, so the press decides: a gesture that starts inside the surface keeps it open
+// even when it is released over the page. The handler is kept in a ref so the listeners
+// are registered once and always call the latest onExit.
export function useExitOnClickOutside(ref: RefObject<HTMLElement | null>, onExit: () => void) {
const onExitRef = useRef(onExit);
onExitRef.current = onExit;
@@ -31,8 +26,12 @@ export function useExitOnClickOutside(ref: RefObject<HTMLElement | null>, onExit
}
function onClick(e: MouseEvent) {
+ // Read and clear, so an activation that raises no press (a keyboard one, or a
+ // synthetic click) is judged on its own target rather than on the last press.
+ const pressed = pressedInside;
+ pressedInside = false;
+ if (pressed) return;
const surface = ref.current;
- if (pressedInside) return;
if (!surface || !(e.target instanceof Element)) return;
if (surface.contains(e.target)) return;
if (!surface.closest('body > *')?.contains(e.target)) return;