npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-display-html

v1.2.5

Published

Display HTML content in an automatically sized webview and eases javascript injection inside it.

Downloads

68

Readme

react-native-display-html

Display HTML content in an automatically sized webview and eases javascript injection inside it. Pull requests are very welcome!

The motivation behind this is to stop parsing HTML to render it as native components. It might work on simple HTML when you have the freedom of tweaking it, but on much more complex HTML... it's just not possible yet. Don't even get me started on platform's specific rendering issues (don't you ever render an image inside a text component on Android !).

We gave up, so here's react-native-display-html.

Table of contents

  1. Usage
  2. Props
  3. Tips and tricks
  4. TODO
  5. Credits

Installation

$ npm install --save react-native-display-html

You also need to link react-native-webview-bridge as native dependency. Check the README here (you don't have to add the package manually, it will be installed by react-native-display-html in your node_modules folder).

We will continue relying on it until the Webview component backed into react-native's core is stable enough for our needs.

Usage

...

render () {
    return (
        <DisplayHTML
            htmlString={'<p>Hello there !</p><img src="http://placehold.it/500x1000" />'}
            HTMLStyles={'body { background-color:lightblue }'}
        />
    );
}

Props

In addition to these props, you can provide any prop from the Webview component except for injectedJavaScript, source and onMessage which are handled in a specific way.

Prop | Description | Type | Default ------ | ------ | ------ | ------ htmlString | HTML to display (doctype & <body> not needed) | string | Required onMessage | Function called when webview sent data from window.WebViewBridge.send | func | additionalScripts | Array of functions to be injected in webview | array | title | Title of the page for easier debugging | string | react-native-display-html-${Date.now()} style | Style of the webview component | Webview style object | {flex: 1} containerStyle | Style the container wrapping the webview | View style object | {} HTMLStyles | CSS style to be injected | string | defaultHeight | Webview's height before it's updated | number | 100 additionalHeight | Add some height to the webview once it's calculated | number | 0 bodyClass | Add some height to the webview once it's calculated | string | ''

Tips and tricks

Injecting javascript

Instead of sending plain strings as in React Native's core Webview component, you can now directly send functions. Just make an array of all the functions you need to inject, and react-native-display-html will handle the heavy-lifting by itself by stringify-ing your functions and adding closures, ez pz.

Using an array might allow you to inject javascript only on some devices that could need it with ease.

Note : you need to write them in ES5, as they won't be transpiled !

Sending data from webview to react-native

You can send data by using window.WebViewBridge.send from the webview's javascript. You should use it in your injected code. For a demo of how it's working, check out the heightScript method of this component. The best way to send objects is to stringify them and parse them back in your onMessage prop.

For instance, this is a great way to implement specific actions on links. We had to rewrite an old hybrid application with react-native and keep the HTML of every article which contained a lot of data-action. Some opening maps, articles, webviews...

We could easily add these functionalities back thanks to this component.

Fonts

This is a bit tricky and doesn't work for now on Android < 4.4. You need the allowUniversalAccessFromFileURLs and allowFileAccess props to be set to true on your DisplayHTML component. Then, in your HTMLStyles prop, you can declare a font with url("/pathtoyourfont.ttf") for iOS and url("file:///android_asset/pathtoyourfont.ttf") on Android.

Note : Be sure to have your fonts inside your XCode and Android projects.

Debugging

You can debug your webview just as you would inspect a regular web page on iOS. Just open Safari and go to Development/Your Device/Title of your component. This way, you can check your console logs you injected, or work on your HTML/CSS.

TypeScript

This plugin ships with typescript definitions. Feel free to improve these declarations and open a pull request if you're a ts lover.

TODO

Credits

Written by Maxime Bertonnier at Archriss.