Skip to content
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

Dynamic height of webview wrapper for the HTML content #28

Open
ajitdas123 opened this issue May 21, 2020 · 4 comments
Open

Dynamic height of webview wrapper for the HTML content #28

ajitdas123 opened this issue May 21, 2020 · 4 comments

Comments

@ajitdas123
Copy link

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

As per the suggestion here
react-native-webview/react-native-webview#413 (comment) 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)}
               />
@tajinder-logiciel
Copy link

i am facing same issue any solution for that @ajitdas123

@ajitdas123
Copy link
Author

ajitdas123 commented Aug 11, 2020

@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

@jsamr
Copy link

jsamr commented Sep 26, 2020

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

@Cogneter
Copy link

Cogneter commented Jun 3, 2022

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 #55 and #37 for more info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants