From af2cb37bd616308f6fa08456a203d55d4bc087f8 Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Fri, 24 Jul 2026 13:42:02 +0200 Subject: [PATCH] fix(android): dedup synthetic click after mobile tap activation chore: format and add changeset --- .changeset/fix-mobile-back-double-nav.md | 5 +++ src/app/components/MobileMenuItem.tsx | 2 +- src/app/features/room-nav/RoomNavItem.tsx | 20 +++++---- src/app/hooks/useMobileTapActivation.ts | 44 ++++++++++++++++---- src/app/pages/client/sidebar/InboxTab.tsx | 7 +++- src/app/pages/client/sidebar/MessageTab.tsx | 3 +- src/app/pages/client/sidebar/NavigateTab.tsx | 3 +- src/app/pages/client/sidebar/SpaceTabs.tsx | 7 +++- src/app/pages/client/sidebar/UserMenuTab.tsx | 11 +++-- 9 files changed, 73 insertions(+), 29 deletions(-) create mode 100644 .changeset/fix-mobile-back-double-nav.md diff --git a/.changeset/fix-mobile-back-double-nav.md b/.changeset/fix-mobile-back-double-nav.md new file mode 100644 index 000000000..a6e427f5f --- /dev/null +++ b/.changeset/fix-mobile-back-double-nav.md @@ -0,0 +1,5 @@ +--- +default: patch +--- + +Stop opening rooms (and other mobile nav surfaces) twice on a single tap, which made backing out take two back presses. diff --git a/src/app/components/MobileMenuItem.tsx b/src/app/components/MobileMenuItem.tsx index ff729767f..9416aa5f7 100644 --- a/src/app/components/MobileMenuItem.tsx +++ b/src/app/components/MobileMenuItem.tsx @@ -19,5 +19,5 @@ export function MobileMenuItem({ isMobile, onClick, ...props }: MobileMenuItemPr const activation = useMobileTapActivation(isMobile, (evt) => { onClick?.(evt); }); - return ; + return ; } diff --git a/src/app/features/room-nav/RoomNavItem.tsx b/src/app/features/room-nav/RoomNavItem.tsx index f96441498..91714a0c1 100644 --- a/src/app/features/room-nav/RoomNavItem.tsx +++ b/src/app/features/room-nav/RoomNavItem.tsx @@ -448,13 +448,17 @@ export function RoomNavItem({ } }; - const mobileTapActivation = useMobileTapActivation(isMobile && !room.isCallRoom(), () => { - if (openMobileDrawerContent) { - openMobileDrawerContent(linkPath); - } else { - navigate(linkPath); - } - }); + const mobileTapActivation = useMobileTapActivation( + isMobile && !room.isCallRoom(), + () => { + if (openMobileDrawerContent) { + openMobileDrawerContent(linkPath); + } else { + navigate(linkPath); + } + }, + handleNavItemClick + ); const handleChatButtonClick = (evt: MouseEvent) => { evt.stopPropagation(); @@ -521,7 +525,7 @@ export function RoomNavItem({ > {(triggerRef) => ( { warmupRoomDecryption(mx, room.roomId); mobileTapActivation.onPointerDown(evt); diff --git a/src/app/hooks/useMobileTapActivation.ts b/src/app/hooks/useMobileTapActivation.ts index f59ab89c0..7e9ec88d7 100644 --- a/src/app/hooks/useMobileTapActivation.ts +++ b/src/app/hooks/useMobileTapActivation.ts @@ -1,23 +1,34 @@ -import type { PointerEventHandler, PointerEvent as ReactPointerEvent } from 'react'; -import { useRef } from 'react'; +import type { MouseEvent as ReactMouseEvent, MouseEventHandler, PointerEventHandler } from 'react'; +import { useCallback, useRef } from 'react'; const TAP_MOVEMENT_THRESHOLD = 10; const MAX_TAP_DURATION = 500; -// Android WebView suppresses click synthesis after a drag, so the first tap -// on a nav control after swiping the drawer produces no click event. Activate -// directly on pointerup instead. Do not wrap the callback in startTransition -// or defer it. That reintroduces the double-tap. +// Android WebView suppresses click synthesis after a drag gesture, so the first +// tap on a nav control after swiping the drawer produces no click event. Activate +// directly on pointerup instead, and swallow the synthetic click that follows a +// touch tap so the action fires exactly once. (preventDefault on pointerup does +// NOT suppress click — click is an activation event, not a compatibility mouse +// event — so the dedup is handled by the returned `onClick` wrapper below.) +// +// `onActivate` runs on a qualifying touch pointerup. The returned `onClick` +// wraps the caller's click handler: if a touch tap already activated, the click +// is swallowed; otherwise the click handler runs (desktop, keyboard, AT, and +// the post-swipe case where no click arrives). Do not wrap the callbacks in +// startTransition or defer them — that reintroduces the double-tap. export function useMobileTapActivation( enabled: boolean, - onActivate: (evt: ReactPointerEvent) => void + onActivate: (evt: ReactMouseEvent) => void, + onClick?: MouseEventHandler ): { onPointerDown: PointerEventHandler; onPointerMove: PointerEventHandler; onPointerUp: PointerEventHandler; onPointerCancel: PointerEventHandler; + onClick: MouseEventHandler; } { const onActivateRef = useRef(onActivate); + const onClickRef = useRef(onClick); const pointerDownRef = useRef<{ pointerId: number; x: number; @@ -25,9 +36,14 @@ export function useMobileTapActivation( timestamp: number; eligible: boolean; } | null>(null); + // True between a qualifying touch pointerup and the click it synthesises. + // Reset on every pointerdown so a stale flag can never outlive one tap. + const activatedRef = useRef(false); onActivateRef.current = onActivate; + onClickRef.current = onClick; const onPointerDown: PointerEventHandler = (evt) => { + activatedRef.current = false; if (!enabled || evt.pointerType !== 'touch' || !evt.isPrimary || evt.button !== 0) { pointerDownRef.current = null; return; @@ -70,6 +86,7 @@ export function useMobileTapActivation( } evt.preventDefault(); + activatedRef.current = true; onActivateRef.current(evt); }; const onPointerCancel: PointerEventHandler = (evt) => { @@ -77,5 +94,16 @@ export function useMobileTapActivation( pointerDownRef.current = null; }; - return { onPointerDown, onPointerMove, onPointerUp, onPointerCancel }; + // Swallow the synthetic click that follows a touch pointerup; otherwise run + // the caller's click handler (or onActivate when none was provided). + const handleClick: MouseEventHandler = useCallback((evt) => { + if (activatedRef.current) { + activatedRef.current = false; + return; + } + if (onClickRef.current) onClickRef.current(evt); + else onActivateRef.current(evt); + }, []); + + return { onPointerDown, onPointerMove, onPointerUp, onPointerCancel, onClick: handleClick }; } diff --git a/src/app/pages/client/sidebar/InboxTab.tsx b/src/app/pages/client/sidebar/InboxTab.tsx index 4c0fef696..6d25388a7 100644 --- a/src/app/pages/client/sidebar/InboxTab.tsx +++ b/src/app/pages/client/sidebar/InboxTab.tsx @@ -55,7 +55,11 @@ export function InboxTab({ isBottom, isMobile }: { isBottom?: boolean; isMobile? const path = inviteCount > 0 ? getInboxInvitesPath() : getInboxNotificationsPath(); navigate(path); }; - const mobileTapActivation = useMobileTapActivation(isMobile ?? false, handleInboxClick); + const mobileTapActivation = useMobileTapActivation( + isMobile ?? false, + handleInboxClick, + handleInboxClick + ); return ( @@ -66,7 +70,6 @@ export function InboxTab({ isBottom, isMobile }: { isBottom?: boolean; isMobile? as="button" ref={triggerRef} outlined={!isMobile} - onClick={handleInboxClick} {...mobileTapActivation} size={'400'} > diff --git a/src/app/pages/client/sidebar/MessageTab.tsx b/src/app/pages/client/sidebar/MessageTab.tsx index 880f461db..24fa2caa3 100644 --- a/src/app/pages/client/sidebar/MessageTab.tsx +++ b/src/app/pages/client/sidebar/MessageTab.tsx @@ -49,7 +49,7 @@ export function MessageTab({ isBottom, isMobile }: { isBottom?: boolean; isMobil navigate(getSpacePath(lastSpaceId)); }; - const mobileTapActivation = useMobileTapActivation(isMobile ?? false, onBack); + const mobileTapActivation = useMobileTapActivation(isMobile ?? false, onBack, onBack); const [showUnreadCounts] = useSetting(settingsAtom, 'showUnreadCounts'); const [badgeCountDMsOnly] = useSetting(settingsAtom, 'badgeCountDMsOnly'); @@ -73,7 +73,6 @@ export function MessageTab({ isBottom, isMobile }: { isBottom?: boolean; isMobil as="button" ref={triggerRef} outlined={!isMobile} - onClick={onBack} {...mobileTapActivation} size={'400'} > diff --git a/src/app/pages/client/sidebar/NavigateTab.tsx b/src/app/pages/client/sidebar/NavigateTab.tsx index e714a4f0b..c9bbfb906 100644 --- a/src/app/pages/client/sidebar/NavigateTab.tsx +++ b/src/app/pages/client/sidebar/NavigateTab.tsx @@ -18,7 +18,7 @@ export function NavigateTab({ isBottom, isMobile }: { isBottom?: boolean; isMobi if (isMobile) navigate(getNavigatePath()); else setOpen(true); }; - const mobileTapActivation = useMobileTapActivation(isMobile ?? false, open); + const mobileTapActivation = useMobileTapActivation(isMobile ?? false, open, open); return ( @@ -29,7 +29,6 @@ export function NavigateTab({ isBottom, isMobile }: { isBottom?: boolean; isMobi as="button" ref={triggerRef} outlined={!isMobile} - onClick={open} {...mobileTapActivation} size={'400'} > diff --git a/src/app/pages/client/sidebar/SpaceTabs.tsx b/src/app/pages/client/sidebar/SpaceTabs.tsx index d043b0fbc..a1954644c 100644 --- a/src/app/pages/client/sidebar/SpaceTabs.tsx +++ b/src/app/pages/client/sidebar/SpaceTabs.tsx @@ -555,7 +555,11 @@ function SpaceTab({ }: Readonly) { const isMobile = useScreenSizeContext() === ScreenSize.Mobile; const targetRef = useRef(null); - const mobileTapActivation = useMobileTapActivation(isMobile, () => onSelect(space.roomId)); + const mobileTapActivation = useMobileTapActivation( + isMobile, + () => onSelect(space.roomId), + () => onSelect(space.roomId) + ); const spaceDraggable: SidebarDraggable = useMemo( () => @@ -602,7 +606,6 @@ function SpaceTab({ data-id={space.roomId} ref={triggerRef} size={folder ? '300' : '400'} - onClick={() => onSelect(space.roomId)} onContextMenu={handleContextMenu} {...mobileTapActivation} > diff --git a/src/app/pages/client/sidebar/UserMenuTab.tsx b/src/app/pages/client/sidebar/UserMenuTab.tsx index 64182785e..f630a8731 100644 --- a/src/app/pages/client/sidebar/UserMenuTab.tsx +++ b/src/app/pages/client/sidebar/UserMenuTab.tsx @@ -682,9 +682,13 @@ export function UserMenuTab({ isBottom, isMobile }: { isBottom?: boolean; isMobi }; const handleCloseMenu = () => setMenuAnchor(undefined); - const mobileTapActivation = useMobileTapActivation(isMobile ?? false, () => { - navigate(getProfilePath()); - }); + const mobileTapActivation = useMobileTapActivation( + isMobile ?? false, + () => { + navigate(getProfilePath()); + }, + handleToggle + ); const isActive = (!!menuAnchor || profileSelected) && !isMobile; @@ -693,7 +697,6 @@ export function UserMenuTab({ isBottom, isMobile }: { isBottom?: boolean; isMobi