Giter Site home page Giter Site logo

jsx's Introduction

jsx

write jsx in your html. only 1kb dep.

// it returns a dom object.
const app = jsx`<div>Hello World!</div>` // <div>Hello World!</div>

document.body.append(app) // append it.

it's the same as:

const app = document.createElement("div")
app.innerHTML = "Hello World!"
document.body.append(app)

or use jsx.render:

jsx.render(app, document.body)

Start

add it to the head. have a try!

<script src="https://saber2pr.top/jsx/jsx.min.js"></script>

Example

demo

<html>
  <head>
    <!-- import jsx shim -->
    <script src="https://saber2pr.top/jsx/jsx.min.js"></script>
  </head>
  <body>
    <div id="root"></div>
    <script>
      // functional component
      const List = ({ list }) =>
        list.map(n => jsx`<li style=${{ color: "red" }}>${n}</li>`)

      // <ul />
      const Ul = jsx`<ul><${List} list=${[1, 2, 3]} /></ul>`

      // append to dom.
      document.getElementById("root").append(Ul)
    </script>
  </body>
</html>

1. Ref

const ref = {} // must be an object
const button = jsx`<button ref=${ref} onClick=${() =>
  console.log(ref.current)}>add</button>`

// functional ref handle
const button = jsx`<button ref=${btn => (ref.current = btn)} onClick=${() =>
  console.log(ref.current)}>add</button>`

the ref could be passed from functional component to the functional component, no need forwardRef.(different from react)

2. Fragment

const paras = jsx`
  <${jsx.frag}>
    <p>1</p>
    <p>2</p>
    <p>3</p>
  </${jsx.frag}>` // #document-fragment

document.getElementById("root").append(paras)

3. Lazy

const fetchData = value =>
  new Promise(resolve => setTimeout(resolve, 1000, value))

const App = async ({ value }) => {
  const data = await fetchData(value)
  return jsx`<h1>${data}</h1>`
}

const Index = jsx`
  <${jsx.Suspense} fallback=${jsx`<p>loading...</p>`}>
    <${App} value=${"qwq"}/>
  </${jsx.Suspense}>`

document.getElementById("root").append(Index)

or import()

const App = jsx.lazy(import("./app.js"))

// or return a default prop: () => Promise<{default: HTMLElement}>
const App = jsx.lazy(async ({ value }) => {
  const data = await fetchData(value)
  return {
    default: jsx`<h1>${data}</h1>`
  }
})

4. createElement

jsx.createElement(`div`, { id: "hello" }, "Hello World!") // <div id="hello">Hello World!</div>

const fontSize = 2
jsx.createElement(`h${fontSize}`, {}, "Hello World!") // <h2>Hello World!</h2>

Reference

htm

Author: saber2pr

jsx's People

Contributors

saber2pr avatar

Stargazers

 avatar liyuanqiu avatar 飘香豆腐 avatar

Watchers

James Cloos avatar  avatar  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.