Skip to content

Commit 954c647

Browse files
committed
Add onMessage support
1 parent 78e3cdf commit 954c647

File tree

10 files changed

+87
-11
lines changed

10 files changed

+87
-11
lines changed

docs/.storybook/addons.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
import '@storybook/addon-options/register';
2+
import '@storybook/addon-actions/register';

docs/.storybook/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ setOptions({
66
url: 'https://react-native-web-community.github.io/react-native-web-webview',
77
goFullScreen: false,
88
showLeftPanel: true,
9-
showDownPanel: false,
10-
downPanelInRight: false
9+
showDownPanel: true,
10+
downPanelInRight: false,
1111
});
1212

1313
function loadStories() {

docs/.storybook/webpack.config.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,28 @@ module.exports = (storybookBaseConfig, configType) => {
99
exclude: /node_modules/,
1010
use: {
1111
loader: 'babel-loader',
12-
options: { cacheDirectory: true }
13-
}
12+
options: { cacheDirectory: true },
13+
},
14+
});
15+
16+
storybookBaseConfig.module.rules.push({
17+
test: /\.html$/,
18+
use: {
19+
loader: 'file-loader',
20+
options: { name: '[name].[ext]' },
21+
},
1422
});
1523

1624
storybookBaseConfig.plugins.push(
1725
new webpack.DefinePlugin({
1826
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'),
19-
'process.env.__REACT_NATIVE_DEBUG_ENABLED__': DEV
27+
'process.env.__REACT_NATIVE_DEBUG_ENABLED__': DEV,
2028
})
2129
);
2230

2331
storybookBaseConfig.resolve.alias = {
2432
'react-native': 'react-native-web',
25-
'WebView': path.join(__dirname, '../../src/')
33+
WebView: path.join(__dirname, '../../src/'),
2634
};
2735

2836
return storybookBaseConfig;

docs/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@
77
"dependencies": {
88
"@storybook/addon-options": "^3.2.10",
99
"@storybook/react": "^3.1.9"
10+
},
11+
"devDependencies": {
12+
"@storybook/addon-actions": "^3.2.11"
1013
}
1114
}

docs/stories/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ import { storiesOf } from '@storybook/react';
66

77
storiesOf('HTML source', module).add('basic', html.basic);
88

9-
storiesOf('URI source', module).add('basic', uri.basic);
9+
storiesOf('URI source', module)
10+
.add('basic', uri.basic)
11+
.add('onMessage', uri.onMessage);

docs/stories/onMessage.html

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<html>
2+
<body>
3+
<h1>Hello world!</h1>
4+
<button id="button">Send message</button>
5+
<script type="text/javascript">
6+
// React Native
7+
function onBridgeReady(cb) {
8+
if (window.postMessage.length !== 1) {
9+
setTimeout(function() {
10+
onBridgeReady(cb)
11+
}, 100);
12+
} else {
13+
cb();
14+
}
15+
}
16+
17+
var payload = JSON.stringify({
18+
name: 'WebView',
19+
});
20+
21+
document.getElementById('button').addEventListener('click', function() {
22+
if (window.parent) {
23+
// Web iframe
24+
window.parent.postMessage(payload, window.parent.origin);
25+
} else {
26+
// React Native
27+
onBridgeReady(function() {
28+
window.postMessage(payload);
29+
});
30+
}
31+
});
32+
</script>
33+
</body>
34+
</html>

docs/stories/uri.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import React from 'react';
22
import WebView from 'WebView';
3+
import { action } from '@storybook/addon-actions';
34

45
export const basic = () => <WebView source={{ uri: 'https://www.youtube.com/embed/dQw4w9WgXcQ' }} />;
6+
7+
export const onMessage = () => (
8+
<WebView source={{ uri: require('./onMessage.html') }} onMessage={action('onMessage')} />
9+
);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"babel-core": "^6.26.0",
3131
"babel-loader": "^7.1.2",
3232
"babel-preset-react-native": "^4.0.0",
33+
"file-loader": "^1.1.4",
3334
"prettier": "^1.7.3",
3435
"react": "^16.0.0",
3536
"react-dom": "^16.0.0",

src/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
import React, { Component } from 'react';
22

33
export default class extends Component {
4+
constructor(props) {
5+
super(props);
6+
7+
if (props.onMessage) {
8+
window.addEventListener('message', this.onMessage, true);
9+
}
10+
}
11+
12+
onMessage = nativeEvent => nativeEvent.isTrusted && this.props.onMessage({ nativeEvent });
13+
414
render() {
515
return (
616
<iframe
717
src={this.props.source.uri}
818
srcDoc={this.props.source.html}
919
style={{ width: '100%', height: '100%', border: 0 }}
10-
allowfullscreen
11-
allowpaymentrequest
20+
allowFullScreen
21+
allowpaymentrequest="true"
1222
frameBorder="0"
1323
seamless
1424
/>
1525
);
1626
}
1727
}
18-

yarn.lock

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ ajv@^4.9.1:
3131
co "^4.6.0"
3232
json-stable-stringify "^1.0.1"
3333

34-
ajv@^5.1.5:
34+
ajv@^5.0.0, ajv@^5.1.5:
3535
version "5.2.3"
3636
resolved "https://registry.yarnpkg.com/ajv/-/ajv-5.2.3.tgz#c06f598778c44c6b161abafe3466b81ad1814ed2"
3737
dependencies:
@@ -1186,6 +1186,13 @@ fbjs@^0.8.16, fbjs@^0.8.9:
11861186
setimmediate "^1.0.5"
11871187
ua-parser-js "^0.7.9"
11881188

1189+
file-loader@^1.1.4:
1190+
version "1.1.4"
1191+
resolved "https://registry.yarnpkg.com/file-loader/-/file-loader-1.1.4.tgz#5ca9384adfafe008077c3439a435b2781a889ef5"
1192+
dependencies:
1193+
loader-utils "^1.0.2"
1194+
schema-utils "^0.3.0"
1195+
11891196
filename-regex@^2.0.0:
11901197
version "2.0.1"
11911198
resolved "https://registry.yarnpkg.com/filename-regex/-/filename-regex-2.0.1.tgz#c1c4b9bee3e09725ddb106b75c1e301fe2f18b26"
@@ -2338,6 +2345,12 @@ safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@~5.1.0,
23382345
version "5.1.1"
23392346
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.1.tgz#893312af69b2123def71f57889001671eeb2c853"
23402347

2348+
schema-utils@^0.3.0:
2349+
version "0.3.0"
2350+
resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-0.3.0.tgz#f5877222ce3e931edae039f17eb3716e7137f8cf"
2351+
dependencies:
2352+
ajv "^5.0.0"
2353+
23412354
"semver@2 || 3 || 4 || 5", semver@^5.3.0:
23422355
version "5.4.1"
23432356
resolved "https://registry.yarnpkg.com/semver/-/semver-5.4.1.tgz#e059c09d8571f0540823733433505d3a2f00b18e"

0 commit comments

Comments
 (0)