Coder Social home page Coder Social logo

steviant-zz / fractal-react-adapter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sumul/fractal-react-adapter

0.0 1.0 0.0 10 KB

Use React as the template engine for Fractal (http://fractal.build) components.

License: MIT License

JavaScript 100.00%

fractal-react-adapter's Introduction

React Adapter for Fractal

This adapter lets you use React as a template engine in Fractal. It's based on Fractal's Handlebars adapter. This adapter aims to maintain a React flavor rather than achieve complete feature parity with the Handlebars adapter. The goal is to facilitate writing React components that can easily be used in other projects.

Installation

Install the adapter via NPM:

npm i --save fractal-react-adapter

Plug it into your fractal.js file like so:

const reactAdapter = require('fractal-react-adapter')();
fractal.components.engine(reactAdapter);
fractal.components.set('ext', '.jsx');

The adapter uses Babel to compile React components via babel-register (which hooks into require or import and automatically routes those files through Babel). By default, babel-register is configured to compile .jsx files and use the react and es2015 Babel presets, but you can override these with any valid babel-register config (see Configuration below).

// Default babel-register config
{
  extensions: [".jsx"],
  presets: ['es2015', 'react'],
}

The adapter also uses babel-plugin-module-resolver to expose Fractal's component handles as node module names. This allows you to move a component around in the file system without worrying about rewriting your imports.

import Button from '@button';
const Button = require('@button');

Configuration

These options can be overridden when the adapter is set up.

  • babelConfig: any valid configuration options for babel-register
  • renderMethod: 'renderToStaticMarkup' or 'renderToString' (see ReactDOMServer)
const reactAdapter = require('fractal-react-adapter')({
  babelConfig: {
    presets: ['es2015', 'react', 'stage-0']
  },
  renderMethod: 'renderToString',
});

Example components

A simple button component

import React from 'react';
 
class Button extends React.Component {
  render() {
    return <button className={this.props.className}>{this.props.children}</button>
  }
}

module.exports = Button;

A card component that uses the button

import React from 'react';
import Button from '@button'

class Card extends React.Component {
  render() {
    return (
      <div className="card">
        <h1>{this.props.title}</h1>
        <p>{this.props.description}</p>
        <Button className="orange">{this.props.buttonText}</Button>        
      </div>
    );
  }
}

module.exports = Card;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.