diff --git a/src/components/Menu/Menu.tsx b/src/components/Menu/Menu.tsx index f83cec3a47..ea3350dc70 100644 --- a/src/components/Menu/Menu.tsx +++ b/src/components/Menu/Menu.tsx @@ -111,6 +111,8 @@ export type Props = { // Minimum padding between the edge of the screen and the menu const SCREEN_INDENT = 8; +// A desktop scrollbar is about 15px. That change is not a rotation. +const SCROLLBAR_DIMENSION_SLOP = 24; // From https://material.io/design/motion/speed.html#duration const ANIMATION_DURATION = 250; // From the 'Standard easing' section of https://material.io/design/motion/speed.html#easing @@ -262,6 +264,33 @@ const Menu = ({ } }, [onDismiss, visible]); + const lastWindowRef = React.useRef({ + width: WINDOW_LAYOUT.width, + height: WINDOW_LAYOUT.height, + }); + + const handleDimensionChange = React.useCallback( + (event?: { window?: { width: number; height: number } }) => { + const next = event?.window; + const prev = lastWindowRef.current; + if (next) { + lastWindowRef.current = { width: next.width, height: next.height }; + const widthDelta = Math.abs(next.width - prev.width); + const heightDelta = Math.abs(next.height - prev.height); + // Opening a menu that overflows the page shows a scrollbar and fires + // this listener. Dismissing then makes the menu flash closed (#3854). + if ( + widthDelta < SCROLLBAR_DIMENSION_SLOP && + heightDelta < SCROLLBAR_DIMENSION_SLOP + ) { + return; + } + } + handleDismiss(); + }, + [handleDismiss] + ); + // Back and Escape go through the overlay stack, so that a menu opened from a // modal closes first and the modal stays. Without `onDismiss` there is // nothing to close, and the press is left to whatever is underneath. @@ -276,12 +305,14 @@ const Menu = ({ }, []); const attachListeners = React.useCallback(() => { + const current = Dimensions.get('window'); + lastWindowRef.current = { width: current.width, height: current.height }; dimensionsSubscriptionRef.current = addEventListener( Dimensions, 'change', - handleDismiss + handleDimensionChange ); - }, [handleDismiss]); + }, [handleDimensionChange]); const measureMenuLayout = () => new Promise((resolve) => { diff --git a/src/components/__tests__/Menu.test.tsx b/src/components/__tests__/Menu.test.tsx index 7fced51b87..2039abb52d 100644 --- a/src/components/__tests__/Menu.test.tsx +++ b/src/components/__tests__/Menu.test.tsx @@ -586,3 +586,76 @@ it.each([ } } ); + +it('does not dismiss when a scrollbar changes the window by a few pixels', async () => { + const onDismiss = jest.fn(); + const handlers: Function[] = []; + const addSpy = jest + .spyOn(Dimensions, 'addEventListener') + .mockImplementation((type, handler) => { + if (type === 'change') { + handlers.push(handler); + } + return { remove: jest.fn() }; + }); + const dimensionsSpy = jest.spyOn(Dimensions, 'get').mockReturnValue({ + width: 400, + height: 800, + scale: 1, + fontScale: 1, + }); + const measureSpy = jest + .spyOn(ViewPrototype, 'measureInWindow') + .mockImplementation((fn) => fn(100, 100, 80, 32)); + + const size = (width: number, height: number) => ({ + width, + height, + scale: 1, + fontScale: 1, + }); + + function makeMenu(visible: boolean) { + return ( + + Open menu} + testID="scrollbar-menu" + > + + + + ); + } + + try { + const { rerender } = await render(makeMenu(false)); + await act(async () => { + await rerender(makeMenu(true)); + await Promise.resolve(); + }); + await waitFor(() => { + expect(handlers.length).toBeGreaterThan(0); + }); + + const scrollbar = size(385, 800); + await act(() => { + handlers + .at(-1) + ?.call(undefined, { window: scrollbar, screen: scrollbar }); + }); + expect(onDismiss).not.toHaveBeenCalled(); + + const rotated = size(800, 400); + await act(() => { + handlers.at(-1)?.call(undefined, { window: rotated, screen: rotated }); + }); + expect(onDismiss).toHaveBeenCalledTimes(1); + } finally { + addSpy.mockRestore(); + dimensionsSpy.mockRestore(); + measureSpy.mockRestore(); + } +});