All Projects → jsdf → React Native Htmlview

jsdf / React Native Htmlview

Licence: isc
A React Native component which renders HTML content as native views

Programming Languages

javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to React Native Htmlview

modest ex
Elixir library to do pipeable transformations on html strings (with CSS selectors)
Stars: ✭ 31 (-98.78%)
Mutual labels:  html-parser, html-renderer
React Intl Tel Input
Rewrite International Telephone Input in React.js. (Looking for maintainers, and PRs & contributors are also welcomed!)
Stars: ✭ 212 (-91.67%)
Mutual labels:  react-component
React Native Card Flip
Card flip animation for React Native
Stars: ✭ 183 (-92.81%)
Mutual labels:  react-component
React Native Pdfview
📚 PDF viewer for React Native
Stars: ✭ 198 (-92.22%)
Mutual labels:  react-component
React Anchor Link Smooth Scroll
React component for anchor links using the smooth scroll polyfill.
Stars: ✭ 186 (-92.69%)
Mutual labels:  react-component
React Input Color
React color picker
Stars: ✭ 208 (-91.83%)
Mutual labels:  react-component
React Rater
⭐️ Interative & customizable star rater
Stars: ✭ 180 (-92.93%)
Mutual labels:  react-component
React Split Pane
React split-pane component
Stars: ✭ 2,665 (+4.67%)
Mutual labels:  react-component
React Adsense
📽 a simple React-component for Google AdSense / Baidu advertisement.
Stars: ✭ 210 (-91.75%)
Mutual labels:  react-component
React Md Spinner
Material Design spinner components for React.js.
Stars: ✭ 195 (-92.34%)
Mutual labels:  react-component
React Voice Components
Set of React components that use the Web Speech API to bring voice experience to React applications
Stars: ✭ 195 (-92.34%)
Mutual labels:  react-component
React Country Region Selector
Country / region React select boxes for your forms.
Stars: ✭ 189 (-92.58%)
Mutual labels:  react-component
React Reorder
Drag & drop, touch enabled, reorderable / sortable list, React component
Stars: ✭ 209 (-91.79%)
Mutual labels:  react-component
Kanna
Kanna(鉋) is an XML/HTML parser for Swift.
Stars: ✭ 2,227 (-12.53%)
Mutual labels:  html-parser
Nokogiri
HTML parser for PHP - Парсер HTML
Stars: ✭ 214 (-91.59%)
Mutual labels:  html-parser
React Mobile Picker
An iOS like select box component for React
Stars: ✭ 180 (-92.93%)
Mutual labels:  react-component
React Datepicker2
react datepicker component.(include persian jalaali calendar)
Stars: ✭ 191 (-92.5%)
Mutual labels:  react-component
React Times
A time picker react component, no jquery-rely
Stars: ✭ 206 (-91.91%)
Mutual labels:  react-component
React Photo View
一款精致的 React 图片预览组件
Stars: ✭ 218 (-91.44%)
Mutual labels:  react-component
Yoshino
A themable React component library!Flexible Lightweight PC UI Components built on React! Anyone can generate easily all kinds of themes by it!
Stars: ✭ 216 (-91.52%)
Mutual labels:  react-component

React Native HTMLView Build status

A component which takes HTML content and renders it as native views, with customisable style and handling of links, etc.

In action (from ReactNativeHackerNews):

React Native Hacker News Comments

Table of contents

Install

npm install react-native-htmlview --save

Usage

props:

  • value: a string of HTML content to render
  • onLinkPress: a function which will be called with a url when a link is pressed. Passing this prop will override how links are handled (defaults to calling Linking.openURL(url))
  • onLinkLongPress: a function which will be called with a url when a link is long pressed. The default is null.
  • stylesheet: a stylesheet object keyed by tag name, which will override the styles applied to those respective tags.
  • renderNode: a custom function to render HTML nodes however you see fit. If the function returns undefined (not null), the default renderer will be used for that node. The function takes the following arguments:
    • node the html node as parsed by htmlparser2
    • index position of the node in parent node's children
    • siblings parent node's children (including current node)
    • parent parent node
    • defaultRenderer the default rendering implementation, so you can use the normal rendering logic for some subtree. defaultRenderer takes the following arguments:
      • node the node to render with the default rendering logic
      • parent the parent of node of node
  • bullet: text which is rendered before every li inside a ul
  • paragraphBreak: text which appears after every p element
  • lineBreak: text which appears after text elements which create a new line (br, headings)
  • addLineBreaks: when explicitly false, effectively sets paragraphBreak and lineBreak to null
  • NodeComponent, nodeComponentProps, RootComponent, rootComponentProps, TextComponent, textComponentProps: see Customizing things even further below.

Example

import React from 'react';
import {StyleSheet} from 'react-native';
import HTMLView from 'react-native-htmlview';

class App extends React.Component {
  render() {
    const htmlContent = `<p><a href="http://jsdf.co">&hearts; nice job!</a></p>`;

    return (
      <HTMLView
        value={htmlContent}
        stylesheet={styles}
      />
    );
  }
}

const styles = StyleSheet.create({
  a: {
    fontWeight: '300',
    color: '#FF3366', // make links coloured pink
  },
});

Custom Link Handling

When a link is clicked, by default ReactNative.Linking.openURL is called with the link url. You can customise what happens when a link is clicked with onLinkPress:

class App extends React.Component {
  render() {
    return (
      <HTMLView
        value={this.props.html}
        onLinkPress={(url) => console.log('clicked link: ', url)}
      />
    );
  }
}

If you're getting the error "undefined is not an object (evaluating 'RCTLinkingManager.openURL’)” from the LinkingIOS API, try adding ‘RCTLinking' to the project's 'Linked Frameworks and Libraries’. You might have to find RCTLinking.xcodeproj in the react-native package dir and drag that into your main Xcode project first.

Custom Element Rendering

You can implement the renderNode prop to add support for unsupported element types, or override the rendering for supported types. renderNode is a function which is called with the type and attributes of each HTML element found in the input HTML, and from this function you can return a React element to be rendered in its place. If you return null nothing will be rendered in place of this element or its children. If you return undefined (or don't return anything) then HTMLView will drop back to its default rendering for that type of HTML element.

For example, here is how you might implement the <iframe> element:

function renderNode(node, index, siblings, parent, defaultRenderer) {
  if (node.name == 'iframe') {
    const a = node.attribs;
    const iframeHtml = `<iframe src="${a.src}"></iframe>`;
    return (
      <View key={index} style={{width: Number(a.width), height: Number(a.height)}}>
        <WebView source={{html: iframeHtml}} />
      </View>
    );
  }
}

const htmlContent = `
  <div>
    <iframe src="http://info.cern.ch/" width="360" height="300" />
  </div>
`;

class App extends React.Component {
  render() {
    return (
      <HTMLView value={htmlContent} renderNode={renderNode} />
    );
  }
}

Alternatively, this example shows how you could disallow the <iframe> element:

function renderNode(node, index, siblings, parent, defaultRenderer) {
  if (node.name == 'iframe') {
    return null;
  }
}

const htmlContent = `
  <div>
    <iframe src="http://info.cern.ch/" width="360" height="300" />
  </div>
`;

class App extends React.Component {
  render() {
    return (
      <HTMLView value={htmlContent} renderNode={renderNode} />
    );
  }
}

If you want to reuse the default renderer, you need to call it passing an array of nodes. This example shows how to replace a specific HTML tag with something different, but still process the children.

function renderNode(node, index, siblings, parent, defaultRenderer) {
  if (node.name == 'mytag') {
      const specialSyle = node.attribs.style
      return (
        <Text key={index} style={specialSyle}>
          {defaultRenderer(node.children, parent)}
        </Text>
      )
    }
}

const htmlContent = `
  <div>
    <mytag>
      <div>some content processed normally by the engine</div>
    </mytag>
  </div>
`;

class App extends React.Component {
  render() {
    return (
      <HTMLView value={htmlContent} renderNode={renderNode} />
    );
  }
}

For further understanding of the possiblities of the renderNode prop, read through htmlToElement.js. Particularly look at where renderNode is called to see how it can override what sort of React element is created in place of an element in the input HTML.

Customizing things even further

In addition to supplying a custom renderNode function, you can customize what is rendered by the built in renderNode function. Read through htmlToElement.js and note the usage of NodeComponent (for rendering HTML element nodes) and TextComponent (for rendering text strings in the HTML). Both of these components can be injected as the NodeComponent and TextComponent props to HTMLView, or alternatively they can be given extra props by passing an object as the nodeComponentProps and textComponentProps props. Finally you can also use the props RootComponent and rootComponentProps to customize the root wrapper View element that is rendered by the HTMLView in HTMLView.js.

Changelog

See CHANGELOG.md.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].