Browse Source

Add prop for injecting custom styles into the webview head

Rich 6 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,6 +31,7 @@ export default class AutoHeightWebView extends PureComponent {
31 31
     source: WebView.propTypes.source,
32 32
     onHeightUpdated: PropTypes.func,
33 33
     customScript: PropTypes.string,
34
+    customStyle: PropTypes.string,
34 35
     enableAnimation: PropTypes.bool,
35 36
     // if set to false may cause some layout issues (width of container will be than width of screen)
36 37
     scalesPageToFit: PropTypes.bool,
@@ -70,9 +71,12 @@ export default class AutoHeightWebView extends PureComponent {
70 71
         this
71 72
       );
72 73
     }
73
-    const initialScript = props.files
74
+    let initialScript = props.files
74 75
       ? this.appendFilesToHead(props.files, BaseScript)
75 76
       : BaseScript;
77
+    initialScript = props.customStyle
78
+      ? this.appendStylesToHead(props.customStyle, initialScript)
79
+      : initialScript;
76 80
     this.state = {
77 81
       isChangingSource: false,
78 82
       height: 0,
@@ -120,6 +124,9 @@ export default class AutoHeightWebView extends PureComponent {
120 124
     if (nextProps.files) {
121 125
       currentScript = this.appendFilesToHead(nextProps.files, BaseScript);
122 126
     }
127
+    currentScript = nextProps.customStyle
128
+      ? this.appendStylesToHead(nextProps.customStyle, currentScript)
129
+      : currentScript;
123 130
     this.setState({ script: currentScript });
124 131
   }
125 132
 
@@ -229,6 +236,19 @@ export default class AutoHeightWebView extends PureComponent {
229 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 252
   render() {
233 253
     const { height, script, isChangingSource, heightOffset } = this.state;
234 254
     const {

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

@@ -19,6 +19,7 @@ export default class AutoHeightWebView extends PureComponent {
19 19
         source: WebView.propTypes.source,
20 20
         onHeightUpdated: PropTypes.func,
21 21
         customScript: PropTypes.string,
22
+        customStyle: PropTypes.string,
22 23
         enableAnimation: PropTypes.bool,
23 24
         // if set to true may cause some layout issues (smaller font size)
24 25
         scalesPageToFit: PropTypes.bool,
@@ -48,7 +49,13 @@ export default class AutoHeightWebView extends PureComponent {
48 49
         if (this.props.enableAnimation) {
49 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 59
         this.state = {
53 60
             height: 0,
54 61
             script: initialScript
@@ -58,8 +65,11 @@ export default class AutoHeightWebView extends PureComponent {
58 65
     componentWillReceiveProps(nextProps) {
59 66
         let currentScript = nextProps.hasIframe ? IframeBaseScript : BaseScript;
60 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 73
         this.setState({ script: currentScript });
64 74
     }
65 75
 
@@ -80,6 +90,19 @@ export default class AutoHeightWebView extends PureComponent {
80 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 106
     onHeightUpdated(height) {
84 107
         if (this.props.onHeightUpdated) {
85 108
             this.props.onHeightUpdated(height);