Browse Source

Add prop for injecting custom styles into the webview head

Rich 7 years ago
parent
commit
01216cf154
2 changed files with 46 additions and 3 deletions
  1. 21
    1
      autoHeightWebView/index.android.js
  2. 25
    2
      autoHeightWebView/index.ios.js

+ 21
- 1
autoHeightWebView/index.android.js View File

31
     source: WebView.propTypes.source,
31
     source: WebView.propTypes.source,
32
     onHeightUpdated: PropTypes.func,
32
     onHeightUpdated: PropTypes.func,
33
     customScript: PropTypes.string,
33
     customScript: PropTypes.string,
34
+    customStyle: PropTypes.string,
34
     enableAnimation: PropTypes.bool,
35
     enableAnimation: PropTypes.bool,
35
     // if set to false may cause some layout issues (width of container will be than width of screen)
36
     // if set to false may cause some layout issues (width of container will be than width of screen)
36
     scalesPageToFit: PropTypes.bool,
37
     scalesPageToFit: PropTypes.bool,
70
         this
71
         this
71
       );
72
       );
72
     }
73
     }
73
-    const initialScript = props.files
74
+    let initialScript = props.files
74
       ? this.appendFilesToHead(props.files, BaseScript)
75
       ? this.appendFilesToHead(props.files, BaseScript)
75
       : BaseScript;
76
       : BaseScript;
77
+    initialScript = props.customStyle
78
+      ? this.appendStylesToHead(props.customStyle, initialScript)
79
+      : initialScript;
76
     this.state = {
80
     this.state = {
77
       isChangingSource: false,
81
       isChangingSource: false,
78
       height: 0,
82
       height: 0,
120
     if (nextProps.files) {
124
     if (nextProps.files) {
121
       currentScript = this.appendFilesToHead(nextProps.files, BaseScript);
125
       currentScript = this.appendFilesToHead(nextProps.files, BaseScript);
122
     }
126
     }
127
+    currentScript = nextProps.customStyle
128
+      ? this.appendStylesToHead(nextProps.customStyle, currentScript)
129
+      : currentScript;
123
     this.setState({ script: currentScript });
130
     this.setState({ script: currentScript });
124
   }
131
   }
125
 
132
 
229
     return script;
236
     return script;
230
   }
237
   }
231
 
238
 
239
+  appendStylesToHead(styles, script) {
240
+    if (!styles) {
241
+      return script;
242
+    }
243
+    return `
244
+      var styleElement = document.createElement('style');
245
+      var styleText = document.createTextNode('${styles}');
246
+      styleElement.appendChild(styleText);
247
+      document.head.appendChild(styleElement);
248
+      ${script}
249
+    `
250
+  }
251
+
232
   render() {
252
   render() {
233
     const { height, script, isChangingSource, heightOffset } = this.state;
253
     const { height, script, isChangingSource, heightOffset } = this.state;
234
     const {
254
     const {

+ 25
- 2
autoHeightWebView/index.ios.js View File

19
         source: WebView.propTypes.source,
19
         source: WebView.propTypes.source,
20
         onHeightUpdated: PropTypes.func,
20
         onHeightUpdated: PropTypes.func,
21
         customScript: PropTypes.string,
21
         customScript: PropTypes.string,
22
+        customStyle: PropTypes.string,
22
         enableAnimation: PropTypes.bool,
23
         enableAnimation: PropTypes.bool,
23
         // if set to true may cause some layout issues (smaller font size)
24
         // if set to true may cause some layout issues (smaller font size)
24
         scalesPageToFit: PropTypes.bool,
25
         scalesPageToFit: PropTypes.bool,
48
         if (this.props.enableAnimation) {
49
         if (this.props.enableAnimation) {
49
             this.opacityAnimatedValue = new Animated.Value(0);
50
             this.opacityAnimatedValue = new Animated.Value(0);
50
         }
51
         }
51
-        const initialScript = props.files ? this.appendFilesToHead(props.files, props.hasIframe ? IframeBaseScript : BaseScript) : props.hasIframe ? IframeBaseScript : BaseScript;
52
+        let initialScript = props.hasIframe ? IframeBaseScript : BaseScript;
53
+        initialScript = props.files
54
+          ? this.appendFilesToHead(props.files, BaseScript)
55
+          : BaseScript;
56
+        initialScript = props.customStyle
57
+          ? this.appendStylesToHead(props.customStyle, initialScript)
58
+          : initialScript;
52
         this.state = {
59
         this.state = {
53
             height: 0,
60
             height: 0,
54
             script: initialScript
61
             script: initialScript
58
     componentWillReceiveProps(nextProps) {
65
     componentWillReceiveProps(nextProps) {
59
         let currentScript = nextProps.hasIframe ? IframeBaseScript : BaseScript;
66
         let currentScript = nextProps.hasIframe ? IframeBaseScript : BaseScript;
60
         if (nextProps.files) {
67
         if (nextProps.files) {
61
-            currentScript = this.appendFilesToHead(nextProps.files, nextProps.hasIframe ? IframeBaseScript : BaseScript);
68
+            currentScript = this.appendFilesToHead(nextProps.files, currentScript);
62
         }
69
         }
70
+        currentScript = nextProps.customStyle
71
+          ? this.appendStylesToHead(nextProps.customStyle, currentScript)
72
+          : currentScript;
63
         this.setState({ script: currentScript });
73
         this.setState({ script: currentScript });
64
     }
74
     }
65
 
75
 
80
         return script;
90
         return script;
81
     }
91
     }
82
 
92
 
93
+    appendStylesToHead(styles, script) {
94
+      if (!styles) {
95
+        return script;
96
+      }
97
+      return `
98
+        var styleElement = document.createElement('style');
99
+        var styleText = document.createTextNode('${styles}');
100
+        styleElement.appendChild(styleText);
101
+        document.head.appendChild(styleElement);
102
+        ${script}
103
+      `
104
+    }
105
+
83
     onHeightUpdated(height) {
106
     onHeightUpdated(height) {
84
         if (this.props.onHeightUpdated) {
107
         if (this.props.onHeightUpdated) {
85
             this.props.onHeightUpdated(height);
108
             this.props.onHeightUpdated(height);