Skip to content

Upgrade Prettier and add Travis #34

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

Merged
merged 1 commit into from
Aug 1, 2020
Merged
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
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
package.json
gift.json
dist
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: node_js
node_js:
- '12'
install:
- yarn --frozen-lockfile
script: yarn test
cache:
directories:
- node_modules
env:
matrix:
- CI=true TZ=Europe/Paris
notifications:
email: false
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# react-native-web-webview

> React Native for Web implementation of RN's WebView

## Getting started

`$ npm install react-native-web-webview --save`

Alias the package in your webpack config:
Expand Down Expand Up @@ -31,30 +33,35 @@ const rule = {
```

## Usage

```js
import { WebView } from 'react-native-webview';
```

See [RN's doc](https://github.com/react-native-community/react-native-webview).

Supported props are:

- `source`
- `onMessage`
- `scrollEnabled`
- `injectedJavaScript`
- `style`

Additional, web-specific props are:
- `newWindow`: (*boolean*|*{ name: string, features: string}*)
This will open the source in a new window, optionally giving it an [internal name and custom features](https://developer.mozilla.org/en-US/docs/Web/API/Window/open).
By default, the name is `webview` and there are no features set.
This is useful when your target has X-Frame-Options or a no-CORS policy.
It currently only supports a `source` prop with a `method` set to `POST`.
Please feel free to do a PR to support more request types!
- `title`: (*string*) This prop will set the `webview` title.

- `newWindow`: (_boolean_|_{ name: string, features: string}_)
This will open the source in a new window, optionally giving it an [internal name and custom features](https://developer.mozilla.org/en-US/docs/Web/API/Window/open).
By default, the name is `webview` and there are no features set.
This is useful when your target has X-Frame-Options or a no-CORS policy.
It currently only supports a `source` prop with a `method` set to `POST`.
Please feel free to do a PR to support more request types!
- `title`: (_string_) This prop will set the `webview` title.

## Examples

See the [storybook](https://react-native-web-community.github.io/react-native-web-webview/storybook).

## Contributing

PRs are welcome!
4 changes: 1 addition & 3 deletions docs/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { storiesOf } from '@storybook/react';

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

storiesOf('URI source', module)
.add('basic', uri.basic)
.add('onMessage', uri.onMessage);
storiesOf('URI source', module).add('basic', uri.basic).add('onMessage', uri.onMessage);

storiesOf('With method', module).add('basic', method.basic);
66 changes: 33 additions & 33 deletions docs/stories/onMessage.html
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
<html>
<body>
<h1>Hello world!</h1>
<button id="button">Send message</button>
<script type="text/javascript">
// React Native
function onBridgeReady(cb) {
if (window.postMessage.length !== 1) {
setTimeout(function() {
onBridgeReady(cb)
}, 100);
} else {
cb();
}
}
<body>
<h1>Hello world!</h1>
<button id="button">Send message</button>
<script type="text/javascript">
// React Native
function onBridgeReady(cb) {
if (window.postMessage.length !== 1) {
setTimeout(function () {
onBridgeReady(cb);
}, 100);
} else {
cb();
}
}

var payload = JSON.stringify({
name: 'WebView',
});
var payload = JSON.stringify({
name: 'WebView',
});

document.getElementById('button').addEventListener('click', function() {
if (window.opener) {
// Web new window
window.opener.postMessage(payload, window.opener.origin);
window.close();
} else if (window.parent) {
// Web iframe
window.parent.postMessage(payload, window.parent.origin);
} else {
// React Native
onBridgeReady(function() {
window.postMessage(payload);
document.getElementById('button').addEventListener('click', function () {
if (window.opener) {
// Web new window
window.opener.postMessage(payload, window.opener.origin);
window.close();
} else if (window.parent) {
// Web iframe
window.parent.postMessage(payload, window.parent.origin);
} else {
// React Native
onBridgeReady(function () {
window.postMessage(payload);
});
}
});
}
});
</script>
</body>
</script>
</body>
</html>
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"scripts": {
"prepublish": "yarn build",
"build": "mkdir -p dist && babel src --out-dir dist --copy-files",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "prettier --check ."
},
"babel": {
"presets": [
Expand All @@ -40,7 +40,7 @@
"babel-loader": "^7.1.2",
"babel-preset-react-native": "^4.0.0",
"file-loader": "^1.1.4",
"prettier": "^1.7.3",
"prettier": "^2.0.5",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"react-native-web": "^0.1.1",
Expand Down
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class WebView extends Component {
this.handleSource(props.source, props.newWindow);
}

setRef = ref => this.frameRef = ref;
setRef = (ref) => (this.frameRef = ref);

handleSource = (source, newWindow) => {
if (!source.method) return;
Expand All @@ -26,12 +26,12 @@ export class WebView extends Component {
}
};

handleSourceInIFrame = source => {
handleSourceInIFrame = (source) => {
const { uri, ...options } = source;
const baseUrl = uri.substr(0, uri.lastIndexOf('/') + 1);
fetch(uri, options)
.then(response => response.text())
.then(html => this.setState({ html: `<base href="${baseUrl}" />` + html }));
.then((response) => response.text())
.then((html) => this.setState({ html: `<base href="${baseUrl}" />` + html }));
};

handleSourceInNewWindow = (source, newWindow) => {
Expand Down Expand Up @@ -88,13 +88,13 @@ export class WebView extends Component {
}
}

onMessage = nativeEvent => this.props.onMessage({ nativeEvent });
onMessage = (nativeEvent) => this.props.onMessage({ nativeEvent });

postMessage = (message, origin) => {
this.frameRef.contentWindow.postMessage(message, origin);
};

handleInjectedJavaScript = html => {
handleInjectedJavaScript = (html) => {
if (this.props.injectedJavaScript) {
if (html) {
return html.replace('</body>', `<script>${this.props.injectedJavaScript}</script></body>`);
Expand Down
14 changes: 6 additions & 8 deletions src/postMock.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<html>
<body>
<div id="content" style="display: none"></div>
</body>
<script>
<body>
<div id="content" style="display: none;"></div>
</body>
<script>
const source = new URLSearchParams(window.location.search);

let html = `<form method="POST" action="${source.get('uri')}">`;
Expand All @@ -11,13 +11,11 @@

Object.entries(params).forEach(([name, value]) => (html += `<input name="${name}" value="${value}" />`));

html +=
"<input id='submit' type='submit' />" +
"</form>";
html += "<input id='submit' type='submit' />" + '</form>';

const div = document.createElement('div');
div.innerHTML = html;
document.getElementById('content').appendChild(div);
document.getElementById('submit').click();
</script>
</script>
</html>
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2109,10 +2109,10 @@ preserve@^0.2.0:
resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"
integrity sha1-gV7R9uvGWSb4ZbMQwHE7yzMVzks=

prettier@^1.7.3:
version "1.7.3"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.7.3.tgz#8e6974725273914b1c47439959dd3d3ba53664b6"
integrity sha1-jml0clJzkUscR0OZWd09O6U2ZLY=
prettier@^2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.0.5.tgz#d6d56282455243f2f92cc1716692c08aa31522d4"
integrity sha512-7PtVymN48hGcO4fGjybyBSIWDsLU4H4XlvOHfq91pz9kkGlonzwTfYkaIEwiRg/dAJF9YlbsduBAgtYLi+8cFg==

private@^0.1.6, private@^0.1.7:
version "0.1.7"
Expand Down