react-native-web-webview icon indicating copy to clipboard operation
react-native-web-webview copied to clipboard

Dynamic height of webview wrapper for the HTML content

Open ajitdas123 opened this issue 5 years ago • 4 comments

How can we adjust the height of webview dynamically based on the HTML content?

As per the suggestion here https://github.com/react-native-community/react-native-webview/issues/413#issuecomment-473507895 I have added the code below which works fine on Android as well as on IOS but doesn't work on the web, on the web it showing scrolling bar, not adjusting to the height of the content.

Here is my code:

handleWebViewNavigationStateChange = (event) => {
      
       let htmlHeight = Number(event.nativeEvent.data);
       console.log('HTML Webview height',htmlHeight); // This is coming null 
       if (htmlHeight) {
           this.setState({webViewHeight: htmlHeight});
       }
   };

.........
<WebView
                   originWhitelist={['*']}
                   source={{html: html}}
                   style={{height: this.state.webViewHeight}}
                   injectedJavaScript={"const meta = document.createElement('meta'); " +
                   "meta.setAttribute('content', 'width=device-width, initial-scale=1.0');" +
                   " meta.setAttribute('name', 'viewport'); " +
                   "document.getElementsByTagName('head')[0].appendChild(meta);" +
                   "window.ReactNativeWebView.postMessage(document.body.scrollHeight)"}
                   //   scalesPageToFit={true}
                   javaScriptEnabled={true}
                   automaticallyAdjustContentInsets={true}
                   onMessage={event => this.handleWebViewNavigationStateChange(event)}
               />

ajitdas123 avatar May 21 '20 10:05 ajitdas123

i am facing same issue any solution for that @ajitdas123

tajinder-logiciel avatar Aug 11 '20 11:08 tajinder-logiciel

@tajinder-logiciel i didn't use the web view on web, i checked if the platform is web then i injected the HTML content inside the div. Like below

  <div dangerouslySetInnerHTML={{__html:html}}></div>        

You may check more info about it here

ajitdas123 avatar Aug 11 '20 19:08 ajitdas123

I don't know if that may help: https://formidable-webview.github.io/webshell/docs/autoheight

jsamr avatar Sep 26 '20 18:09 jsamr

I had the same problem - it doesn't work because window.ReactNativeWebView.postMessage doesn't work. window.ReactNativeWebView is defined in react-native-webview, but it's undefined in react-native-web-webview, ergo it doesn't work on web.

See https://github.com/react-native-web-community/react-native-web-webview/issues/55 and https://github.com/react-native-web-community/react-native-web-webview/issues/37 for more info.

Cogneter avatar Jun 03 '22 08:06 Cogneter