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

@rjaros/snabbdom-virtualize

v1.0.0-beta.5

Published

Library for turning strings and DOM nodes into virtual DOM nodes compatible with snabbdom.

Downloads

842

Readme

snabbdom-virtualize Build Status

Library for turning strings and DOM nodes into virtual DOM nodes compatible with Snabbdom.

API

virtualize(nodes, options)

  • nodes: Element|String - Either a DOM Element or a string of HTML to turn into a set of virtual DOM nodes.
  • options: Object - A hash of options to pass into the virtualize call. Available options are currently:
    • context: Document - An alternative DOM document to use (default is window.document).
    • hooks: Object - An object specifying hooks to call during the virtualization process. See the hooks section below.

Usage

Add it to your application with

npm install --save snabbdom-virtualize

Require/import it.

// ES6
import virtualize from 'snabbdom-virtualize';

// Require.
let virtualize = require('snabbdom-virtualize').default;

Pass it a set of DOM nodes or a string representing DOM nodes with one root node.

// Actual DOM nodes
let topNode = document.createElement('div');
let textNode = document.createTextNode('Click ');
let linkNode = document.createElement('a');
linkNode.setAttribute('href', 'http://example.com');
linkNode.textContent = 'here';
topNode.appendChild(textNode);
topNode.appendChild(linkNode);
let vnode = virtualize(topNode);


// String
let vnode = virtualize('<div>Click <a href="http://example.com">here</a></div>');

Specifying a different document

You can specify a different DOM document (other than the default window.document in a browser) by passing a context option into your calls to virtualize. This will allow for usage in a server-side setting, where there is no browser. One option is to use jsdom to create a DOM document that can be used:

const virtualize = require('snabbdom-virtualize').default;
const jsdom = require('jsdom').jsdom;
virtualizeString('<div>Click <a href="http://example.com">here</a></div>', {
  context: jsdom('<html></html>')
});

Using modules à la carte

If you'd prefer to import just the function for virtualizing DOM nodes or just the function for virtualizing HTML strings, you're in luck. Just import snabbdom-virtualize/nodes or snabbdom-virtualize/strings and use in the same way:

// DOM nodes.
import virtualize from 'snabbdom-virtualize/nodes';

let topNode = document.createElement('div');
let textNode = document.createTextNode('Click ');
let linkNode = document.createElement('a');
linkNode.setAttribute('href', 'http://example.com');
linkNode.textContent = 'here';
topNode.appendChild(textNode);
topNode.appendChild(linkNode);
let vnode = virtualize(topNode);


// HTML strings.
import virtualize from 'snabbdom-virtualize/strings';

let vnode = virtualize('<div>Click <a href="http://example.com">here</a></div>');

Hooks

You can register a create hook with any of the virtualize functions. This will be called once for each vnode that was created. It's called after the virtualization process is completed. The function receives one argument - the VNode that was created.

// The function passed as the 'create' hook is called 3 times: once for the
// <div>, once for the <span> and once for the text node inside the <span>.
virtualize("<div><span>Hi!</span></div>", {
    hooks: {
        create: function(vnode) { ... }
    }
});

Hooks allow you to perform some operations on your VNodes after virtualization but before patching with snabbdom.

Project setup

Written in ES6, compiled using Babel. To get started:

npm install
npm run build

This will output compiled files in the lib directory.

Tests

Tests can be run with npm test.