1.2.1 • Published 4 years ago

@busfor/react-native-html-to-native v1.2.1

Weekly downloads
9
License
MIT
Repository
github
Last release
4 years ago

react-native-html-to-native

npm npm (tag) npm.io

Library for parsing HTML code into native iOS and Android components with CSS selector-like styling and rendering


Getting started

$ yarn add @busfor/react-native-html-to-native

Usage

import { HTMLView } from '@busfor/react-native-html-to-native'
<HTMLView
  onLinkPress={(url) => console.log(url)}
  onError={(err) => console.log(err)}
  styles={styles}
  renderers={{
    'a.link': (renderedChildren, style, props) => {
      return (
        <TouchableOpacity key={props.key} onPress={() => console.log('Clicked', props.attributes.href)} style={style}>
          {renderedChildren}
        </TouchableOpacity>
      )
    },
  }}
  html={'<div><p>Paragraph</p></div>'}
  onLoading={(loading) => console.log(loading)}
/>

Docs

Properties

NameDescriptionType
html*Raw HTML code to be parsed and presentedstring
renderersHTML nodes render functionsObject
passPropsCustom props passed to node rendererObject
stylesCustom node stylesObject
onErrorError callback functionFunction
onLinkPressLink press callback functionFunction
parserOptionsParser options, see ParserOptions typeParserOptions
onLoadingLoading state callback, only parameter is loadingState of type booleanFunction
renderLoadingFunction to render custom loading indicatorFunction

* - required property

Types

ParserOptions

OptionDescriptionTypeDefault
normalizeWhitespaceIndicates whether whitespace in text nodes should be normalizedbooleanfalse
recognizeSelfClosingRecognize self-closing HTML tagsbooleantrue
decodeEntitiesIf set to true, entities within HTML code will be decodedbooleantrue

ElementRenderer - function for rendering html nodes as native elements

(renderedChildren: Array\, style: StyleProp\, props: ElementProps) => ReactNode

Function accepts rendered node's children, node's style and props

Should return ReactNode

For more see Default renderers


ElementProps - props of rendering HTML node

NameDescriptionType
attributesHTML tag attributesObject or undefined
passPropsCustom props passed to component render from HTMLViewObject
handleLinkPressLink press handler functionFunction
nodeHTML node for which element is renderedNode
childrenNode's children arrayArray\
siblingsNode's siblings arrayArray\
parentNode's parentNode
dataText data for text nodesstring
keyUnique key for component renderingstring

Selectors

Selectors system built in a CSS-like manner

To select element by tag name just use it as is: ol, p, etc.

For selecting by class or id use .class and #id. For more specific selection of component with class(same for component with id) use it like: p.class

Also it is possible to select HTML node by path like ol>li or ol.class>li#id etc.

Some HTML node have custom selectors. Text nodes are rendered as React Native \ components and can be accessed by TextNode selector. Same for list items indicators nodes - access it by IndicatorNode selector.

For more examples of using selectors see Usage, Example app and Default renderers and styles

Default renderers

Some HTML tags are rendered by default, some are skipped while rendering.

To know how HTML tags are rendered and how to use ElementRenderer functions see default renderers

Default styles

Some default styles for tags could be specified in this library

To see what styles are default for tags see default styles

Issues and contributing

Feel free to report any bug or request any functionality you would like to be done with Open Issue functionality of GitHub

Also feel free to fork and contribute by opening Pull Request. All pull requests will be reviewed and merged if everything is OK!

TODO:

⬜️ Improve CSS selectors

⬜️ Complete example app

⬜️ Add HTML parsing from URL

1.2.1

4 years ago

1.2.0

4 years ago

1.2.0-rc.10

4 years ago

1.2.0-rc.9

4 years ago

1.2.0-rc.8

4 years ago

1.2.0-rc.7

4 years ago

1.2.0-rc.6

4 years ago

1.2.0-rc.5

4 years ago

1.2.0-rc.4

4 years ago

1.2.0-rc.3

4 years ago

1.2.0-rc.2

4 years ago

1.2.0-rc.1

4 years ago

1.2.0-rc.0

4 years ago

1.1.0

4 years ago

1.1.0-rc.7

4 years ago

1.1.0-rc.9

4 years ago

1.1.0-rc.8

4 years ago

1.1.0-rc.6

4 years ago

1.1.0-rc.5

4 years ago

1.1.0-rc.4

4 years ago

1.1.0-rc.3

4 years ago

1.1.0-rc.2

4 years ago

1.1.0-rc.1

4 years ago

1.1.0-rc.0

4 years ago

1.0.10

4 years ago

1.0.9-rc.1

4 years ago

1.0.9-rc.2

4 years ago

1.0.9-rc.3

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago