From 6bac272a5f9cd2eda122b8d6be2cf80ffacee4b6 Mon Sep 17 00:00:00 2001 From: Rodolphe Lemasquerier Date: Mon, 9 Aug 2021 19:14:26 +0200 Subject: [PATCH] feat: add prop to allow nested scroll on android (#2056) * feat: add prop to allow nested scroll on android When set to true, this prop allow to scroll inside the webview when the webview is inside a scrollview on android * docs: nestedScrollEnabled prop usage --- .../webview/RNCWebViewManager.java | 19 +++++++++++++++++++ docs/Reference.md | 13 +++++++++++++ src/WebView.android.tsx | 1 + src/WebViewTypes.ts | 10 ++++++++++ 4 files changed, 43 insertions(+) diff --git a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java index c9d43d5..c245ca3 100644 --- a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java +++ b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java @@ -19,6 +19,7 @@ import android.os.SystemClock; import android.text.TextUtils; import android.util.Log; import android.view.Gravity; +import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; @@ -343,6 +344,11 @@ public class RNCWebViewManager extends SimpleViewManager { view.setOverScrollMode(overScrollMode); } + @ReactProp(name = "nestedScrollEnabled") + public void setNestedScrollEnabled(WebView view, boolean enabled) { + ((RNCWebView) view).setNestedScrollEnabled(enabled); + } + @ReactProp(name = "thirdPartyCookiesEnabled") public void setThirdPartyCookiesEnabled(WebView view, boolean enabled) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { @@ -1405,6 +1411,7 @@ public class RNCWebViewManager extends SimpleViewManager { protected boolean sendContentSizeChangeEvents = false; private OnScrollDispatchHelper mOnScrollDispatchHelper; protected boolean hasScrollEvent = false; + protected boolean nestedScrollEnabled = false; protected ProgressChangedFilter progressChangedFilter; /** @@ -1431,6 +1438,10 @@ public class RNCWebViewManager extends SimpleViewManager { this.hasScrollEvent = hasScrollEvent; } + public void setNestedScrollEnabled(boolean nestedScrollEnabled) { + this.nestedScrollEnabled = nestedScrollEnabled; + } + @Override public void onHostResume() { // do nothing @@ -1446,6 +1457,14 @@ public class RNCWebViewManager extends SimpleViewManager { cleanupCallbacksAndDestroy(); } + @Override + public boolean onTouchEvent(MotionEvent event) { + if (this.nestedScrollEnabled) { + requestDisallowInterceptTouchEvent(true); + } + return super.onTouchEvent(event); + } + @Override protected void onSizeChanged(int w, int h, int ow, int oh) { super.onSizeChanged(w, h, ow, oh); diff --git a/docs/Reference.md b/docs/Reference.md index f4df5ac..35519f2 100644 --- a/docs/Reference.md +++ b/docs/Reference.md @@ -51,6 +51,7 @@ This document lays out the current public properties and methods for the React N - [`contentMode`](Reference.md#contentMode) - [`dataDetectorTypes`](Reference.md#datadetectortypes) - [`scrollEnabled`](Reference.md#scrollenabled) +- [`nestedScrollEnabled`](Reference.md#nestedscrollenabled) - [`directionalLockEnabled`](Reference.md#directionalLockEnabled) - [`geolocationEnabled`](Reference.md#geolocationenabled) - [`allowFileAccessFromFileURLs`](Reference.md#allowFileAccessFromFileURLs) @@ -1040,6 +1041,18 @@ Boolean value that determines whether scrolling is enabled in the `WebView`. The --- +### `nestedScrollEnabled`[⬆](#props-index) + +Boolean value that determines whether scrolling is possible in the `WebView` when used inside a `ScrollView` on Android. The default value is `false`. + +Setting this to `true` will prevent the `ScrollView` to scroll when scrolling from inside the `WebView`. + +| Type | Required | Platform | +| ---- | -------- | ------------- | +| bool | No | Android | + +--- + ### `directionalLockEnabled`[⬆](#props-index) A Boolean value that determines whether scrolling is disabled in a particular direction. diff --git a/src/WebView.android.tsx b/src/WebView.android.tsx index 8419ff3..9625c9d 100644 --- a/src/WebView.android.tsx +++ b/src/WebView.android.tsx @@ -64,6 +64,7 @@ class WebView extends React.Component { androidLayerType: 'none', originWhitelist: defaultOriginWhitelist, setSupportMultipleWindows: true, + nestedScrollEnabled: false, }; static isFileUploadSupported = async () => { diff --git a/src/WebViewTypes.ts b/src/WebViewTypes.ts index 4cfc613..856ea71 100644 --- a/src/WebViewTypes.ts +++ b/src/WebViewTypes.ts @@ -301,6 +301,7 @@ export interface AndroidNativeWebViewProps extends CommonNativeWebViewProps { textZoom?: number; thirdPartyCookiesEnabled?: boolean; messagingModuleName?: string; + nestedScrollEnabled?: boolean; readonly urlPrefixesForDefaultIntent?: string[]; } @@ -951,6 +952,15 @@ export interface AndroidWebViewProps extends WebViewSharedProps { * Sets ability to open fullscreen videos on Android devices. */ allowsFullscreenVideo?: boolean; + + /** + * Allows to scroll inside the webview when used inside a scrollview. + * Behaviour already existing on iOS. + * Default to false + * + * @platform android + */ + nestedScrollEnabled?: boolean; } export interface WebViewSharedProps extends ViewProps {