3.0.5 • Published 2 years ago

babel-plugin-transform-redom-jsx v3.0.5

Weekly downloads
414
License
MIT
Repository
github
Last release
2 years ago

babel-plugin-transform-redom-jsx

Build Status npm version

Installation

npm install babel-plugin-transform-redom-jsx --save-dev

Usage

Babel config:

{
  "plugins": [
    "babel-plugin-transform-redom-jsx",
    [
      "transform-react-jsx",
      {
        "pragma": "el"
      }
    ]
  ]
}

Component

The plugin transpiles the following JSX:

import { el, text, mount } from "redom";
class A {
  constructor(attr, text) {
    <div this="el">
      <h3>{attr.title}</h3>
      <span this="span">Hello</span> {text}
    </div>;
  }
  update() {
    this.span.textContent = "Hi";
  }
}

class B {
  constructor() {
    <div this="el">
      <A this="a" title="Hello World example">
        <span this="span">World</span>
      </A>
    </div>;
  }
  update() {
    this.span.textContent = "You";
    this.a.update();
  }
}

const b = <B />;

mount(document.body, b);

b.update();

To the following JavaScript:

import { el, text, mount } from "redom";
class A {
  constructor(attr, text) {
    this["el"] = el(
      "div",
      null,
      el("h3", null, attr.title),
      (this["span"] = el("span", null, "Hello")),
      " ",
      text
    );
  }
  update() {
    this.span.textContent = "Hi";
  }
}

class B {
  constructor() {
    this["el"] = el(
      "div",
      null,
      (this["a"] = new A(
        { title: "Hello World example" },
        (this["span"] = el("span", null, "World"))
      ))
    );
  }
  update() {
    this.span.textContent = "You";
    this.a.update();
  }
}

const b = new B({});

mount(document.body, b);

b.update();

Assign to this

The plugin transpiles the following JSX:

import { el, mount } from "redom";

// define Login component
class Login {
  constructor() {
    <form
      this="el"
      id="login"
      onsubmit={(e) => {
        e.preventDefault();
        const email = this.email.value;
        const pass = this.pass.value;
        console.log(email, pass);
      }}
    >
      <input type="email" class="email" this="email" />
      <input type="password" class="pass" this="pass" />
      <button type="submit">Sign in</button>
    </form>;
  }
}

// mount to DOM
mount(document.body, <Login />);

To the following JavaScript:

import { el, mount } from "redom";

// define Login component
class Login {
  constructor() {
    this["el"] = el(
      "form",
      {
        id: "login",
        onsubmit: (e) => {
          e.preventDefault();
          const email = this.email.value;
          const pass = this.pass.value;
          console.log(email, pass);
        },
      },
      (this["email"] = el("input", { type: "email", class: "email" })),
      (this["pass"] = el("input", { type: "password", class: "pass" })),
      el("button", { type: "submit" }, "Sign in")
    );
  }
}

// mount to DOM
mount(document.body, new Login());
3.0.5

2 years ago

3.0.4

2 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago