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 mSafari focus #23922

Closed
wants to merge 39 commits into from
Closed

Conversation

staszekscp
Copy link
Contributor

@staszekscp staszekscp commented Jul 31, 2023

Details

This PR fixes a bug of autofocus on mobile Safari, where keyboard was not opening, while navigating from a Modal opened via FAB.

cc: @ArekChr

Fixed Issues

$ #10414
PROPOSAL: #10414 (comment)

Tests

  • Verify that no errors appear in the JS console

Offline tests

QA Steps

  • Verify that no errors appear in the JS console
  1. Make sure the software keyboard is on - for simulator go to: I/O -> Keyboard and make sure the hardware keyboard is not connected
  2. Open mSafari
  3. Press FAB (the green button with + in bottom right corner)
  4. Open New Chat
  5. See if the keyboard opens
  6. Go back
  7. Repeat steps 4-6 for New Group and New Room also

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I verified the translation was requested/reviewed in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • If we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG))
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Web
Screen.Recording.2023-07-31.at.15.03.44.mov
Mobile Web - Chrome
IMG_4122.MOV
Mobile Web - Safari
safari.mov
Desktop
Screen.Recording.2023-07-31.at.15.10.26.mov
iOS
ios.mov
Android
android.mov

@staszekscp staszekscp requested a review from a team as a code owner July 31, 2023 13:48
@melvin-bot
Copy link

melvin-bot bot commented Jul 31, 2023

Hey! I see that you made changes to our Form component. Make sure to update the docs in FORMS.md accordingly. Cheers!

@melvin-bot melvin-bot bot requested review from deetergp and removed request for a team July 31, 2023 13:49
@melvin-bot
Copy link

melvin-bot bot commented Jul 31, 2023

@deetergp Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@ArekChr
Copy link
Contributor

ArekChr commented Aug 1, 2023

cc: parasharrajat as C+ reviewer as well

>
{props.isSmallScreenWidth && props.network.isOffline && <OfflineIndicator style={[StyleUtils.getBackgroundColorStyle(themeColors.greenHighlightBackground)]} />}
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems the offline indicator is unrelated to this PR's purpose. Can you clarify if this is intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure - the coverScreen prop set to false in react-native-modal changes the component from Modal to View. It caused the OfflineIndicator component to become visible, because it was adjacent to the PopoverMenu. I decided to slightly change the components' order to make sure that everything appears correctly on the screen, and the PopoverMenu stays on top.

Copy link
Member

Choose a reason for hiding this comment

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

I see but this is a duplicate. Can't we just do that with zIndex?

Copy link
Member

Choose a reason for hiding this comment

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

@staszekscp Thoughts? Is there a way to DRY it and keep using the existing OfflineIndicator from screenWrapper?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately I was checking it, while I was writing this code, and a simple zIndex change will not work. The problem is most probably connected to the component tree and their position value, so when I was playing with it, it seemed that some bigger refactor would have to be applied in order to make it work with zIndex.

Copy link
Member

Choose a reason for hiding this comment

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

@staszekscp I was checking I didn't see any such issues. Could you please explain the issue? if possible, a video of it.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By using a simple zIndex we end up with a wrong layout when the OfflineIndicator is displayed, because its backgroundColor is transparent. And it should stay this way, because it can appear on differet screens with different colors.
image

Without changing anything, the OfflineIndicator appears incorrectly when modal is displayed. That's why I decided to change components' hierarchy a bit to adjust that
image

Copy link
Member

Choose a reason for hiding this comment

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

I see. I wasn't seeing UI as per the second image so I asked. Might be some config. I will recheck.

}
props.onClose();
}}
style={[styles.fullscreenFixed, {opacity: variables.overlayOpacity, backgroundColor: themeColors.overlay}]}
Copy link
Contributor

Choose a reason for hiding this comment

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

No inline styles. Keep it in styles files for maintainability and reusability.

coverScreen: PropTypes.bool,

/** Custom backdrop JSX element */
customBackdrop: PropTypes.object,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
customBackdrop: PropTypes.object,
customBackdrop: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),

/** Should we use coverScreen prop for ReactNativeModal? */
coverScreen: PropTypes.bool,

/** Custom backdrop JSX element */
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/** Custom backdrop JSX element */
/** The customBackdrop prop allows the addition of a custom backdrop element behind the modal. */

vertical: PropTypes.number.isRequired,
}).isRequired,

/** Where the popover should be positioned relative to the anchor points. */
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/** Where the popover should be positioned relative to the anchor points. */
/** Sets the popover's position relative to the anchor points */

...createMenuPropTypes,
...windowDimensionsPropTypes,

/** The horizontal and vertical anchors points for the popover */
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/** The horizontal and vertical anchors points for the popover */
/** Defines the anchor points for the popover */

vertical: PropTypes.oneOf(_.values(CONST.MODAL.ANCHOR_ORIGIN_VERTICAL)),
}),

shouldNavigateBeforeClosingModal: PropTypes.bool,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
shouldNavigateBeforeClosingModal: PropTypes.bool,
/** Indicates whether navigation should occur before closing the modal */
shouldNavigateBeforeClosingModal: PropTypes.bool,

@@ -466,6 +466,7 @@ export default compose(
withOnyx({
formState: {
key: (props) => props.formID,
initWithStoredValues: false,
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you clarify why this change has been made? The form is used in many places across the codebase, and I'm not sure I understand why this modification has been made.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

While I was working on this issue I noticed, that keyboard was not opening for Safari when some async calls from Onyx were executed. It was linked to reaching to the browser's local storage - especially when the call was taking ~50 ms. You are 100% right that it doesn't have to be changed for other platforms, so I'll use the Browser.isMobileSafari() instead. I'll also add explanation to make clear why it had to be added.

@staszekscp
Copy link
Contributor Author

Hey @parasharrajat! Have you had a chance to see if there is still something to be improved?

@parasharrajat
Copy link
Member

parasharrajat commented Aug 22, 2023

Yeah, reviewed it. It is correctly setting the focus but I am looking into other things like below.

  1. I suspect that due to changes to the form, it will not show backend errors on page load on Safari. It is hard to reproduce these errors as I don't know exactly when the backend throws one. A possible test target is Connect Bank account flow.
    Test Scenario can be:

User fills out a form on page 1 and then goes to page 2 for some action, we optimistically complete the action on that page and move back to page 1. The error was already thrown on page 2 but as page 1 has the same form, it might be showing that error.

I didn't exactly find any such page yet.

  1. I am curious about optimizing the offline loader, I understand that it is deeply nested but using the flag to suppress that and adding the same on the upper layer does not look very elegant.

I couldn't post it yesterday as I was feeling low.

@staszekscp
Copy link
Contributor Author

Hey! Sorry for the delay, I am currently focused on another task, which takes almost 100% of my time. Also, sorry to hear that you were feeling low, I hope you're better now!

As per your comment - Seeing the Form component I suspect that its refactor could help, so we wouldn't have to change the initWithStoredValues flag, but I cannot be 100% sure. In order to analyse the impact we would have to get access to the Safari code base which is not open-sourced. I understand your concern, however for now I don't see any alternative solution to make sure that opening keyboard would reliably open in each case.

Therefore the question is - should we let it be merged, since there were no errors that were reproduced, and discovered so far, or should we maybe wait for the Form component refactor, and see if it helps in this edge case? I know that the work is in progress, but because it is a big component it may take a while.

Talking about the OfflineIndicator - I think that this change isn't an exception in the codebase, since there are already multiple cases when shouldShowOfflineIndicator prop is used. That's also why I decided to use this pattern :)

@parasharrajat
Copy link
Member

Thanks for the reply. I am fine with merging without worrying about the form error case. Let me check the offline indicator code and see if I can come up with some quick optimization.

@staszekscp
Copy link
Contributor Author

Hi! Because we're after the merge freeze I'd like to ask, should we proceed with this task?

@parasharrajat
Copy link
Member

@staszekscp Sure. Can you please merge main?

@@ -124,7 +133,8 @@ class BaseModal extends PureComponent {
backdropOpacity={hideBackdrop ? 0 : variables.overlayOpacity}
backdropTransitionOutTiming={0}
hasBackdrop={this.props.fullscreen}
coverScreen={this.props.fullscreen}
customBackdrop={this.props.customBackdrop && this.props.customBackdrop(this.onBackdropPress)}
coverScreen={!this.props.isSmallScreenWidth || this.props.coverScreen}
Copy link
Member

Choose a reason for hiding this comment

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

I feel like we should pass this !this.props.isSmallScreenWidth from index.web.js file and here only pass prop value

Comment on lines +46 to +54
coverScreen={false}
customBackdrop={(onBackdropPress) => (
<Pressable
onPress={onBackdropPress}
style={styles.modalBackdropWeb}
accessibilityLabel="backdrop"
accessibilityRole={CONST.ACCESSIBILITY_ROLE.ADJUSTABLE}
/>
)}
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't these changes be done to desktop as well?

Copy link
Member

Choose a reason for hiding this comment

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

Bump.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think they are needed, since this is a fix only for mobile safari, we do not open keyboard on desktop, and the app works correctly there anyway

Comment on lines 68 to 73
if (!props.shouldDelayFocus) {
console.log('delays')
focusTimeout = setTimeout(() => input.current.focus(), CONST.ANIMATED_TRANSITION);
return;
}
console.log('doesnt delay')
Copy link
Member

Choose a reason for hiding this comment

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

Extra changes

@@ -56,4 +62,4 @@ function SidebarScreen(props) {
SidebarScreen.propTypes = sidebarPropTypes;
SidebarScreen.displayName = 'SidebarScreen';

export default withWindowDimensions(SidebarScreen);
export default compose(withWindowDimensions, withNetwork())(SidebarScreen);
Copy link
Member

Choose a reason for hiding this comment

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

Let's use useNetwork, useWindowDimensions hooks instead.

@parasharrajat
Copy link
Member

parasharrajat commented Sep 13, 2023

Please check the comments as well. It seems something is broken on the main which leads to action failure.

@staszekscp
Copy link
Contributor Author

Hey @parasharrajat, actually I noticed that something is not working after the newest main has been merged, so I have to investigate the issue, I'll let you know when I figure that out

Copy link
Member

Choose a reason for hiding this comment

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

@@ -181,7 +188,8 @@ function BaseModal({
backdropOpacity={hideBackdrop ? 0 : variables.overlayOpacity}
backdropTransitionOutTiming={0}
hasBackdrop={fullscreen}
coverScreen={fullscreen}
coverScreen={!isSmallScreenWidth || coverScreen}
Copy link
Member

Choose a reason for hiding this comment

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

Because we are already passing coverScreen prop here, can we control its value from the parent index.js file and pass !isSmallScreenWidth there?

This way, it will be more generic and clear that !isSmallScreenWidth is applicable to web | desktop.

@parasharrajat
Copy link
Member

@staszekscp There is a comment here #23922 (comment)

@staszekscp
Copy link
Contributor Author

I made this solution work, but I noticed that the initWithStoredValues case has broaden a bit. After investigating the topic it turns out that the root cause of this edge case is react-native-onyx, and the fact that withOnyx while fetching data returns null. Apparently it is enough for Safari to not allow the keyboard opening.

Setting initWithStoredValues to false for all the keys in the Form component "solves" the issue, but it seems to be a workaround anyway. I would like to have a closer look at that, but currently I'm involved in some high priority performance issues, so I won't be able to focus on this specific case early next week. On the other hand one of those issues is closely related to react-native-onyx, so I hope to get some more knowledge about the tool and come back with a solution (or at least an idea for it) soon. 😄

I am sorry for the delays here. Unfortunately working with mSafari keyboard API is mostly guessing, since there is no clear explanation (or source code) available on how the mechanism of opening the keyboard exactly works. I think I got much understanding of it anyway, and I believe that fixing onyx is our last piece to make this merged.

@parasharrajat
Copy link
Member

and the fact that withOnyx while fetching data returns null. Apparently, it is enough for Safari to not allow the keyboard to open.

Interesting. Maybe this means that Safari is preventing focus for dynamically loaded dom.

@parasharrajat
Copy link
Member

I think delay is fine with this issue. This is not a high priority.

@tgolen
Copy link
Contributor

tgolen commented Oct 5, 2023

@staszekscp Let's keep up the work on this if you have some promising leads. You mentioned initWithStoredValues and there might be another recent change to withOnyx() that might be helpful. Check out this PR. It's possible that by using initialValue or delayUpdates that it might help out the situation by letting the components render without waiting for data to be loaded from Onyx.

We have several experts when it comes to Onyx, so don't let your lack of knowledge on that lib hinder you! Start up a convo in Slack and we can help you out with any questions.

@staszekscp
Copy link
Contributor Author

Hello! Recently I had a look again at this bug, and merged the newest main. Unfortunately it appears that my solution suddenly stopped working, and I have not been able to make it work again - regardless of changes in react-native-onyx.

This PR has been here for a while, and even though it seemed that my solution worked (as you can see in videos), there was an edge case that I was able to solve only with a workaround. That's why I didn't want to push it to be merged, since I was not satisfied with the solution, anyway.

Right now I can see that my solution was fragile, and the bug could be easily introduced again. Currently I am focused on another task (wave-10), and I do not have much time to investigate the bug again. Therefore I don't want to block this task anymore and let someone alse to have a go and try to solve it... I am very sorry for it, and I hope someone else will be able to solve it. If that person has any questions I am available to share my knowledge about the subject. 😞

Once again, I'm sorry 😢

@parasharrajat
Copy link
Member

No worries, Thanks for the attempt. You can unassign the issue with the same reason and we can send it back to the pool.

@staszekscp
Copy link
Contributor Author

Yes @parasharrajat, I'll do it. Also I'm going to close this PR. Sorry again 😞

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

Successfully merging this pull request may close these issues.

4 participants