Skip to content
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

fix(EMI-1396): Fix Android hardware back button in WebViews #9354

Merged

Conversation

MrSltun
Copy link
Member

@MrSltun MrSltun commented Sep 28, 2023

This PR resolves EMI-1396

Description

This PR upgrades react-native-webview, and adds a handler for the back button in WebViews

Videos

Before After
Android
android-before.mp4
android-after.mp4
iOS
ios-before.mov
ios-after.mov

PR Checklist

  • I have tested my changes on iOS and Android.
  • I hid my changes behind a feature flag, or they don't need one.
  • I have included screenshots or videos, or I have not changed the UI.
  • I have added tests, or my changes don't require any.
  • I added an app state migration, or my changes do not require one.
  • I have documented any follow-up work that this PR will require, or it does not require any.
  • I have added a changelog entry below, or my changes do not require one.

To the reviewers 👀

  • I would like at least one of the reviewers to run this PR on the simulator or device.
Changelog updates

Changelog updates

Cross-platform user-facing changes

  • Add back handler for web views on Android - mrsltun

iOS user-facing changes

Android user-facing changes

Dev changes

Need help with something? Have a look at our docs, or get in touch with us.

MrSltun and others added 3 commits September 28, 2023 16:35
@ArtsyOpenSource
Copy link
Contributor

ArtsyOpenSource commented Sep 28, 2023

This PR contains the following changes:

  • Cross-platform user-facing changes (Add back handler for web views on Android - mrsltun)

Generated by 🚫 dangerJS against f2d51cc

Comment on lines -261 to +263
userAgent={userAgent}
applicationNameForUserAgent={userAgent}
Copy link
Member Author

Choose a reason for hiding this comment

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

Updated the userAgent due to this reported issue

Comment on lines 27 to 38
jest.mock("react-native-webview", () => {
const React = require("react")
const { View } = require("react-native")

return {
__esModule: true,
default: React.forwardRef((props: any, ref: any) => {
return <View ref={ref} {...props} />
}),
}
})

Copy link
Member Author

Choose a reason for hiding this comment

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

We have to mock the package after the update due to the new architecture (react-native-webview/react-native-webview#2686 (comment))

Copy link
Member

@gkartalis gkartalis left a comment

Choose a reason for hiding this comment

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

The tests should be fixed if you move the lines below to setupJEst file:

jest.mock("react-native-webview", () => {
  const React = require("react")
  const { View } = require("react-native")

  return {
    __esModule: true,
    default: React.forwardRef((props: any, ref: any) => {
      return <View ref={ref} {...props} />
    }),
  }
})

Comment on lines 27 to 38
jest.mock("react-native-webview", () => {
const React = require("react")
const { View } = require("react-native")

return {
__esModule: true,
default: React.forwardRef((props: any, ref: any) => {
return <View ref={ref} {...props} />
}),
}
})

Copy link
Member

Choose a reason for hiding this comment

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

fancy 🤑

}
}
}
onLeftButtonPress={useRightCloseButton && !canGoBack ? undefined : handleBackButtonPress}
Copy link
Member

Choose a reason for hiding this comment

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

nitpick(non-blocking): could we move this to an actual function with explicit returns over implicit ternary hell?

Suggested change
onLeftButtonPress={useRightCloseButton && !canGoBack ? undefined : handleBackButtonPress}
const handleLeftButtonPress = () => {
if (useRightCloseButton && !canGoBack) {
return
}
handleBackButtonPress()
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you may have to make the onLeftOnButtonPress undefined explicitly if I remember correct otherwise the component renders buttons or something, 🤔 I may be remembering wrong but something to check

Copy link
Member Author

Choose a reason for hiding this comment

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

@brainbicycle Yes! I tried a return when the condition is met, and there was a button showing. I'll try to explicitly return undefined and see

Copy link
Member Author

Choose a reason for hiding this comment

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

It always shows a button unless the prop is undefined (not a function) 🤷
But I will extract the trenary fo readability

Copy link
Contributor

@brainbicycle brainbicycle Sep 28, 2023

Choose a reason for hiding this comment

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

maybe we can update the component to not do that at some point, but def can be separate from this!

@@ -197,7 +197,7 @@
"react-native-url-polyfill": "1.3.0",
"react-native-view-shot": "3.4.0",
"react-native-vimeo-iframe": "1.2.1",
"react-native-webview": "11.22.7",
Copy link
Contributor

Choose a reason for hiding this comment

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

nice!

Copy link
Member

Choose a reason for hiding this comment

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

👋 bye bye old dependency

brainbicycle
brainbicycle previously approved these changes Sep 28, 2023
Copy link
Contributor

@brainbicycle brainbicycle left a comment

Choose a reason for hiding this comment

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

nice one!

Copy link
Member

@gkartalis gkartalis left a comment

Choose a reason for hiding this comment

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

🔥👏 awesome!

@MrSltun MrSltun merged commit 6947da1 into main Sep 29, 2023
8 checks passed
@MrSltun MrSltun deleted the mrsltun/EMI-1396/fix-Android-hardware-back-button-in-webviews branch September 29, 2023 13:05
handleGoBack()
} else {
if (systemBackAction) {
systemBackAction()
Copy link
Member

Choose a reason for hiding this comment

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

thought: I assume systemBackAction does the same as above

if (isPresentedModally && !canGoBack) {
      dismissModal()
    } else if (!canGoBack) {
      handleGoBack()
    }

return true
})

const leftButton = useRightCloseButton && !canGoBack ? undefined : handleBackButtonPress
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: onLeftButtonPress*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants