@@ -140,6 +140,10 @@ const renderWithConditionalWrapper = (shouldUseScrollView: boolean, contentConta
140
140
return < > { children } </ > ;
141
141
} ;
142
142
143
+ function getSelectedItemIndex ( menuItems : PopoverMenuItem [ ] ) {
144
+ return menuItems . findIndex ( ( option ) => option . isSelected ) ;
145
+ }
146
+
143
147
function PopoverMenu ( {
144
148
menuItems,
145
149
onItemSelected,
@@ -178,7 +182,7 @@ function PopoverMenu({
178
182
// eslint-disable-next-line rulesdir/prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth
179
183
const { isSmallScreenWidth} = useResponsiveLayout ( ) ;
180
184
const [ currentMenuItems , setCurrentMenuItems ] = useState ( menuItems ) ;
181
- const currentMenuItemsFocusedIndex = currentMenuItems ?. findIndex ( ( option ) => option . isSelected ) ;
185
+ const currentMenuItemsFocusedIndex = getSelectedItemIndex ( currentMenuItems ) ;
182
186
const [ enteredSubMenuIndexes , setEnteredSubMenuIndexes ] = useState < readonly number [ ] > ( CONST . EMPTY_ARRAY ) ;
183
187
const { windowHeight} = useWindowDimensions ( ) ;
184
188
@@ -304,7 +308,7 @@ function PopoverMenu({
304
308
) ;
305
309
306
310
const onModalHide = ( ) => {
307
- setFocusedIndex ( - 1 ) ;
311
+ setFocusedIndex ( currentMenuItemsFocusedIndex ) ;
308
312
} ;
309
313
310
314
// When the menu items are changed, we want to reset the sub-menu to make sure
@@ -316,7 +320,17 @@ function PopoverMenu({
316
320
}
317
321
setEnteredSubMenuIndexes ( CONST . EMPTY_ARRAY ) ;
318
322
setCurrentMenuItems ( menuItems ) ;
319
- } , [ menuItems ] ) ;
323
+
324
+ // Update the focused item to match the selected item, but only when the popover is not visible.
325
+ // This ensures that if the popover is visible, highlight from the keyboard navigation is not overridden
326
+ // by external updates.
327
+ if ( isVisible ) {
328
+ return ;
329
+ }
330
+ setFocusedIndex ( getSelectedItemIndex ( menuItems ) ) ;
331
+
332
+ // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
333
+ } , [ menuItems , setFocusedIndex ] ) ;
320
334
321
335
return (
322
336
< PopoverWithMeasuredContent
0 commit comments