Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions src/components/Menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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<LayoutRectangle>((resolve) => {
Expand Down
73 changes: 73 additions & 0 deletions src/components/__tests__/Menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Portal.Host>
<Menu
visible={visible}
onDismiss={onDismiss}
anchor={<Button mode="outlined">Open menu</Button>}
testID="scrollbar-menu"
>
<Menu.Item onPress={jest.fn()} title="Undo" />
</Menu>
</Portal.Host>
);
}

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();
}
});
Loading