GithubHelp home page GithubHelp logo

schneiderfelipe / hyperscript Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 52 KB

๐Ÿ“Ž Create HyperText with Nim.

Home Page: https://github.com/schneiderfelipe/hyperscript

Nim 100.00%
nim-lang hyperscript templating

hyperscript's Introduction

hyperscript

A functional Nim library for combining DOM pieces, with compile-time superpowers. hyperscript creates composable HTML and SVG with Nim, both client- and server-side:

import hyperscript

let example =
  h("div#page",
    h("div#header",
      h("h1.classy", "h", { style: {"background-color": "#22f"} })),
    h("div#menu", { style: {"background-color": "#2f2"} },
      h("ul",
        h("li", "one"),
        h("li", "two"),
        h("li", "three"))),
      h("h2", "content title",  { style: {"background-color": "#f22"} }),
      h("p",
        "so it's just like a templating engine,\n",
        "but easy to use inline with Nim\n"),
      h("p",
        "the intention is for this to be used to create\n",
        "reusable, interactive HTML widgets. "))

(Currently, only literal arguments are supported, but this limitation will be removed in a future release, see #8 and #10.)

Installation

hyperscript works with Nim 1.2.6+ and can be installed using Nimble:

$ nimble install hyperscript

How does it work?

The basic design consists of compiling down to efficient calls to the DOM (through the dom standard library). As such, the following,

let example = h("p#example",
  h("input.name[value=Name]",
    style: {"background": "yellow"},
  ),
)

compiles roughly to

let example =
  let node = document.createElement("p")
  for attr in items([("id", "example")]):
    node.setAttribute(attr[0], attr[1])
  for child in items([
    let node = document.createElement("input")
    for attr in items([("value", "Name"), ("class", "name"), ("style", "background: yellow;")]):
      node.setAttribute(attr[0], attr[1])
    node]):
    node.appendChild(child)
  node

(We intend to unroll all loops in the future, see #6.)

When not compiling to JavaScript, XmlNode objects are generated using the xmltree standard library. Using the C backend, for instance, the example above compiles to

let example =
  newXmlTree("p", [
      newXmlTree("input", @[],
        {"value": "Name", "class": "name", "style": "background: yellow;"}.toXmlAttributes,
      ),
    ], {"id": "example"}.toXmlAttributes,
  )

Some references

Convert HTML snippets to hyperscript:

hyperscript's People

Contributors

schneiderfelipe avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

hyperscript's Issues

Scoped CSS

EDIT: CSS in Nim (JS).

Requirements:

  • Scoped
  • Server-side renderable
  • No inline styles (most of the time?)
  • Object style notation

Support indicating multiple classes at construction

With 70bb94d we lost the ability of indicating multiple classes at tag construction. That heavily simplifies the code, which is the correct behavior for the class attribute.

But having a way of indicating multiple classes is beneficial. Maybe using a special classList named parameter?

Support mutating objects

Use a composable API (like D3.js, jQuery, etc.).

EDIT: roadmap

  • #13
    • appending children
    • adding general attributes
    • inserting event listeners
    • stuff related to CSS selectors
      • adding classes
      • adding IDs
      • modifying tags?
  • things related to destroying nodes
    • removing children
    • removing attributes
    • removing event listeners?
  • unroll loops for not-so-large changes (up to eight items)
    • for children
    • for attributes
    • for events
  • tests for everything above

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.