-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Scroll position of ScrollView jumps to WebView when pressed #3014
Comments
Same issue here |
I have the same issue. Bug description: To Reproduce: To test on real device
Source (static HTML or url):
Expected behavior: Screenshots/Videos: 2023-06-13.14.12.57.mp4Environment:
|
It looks like the auto-scrolling is a behavior of the React Native ScrollView. The only thing I imagine could be done from the |
The |
I'm not sure the |
Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically |
Sadly we are still searching for solutions |
same issue here |
Same issue here in IOS in version 13.6.2 |
Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically |
We are still looking for solutions |
Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically |
We are sadly still experiencing this :/ |
Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically |
This sadly still happens :/ |
This sadly still happen with me too :/ |
Still happening |
This solution is for people who need 2 lines of code right now to provide a temporary but relatively stable fix for this issue without installing forks or diving in to native code. This solution only works when there are no other focusable elements in your scrollview. This solution is particularly useful when users need to be fully able to focus and interact with various elements inside of the webview. The solution relies on a // Prevent initial touch jumping ([REDACTED]-78)
//#region Here be dragons. Thou art forewarned.
useEffect(() => {
webViewRef.current?.requestFocus();
scrollViewRef.current?.scrollTo({ x: 0, y: 0, animated: false });
}, [loaded]);
//#endregion Anyway, long story short; Focus the webview before the user does it, and reset the scroll position immediately after. The user shouldn't see this, but since the webview is now focused, the user can interact with it freely without messing up your scrollview. That is, until another element inside your scrollview needs to be focused, in which case this solution is not suitable. |
I found a temporary, quick, and forceful way to solve the issue. Add the following content to @Override
public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
return false;
} However, this will cause input or textarea elements inside the WebView to lose the ability to focus and bring up the keyboard. So, I forked a project:
You can modify it based on your needs to control whether the WebView can be focused or not. Here’s an example: const webViewScript = `
(function() {
const inputs = document.getElementsByTagName('input');
const textareas = document.getElementsByTagName('textarea');
const focusableElements = [...inputs, ...textareas];
window.ReactNativeWebView.postMessage(focusableElements.length > 0 ? 'allow-focus' : 'disallow-focus');
return true;
})();
`;
const [focusEnabled, setFocusEnabled] = useState<boolean>(false);
const onMessage = (event: WebViewMessageEvent) => {
const message = event.nativeEvent.data;
if (message === 'allow-focus') {
setFocusEnabled(true);
} else if (message === 'disallow-focus') {
setFocusEnabled(false);
}
}
<WebView
focusEnabled={focusEnabled}
injectedJavaScript={webViewScript}
/> If you only need a simple solution where nothing in the WebView can be focused, you can directly set focusEnabled to false:
Although it’s not the best solution, I hope it helps those who urgently need to resolve this issue. |
Here are some experiences from native Android developers. Unfortunately, the ScrollView component in React Native does not expose this property, so there isn't much we can do.😥
|
#3575 fixes the issue for us. This fork already includes the fix: https://github.com/birdofpreyru/react-native-webview |
Bug description:
When pressing a WebView inside a scrollable ScrollView, the scroll position jumps down to the WebView.
To Reproduce:
The first time you press, it will jump down slightly.
Expected behavior:
When clicking a WebView inside a ScrollView, it should not change the scroll position of the parent ScrollView.
Environment:
The text was updated successfully, but these errors were encountered: