- Notifications
You must be signed in to change notification settings - Fork0
Converts HTML pages into React components
License
ProReactJS/html-to-react-components
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Extract annotated portions of HTML into React components as separate modules. The structure of HTML is preserved by importing child components and replacing appropriate pieces of HTML with them. As a result you get an entire components tree ready to be rendered.
Try inonline REPL
- When to use it
- Installation
- Usage
- CLI
- Options
- Node.js API
- Building for browser
- Resources
- Ecosystem
- Contributing
- License
This utility was designed to free React developers from a boring task of translating HTML into components.
Imagine you just got a pile of HTML from your designers. The first thing you will do is break HTML into React components. This is boring and should be automated!
$ npm i -g html-to-react-components
HTML element withdata-component
attribute will be converted into separate React components. The value of the attribute is the name of the React component.
Additionally specify which HTML attributes should be exposed as React props usingpublic:
prefix.
<inputpublic:type="text"id="input"data-component="Input"/>
// at usage place<Inputtype="text"/>;// ----^^^^^^^^^^^// in component's moduleclassInputextendsReact.Component{render(){const{ type}=this.props;// <----return<inputtype={type}id="input"/>;// -----------^^^^^^^^^^^}}
See and runtest.js
file for usage example and output.
$ html2react "./src/*.html"
You can also use any glob pattern to recursively generate the corresponding react file. Just make sure to use double quotes when specifying the pattern:
$ html2react "./src/**/*.html"
Type of generated React components.
Values:
stateless
es5
es6
(default)
Type of generated JavaScript modules.
Values:
es6
(default)cjs
(CommonJS)
Delimiter character to be used in modules filename.
If you don't specify a delimiter, or pass -d without a value, then the componentname in the HTML will be used unchanged as the filename. If you do specify adelimiter character, then the module name is broken into words, joined with thedelimiter and lower-cased.
Configuration options for output to file system.
Output directory path.
Default iscomponents
directory in the current directory.
Output files extension.
Default value isjs
.
constextractReactComponents=require("html-to-react-components");extractReactComponents(`<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title></head><body> <header data-component="Header"> <h1 data-component="Heading">Hello, world!</h1> <nav data-component="Nav"> <ul> <li data-component="ListItem">#1</li> <li data-component="ListItem">#2</li> </ul> </nav> </header></body></html>`,{componentType:"stateless",moduleType:false});/*{ Header: 'const Header = () => <header className="header">\n\n <Heading></Heading>\n\n <Nav></Nav>\n\n </header>;', Heading: 'const Heading = () => <h1 className="heading">Hello, world!</h1>;', Nav: 'const Nav = () => <nav className="nav">\n <ul className="list">\n <ListItem></ListItem>\n <ListItem></ListItem>\n </ul>\n </nav>;', ListItem: 'const ListItem = () => <li className="list-item">#2</li>;' }*/
When building for in-browser usage an env variableIN_BROWSER
is required to be set at compile time in order to disable Node.js-specific modules. Note that code formatting is not included in in-browser bundle.
Example of defining a var in Webpack config:
plugins:[newwebpack.DefinePlugin({IN_BROWSER:JSON.stringify(true),}),],
A quickvideo demo on converting a simple HTML page into React components and rendering them into the same looking UI.
Annotating HTML in the editor is not the best experience, because you cannot see rendered UI itself. It's possible to annotate HTML using DevTools. Be aware that you'll have to spend time on copying and pasting markup from DevTools into files which will be processed.
- extract-to-react is an extension for Chrome and Chromium browsers built on top ofhtml-to-react-components which allows you to extract HTML and CSS into React components and load them in CodePen or JSFiddle.
If you spotted a bug, please, submit a pull request with a bug fix. If you would like to add a feature or change existing behaviour, open an issue and tell about what exactly you want to change/add.
MIT
About
Converts HTML pages into React components
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Languages
- JavaScript96.9%
- HTML3.1%