@native-html/iframe-plugin

🌐 A WebView-based plugin to render iframes in react-native-render-html

Usage no npm install needed!

<script type="module">
  import nativeHtmlIframePlugin from 'https://cdn.skypack.dev/@native-html/iframe-plugin';
</script>

README

:warning: This documentation is for react-native-render-html v6. For v5, go here.

@native-html/iframe-plugin

npm semver codecov CI

🌐 A WebView-based plugin to render iframes in react-native-render-html.


Install:

:warning: With expo, use expo install instead to grab a compatible react-native-webview version.

npm add --save @native-html/iframe-plugin react-native-webview
yarn add @native-html/iframe-plugin react-native-webview

Features:

  • Supports onLinkPress;
  • Supports defaultWebViewProps;
  • Compliance with RFC001: scales to available width;
  • Autoscale feature (adapt zoom level to available width! Disabled by default.);
  • A single renderer component exported as default, super easy to plug-in!
  • Compatible with react-native-web via @formidable-webview/web

Known Limitations:

  • With react-native-web, onLinkPress will not work for external domains.

Compat Table

react-native-render-html @native-html/iframe-plugin
≥ 5.0.0 < 6.0.0 1.x (documentation)
≥ 6.0.0 2.x (documentation

Minimal working example

import IframeRenderer from '@native-html/iframe-plugin';
import RenderHTML from 'react-native-render-html';
import WebView from 'react-native-webview';

const renderers = {
  iframe: IframeRenderer
}

// ...

<RenderHTML renderers={renderers}
      WebView={WebView}
      source={{ html: '<iframe ...></iframe>' }}
      defaultWebViewProps={{ /* Any prop you want to pass to all WebViews */ }}
      renderersProps={{ iframe: { scalesPageToFit: true, webViewProps: { /* Any prop you want to pass to iframe WebViews */ } }}}
/>

Autoscale feature

When scalesPageToFit is set to true, if the iframe width (as determined by the width element attribute) is greater than the available width (as determined by HTML props contentWidth and computeEmbeddedMaxWidth), the WebView will be zoomed out by just the right amount to have no horizontal cropping. This is equivalent to resizeMode: 'contain' for images. See example below with scalesPageToFit enabled (left) and disabled (right):

Customizing the renderer

You can customize the renderer logic thanks to useHtmlIframeProps hook, iframeModel and HTMLIframe exports:

import {useHtmlIframeProps, HTMLIframe, iframeModel} from '@native-html/iframe-plugin';

const IframeRenderer = function IframeRenderer(props) {
  const iframeProps = useHtmlIframeProps(props);
  // Do customize the props here; wrap with your own container...
  return iframeProps ? <HTMLIframe {..iframeProps} /> : null;
};

IframeRenderer.model = iframeModel;

const renderers = {
  iframe: IframeRenderer
}

// use "renderers" prop in your RenderHTML instance