npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

h-html

v1.1.0

Published

A library to quickly create virtual nodes for Composi using HTML tag names instead of Composi's h function.

Downloads

3

Readme

h-html

A library to quickly create virtual nodes for Composi using HTML tag names instead of Composi's h function. It enables you to use a simpler syntax for hyperscript using tag names. This is a great choice for those who do not like using JSX for defining component markup.

Installation

Open your terminal and cd to your project. Then run:

npm i -D h-html

Call Singature

All tag methods take two arguments:

  1. An object for properties/attributes
  2. A child or array of children

If the tag has no properties, just provide an empty object: {}. You could also pass null, but two curly braces is shorter.

In the example below we pass an empty object for properties and a string as the child.

h1({}, 'The Title')

This will result in:

<h1>The Title</h1>

Here we give the tag a class and a title attribute:

h1({class: 'main-title', title: 'This is the title'}, 'The Title')

This will result in:

<h1 class='main-title' title='This is the title'>The Title</h1>

Import All Tags

Since this uses ES6 imports, if you need to use a lot of tags, you can simplify the import using an alias:

import * as tag from 'h-html'

function title(message) {
  return tag.nav(
    {},
    tag.h1({}, 'The Title')
  )
}

function list() {
  return tag.ul(
    {class: 'list'},
    [
      tag.li({}, 'Item One'),
      tag.li({}, 'Item Two'),
      tag.li({}, 'Item Three')
    ]
  )
}

Use with Composi

After installing h-html you can start using it with Composi. Below is an example:

import {h, render} from 'composi'
import {div, nav, h1, ul, li} from 'h-html'

// Define the title:
function title(message) {
  return nav({}, h1({}, `Hello, ${message}!`))
}
// Render the tag:
render(title('World'), 'header')

//******//
// List //
//******//

// Data for list component:
const fruits = ['Apples', 'Oranges', 'Bananas']

// Create list items:
function listItems(data) {
  return data.map(item => li({}, item))
}

// Define list component.
// Pass it listItems function:
function list(data) {
  return ul(
    {class: 'list'},
    listItems(data)
  )
}

// Render list:
render(list(fruits), 'section')

Notice how we had to break out the list items as a separate function. With JSX you can include an array with map inside curly braces. With hyperscript functions you need to define a separate function to do that. That's because h is expecting either a primitive value as a child (string, number, boolean), or an array of children.

ES6

You could refactor the above example to use ES6 arrow functions for more concise code:

import {h, render} from 'composi'
import {div, nav, h1, ul, li} from 'h-html'

// Define the title:
const title = message => nav({}, h1({}, `Hello ${message}`))

// Render the tag:
render(title('World'), 'header')

//******//
// List //
//******//

// Data for list component:
const fruits = ['Apples', 'Oranges', 'Bananas']

// Create list items:
const listItems = data => data.map(item => li({}, item))

// Define list component.
// Pass it listItems function.
const list = data => ul(
  {class: 'list'},
  listItems(data)
)

// Render list:
render(list(fruits), 'section')