Skip to content

Commit 61b9790

Browse files
SunkenInTimeclaude
andauthored
fix(mobile): HTML pages in a thread no longer trap scrolling on Android (#17211)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 4daec10 commit 61b9790

4 files changed

Lines changed: 138 additions & 70 deletions

File tree

‎apps/mobile/src/features/threads/HtmlRenderWebView.tsx‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,26 @@ function postTheme(view: WebView<object> | null, theme: HtmlRenderTheme) {
5151

5252
const OVERFLOW_MESSAGE_TYPE = "t3-html-render-overflow";
5353

54-
// Reports whether the page overflows its frame, so a feed row only takes scroll
55-
// gestures from a page that can use them.
56-
const OVERFLOW_SCRIPT = `(function(){var last;function report(){var d=document.documentElement,b=document.body;var o=Math.max(d.scrollHeight,b?b.scrollHeight:0)>window.innerHeight+1||Math.max(d.scrollWidth,b?b.scrollWidth:0)>window.innerWidth+1;if(o===last)return;last=o;window.ReactNativeWebView.postMessage(JSON.stringify({type:${JSON.stringify(OVERFLOW_MESSAGE_TYPE)},overflow:o}));}report();if(window.ResizeObserver){var r=new ResizeObserver(report);r.observe(document.documentElement);if(document.body)r.observe(document.body);}window.addEventListener("resize",report);})();true;`;
54+
// Reports which ways the page overflows its frame, so a feed row only takes
55+
// scroll gestures from a page that can use them.
56+
const OVERFLOW_SCRIPT = `(function(){var last;function report(){var d=document.documentElement,b=document.body;var x=Math.max(d.scrollWidth,b?b.scrollWidth:0)>window.innerWidth+1,y=Math.max(d.scrollHeight,b?b.scrollHeight:0)>window.innerHeight+1,o=x+","+y;if(o===last)return;last=o;window.ReactNativeWebView.postMessage(JSON.stringify({type:${JSON.stringify(OVERFLOW_MESSAGE_TYPE)},x:x,y:y}));}report();if(window.ResizeObserver){var r=new ResizeObserver(report);r.observe(document.documentElement);if(document.body)r.observe(document.body);}window.addEventListener("resize",report);})();true;`;
5757

58-
function readOverflowMessage(data: string) {
58+
type Overflow = { readonly x: boolean; readonly y: boolean };
59+
60+
const NO_OVERFLOW: Overflow = { x: false, y: false };
61+
62+
function readOverflowMessage(data: string): Overflow | null {
5963
try {
6064
const message: unknown = JSON.parse(data);
6165
return typeof message === "object" &&
6266
message !== null &&
6367
"type" in message &&
6468
message.type === OVERFLOW_MESSAGE_TYPE &&
65-
"overflow" in message &&
66-
typeof message.overflow === "boolean"
67-
? message.overflow
69+
"x" in message &&
70+
typeof message.x === "boolean" &&
71+
"y" in message &&
72+
typeof message.y === "boolean"
73+
? { x: message.x, y: message.y }
6874
: null;
6975
} catch {
7076
return null;
@@ -88,7 +94,7 @@ export function HtmlRenderWebView(props: {
8894
const [initialTheme] = useState(theme);
8995
const [generation, setGeneration] = useState(0);
9096
const [loaded, setLoaded] = useState(false);
91-
const [overflows, setOverflows] = useState(false);
97+
const [overflow, setOverflow] = useState(NO_OVERFLOW);
9298
const webView = useRef<WebView<object>>(null);
9399
const crashes = useRef(0);
94100
// The theme the loaded document shows; null until it loads.
@@ -111,10 +117,10 @@ export function HtmlRenderWebView(props: {
111117
}
112118
shownTheme.current = null;
113119
setLoaded(false);
114-
setOverflows(false);
120+
setOverflow(NO_OVERFLOW);
115121
setGeneration((value) => value + 1);
116122
};
117-
const scrollable = !props.nested || overflows;
123+
const scrollable = !props.nested || overflow.x || overflow.y;
118124
// Pages have no horizontal padding of their own, so full screen adds the
119125
// feed's gutter in the page's background color.
120126
return (
@@ -141,7 +147,10 @@ export function HtmlRenderWebView(props: {
141147
showsVerticalScrollIndicator={!props.nested}
142148
showsHorizontalScrollIndicator={!props.nested}
143149
scrollEnabled={scrollable}
144-
nestedScrollEnabled={props.nested && overflows}
150+
// Android only: the page holds vertical drags until it reaches an edge,
151+
// then hands them to the feed (patches/react-native-webview). A page
152+
// that only overflows sideways leaves vertical drags to the feed.
153+
nestedScrollEnabled={props.nested && overflow.y}
145154
overScrollMode={props.nested ? "never" : "always"}
146155
// Only the page itself loads here; other top-frame navigations are
147156
// dropped. A link the reader taps opens as a new window, which the
@@ -167,8 +176,8 @@ export function HtmlRenderWebView(props: {
167176
? {
168177
injectedJavaScript: OVERFLOW_SCRIPT,
169178
onMessage: (event: WebViewMessageEvent) => {
170-
const overflow = readOverflowMessage(event.nativeEvent.data);
171-
if (overflow !== null) setOverflows(overflow);
179+
const next = readOverflowMessage(event.nativeEvent.data);
180+
if (next !== null) setOverflow(next);
172181
},
173182
}
174183
: {})}

‎patches/[email protected]‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
diff --git a/android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java b/android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java
2+
index 80c6803..374f824 100644
3+
--- a/android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java
4+
+++ b/android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java
5+
@@ -122,14 +122,50 @@ public class RNCWebView extends WebView implements LifecycleEventListener {
6+
cleanupCallbacksAndDestroy();
7+
}
8+
9+
+ private float touchDownX;
10+
+ private float touchDownY;
11+
+ private float lastTouchY;
12+
+ private boolean multiTouch;
13+
+
14+
@Override
15+
public boolean onTouchEvent(MotionEvent event) {
16+
if (this.nestedScrollEnabled) {
17+
- requestDisallowInterceptTouchEvent(true);
18+
+ requestDisallowInterceptTouchEvent(!shouldYieldToParent(event));
19+
}
20+
return super.onTouchEvent(event);
21+
}
22+
23+
+ // A page that scrolls vertically hands a mostly vertical drag to the scrolling
24+
+ // parent once it cannot scroll further that way, as nested scroll views do on iOS
25+
+ // and the web. A page that never scrolls vertically keeps every gesture, since it
26+
+ // may scroll inside an iframe this view cannot measure. A gesture that ever has
27+
+ // a second finger, such as a pinch, stays with the page until every finger lifts.
28+
+ private boolean shouldYieldToParent(MotionEvent event) {
29+
+ switch (event.getActionMasked()) {
30+
+ case MotionEvent.ACTION_DOWN:
31+
+ touchDownX = event.getX();
32+
+ touchDownY = event.getY();
33+
+ lastTouchY = touchDownY;
34+
+ multiTouch = false;
35+
+ return false;
36+
+ case MotionEvent.ACTION_POINTER_DOWN:
37+
+ multiTouch = true;
38+
+ return false;
39+
+ case MotionEvent.ACTION_MOVE:
40+
+ if (multiTouch) return false;
41+
+ float dy = event.getY() - lastTouchY;
42+
+ lastTouchY = event.getY();
43+
+ if (dy == 0) return false;
44+
+ if (Math.abs(event.getY() - touchDownY) < Math.abs(event.getX() - touchDownX)) {
45+
+ return false;
46+
+ }
47+
+ if (!canScrollVertically(1) && !canScrollVertically(-1)) return false;
48+
+ return !canScrollVertically(dy > 0 ? -1 : 1);
49+
+ default:
50+
+ return false;
51+
+ }
52+
+ }
53+
+
54+
@Override
55+
protected void onSizeChanged(int w, int h, int ow, int oh) {
56+
super.onSizeChanged(w, h, ow, oh);

0 commit comments

Comments
 (0)