Coder Social home page Coder Social logo

gravytrain / aurelia-react-element Goto Github PK

View Code? Open in Web Editor NEW

This project forked from johot/aurelia-react-element

0.0 1.0 0.0 114 KB

Makes it super easy to use react components inside Aurelia! Works with the Aurelia CLI and TypeScript.

TypeScript 73.21% HTML 7.41% JavaScript 13.34% CSS 6.04%

aurelia-react-element's Introduction

Aurelia React Element

Aurelia React Element makes it super easy to use React components inside Aurelia!

You create a new Aurelia custom element, inherit our ReactElement base class and import the React component. Now you can simply use it in Aurelia by doing:

<WrappedReactComponent props="{ message: 'Hello World'}">

Installing

Install aurelia component

npm install --save aurelia-react-element

React setup

npm install --save react react-dom @types/react @types/react-dom

Configuring an Aurelia CLI project (Aurelia.json etc)

Aurelia.json setup

Example where we are wrapping the react component named react-circular-progressbar from npm.

In the "dependencies": section in aurelia.json.

"react-circular-progressbar",
{
    "name": "react",
    "path": "../node_modules/react/umd",
    "main": "react.development"
},
{
    "name": "react-dom",
    "path": "../node_modules/react-dom/umd",
    "main": "react-dom.development"
},
{
    "name": "aurelia-react-element",
    "path": "../node_modules/aurelia-react-element",
    "main": "index"
},

tsconfig.json setup (for TypeScript users)

Add "jsx": "react" to your tsconfig.json.

Example tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "sourceMap": true,
    "target": "es5",
    "module": "amd",
    "declaration": false,
    "noImplicitAny": false,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowJs": true,
    "moduleResolution": "node",
    "lib": ["es2017", "dom"],
    "jsx": "react"
  },
  "exclude": ["node_modules", "aurelia_project"],
  "filesGlob": [
    "./src/**/*.ts",
    "./test/**/*.ts",
    "./custom_typings/**/*.d.ts"
  ],
  "atom": {
    "rewriteTsconfig": false
  }
}

Wrapping a React component in an Aurelia custom element

Note samples are made using TypeScript but should be easy to convert to JavaScript.

Wrapping your own React component

Your Aurelia custom element

import { bindable } from "aurelia-framework";
import ReactElement from "aurelia-react-element";
import ReactSampleComponent from "./react-components/react-sample-component";

export class SampleComponent extends ReactElement {
  props = { message: "Hello from Aurelia!" };
  component: any = ReactSampleComponent;
}

Note: No .html view is needed.

Your React component (sample)

This file should have a file ending of tsx (TypeScript) or jsx (JavaScript)

import * as React from "react";
import { Component } from "react";
// import "./App.css";

//const logo = require("./logo.svg");

interface ReactSampleComponentProps {
  message: string;
}

export default class ReactSampleComponent extends Component<
  ReactSampleComponentProps
> {
  render() {
    return (
      <div className="App">
        <div className="App-header" style={{ marginBottom: "10px" }}>
          <img src="img/logo.svg" className="App-logo" alt="logo" />
          <h2>Welcome to React</h2>
          <h3>
            Message from Aurelia: <i>{this.props.message}</i>
            <br />
          </h3>
        </div>
        <p className="App-intro">
          To get started, edit
          <code>src/react-components/sample-component.tsx</code> and save to reload.
        </p>
      </div>
    );
  }
}

Wrapping an existing component (from npm)

In this example we are wrapping the react-circular-progressbar from npm.

import { bindable } from "aurelia-framework";
import ReactElement from "aurelia-react-element";
import CircularProgressbar from "react-circular-progressbar";

export class ProgressBar extends ReactElement {
  @bindable props = {};
  component: any = CircularProgressbar;

  @bindable percentage: number = 0;

  bind() {
    this.syncProps();
  }

  syncProps() {
    this.props = { percentage: this.percentage };
  }

  percentageChanged(newValue: number, oldValue: number) {
    this.syncProps();
  }
}

Using in Aurelia:

Sample 1

<template>
  <require from="./sample-component"></require>
  <sample-component></sample-component>
</template>

Sample 2

<template>
 <require from="./progress-bar"></require>
 <progress-bar percentage.two-way="percentCompleted"></progress-bar>
 
 <!-- Or use the props property -->
 <!-- <progress-bar props.bind="{ percentage: '33' }"></progress-bar> -->
</template>

More reading

Ashley Grant has another great example of using React with Aurelia, read more here.

aurelia-react-element's People

Contributors

johot avatar

Watchers

James Cloos avatar

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.