diff --git a/.changeset/fix-menu-open-toggle-races.md b/.changeset/fix-menu-open-toggle-races.md
new file mode 100644
index 000000000..6659e274e
--- /dev/null
+++ b/.changeset/fix-menu-open-toggle-races.md
@@ -0,0 +1,5 @@
+---
+default: patch
+---
+
+Fix context menus dismissing themselves instead of opening.
diff --git a/src/app/components/sidebar/SidebarTab.test.tsx b/src/app/components/sidebar/SidebarTab.test.tsx
new file mode 100644
index 000000000..ec9838120
--- /dev/null
+++ b/src/app/components/sidebar/SidebarTab.test.tsx
@@ -0,0 +1,71 @@
+import type { ReactNode } from 'react';
+import { fireEvent, render, screen } from '@testing-library/react';
+import { describe, expect, it, vi } from 'vitest';
+import type { MenuAnchor } from '$hooks/useMenuAnchor';
+import { SidebarTab } from './SidebarTab';
+
+vi.mock('$components/sidebar', () => ({
+ SidebarItemLeft: ({ children }: { children: ReactNode }) =>
{children}
,
+ SidebarItemTooltip: ({ children }: { children: (ref: unknown) => ReactNode }) => children(null),
+ SidebarUnreadBadge: () => null,
+ SidebarAvatar: ({ children, ...props }: { children: ReactNode }) => (
+
+ ),
+}));
+
+vi.mock('$components/ResponsiveMenu', () => ({
+ ResponsiveMenu: ({ children }: { children: ReactNode }) => {children}
,
+}));
+
+const menuAnchorStub = (longPressFired: boolean): MenuAnchor => ({
+ anchor: undefined,
+ isPressing: false,
+ close: vi.fn<() => void>(),
+ openAt: vi.fn<() => void>(),
+ open: vi.fn<() => void>(),
+ consumeLongPressFired: () => longPressFired,
+ triggerProps: {
+ onClick: vi.fn<() => void>(),
+ onContextMenu: vi.fn<() => void>(),
+ onTouchStart: vi.fn<() => void>(),
+ onTouchEnd: vi.fn<() => void>(),
+ onTouchMove: vi.fn<() => void>(),
+ onTouchCancel: vi.fn<() => void>(),
+ },
+});
+
+const renderTab = (longPressFired: boolean, onClick: () => void) =>
+ render(
+ icon}
+ selected={false}
+ tooltip="Home"
+ onClick={onClick}
+ menu={menu}
+ menuAnchor={menuAnchorStub(longPressFired)}
+ />
+ );
+
+describe('SidebarTab', () => {
+ it('does not navigate on the click that follows a long press', () => {
+ const onClick = vi.fn<() => void>();
+ renderTab(true, onClick);
+
+ // WKWebView sends no contextmenu on long press, so the release still produces a
+ // click; acting on it would navigate away from the menu just opened.
+ fireEvent.click(screen.getByRole('button'));
+
+ expect(onClick).not.toHaveBeenCalled();
+ });
+
+ it('navigates on an ordinary click', () => {
+ const onClick = vi.fn<() => void>();
+ renderTab(false, onClick);
+
+ fireEvent.click(screen.getByRole('button'));
+
+ expect(onClick).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/src/app/components/sidebar/SidebarTab.tsx b/src/app/components/sidebar/SidebarTab.tsx
index a75e88fc1..dc6909dcf 100644
--- a/src/app/components/sidebar/SidebarTab.tsx
+++ b/src/app/components/sidebar/SidebarTab.tsx
@@ -31,7 +31,14 @@ export function SidebarTab({
unreadCount = 0,
dm = false,
}: SidebarTabProps) {
- const { anchor, close, triggerProps } = menuAnchor;
+ const { anchor, close, triggerProps, consumeLongPressFired } = menuAnchor;
+
+ // Platforms that do not fire `contextmenu` on a long press (WKWebView) still send a
+ // synthetic click on release, which would navigate away from the menu just opened.
+ const handleClick = () => {
+ if (consumeLongPressFired()) return;
+ onClick();
+ };
return (
@@ -48,7 +55,7 @@ export function SidebarTab({
as="button"
ref={triggerRef}
outlined
- onClick={onClick}
+ onClick={handleClick}
onContextMenu={triggerProps.onContextMenu}
onTouchStart={triggerProps.onTouchStart}
onTouchEnd={triggerProps.onTouchEnd}
diff --git a/src/app/features/room/message/Message.tsx b/src/app/features/room/message/Message.tsx
index 0b93c06e1..3bb3604e6 100644
--- a/src/app/features/room/message/Message.tsx
+++ b/src/app/features/room/message/Message.tsx
@@ -266,7 +266,6 @@ type WrappedMessageProps = {
handleSwipeReply?: () => void;
handleSwipeEdit?: () => void;
handleSwipeActionChange?: (mode: SwipeActionMode) => void;
- handleContextMenu: MouseEventHandler;
align?: 'left' | 'right';
};
function WrappedMessage({
@@ -277,7 +276,6 @@ function WrappedMessage({
handleSwipeReply,
handleSwipeEdit,
handleSwipeActionChange,
- handleContextMenu,
align,
}: WrappedMessageProps) {
if (messageLayout === undefined) return <>{msgContentJSX}>;
@@ -289,9 +287,7 @@ function WrappedMessage({
onEdit={handleSwipeEdit}
onActionModeChange={handleSwipeActionChange}
>
-
- {msgContentJSX}
-
+ {msgContentJSX}
);
if (messageLayout === MessageLayout.Bubble)
@@ -301,12 +297,7 @@ function WrappedMessage({
onEdit={handleSwipeEdit}
onActionModeChange={handleSwipeActionChange}
>
-
+
{msgContentJSX}
@@ -317,7 +308,7 @@ function WrappedMessage({
onEdit={handleSwipeEdit}
onActionModeChange={handleSwipeActionChange}
>
-
+
{headerJSX}
{msgContentJSX}
@@ -880,7 +871,6 @@ function MessageInternal(
avatarJSX={avatarJSX()}
msgContentJSX={msgContentJSX}
messageLayout={messageLayout}
- handleContextMenu={() => {}}
align={useRightBubbles && senderId === mx.getUserId() ? 'right' : 'left'}
/>
@@ -1009,7 +999,6 @@ function MessageInternal(
handleSwipeReply={handleSwipeReply}
handleSwipeEdit={handleSwipeEdit}
handleSwipeActionChange={setSwipeActionMode}
- handleContextMenu={contextMenuHandler}
align={useRightBubbles && senderId === mx.getUserId() ? 'right' : 'left'}
/>
diff --git a/src/app/hooks/useMenuAnchor.test.ts b/src/app/hooks/useMenuAnchor.test.ts
index 30694d501..624d4c18e 100644
--- a/src/app/hooks/useMenuAnchor.test.ts
+++ b/src/app/hooks/useMenuAnchor.test.ts
@@ -127,6 +127,23 @@ describe('useMenuAnchor', () => {
});
});
+ describe('open', () => {
+ it('moves an already open menu to another element instead of closing it', () => {
+ const { result } = renderHook(() => useMenuAnchor());
+
+ act(() => {
+ result.current.open(createMockElement({ x: 10 }));
+ });
+ expect(result.current.anchor?.x).toBe(10);
+
+ // The toggling `openAt` would close here, stranding the caller's new target.
+ act(() => {
+ result.current.open(createMockElement({ x: 90 }));
+ });
+ expect(result.current.anchor?.x).toBe(90);
+ });
+ });
+
describe('onClick', () => {
it('opens the menu at the clicked element', () => {
const { result } = renderHook(() => useMenuAnchor());
@@ -234,6 +251,70 @@ describe('useMenuAnchor', () => {
expect(result.current.anchor).not.toBeUndefined();
expect(result.current.anchor!.x).toBe(50);
});
+
+ it('stays open when the native contextmenu beats the long-press timer', () => {
+ const { result } = renderHook(() => useMenuAnchor());
+ const element = createMockElement({ x: 50, y: 100 });
+
+ act(() => {
+ result.current.triggerProps.onTouchStart({
+ currentTarget: element,
+ touches: [{ clientX: 50, clientY: 100 } as Touch],
+ changedTouches: [{ clientX: 50, clientY: 100 } as Touch],
+ preventDefault: vi.fn<() => void>(),
+ nativeEvent: new TouchEvent('touchstart'),
+ } as unknown as React.TouchEvent);
+ });
+
+ // The platform's own long press lands just before our timer does.
+ act(() => {
+ result.current.triggerProps.onContextMenu({
+ currentTarget: element,
+ preventDefault: vi.fn<() => void>(),
+ stopPropagation: vi.fn<() => void>(),
+ nativeEvent: { clientX: 120, clientY: 300 } as MouseEvent,
+ } as unknown as React.MouseEvent);
+ });
+ expect(result.current.anchor).not.toBeUndefined();
+
+ act(() => {
+ vi.advanceTimersByTime(500);
+ });
+ act(() => {
+ result.current.triggerProps.onTouchEnd();
+ });
+
+ expect(result.current.anchor).not.toBeUndefined();
+ expect(result.current.anchor!.x).toBe(120);
+ });
+
+ it('cancels a pending custom long press when its contextmenu path consumes the event', () => {
+ const onLongPress = vi.fn<() => void>();
+ const { result } = renderHook(() => useMenuAnchor({ onLongPress }));
+ const element = createMockElement();
+
+ act(() => {
+ result.current.triggerProps.onTouchStart({
+ currentTarget: element,
+ touches: [{ clientX: 50, clientY: 100 } as Touch],
+ changedTouches: [{ clientX: 50, clientY: 100 } as Touch],
+ preventDefault: vi.fn<() => void>(),
+ nativeEvent: new TouchEvent('touchstart'),
+ } as unknown as React.TouchEvent);
+ });
+
+ let longPressFired: boolean | undefined;
+ act(() => {
+ longPressFired = result.current.consumeLongPressFired();
+ });
+ expect(longPressFired).toBe(false);
+
+ act(() => {
+ vi.advanceTimersByTime(500);
+ });
+
+ expect(onLongPress).not.toHaveBeenCalled();
+ });
});
describe('isPressing', () => {
diff --git a/src/app/hooks/useMenuAnchor.ts b/src/app/hooks/useMenuAnchor.ts
index 7677edbc4..e284f9733 100644
--- a/src/app/hooks/useMenuAnchor.ts
+++ b/src/app/hooks/useMenuAnchor.ts
@@ -18,8 +18,10 @@ export type MenuAnchor = {
/** Drives the press-feedback highlight while a long press is in progress. */
isPressing: boolean;
close: () => void;
- /** Anchors the menu to an element's box. */
+ /** Toggles the menu on an element's box. Click semantics: press again to close. */
openAt: (element: HTMLElement) => void;
+ /** Anchors the menu to an element's box without ever closing it. */
+ open: (element: HTMLElement) => void;
/** Click, right-click and long-press wiring for the trigger element. */
triggerProps: TriggerProps;
/** True once if a long press just fired, for callers that own their contextmenu handler. */
@@ -49,15 +51,23 @@ export function useMenuAnchor(
setAnchor((current) => (current ? undefined : cords));
}, []);
+ const open = useCallback((element: HTMLElement) => {
+ setAnchor(element.getBoundingClientRect());
+ }, []);
+
const longPress = useMobileLongPress(() => {
if (onLongPress) {
onLongPress();
return;
}
- if (triggerRef.current) openAt(triggerRef.current);
+ // The platform fires its own contextmenu at roughly this delay, and whichever
+ // lands first opens the menu. Opening, not toggling: a toggle would shut the
+ // menu that contextmenu just opened.
+ if (triggerRef.current) open(triggerRef.current);
});
const consumeLongPressFired = useCallback(() => {
+ longPress.onTouchCancel();
if (!longPress.firedRef.current) return false;
longPress.firedRef.current = false;
return true;
@@ -79,6 +89,7 @@ export function useMenuAnchor(
const onContextMenu: MouseEventHandler = useCallback(
(evt) => {
evt.preventDefault();
+ longPress.onTouchCancel();
// The long-press timer already opened it; a synthetic contextmenu would toggle it shut.
if (longPress.firedRef.current) {
longPress.firedRef.current = false;
@@ -105,6 +116,7 @@ export function useMenuAnchor(
consumeLongPressFired,
close,
openAt,
+ open,
triggerProps: {
onClick,
onContextMenu,
diff --git a/src/app/pages/client/sidebar/SpaceTabs.tsx b/src/app/pages/client/sidebar/SpaceTabs.tsx
index a4c7fd696..ffe356f27 100644
--- a/src/app/pages/client/sidebar/SpaceTabs.tsx
+++ b/src/app/pages/client/sidebar/SpaceTabs.tsx
@@ -770,7 +770,9 @@ export function SpaceTabs({ scrollRef }: Readonly) {
(evt) => {
evt.preventDefault();
setFolderMenuTarget(folder);
- folderMenu.openAt(evt.currentTarget as HTMLElement);
+ // Opening, not toggling: right-clicking a second folder while the first
+ // menu is open must move the menu, not close it.
+ folderMenu.open(evt.currentTarget as HTMLElement);
},
[folderMenu]
);