Skip to content

Commit 8ec2a86

Browse files
committed
First commit
0 parents  commit 8ec2a86

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# react-native-web-container
2+
3+
## Installation
4+
> npm install --save react-native-web-container
5+
6+
## Usage

index.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict';
2+
3+
import React from 'react-native';
4+
let { WebView } = React;
5+
import safeHtml from 'safe-html';
6+
import _ from 'lodash';
7+
8+
const script = '<script>window.location.hash = 1;document.title = document.height;</script>';
9+
10+
const defaultSafeConfig = _.defaults(
11+
{
12+
allowedTags: safeHtml.DEFAULT_CONFIG.allowedTags.concat(['img', 'style']),
13+
allowedAttributes: _.defaults(
14+
{
15+
id: {allTags: true},
16+
style: {allTags: true},
17+
src: {allowedTags: ['img']}
18+
}, safeHtml.DEFAULT_CONFIG.allowedAttributes)
19+
},
20+
safeHtml.DEFAULT_CONFIG
21+
);
22+
23+
let scrubHtml = function (html, makeSafe) {
24+
if (!html || makeSafe === false) {
25+
return html;
26+
}
27+
28+
return safeHtml(
29+
html,
30+
_.isObject(makeSafe) ? makeSafe : defaultSafeConfig);
31+
};
32+
33+
class WebContainer extends React.Component {
34+
constructor() {
35+
super();
36+
this.state = {};
37+
}
38+
39+
onNavigationStateChange(navState) {
40+
this.setState({
41+
height: navState.title
42+
});
43+
}
44+
45+
render() {
46+
let {
47+
html,
48+
style,
49+
autoHeight,
50+
makeSafe,
51+
scrollEnabled,
52+
...props
53+
} = this.props;
54+
55+
let scrubbedHtml = scrubHtml(html, makeSafe);
56+
57+
return (
58+
<WebView
59+
{...props}
60+
style={[style, (autoHeight ? {height: Number(this.state.height)} : {})]}
61+
scrollEnabled={autoHeight ? false : scrollEnabled}
62+
html={autoHeight ? (scrubbedHtml + script) : scrubbedHtml}
63+
onNavigationStateChange={this.onNavigationStateChange.bind(this)} />
64+
);
65+
}
66+
}
67+
68+
WebContainer.propTypes = {
69+
autoHeight: React.PropTypes.boolean,
70+
makeSafe: React.PropTypes.oneOf(
71+
React.PropTypes.boolean,
72+
React.PropTypes.object
73+
)
74+
};
75+
76+
WebContainer.defaultProps = {
77+
makeSafe: false
78+
};
79+
80+
export default WebContainer;

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "react-native-web-container",
3+
"version": "1.0.0",
4+
"description": "A wrapper around the react native WebView to add autoHeight, scrub html, etc",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/danrigsby/react-native-web-container.git"
12+
},
13+
"keywords": [
14+
"react-native",
15+
"react",
16+
"WebView"
17+
],
18+
"author": "Dan Rigsby",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/danrigsby/react-native-web-container/issues"
22+
},
23+
"homepage": "https://github.com/danrigsby/react-native-web-container#readme",
24+
"devDependencies": {
25+
},
26+
"dependencies": {
27+
"lodash": "3.10.1",
28+
"react-native": "*",
29+
"safe-html": "*"
30+
}
31+
}

0 commit comments

Comments
 (0)