All Projects → choojs → Nanohtml

choojs / Nanohtml

Licence: mit
🐉 HTML template strings for the Browser with support for Server Side Rendering in Node.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Nanohtml

Elementx
⚡️ Functionally create DOM elements and compose them to a tree quickly
Stars: ✭ 62 (-90.48%)
Mutual labels:  element, frontend, dom
Nipplejs
🎮 A virtual joystick for touch capable interfaces.
Stars: ✭ 1,313 (+101.69%)
Mutual labels:  frontend, dom
Crab
JavaScript library for building user interfaces with Custom Elements, Shadow DOM and React like API
Stars: ✭ 22 (-96.62%)
Mutual labels:  frontend, dom
Angular Ru Interview Questions
Вопросы на собеседовании по Angular
Stars: ✭ 224 (-65.59%)
Mutual labels:  frontend, dom
Zeroclipboard
The ZeroClipboard library provides an easy way to copy text to the clipboard using an invisible Adobe Flash movie and a JavaScript interface.
Stars: ✭ 6,650 (+921.51%)
Mutual labels:  frontend, dom
Interview Problem Summary
🎤 Prepare for the interviews and sum up the most popular interview problems for front-end(HTML/CSS/Javascript), Web development, full-stack. Also did some typical coding practice questions, such as UI caculator
Stars: ✭ 112 (-82.8%)
Mutual labels:  frontend, dom
Marko
A declarative, HTML-based language that makes building web apps fun
Stars: ✭ 10,796 (+1558.37%)
Mutual labels:  frontend, dom
Mogwai
The minimalist, obvious, graphical, web application interface
Stars: ✭ 249 (-61.75%)
Mutual labels:  frontend, dom
ready
Detect element availability on the initial page load and those dynamically appended to the DOM
Stars: ✭ 77 (-88.17%)
Mutual labels:  dom, element
Snuggsi
snuggsi ツ - Easy Custom Elements in ~1kB
Stars: ✭ 288 (-55.76%)
Mutual labels:  frontend, dom
Displayjs
A simple JavaScript framework for building ambitious UIs 😊
Stars: ✭ 590 (-9.37%)
Mutual labels:  frontend, dom
Ply
CSS inspection aided by visual regression pruning
Stars: ✭ 370 (-43.16%)
Mutual labels:  frontend, dom
Imba
🐤 The friendly full-stack language
Stars: ✭ 5,434 (+734.72%)
Mutual labels:  frontend, dom
Redux Bundler
Compose a Redux store out of smaller bundles of functionality.
Stars: ✭ 579 (-11.06%)
Mutual labels:  frontend
Awesome Svelte Resources
[deprecated for svelte-society/sveltesociety.dev] useful resources for Svelte v3+
Stars: ✭ 607 (-6.76%)
Mutual labels:  frontend
Fes.js
Fes.js 是一套优秀的中后台前端解决方案。提供初始项目、开发调试、Mock接口、编译打包的命令行工具。内置布局、权限、数据字典、状态管理、存储、Api等多个模块。以约定、配置化、组件化的设计思想,让用户仅仅关心用组件搭建页面内容。基于Vue.js,上手简单。经过多个项目中打磨,趋于稳定。
Stars: ✭ 579 (-11.06%)
Mutual labels:  frontend
Jailer
Database Subsetting and Relational Data Browsing Tool.
Stars: ✭ 576 (-11.52%)
Mutual labels:  frontend
New Bee
开源社区 vue + springBoot - 前后分离微服务的最佳实践
Stars: ✭ 619 (-4.92%)
Mutual labels:  element
Frontend Cheat Sheets
Collection of cheat sheets(HTML, CSS, JS, Git, Gulp, etc.,) for your frontend development needs & reference
Stars: ✭ 604 (-7.22%)
Mutual labels:  frontend
Full Stack Fastapi Postgresql
Full stack, modern web application generator. Using FastAPI, PostgreSQL as database, Docker, automatic HTTPS and more.
Stars: ✭ 7,635 (+1072.81%)
Mutual labels:  frontend

nanohtml

npm version build status downloads js-standard-style

HTML template strings for the Browser with support for Server Side Rendering in Node.

Installation

$ npm install nanohtml

Usage

Browser

var html = require('nanohtml')

var el = html`
  <body>
    <h1>Hello planet</h1>
  </body>
`

document.body.appendChild(el)

Node

Node doesn't have a DOM available. So in order to render HTML we use string concatenation instead. This has the fun benefit of being quite efficient, which in turn means it's great for server rendering!

var html = require('nanohtml')

var el = html`
  <body>
    <h1>Hello planet</h1>
  </body>
`

console.log(el.toString())

Node with custom DOM

Modules like jsdom implement (parts of) the DOM in pure JavaScript. If you don't really need the performance of string concatenation, or use nanohtml components that modify the raw DOM, use nanohtml/dom to give nanohtml a custom Document.

var JSDOM = require('jsdom').JSDOM
var nanohtml = require('nanohtml/dom')
var jsdom = new JSDOM()

var html = nanohtml(jsdom.window.document)
var el = html`
  <body>
    <h1>Hello planet</h1>
  </body>
`
el.appendChild(html`<p>A paragraph</p>`)

el.outerHTML === '<body><h1>Hello planet</h1><p>A paragraph</p></body>'

Interpolating unescaped HTML

By default all content inside template strings is escaped. This is great for strings, but not ideal if you want to insert HTML that's been returned from another function (for example: a markdown renderer). Use nanohtml/raw for to interpolate HTML directly.

var raw = require('nanohtml/raw')
var html = require('nanohtml')

var string = '<h1>This a regular string.</h1>'
var el = html`
  <body>
    ${raw(string)}
  </body>
`

document.body.appendChild(el)

Attaching event listeners

var html = require('nanohtml')

var el = html`
  <body>
    <button onclick=${onclick}>
      Click Me
    </button>
  </body>
`

document.body.appendChild(el)

function onclick (e) {
  console.log(`${e.target} was clicked`)
}

Multiple root elements

If you have more than one root element they will be combined with a DocumentFragment.

var html = require('nanohtml')

var el = html`
  <li>Chashu</li>
  <li>Nori</li>
`

document.querySelector('ul').appendChild(el)

Static optimizations

Parsing HTML has significant overhead. Being able to parse HTML statically, ahead of time can speed up rendering to be about twice as fast.

Browserify

From the command line

$ browserify -t nanohtml index.js > bundle.js

Programmatically

var browserify = require('browserify')
var nanohtml = require('nanohtml')
var path = require('path')

var b = browserify(path.join(__dirname, 'index.js'))
  .transform(nanohtml)

b.bundle().pipe(process.stdout)

In package.json

{
  "name": "my-app",
  "private": true,
  "browserify": {
    "transform": [
      "nanohtml"
    ]
  },
  "dependencies": {
    "nanohtml": "^1.0.0"
  }
}

Bundling

Webpack

At the time of writing there's no Webpack loader yet. We'd love a contribution!

Babel / Parcel

Add nanohtml to your .babelrc config.

Without options:

{
  "plugins": [
    "nanohtml"
  ]
}

With options:

{
  "plugins": [
    ["nanohtml", {
      "useImport": true
    }]
  ]
}

Options

  • useImport - Set to true to use import statements for injected modules. By default, require is used.
  • appendChildModule - Import path to a module that contains an appendChild function. Defaults to "nanohtml/lib/append-child".

Rollup

Use the @rollup/plugin-commonjs plugin with @rollup/plugin-node-resolve. Explicitly import the browser or server entrypoint in your application. E.g.:

import html from 'nanohtml/lib/browser';

Attributions

Shout out to Shama and Shuhei for their contributions to Bel, yo-yoify and pelo. This module is based on their work, and wouldn't have been possible otherwise!

See Also

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].