Skip to content
Merged
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
19 changes: 16 additions & 3 deletions src/components/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,29 @@ function Modal({fullscreen = true, onModalHide = () => {}, type, onModalShow = (
const hideModal = () => {
window.removeEventListener('popstate', handlePopState);
if ((window.history.state as WindowState)?.shouldGoBack && shouldHandleNavigationBack) {
window.addEventListener('popstate', onModalHide, {once: true});
window.history.back();
onModalHide();
// Defer history.back() so it runs after any pending navigation
// callbacks (from onModalDidClose) have pushed their history entries.
// This prevents the popstate from undoing navigations triggered by
// menu item selection callbacks.
setTimeout(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daledah , is there another way we can avoid adding a new setTimeout here?

@daledah daledah Mar 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jayeshmangwani There's no synchronous alternative here - popstate is inherently async by spec, so setTimeout(0) is the minimal and most predictable way to yield

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arosiclair, we added zsetTimeout(0)` as a workaround to fix this issue #81239. Are we fine keeping it, or should we open the main issue to find a better solution?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

friendly bump here @jayeshmangwani

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, @daledah , could you clarify what you want me to confirm here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @jayeshmangwani. I missed the comment above. I'm checking now

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Came from this issue
This setTimeout creates conflicts with setNavigationActionToMicrotaskQueue, which can cause bugs
For example this

if (!(window.history.state as WindowState)?.shouldGoBack) {
return;
}
Comment on lines +55 to +57

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Remove modal guard entry after deferred navigation check

This early return skips history.back() whenever a menu action has already navigated, but those actions typically use Navigation.navigate, which pushes a new browser entry by default (src/libs/Navigation/helpers/linkTo/index.ts:159-161). That means the synthetic shouldGoBack entry created when the modal opened is never popped, so the history stack keeps an extra duplicate route and users need an extra Back press (or appear to go back to the same page) after navigating from the FAB menu.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daledah , I tested a few FAB navigation cases but couldn’t reproduce the issue with pressing back twice as GPT suggested above. Could you confirm on your side as well?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daledah can you explain this change a bit more? It looks like this is a fix for #81239. What was the root problem there and how does this fix it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daledah Please check the comment #82977 (comment) above⬆️.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was above, so I missed it. I'm checking again now.

@daledah daledah Mar 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daledah , I tested a few FAB navigation cases but couldn’t reproduce the issue with pressing back twice as GPT suggested above. Could you confirm on your side as well?

I've retested multiple times and couldn't reproduce the extra back-press issue

@daledah can you explain this change a bit more? It looks like this is a fix for #81239. What was the root problem there and how does this fix it?

@arosiclair
Rootcause: When shouldHandleNavigationBack is enabled, opening the FAB menu calls window.history.pushState({shouldGoBack: true}). When the modal closes, hideModal calls window.history.back() — which fires a popstate event asynchronously. If a menu item uses shouldCallAfterModalHide: true, its onSelected callback runs via onModalDidClose() synchronously right after history.back() is called, but before the popstate fires.

=> We should call onModalHide() first (so onModalDidClose() → menu callback → navigation runs synchronously), then defer history.back() via setTimeout(0) => the navigation callback has already pushed a new history state, so window.history.state.shouldGoBack is no longer true - and the history.back() call is skipped by the guard check.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rootcause: When shouldHandleNavigationBack is enabled, opening the FAB menu calls window.history.pushState({shouldGoBack: true}). When the modal closes, hideModal calls window.history.back()

So hideModal always calls window.history.back() even when we're not closing the menu using the back button? Is there a way to just prevent that in the non-back-button flow? Or would it be easier to implement shouldHandleNavigationBack without using window.history.pushState?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So hideModal always calls window.history.back() even when we're not closing the menu using the back button?

history.back() is not always called. When the modal is closed via the browser back button, popstate fires first - which already pops the {shouldGoBack: true} guard entry from the history stack. By the time hideModal runs, window.history.state.shouldGoBack is already false, so it falls to the else branch and calls onModalHide() directly. No history.back() needed.

Or would it be easier to implement shouldHandleNavigationBack without using window.history.pushState?

The push state or pop state pattern is the only web API that allows intercepting the browser's back button before it navigates. So pushState is necessary for this approach.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay I see. Thanks for explaining. This LGTM 👍

window.history.back();
}, 0);
} else {
onModalHide();
}
};

const showModal = () => {
if (shouldHandleNavigationBack) {
window.history.pushState({shouldGoBack: true}, '', null);
// Preserve React Navigation's state in the guard entry so that if
// popstate fires for this entry, RN recognizes the current route
// and does not perform an unexpected back navigation.
const currentState = (window.history.state ?? {}) as WindowState;
window.history.pushState({...currentState, shouldGoBack: true}, '', null);
window.addEventListener('popstate', handlePopState);
}
onModalShow?.();
Expand Down
4 changes: 4 additions & 0 deletions src/components/PopoverMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ type PopoverMenuProps = Partial<ModalAnimationProps> & {
/** Whether we want to show the popover on the right side of the screen */
fromSidebarMediumScreen?: boolean;

shouldHandleNavigationBack?: boolean;

/**
* Whether the modal should enable the new focus manager.
* We are attempting to migrate to a new refocus manager, adding this property for gradual migration.
Expand Down Expand Up @@ -271,6 +273,7 @@ function BasePopoverMenu({
onModalHide,
headerText,
fromSidebarMediumScreen,
shouldHandleNavigationBack,
anchorAlignment = {
horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.LEFT,
vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.BOTTOM,
Expand Down Expand Up @@ -610,6 +613,7 @@ function BasePopoverMenu({
restoreFocusType={restoreFocusType}
innerContainerStyle={{...styles.pv0, ...innerContainerStyle}}
shouldUseModalPaddingStyle={shouldUseModalPaddingStyle}
shouldHandleNavigationBack={shouldHandleNavigationBack}
testID={testID}
>
<FocusTrapForModal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ function FloatingActionButtonAndPopover({onHideCreateMenu, onShowCreateMenu, ref
{FabCreateReportConfirmationModal}
<PopoverMenu
onClose={hideCreateMenu}
shouldHandleNavigationBack
shouldEnableMaxHeight={false}
isVisible={isCreateMenuActive && (!shouldUseNarrowLayout || isFocused)}
anchorPosition={styles.createMenuPositionSidebar(windowHeight)}
Expand Down
Loading