Skip to content

Commit 4211161

Browse files
Fix focus stealing on render
When the menu is rendered, the portal is updated async. This steals the focus as `hide()` focuses the button. Instead only focus when requested, for example when the menu was visible and hiding should focus the trigger.
1 parent 8b2b1eb commit 4211161

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

src/components/Menu/Menu.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ const Menu = ({
364364
});
365365
}, [anchor, attachListeners, measureAnchorLayout, theme]);
366366

367-
const hide = React.useCallback(() => {
367+
const hide = React.useCallback((focus = true) => {
368368
removeListeners();
369369

370370
const { animation } = theme;
@@ -378,7 +378,7 @@ const Menu = ({
378378
setMenuLayout({ width: 0, height: 0 });
379379
setRendered(false);
380380
prevRendered.current = false;
381-
focusFirstDOMNode(anchorRef.current);
381+
if (focus) { focusFirstDOMNode(anchorRef.current) };
382382
});
383383
}, [removeListeners, theme]);
384384

@@ -393,7 +393,7 @@ const Menu = ({
393393
}
394394

395395
if (!display) {
396-
hide();
396+
hide(prevRendered.current);
397397
}
398398

399399
return;

0 commit comments

Comments
 (0)