• Stars
    star
    2,131
  • Rank 21,658 (Top 0.5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Converts HTML pages into React components

Logo

If you like what I do, consider supporting my work via donation

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:

  • functional (default)
  • class
  • es5

moduleType, --module, -m

Type of generated JavaScript modules.

Values:

  • es (EcmaScript module, 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: "functional",
    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

More Repositories

1

webpack-closure-compiler

[DEPRECATED] Google Closure Compiler plugin for Webpack
JavaScript
464
star
2

closure-compiler-handbook

How to use Google's Closure Compiler
441
star
3

uix

Idiomatic ClojureScript interface to modern React.js
HTML
431
star
4

threegn

Procedural 3D graphics editor for the web
JavaScript
309
star
5

react-native-babel

Configuration to build React Native apps with ES6 using webpack and Babel
Objective-C
234
star
6

clj-wasm

Clojure-flavored WASM's text format
Clojure
159
star
7

react-flux-workshop

Материалы к воркшопу «React.js и архитектура Flux»
JavaScript
128
star
8

react-horizon

React Horizon makes it easier to use your React application with horizon.io realtime backend
JavaScript
119
star
9

JSONFormData

HTML JSON form submission polyfill
JavaScript
104
star
10

javascript-to-clojurescript

JavaScript to ClojureScript translator
JavaScript
102
star
11

react-webrtc

WebRTC React mixins for real-time communication in React components
JavaScript
85
star
12

slack-traductor

Slack bot to translate chat messages of any language into specified language
JavaScript
85
star
13

babel-plugin-stateful-functional-react-components

Stateful functional React components without runtime overhead
JavaScript
83
star
14

proton-native-cljs

Clojure
79
star
15

prum

ClojureScript's Rum with Preact.js instead of React
HTML
64
star
16

anybar-webpack

Webpack build status plugin for menubar status indicator applications
JavaScript
62
star
17

js-memory-usage

Data Structures Memory Usage in JavaScript
JavaScript
59
star
18

minimax

Minimalist 3D game engine in Clojure
Clojure
59
star
19

scrum-ssr-example

HN clone app with server-side rendering built with Scrum
Clojure
57
star
20

solid-cljs

ClojureScript bindings to SolidJS
Clojure
57
star
21

cljs-rum-realworld-example-app

ClojureScript + Rum codebase containing real world examples
Clojure
53
star
22

github-issues

Sample React application built with Flux
JavaScript
48
star
23

threejs-cljs-playground

three.js playground in ClojureScript
CSS
44
star
24

figcup

Converts Figma designs into Reagent/Hiccup components to render in the browser
Clojure
40
star
25

cljs-async-await

Experimental ClojureScript's compiler extension that enables JavaScript's async/await
Clojure
36
star
26

clojurescript-workshop

Repository of related materials to the workshop
Clojure
36
star
27

recursion-exercises

Recursion exercises in JavaScript
JavaScript
34
star
28

cljs-react-devtools

React DevTools for ClojureScript wrappers
Clojure
30
star
29

react-horizon-example

Example React application built with React Horizon connector to horizon.io realtime backend
JavaScript
29
star
30

amsterdamjs-clojurescript-workshop

Educational materials for ClojureScript workshop @ AmsterdamJS '18
Clojure
28
star
31

hooks

React Hooks for ClojureScript
Clojure
22
star
32

grunt-load-perf

Grunt task for generating visual report to measure web applications loading and rendering performance.
JavaScript
21
star
33

impact-node

Node.js CLI for game development with Impact HTML5 game engine
JavaScript
21
star
34

react-dom-visualizer

Visualize components structure in your React application as a tree chart
JavaScript
20
star
35

babel-plugin-react-hyperscript

HyperScript syntax for React components without runtime overhead
JavaScript
20
star
36

cljs-source-mapped-stack-traces

Displays exceptions with stack frames mapped to ClojureScript source
JavaScript
19
star
37

path-following

JavaScript implementation of Craig Reynolds’s path-following algorithm
JavaScript
18
star
38

adapton

A Minimal Implementation of Incremental Computation in Clojure and ClojureScript
Clojure
17
star
39

imtbl

Immutable operations for JavaScript data structures
JavaScript
17
star
40

web-components-polyfills

A set of lightweight polyfills to enable Web Components support in major browsers, including IE9+
JavaScript
16
star
41

cljs-worklet

Run ClojureScript functions on a worklet thread in React Native
Clojure
16
star
42

voice-to-chat

A small CLI and menubar app that transcribes voice recording and rewrites it in a style of a written text
JavaScript
14
star
43

0ui

A set of utilities for building UIs with React
JavaScript
14
star
44

leap-motion-controlled-hand

Leap Motion controlled 3D hand made with Three.js
JavaScript
14
star
45

vscode-structed

Structured editing in JavaScript extension for VS Code
JavaScript
13
star
46

part-js

Persistent Adaptive Radix Tree (PART) for JavaScript
TypeScript
13
star
47

zfort-js-course

JavaScript course for Zfort company.
JavaScript
13
star
48

postcss-camel-case

PostCSS plugin to convert CSS selector names to camelCase
JavaScript
13
star
49

clojurescript-studio

Online coding sandbox tailored for web development
Clojure
13
star
50

react-a11y-video

Accessible HTML5 Video player React component
JavaScript
10
star
51

node-stm

Multiversion Concurrency Control (MVCC) based Software Transactional Memory (STM)
JavaScript
10
star
52

atom-cljs-doc

ClojureScript core library documentation viewer for Atom
JavaScript
9
star
53

kottans-intro-into-fp

Introduction into functional programming
JavaScript
8
star
54

virtual.list

Virtual list component for Prum
Clojure
8
star
55

grunt-mocha-casperjs

Grunt wrapper for mocha-casperjs
JavaScript
8
star
56

websockets-device-controller

WebGL demo powered by WebSockets and device accelerometer data to control things on the web.
JavaScript
7
star
57

generator-website

A website generator for Yeoman
JavaScript
7
star
58

advent-of-code-2018

Solutions to puzzles from Advent of Code 2018
Clojure
6
star
59

shapy

Web-based UI design tool
Clojure
6
star
60

roman01la-devtools

Custom UI theme for Chrome's DevTools
HTML
5
star
61

structed

Structured editing for JavaScript
JavaScript
5
star
62

jsbin-cljs

ClojureScript for JS Bin
Clojure
5
star
63

zprint-atom

Atom plugin for zprint-clj
JavaScript
5
star
64

rc-cljs-workshop

ClojureScript Workshop @ ReactiveConf '17
5
star
65

uix.css

CSS-in-CLJS
4
star
66

react-images-preloader

HOC to preload images before rendering
JavaScript
4
star
67

babel-plugin-inline

Array map/filter/reduce inliner plugin
JavaScript
4
star
68

webpack-sri

Subresource Integrity hash generator plugin for Webpack
JavaScript
4
star
69

clojure-hacking-day

Clojure
4
star
70

pwl-kyiv

Papers We Love @ Kyiv
HTML
3
star
71

re-frame-chat

Clojure
3
star
72

uix-cookbook

UIx Cookbook is a set of recipes that will assist you in creating web apps with UIx
2
star
73

rc18-clojure-workshop

Clojure
2
star
74

happy-paw

HappyPaw + Tinder
Clojure
2
star
75

sqlite-vector-search

A demo of vector search with HNSW and SQLite
JavaScript
2
star
76

create-uix-app

Scaffolding tool for UIx
JavaScript
1
star
77

rum-code-splitting

Clojure
1
star
78

roman01la

1
star
79

sub-deep

Transcribe and translate audio with AI
JavaScript
1
star
80

canvas-text-layout

JavaScript
1
star
81

slack-yue

Clojure
1
star
82

subform-css-layout-worklet

Subform’s Layout engine via CSS Layout API
JavaScript
1
star
83

libx.core

Enhanced Clojure's standard library
Clojure
1
star