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

@busfor/react-native-html-to-native

v1.2.1

Published

Create customizable React Native views from HTML markup

Downloads

90

Readme

react-native-html-to-native

npm npm (tag)

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

| Name | Description | Type | | ----------------- | ---------------------------------------------------------------------- | --------------- | | html* | Raw HTML code to be parsed and presented | string | | renderers | HTML nodes render functions | Object | | passProps | Custom props passed to node renderer | Object | | styles | Custom node styles | Object | | onError | Error callback function | Function | | onLinkPress | Link press callback function | Function | | parserOptions | Parser options, see ParserOptions type | ParserOptions | | onLoading | Loading state callback, only parameter is loadingState of type boolean | Function | | renderLoading | Function to render custom loading indicator | Function |

* - required property

Types

ParserOptions

| Option | Description | Type | Default | | ------------------------ | --------------------------------------------------------------- | ------- | ------- | | normalizeWhitespace | Indicates whether whitespace in text nodes should be normalized | boolean | false | | recognizeSelfClosing | Recognize self-closing HTML tags | boolean | true | | decodeEntities | If set to true, entities within HTML code will be decoded | boolean | true |


ElementRenderer - function for rendering html nodes as native elements

(renderedChildren: Array<ReactNode>, style: StyleProp<any>, 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

| Name | Description | Type | | --------------- | ----------------------------------------------------- | ------------------- | | attributes | HTML tag attributes | Object or undefined | | passProps | Custom props passed to component render from HTMLView | Object | | handleLinkPress | Link press handler function | Function | | node | HTML node for which element is rendered | Node | | children | Node's children array | Array<Node> | | siblings | Node's siblings array | Array<Node> | | parent | Node's parent | Node | | data | Text data for text nodes | string | | key | Unique key for component rendering | string |


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 <Text> 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