Giter Site home page Giter Site logo

elementreejs / elementree Goto Github PK

View Code? Open in Web Editor NEW
12.0 4.0 0.0 2.85 MB

Create Web UIs with Proxy Observables and JS Template Literals

Home Page: https://npm.im/elementree

License: MIT License

JavaScript 100.00%
javascript javascript-framework front-end elementree observable tiny mvvm mvvm-framework

elementree's Introduction

stability-experimental JavaScript Style Guide Build Status

Elementree

"Celebrate the code of the problem domain as opposed to the framework."

Elementree is an extremely small front-end JavaScript "framework" with a focus on getting the job done with the mimimum amount of framework-y concepts.

Example

<!DOCTYPE html>
<html>
  <body>
    <script type="module">
      import { merge, prepare, render } from 'https://unpkg.com/elementree'

      function View (state) {
        if (!state.email) state.requestUser()
        return render`
          <body>
            <p>Hello! ${state.email}</p>
            <button onclick=${() => state.nextEmail()}>Next Email</button>
          </body>
        `
      }

      const State = {
        id: 1,
        email: '',
        nextEmail() {
          this.email = null
          this.id += 1
        },
        async requestUser() {
          const response = await fetch(`https://reqres.in/api/users/${this.id}`)
          const { data } = await response.json()
          this.email = data.email
        }
      }
      merge('body', prepare(View, State))
    </script>
  </body>
</html>

Elementree API

merge(to: String, view: Function [, state: Object]) -> undefined

merge wires up a view and an optional object representing the application state and merges it to a selector. Simply put, merge renders your root view to the DOM.

The first argument to merge is a string which will be used by document.querySelector, after DOMContentLoaded, to find root element. The second argument is the top-level view. This argument is a Function that returns a function that returns an HTMLElement such as a prepare call. The third, optional, argument is an object representing the application's state. This object will passed to the renderer function as an parent argument (i.e. following the view's state if there is one).

Elementree adds a single property to the application's state object. The route property is a concatenation of location.pathname, location.search and location.hash. Updating the route property will cause a history.pushState. Updating the address through browser interations will update the route property.

If the window object does not exist the call to merge will return the outerHTML on the result of the rendering.

prepare(template: Function [, state: Object]) -> (Function -> HTMLElement)

prepare a template function with a state object, creating a view function. At a minimum, a template function is required to be passed as the first argument. The second argument, which is optional, is an object representing the local view state. If the template function is joined with a state, the state object will ALWAYS be the 0th argument to the view function. All parent arguments will follow.

render`template: String` -> HTMLElement

A tagged template function. Turn a JavaScript template string into an HTMLElement. If the template has more than one root element a DocumentFragment is returned.

html`unescaped: String` -> HTMLElement

Use html to interpolate HTML, without escaping it, directly into your template.

Attribution

This project would suck a whole lot more without the input from my awesome co-workers at Bitovi, the inspiration from @choojs, and the amazing packages from @sindresorhus. Thank you.

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.