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

@nrkn/h

v0.4.2

Published

Hyperscript-like DOM utils

Downloads

7

Readme

h

Hyperscript-like dom functions, TypeScript with no external dependencies.

Inspired by hyperscript, with some differences that cater to my personal preferences. The intention is to provide a convenient way to create elements without relying on JSX or template literals, primarily for my own usage. May not suit everyone's taste.

Please note, it has significant differences to hyperscript, and is not a drop-in replacement. See below for details.

Table of Contents

installation

npm install @nrkn/h

important notes

Expects document to exist in the global namespace - patch it with jsdom, html-element et al if using in Node.js or another runtime without a global document - see example in test fixture

basic usage

HTML

import { div, h1, p, attr } from '@nrkn/h'

const el = div(
  h1('Hello World'),
  p({ class: 'foo' }, 'Lorem ipsum dolor sit amet')
)

attr( el, { id: 'bar' }, { class: 'baz' } )

Or using h function:

import { h } from '@nrkn/h'

const el = h(
  'div',
  h('h1', 'Hello World'),
  h('p', { class: 'foo' }, 'Lorem ipsum dolor sit amet')
)

SVG

import { svg, circle, attr } from '@nrkn/h'

const circ = circle({ cx: 50, cy: 50, r: 40, fill: 'red' })

const svgRoot = svg(
  { viewBox: [ 0, 0, 100, 100 ] },
  circ  
)

attr( circ, { fill: 'blue' } )

Or using s function:

import { s } from '@nrkn/h'

const circ = s(
  'circle',
  { cx: 50, cy: 50, r: 40, fill: 'red' }
)

argument handling

The h function and all the helpers like div etc accept a variable number of arguments.

For h, the first argument is the tag name, the rest can be of mixed type, for helper functions all arguments can be of mixed type:

  • string - converted to text node and appended
  • number - converted to text node and appended
  • Node - appended
  • Record<string,any> - converted to attributes and applied to the element, with some special handling for style and dataset properties, and with function values treated as event handlers

attribute handling

All attribute values are converted to strings, unless they are:

  1. null, undefined or false - in which case the attribute is removed if it already exists
  2. true - in which case the attribute is set to an empty string
  3. An array, in which case it is joined with spaces eg:
// numbers are fine
const circ = s( 'circle', { r: 40 } )

console.log( circ.getAttribute( 'r' ) ) // '40'

h('div', { class: [ 'foo', 'bar' ] })

// array of class was joined with spaces
console.log( el.getAttribute( 'className' ) ) // 'foo bar'

// same here
attr( svgRootEl, { viewBox: [ 0, 0, 100, 100 ] } )

console.log( svgRootEl.getAttribute( 'viewBox' ) ) // '0 0 100 100'

style handling

The style property of the attributes object is treated specially, and can be either a string or an object. If it's a string, it's applied as is. If it's an object, it should be a Partial<CSSStyleDeclaration>, eg:

h('div', { style: { color: 'red', fontSize: '2em' } })

dataset handling

The dataset property of the attributes object is treated specially. It should be an object, and each key is converted to a data- attribute using the same rules as the regular DOM, eg:

const el = h('div', { dataset: { fooBar: 'baz' } })

console.log( el.getAttribute( 'data-foo-bar' ) ) // 'baz'

event handling

If an attribute value is a function, it's treated as an event handler, and attached to the element via addEventListener, eg:

const el = h('div', { click: () => console.log('clicked') })

differences to hyperscript

The main differences between h and hyperscript are:

h exports factory functions

h exports factory functions for each HTML/SVG element, eg:

import { div, h1 } from '@nrkn/h'

const someEl = div( h1('Hello World') )

Compared to hyperscript (note, h supports this syntax as well):

const someEl = h('div', h('h1', 'Hello World'))

h uses standard HTML attribute names

eg class instead of className, for instead of htmlFor etc.

I just prefer this, even though it's inconsistent with my handling of css styles. It is the opposite to how hyperscript works.

This is somewhat annoying in SVG, as you have to wrap property names that contain hyphens in quotes - maybe we will support either syntax in the future, depending on how many edge cases etc (SVG uses both camelCased and kekab-cased attribute names, and camelCased property names - what a mess)

h uses camelCased CSS property names

eg backgroundColor instead of background-color

Inconsistent with HTML attribute names, but I find it more convenient in TypeScript as you can use the CSSStyleDeclaration type to get autocompletion, and also you don't have to escape the property name with quotes. Again, this is the opposite to how hyperscript works.

We may support either syntax in the future.

h doesn't support CSS selector syntax

h doesn't support CSS selector syntax for creating elements that hyperscript allows (e.g., h('div#page', ...)).

h doesn't prefix event handlers with 'on'

For event handling, h uses event names without the "on" prefix (e.g., click instead of onclick).

h doesn't support arrays of children

It could, I just never needed it as you can spread an array of children easily enough:

const children: HTMLElement[] = [ ... ]

const el = div( { id: 'foo' }, ...children, anotherChild, etc )

h doesn't patch the global window or document

It has no external dependencies, so unlike hyperscript it doesn't patch the global window or document objects for you if you use it outside the browser, you will need to patch it yourself as described above.

license

MIT License

Copyright (c) 2023 Nik Coughlin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.