fix(app): keep a submenu open when its row is tapped on a touch screen (#10175)

A tap on the web arrives as pointer events: the finger enters the row as it
lands and leaves as it lifts. The submenu row and its flyout read both as
hover, so the leave queued a close just as the press opened the flyout, and
it disappeared about a quarter of a second later. A tap inside an open
flyout closed it the same way.

Hover intent now ignores pointer events whose pointerType is touch. Mouse
and pen behave as before, including on touch screens with a mouse attached.

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Christian Wu
2026-11-19 06:02:06 +00:00
committed by GitHub
co-authored by Claude Fable 5.2
parent 157abbbbda
commit 9faa3034f1
5 changed files with 122 additions and 15 deletions
@@ -3,7 +3,15 @@ import { createRoot, type Root } from "react-dom/client";
import { Text, View } from "react-native";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { page } from "vitest/browser";
import { MenuRoot, MenuSurface, MenuTrigger, type Size } from "./index";
import {
MenuItem,
MenuRoot,
MenuSubTrigger,
MenuSurface,
MenuTrigger,
type MenuPageDefinition,
type Size,
} from "./index";
// App sources compile against the classic JSX runtime, which expects React on the global.
beforeEach(() => vi.stubGlobal("React", React));
@@ -122,3 +130,65 @@ describe("anchored menu surface on web", () => {
expect(panel.width).toBeGreaterThanOrEqual(body.width);
});
});
const SUB_PAGES: MenuPageDefinition[] = [
{
id: "show",
title: "Show",
content: (
<MenuItem testID="show-option" closeOnSelect={false}>
Archived
</MenuItem>
),
},
];
/** Past the grace a flyout is given after the pointer leaves, so a queued close has landed. */
const HOVER_CLOSE_SETTLED_MS = 600;
/**
* A tap as the browser reports it: the finger enters the row as it lands and leaves it as it
* lifts, and only then does the click arrive.
*/
function tap(target: HTMLElement): void {
const pointer = (type: string) =>
target.dispatchEvent(
new PointerEvent(type, { bubbles: true, pointerType: "touch", isPrimary: true }),
);
act(() => {
pointer("pointerover");
pointer("pointerdown");
pointer("pointerup");
pointer("pointerout");
target.click();
});
}
describe("submenu flyout on a touch screen", () => {
beforeEach(async () => {
await page.viewport(1280, 900);
});
it("stays open after a tap on its row, and after a tap inside it", async () => {
mount(
<MenuRoot>
<MenuTrigger testID="pill" accessibilityLabel="Open menu">
<Text>Open menu</Text>
</MenuTrigger>
<MenuSurface pages={SUB_PAGES} testID="panel">
<MenuSubTrigger id="show" testID="show-row">
Show
</MenuSubTrigger>
</MenuSurface>
</MenuRoot>,
);
act(() => element("pill").click());
tap(element("show-row"));
await wait(HOVER_CLOSE_SETTLED_MS);
tap(element("show-option"));
await wait(HOVER_CLOSE_SETTLED_MS);
expect(document.querySelector('[data-testid="show-option"]')).not.toBeNull();
});
});
@@ -18,6 +18,7 @@ import {
Platform,
Pressable,
View,
type PointerEvent,
type StyleProp,
type ViewStyle,
} from "react-native";
@@ -217,8 +218,8 @@ export interface AnchoredSurfaceProps {
* Hover tracking for the surface box, per docs/hover.md: plain View, pointer events, never
* a Pressable. Web-only by nature, which is all a flyout needs.
*/
onPointerEnter?: () => void;
onPointerLeave?: () => void;
onPointerEnter?: (event: PointerEvent) => void;
onPointerLeave?: (event: PointerEvent) => void;
testID?: string;
keyboardFocusScope?: KeyboardFocusScope;
children: ReactElement;
@@ -0,0 +1,13 @@
import type { PointerEvent } from "react-native";
/**
* Whether a pointer event came from something that can rest on a row without pressing it.
*
* A finger cannot, but the browser still reports one entering a row as the tap lands and leaving
* as it lifts. Read as hover, that leave queues a close just as the tap's own press opens the
* submenu, and the flyout vanishes a moment after it appears. A touch screen can have a mouse
* attached too, so this has to be asked of each event, not of the device.
*/
export function isHoverPointer(event: PointerEvent): boolean {
return event.nativeEvent.pointerType !== "touch";
}
@@ -6,7 +6,7 @@ import {
type PropsWithChildren,
type ReactElement,
} from "react";
import { Text, View } from "react-native";
import { Text, View, type PointerEvent } from "react-native";
import { StyleSheet, withUnistyles } from "react-native-unistyles";
import { ChevronRight } from "lucide-react-native";
import type { Theme } from "@/styles/theme";
@@ -14,6 +14,7 @@ import { useMenuContext, useMenuDepth } from "./menu-context";
import { isSubPageOpen } from "./menu-navigation";
import { useMenuSurface } from "./menu-surface";
import { MenuItem } from "./menu-item";
import { isHoverPointer } from "./menu-pointer";
const ThemedChevronRight = withUnistyles(ChevronRight);
const mutedIconMapping = (theme: Theme) => ({ color: theme.colors.foregroundMuted });
@@ -25,7 +26,8 @@ const mutedIconMapping = (theme: Theme) => ({ color: theme.colors.foregroundMute
*
* Hover lives on a plain `View` wrapping the item's `Pressable`, per docs/hover.md — a
* `Pressable` tracking its own hover would fight the one inside it. Hover only fires on web,
* which is exactly where flyouts exist; every other platform opens the page by pressing.
* which is exactly where flyouts exist; every other platform opens the page by pressing. So does
* a finger on the web, whose taps arrive as pointer events but are not hover.
*/
export function MenuSubTrigger({
id,
@@ -68,14 +70,22 @@ export function MenuSubTrigger({
menu.openSub({ id, depth });
}, [menu, id, depth]);
const handlePointerEnter = useCallback(() => {
cancelHoverClose();
hoverOpen({ id, depth });
}, [cancelHoverClose, hoverOpen, id, depth]);
const handlePointerEnter = useCallback(
(event: PointerEvent) => {
if (!isHoverPointer(event)) return;
cancelHoverClose();
hoverOpen({ id, depth });
},
[cancelHoverClose, hoverOpen, id, depth],
);
const handlePointerLeave = useCallback(() => {
hoverClose(depth);
}, [hoverClose, depth]);
const handlePointerLeave = useCallback(
(event: PointerEvent) => {
if (!isHoverPointer(event)) return;
hoverClose(depth);
},
[hoverClose, depth],
);
const trailing = useMemo(
() => <MenuSubTrailing value={value} indicator={indicator} />,
@@ -9,7 +9,7 @@ import {
type ReactNode,
} from "react";
import { useTranslation } from "react-i18next";
import { Pressable, Text, View, useWindowDimensions } from "react-native";
import { Pressable, Text, View, useWindowDimensions, type PointerEvent } from "react-native";
import { StyleSheet, withUnistyles } from "react-native-unistyles";
import { BottomSheetScrollView } from "@gorhom/bottom-sheet";
import { ChevronLeft } from "lucide-react-native";
@@ -30,6 +30,7 @@ import { useMenuContext, MenuContextProvider } from "./menu-context";
import { MenuPage } from "./menu-item";
import { currentPageId, isSubPageOpen } from "./menu-navigation";
import { AnchoredSurface, MenuOverlay } from "./menu-overlay";
import { isHoverPointer } from "./menu-pointer";
import { getMenuSheetBottomPadding } from "./menu-sheet-layout";
import type { Alignment, Placement } from "../anchor";
import type { KeyboardFocusScope } from "@/keyboard/actions";
@@ -327,7 +328,19 @@ function MenuFlyout({
const menu = useMenuContext("MenuFlyout");
const { hoverClose, cancelHoverClose } = useMenuSurface("MenuFlyout");
const handleClose = useCallback(() => menu.setOpen(false), [menu]);
const handleHoverOut = useCallback(() => hoverClose(depth), [hoverClose, depth]);
const handleHoverIn = useCallback(
(event: PointerEvent) => {
if (isHoverPointer(event)) cancelHoverClose();
},
[cancelHoverClose],
);
// A tap inside the flyout ends with the finger leaving it, which is not the pointer walking off.
const handleHoverOut = useCallback(
(event: PointerEvent) => {
if (isHoverPointer(event)) hoverClose(depth);
},
[hoverClose, depth],
);
if (!anchorRef) return null;
@@ -344,7 +357,7 @@ function MenuFlyout({
maxHeight={maxHeight}
scrollable={scrollable}
backdrop={false}
onPointerEnter={cancelHoverClose}
onPointerEnter={handleHoverIn}
onPointerLeave={handleHoverOut}
testID={testID}
>