From 2417119c60cc1203ffccb83769b2ef6dd655535f Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Mon, 27 Jul 2026 21:26:57 +0200 Subject: [PATCH 1/2] fix(menu): open context menus instead of toggling them shut --- .changeset/fix-menu-open-toggle-races.md | 5 ++ .../components/sidebar/SidebarTab.test.tsx | 71 +++++++++++++++++++ src/app/components/sidebar/SidebarTab.tsx | 11 ++- src/app/features/room/message/Message.tsx | 17 +---- src/app/hooks/useMenuAnchor.test.ts | 54 ++++++++++++++ src/app/hooks/useMenuAnchor.ts | 14 +++- src/app/pages/client/sidebar/SpaceTabs.tsx | 4 +- 7 files changed, 157 insertions(+), 19 deletions(-) create mode 100644 .changeset/fix-menu-open-toggle-races.md create mode 100644 src/app/components/sidebar/SidebarTab.test.tsx 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..85b530f0e 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,43 @@ 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(); + }); + + // The timer must re-anchor, never toggle the menu the contextmenu opened. + expect(result.current.anchor).not.toBeUndefined(); + expect(result.current.anchor!.x).toBe(50); + }); }); describe('isPressing', () => { diff --git a/src/app/hooks/useMenuAnchor.ts b/src/app/hooks/useMenuAnchor.ts index 7677edbc4..6116c6208 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,12 +51,19 @@ 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(() => { @@ -105,6 +114,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] ); From db7e33e507e08207833062e3858ee1c32f9121dc Mon Sep 17 00:00:00 2001 From: 7w1 Date: Mon, 27 Jul 2026 22:49:26 -0500 Subject: [PATCH 2/2] fix long press race condition --- src/app/hooks/useMenuAnchor.test.ts | 31 +++++++++++++++++++++++++++-- src/app/hooks/useMenuAnchor.ts | 2 ++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/app/hooks/useMenuAnchor.test.ts b/src/app/hooks/useMenuAnchor.test.ts index 85b530f0e..624d4c18e 100644 --- a/src/app/hooks/useMenuAnchor.test.ts +++ b/src/app/hooks/useMenuAnchor.test.ts @@ -284,9 +284,36 @@ describe('useMenuAnchor', () => { result.current.triggerProps.onTouchEnd(); }); - // The timer must re-anchor, never toggle the menu the contextmenu opened. expect(result.current.anchor).not.toBeUndefined(); - expect(result.current.anchor!.x).toBe(50); + 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(); }); }); diff --git a/src/app/hooks/useMenuAnchor.ts b/src/app/hooks/useMenuAnchor.ts index 6116c6208..e284f9733 100644 --- a/src/app/hooks/useMenuAnchor.ts +++ b/src/app/hooks/useMenuAnchor.ts @@ -67,6 +67,7 @@ export function useMenuAnchor( }); const consumeLongPressFired = useCallback(() => { + longPress.onTouchCancel(); if (!longPress.firedRef.current) return false; longPress.firedRef.current = false; return true; @@ -88,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;