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

preact-hyperscript

v0.7.1

Published

Hyperscript-like syntax for creating Preact elements

Downloads

7

Readme

preact-hyperscript

Hyperscript-like syntax for creating Preact elements.

Build status NPM version Dependency Status License Js Standard Style

Installation

> npm install preact-hyperscript

Example

→ Try this example on Codepen.io!

const { createElement, div, h1, h2, h3, button, ul, li } = require('preact-hyperscript')
const { render } = require('preact')

const h = createElement

const App = ({ library }) => div([
  h1('.bold', library),
  h2('#subtitle', 'Preact is a fast, 3kb alternative to React, with the same ES2015 API'),
  button({ href: 'http://ghub.io/preact' }, 'Learn More'),
  h3('Features'),
  ul(['preact', 'small', 'es2015', 'fast'].map(key => li(key)))
])

render(
  h(App, { library: 'Preact' }),
  document.body
)

Guide

Component shorthand

Instead of calling createElement(Component, props) you can wrap your component into a createComponent call and invoke it using Component(props) directly:

const { createComponent, h1 } = require('preact-hyperscript')

const App = createComponent((props) => h1(props.text))

render(
  // instead of h(App, { text: 'test' }) you can do:
  App({ text: 'test' }),
  document.body
)

Syntax

Each element in the DOM is exposed as a function when requiring preact-hyperscript.

const { div, h1, p, button } = require('preact-hyperscript')

These functions have the following syntax:

tag(selector, attributes, children)

All arguments are optional with at least one argument needing to be present. This kind of function overloading allows you to iterate on your DOM structure really fast and reduce visual noise.

  • selector can be a class (prefixed with .) or an id (prefixed with a #). These can be mixed as you might expect: .title#id.pad.red
  • attributes is an object of dom attributes: { href: '#header' }
  • children can be a string for a text node or an array of nodes

Built-in sugar

Classes

Conditionally joins class names together. It utilizes JedWatson's awesome classnames. Visit the usage docs for more information.

Inline styles

Automatically converts style objects to a string. For an additional weight of ~400 bytes this is well worth it:

const style = {
  textDecoration: 'underline',
  fontSize: '56px'
}

const node = h1({ style }, 'hello!')
// -> <h1 style='text-decoration:underline;font-size:56px;'>hello!</h1>

Benchmarks

Some basic benchmarks for creating 10^5 nodes:

> npm run bench
native*100000: 31.481ms
hyperscript*100000: 114.727ms

Tests

> npm test

License

MIT