Repository navigation
fix: device back button doesn't close FAB menu #82977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
dd38317
3d836ce
8f3b3a3
5302050
de7a82a
de0df90
b31adb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(() => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Came from this issue |
||
| if (!(window.history.state as WindowState)?.shouldGoBack) { | ||
| return; | ||
| } | ||
|
Comment on lines
+55
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This early return skips Useful? React with 👍 / 👎.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @daledah Please check the comment #82977 (comment) above⬆️.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I've retested multiple times and couldn't reproduce the extra back-press issue
@arosiclair => We should call
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So hideModal always calls
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The push state or pop state pattern is the only web API that allows intercepting the browser's back button before it navigates. So
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?.(); | ||
|
|
||
There was a problem hiding this comment.
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?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 yieldThere was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
friendly bump here @jayeshmangwani
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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