Skip to content

Commit 4790820

Browse files
praveenvenkat06Venkatachalapathy-PraveenRaj-IBMmaradwan26heloiselui2nikhiltom
authored
fix(menu): nested menu closes unexpectedly when hovering back from submenu item to parent menu item (#20881)
* fix(menu): nested menu closes unexpectedly when hovering Menu should not close when relatedTarget is undefined. * fix(menu): unit test case added --------- Co-authored-by: Praveen Raj V <Venkatachalapathy.PraveenRaj@ibm.com> Co-authored-by: Mahmoud <132728978+maradwan26@users.noreply.github.com> Co-authored-by: Heloise Lui <71858203+heloiselui@users.noreply.github.com> Co-authored-by: Nikhil Tomar <63502271+2nikhiltom@users.noreply.github.com>
1 parent d124104 commit 4790820

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

packages/react/src/components/Menu/Menu-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@ describe('Menu', () => {
125125
expect(document.querySelector('.custom-class')).toBeInTheDocument();
126126
document.body.removeChild(el);
127127
});
128+
129+
it('should not call onClose when relatedTarget is null on blur', () => {
130+
const onClose = jest.fn();
131+
render(
132+
<Menu open onClose={onClose} label="Test Menu">
133+
<MenuItem label="item" />
134+
</Menu>
135+
);
136+
137+
const menu = screen.getByRole('menu');
138+
fireEvent.blur(menu, { relatedTarget: null });
139+
140+
expect(onClose).not.toHaveBeenCalled();
141+
});
128142
});
129143

130144
describe('Submenu behavior', () => {

packages/react/src/components/Menu/Menu.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,13 @@ const Menu = forwardRef<HTMLUListElement, MenuProps>(function Menu(
272272
}
273273

274274
function handleBlur(e: React.FocusEvent<HTMLUListElement>) {
275-
if (open && onClose && isRoot && !menu.current?.contains(e.relatedTarget)) {
275+
if (
276+
open &&
277+
onClose &&
278+
isRoot &&
279+
e.relatedTarget &&
280+
!menu.current?.contains(e.relatedTarget)
281+
) {
276282
handleClose();
277283
}
278284
}

0 commit comments

Comments
 (0)