@@ -51,20 +51,26 @@ function postTheme(view: WebView<object> | null, theme: HtmlRenderTheme) {
5151
5252const 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 : { } ) }
0 commit comments