Skip to content

fsladkey/hiccup.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hiccup.js

Hiccup is a javascript library inspired by the awesome clojure library hiccup. Hiccup takes nested arrays and returns HTML, or React elements.

Generating HTML

import { html } from 'hiccup'

=> html(["script"])
"<script></script>"
=> html(["p"])
"<p />"
=> html(
  ["section", { class: "container" },
    ["h1", { class: "banner" }, "Title"],
    ["article",
      ["p", "content 1"]]]
)
"<section class=\"container\"><h1 class=\"banner\">Title</h1><article><p>content 1</p></article></section>"

The same data can be passed to the react method to generate react elements, allowing readable react to be written without JSX.

import React from 'react'
import { renderElement } from 'hiccup'
import Header from './Header'
// hiccup takes a 'createElement' function
// so that you can swap out React for Preact or anything else with the same API.
const r = renderElement(React.createElement)

// no JSX
function MyComponent({ title, content, items }) {
  return r(
    ["section", { class: "container" },
      [Header, { title }],
      ["article",
        ["p", content],
        ["ul",
          items.map(item =>
            ["li", { key: item.id }, item.content]
          )]]]
  )
}

About

HTML/React element creation inspired by the clojure library of the same name.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published