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

snabby

v6.1.1

Published

Use Snabbdom with template strings

Downloads

341

Readme

snabby

Use Snabbdom with template strings

tests

import html from 'snabby'

// Create vnodes:
let foo = html`<div>Hello Earth</div>`
let bar = html`<div>Hello Mars</div>`

// Patch to DOM:
html.update(document.body, foo)

// Patch updates:
html.update(foo, bar)

Snabby is for creating Snabbdom virtual nodes using template strings, and also patch the nodes using an update function (inspired by yo-yo). It makes working with an amazing virtual dom very easy and fun

Installation

Snabby version 2.x is a pure es module. It requires node >= v12.17 or a browser that supports the es module format.

If you want the older commonjs build, use the [email protected] npm module

Usage

snabby

A tag function that creates a node. This function is usually required as html instead of snabby:

import html from 'snabby'

// Function to create VNode from params:
let greet = name => html`
  <div class='greet'>Hello, ${name}!</div>
`

let node1 = greet('Jamen')
// Hello, Jamen!

let node2 = greet('World')
// Hello, World!

You have all the modules documented by Snabbdom loaded by default. See Directives for how to use modules, and snabby/create for loading custom modules

Directives

Directives are attributes that begin with @, and let you interact with Snabbdom modules. In general, the form is @<name>:[prop]:....

Here is an example using the props module:

html`<a @props=${{ href: '/foo', textContent: 'Hello' }}></a>`

// Or using the `:prop` syntax:
html`<a @props:href='/foo' @props:textContent='Hello'></a>`

For the eventlisteners module, you can use a shorthand by prefixing with : instead of @on::

html`<div @on:click=${fn}>...</div>`

// Shorthand:
html`<div :click=${fn}>`

Directives work with any module that makes use of node.data. For example @props:href turns into node.data.props.href.

Delayed Style properties

Snabbdom offers delayed style properties which are set after the next frame.

This makes it easy to declaratively animate the entry of elements.

The all value of transition-property is not supported in snabbdom or snabby.

usage:

html```<span @style:transition="opacity 1s ease"
             @style:delayed=${ { opacity: '0' } }>
         hello, world!!
       </span>```

snabby.update(target, node)

If you want to put a node on the DOM, or push updates on it (i.e. from events), you use this function.

First things first, the Node has to be mounted to the DOM, before you try and update it:

import html from 'snabby'

let visit = location => html`
  <div class='app'>Hello, ${location}!</div>
`

let node1 = visit('Earth')

// Mount node to DOM
html.update(document.body, node1)
// Hello, Earth!

From there, you can patch updates:

let node2 = visit('TRAPPIST-1')

// Patch updates to node1
html.update(node1, node2)
// Hello, TRAPPIST-1!

snabby/create

Create a snabby tag function with your own modules.

Here is an equivalent to snabby for example:

import create                   from './create.js'
import { attributesModule }     from 'https://cdn.jsdelivr.net/npm/snabbdom@3/build/package/modules/attributes.js';
import { classModule }          from 'https://cdn.jsdelivr.net/npm/snabbdom@3/build/package/modules/class.js';
import { propsModule }          from 'https://cdn.jsdelivr.net/npm/snabbdom@3/build/package/modules/props.js';
import { styleModule }          from 'https://cdn.jsdelivr.net/npm/snabbdom@3/build/package/modules/style.js';
import { eventListenersModule } from 'https://cdn.jsdelivr.net/npm/snabbdom@3/build/package/modules/eventlisteners.js';


const html = create([
    attributesModule,
    eventListenersModule,
    classModule,
    propsModule,
    styleModule
])

As mentioned, you can use directives with 3rd party modules fine. Open an issue if you can't!

snabby.thunk(selector, key, renderFn, [stateArguments])

The thunk function takes a selector, a key for identifying a thunk, a function that returns a vnode and a variable amount of state parameters. If invoked, the render function will receive the state arguments.

The renderFn is invoked only if the renderFn is changed or [stateArguments] array length or it's elements are changed.

function counter (count) {

    function numberView (n) {
        return html`<span>Number is ${n}</span>`
    }

    function rand () {
        const randomInt = Math.ceil(Math.random() * 3)
        html.update(view, counter(randomInt))
    }

    const view = html`
        <div class='main'>
          <span>${html.thunk('num', numberView, [count])}</span>
          <button @on:click=${rand}>random</button>
        </div>`
}

This is identical to snabbdom's thunk function. See https://github.com/snabbdom/snabbdom#thunks for more details.

Prior Art

These ideas come from my time using:

  • yo-yo: Inspired some of the API here
  • hyperx: Handles the template string parsing here
  • choo: What inspired me to create this module, as I love the API, but not morphdom as much
  • bel: Notable mention. It's like twin sister to this. DOM and VDOM
  • snabbdom: What gives this the speed
  • vue: A front-end framework that uses snabbdom and loosely inspired me

License

MIT © Jamen Marz


version travis downloads/month downloads license support me follow