Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 46 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,40 @@ import React, { Component } from 'react';
import {
View,
Dimensions,
WebView,
Platform,
} from 'react-native';
import { WebView } from 'react-native-webview';

const injectedScript = function() {
function waitForBridge() {
if (window.postMessage.length !== 1){
setTimeout(waitForBridge, 200);
}
else {
postMessage(
Math.max(document.documentElement.clientHeight, document.documentElement.scrollHeight, document.body.clientHeight, document.body.scrollHeight)
)
}

const injectedScript = `function() {
function postSize() {
//https://stackoverflow.com/questions/1145850/how-to-get-height-of-entire-document-with-javascript
var body = document.body, html = document.documentElement;

var maxHeight = Math.max( body.scrollHeight, body.offsetHeight, html.offsetHeight );

console.log('postSize maxHeight', maxHeight)
window.ReactNativeWebView.postMessage(maxHeight);
}

var postSizeTimeout;
function debouncedPostSize() {
clearTimeout(postSizeTimeout);
postSizeTimeout = setTimeout(function () {
postSize()
}, 500)
}
waitForBridge();
};

debouncedPostSize();
//trigger when DOM changes
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var observer = new MutationObserver(debouncedPostSize);
observer.observe(document, {
subtree: true,
attributes: true
});
window.addEventListener('resize', debouncedPostSize);
}`

export default class MyWebView extends Component {
state = {
Expand All @@ -40,6 +57,7 @@ export default class MyWebView extends Component {

static defaultProps = {
autoHeight: true,
noWidth: false,
}

constructor (props: Object) {
Expand All @@ -52,9 +70,12 @@ export default class MyWebView extends Component {
}

_onMessage(e) {
this.setState({
webViewHeight: parseInt(e.nativeEvent.data)
});
const maxHeight = this.props.maxHeight;
let webViewHeight = parseInt(e.nativeEvent.data);
if (maxHeight && webViewHeight > maxHeight) {
webViewHeight = maxHeight
}
this.setState({ webViewHeight });
}

stopLoading() {
Expand All @@ -66,21 +87,25 @@ export default class MyWebView extends Component {
}

render () {

const _w = this.props.width || Dimensions.get('window').width;
const _h = this.props.autoHeight ? this.state.webViewHeight : this.props.defaultHeight;
const androidScript = 'window.postMessage = String(Object.hasOwnProperty).replace(\'hasOwnProperty\', \'postMessage\');' +
'(' + String(injectedScript) + ')();';
const iosScript = '(' + String(injectedScript) + ')();' + 'window.postMessage = String(Object.hasOwnProperty).replace(\'hasOwnProperty\', \'postMessage\');';

const injectedJavaScript = '(' + String(injectedScript) + ')();';

let style = !!this.props.noWidth ? []: [{width: _w}]
style = style.concat([this.props.style, {height: _h}]);

return (
<WebView
ref={(ref) => { this.webview = ref; }}
injectedJavaScript={Platform.OS === 'ios' ? iosScript : androidScript}
injectedJavaScript={injectedJavaScript}
scrollEnabled={this.props.scrollEnabled || false}
onMessage={this._onMessage}
javaScriptEnabled={true}
automaticallyAdjustContentInsets={true}
{...this.props}
style={[{width: _w}, this.props.style, {height: _h}]}
style={style}
/>
)
}
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
"auto",
"height"
],
"peerDependencies": {
"react": ">= 16.8.0",
"react-native": ">= 0.60.0",
"react-native-webview": ">= 10.9.0"
},
"author": "Elton Jain",
"license": "ISC",
"bugs": {
Expand Down