All Projects → roman01la → Html To React Components

roman01la / Html To React Components

Licence: mit
Converts HTML pages into React components

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to Html To React Components

Ink Link
Link component for Ink
Stars: ✭ 133 (-93.45%)
Mutual labels:  jsx
X0
Document & develop React components without breaking a sweat
Stars: ✭ 1,706 (-16%)
Mutual labels:  jsx
Gantt
Gantt chart library using jsx support SVG, Canvas and SSR
Stars: ✭ 148 (-92.71%)
Mutual labels:  jsx
Preact Minimal
🚀 Minimal preact structure
Stars: ✭ 136 (-93.3%)
Mutual labels:  jsx
React Code Blocks
React code blocks and code snippet components
Stars: ✭ 135 (-93.35%)
Mutual labels:  jsx
Weapp Native
weapp-native (wn) 像React组件开发一样来开发微信小程序,开发微信小程序框架 [此项目短时间内已不再维护,有兴趣继续的可以加群联系我]
Stars: ✭ 144 (-92.91%)
Mutual labels:  jsx
Hyperapp One
Hyperapp One is a Parcel boilerplate for quickstarting a web application with Hyperapp (V1), JSX, and Prettier.
Stars: ✭ 129 (-93.65%)
Mutual labels:  jsx
15 Puzzle
The 15-puzzle is a sliding puzzle that consists of a frame of numbered square tiles in random order with one tile missing, built in react
Stars: ✭ 161 (-92.07%)
Mutual labels:  jsx
Virtual Dom
关于Vue,React,Preact和Omi等框架源码的解读
Stars: ✭ 139 (-93.16%)
Mutual labels:  jsx
React Border Wrapper
A wrapper for placing elements along div borders.
Stars: ✭ 147 (-92.76%)
Mutual labels:  jsx
Dependency Cruiser
Validate and visualize dependencies. Your rules. JavaScript, TypeScript, CoffeeScript. ES6, CommonJS, AMD.
Stars: ✭ 2,326 (+14.52%)
Mutual labels:  jsx
Jsx To Clojurescript
Command and library to convert JSX snippets to Om/Reagent/Rum or other Clojurescript-style format.
Stars: ✭ 138 (-93.21%)
Mutual labels:  jsx
Nativejsx
JSX to native DOM API transpilation. 💛 <div> ⟹ document.createElement('div')!
Stars: ✭ 145 (-92.86%)
Mutual labels:  jsx
Mdx
Markdown for the component era
Stars: ✭ 11,948 (+488.28%)
Mutual labels:  jsx
Vue Ts Admin
一个使用ant-design-vue + vue + typescript + jsx的中后台脚手架
Stars: ✭ 149 (-92.66%)
Mutual labels:  jsx
Storybook Addon Playroom
Design with Playroom inside Storybook, using each story source as a starting point
Stars: ✭ 133 (-93.45%)
Mutual labels:  jsx
Omil
📝Webpack loader for Omi.js React.js and Rax.js components 基于 Omi.js,React.js 和 Rax.js 单文件组件的webpack模块加载器
Stars: ✭ 140 (-93.11%)
Mutual labels:  jsx
Preact Markup
⚡️ Render HTML5 as VDOM, with Components as Custom Elements!
Stars: ✭ 167 (-91.78%)
Mutual labels:  jsx
Omi
Front End Cross-Frameworks Framework - 前端跨框架跨平台框架
Stars: ✭ 12,153 (+498.38%)
Mutual labels:  jsx
Babel Plugin Transform Incremental Dom
Turn JSX into IncrementalDOM
Stars: ✭ 146 (-92.81%)
Mutual labels:  jsx

Logo

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 in online REPL

usage example animation

Contents

When to use it

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!

Installation

$ npm i -g html-to-react-components

Usage

HTML element with data-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 using public: prefix.

<input public:type="text" id="input" data-component="Input" />
// at usage place
<Input type="text" />;
// ----^^^^^^^^^^^

// in component's module
class Input extends React.Component {
  render() {
    const { type } = this.props; // <----
    return <input type={type} id="input" />;
    // -----------^^^^^^^^^^^
  }
}

See and run test.js file for usage example and output.

CLI

$ 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"

Options

componentType, --component, -c

Type of generated React components.

Values:

  • stateless
  • es5
  • es6 (default)

moduleType, --module, -m

Type of generated JavaScript modules.

Values:

  • es6 (default)
  • cjs (CommonJS)

moduleFileNameDelimiter, --delimiter, -d

Delimiter character to be used in modules filename.

If you don't specify a delimiter, or pass -d without a value, then the component name in the HTML will be used unchanged as the filename. If you do specify a delimiter character, then the module name is broken into words, joined with the delimiter and lower-cased.

output

Configuration options for output to file system.

path, --out, -o

Output directory path.

Default is components directory in the current directory.

fileExtension, --ext, -e

Output files extension.

Default value is js.

Node.js API

const extractReactComponents = require("html-to-react-components");

extractReactComponents(
  `<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>

  <header class="header" data-component="Header">

    <h1 class="heading" data-component="Heading">Hello, world!</h1>

    <nav class="nav" data-component="Nav">
      <ul class="list">
        <li class="list-item" data-component="ListItem">#1</li>
        <li class="list-item" 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>;' }
*/

Building for browser

When building for in-browser usage an env variable IN_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: [
    new webpack.DefinePlugin({
      IN_BROWSER: JSON.stringify(true),
    }),
  ],

Resources

A quick video 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.

usage example with DevTools animation

Ecosystem

  • extract-to-react is an extension for Chrome and Chromium browsers built on top of html-to-react-components which allows you to extract HTML and CSS into React components and load them in CodePen or JSFiddle.

Contributing

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.

License

MIT

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].