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

snabbdom-edge

v1.2.2

Published

The I/O for snabbdom (write HTML on server and read DOM on client).

Downloads

7

Readme

Snabbdom Edge

npm version npm downloads Build Status

This package includes two functions, which useful for client-server applications built with snabbdom. Like the snabbdom this library is focused on simplicity, modularity, powerful features and performance.

Modules

This package implements next snabbdom modules:

  • Element attributes (snabbdom-edge/component/attributes.js)
  • Embedded styles (snabbdom-edge/component/style.js)
  • Toggleable classes (snabbdom-edge/component/class.js)
  • Dataset attributes (snabbdom-edge/component/dataset.js)

The element properties module (snabbdom-edge/component/props.js) does not implemented here because properties exists exclusively on client-side.

Write HTML

On the server side you usually want produce HTML output from virtual nodes. The writeHTML function does this thing for you.

The supported features includes attributes, style and class.

The another way to do this is using snabbdom-to-html package. This package is a simple and clean alternative for it.

Typical usage:

TypeScript:


import {init as initHTMLWriter} from 'snabbdom-edge/write-html';
import {classModule} from 'snabbdom-edge/write-html/class';
import {styleModule} from 'snabbdom-edge/write-html/style';
import {datasetModule} from 'snabbdom-edge/write-html/dataset';
import {attributesModule} from 'snabbdom-edge/write-html/attributes';

// Init HTML render function with all available modules
const writeHTML = initHTMLWriter([
  classModule, // makes it support toggled classes
  // the props is only on client-side
  styleModule, // handles styling on elements
  datasetModule, // handles data-attributes
  attributesModule // handles all other attributes
]);

// in request handler we generate virtual DOM
let vnode = render_tree();

// write HTML to response body directly to use lesser RAM
writeHTML(vnode, response);

// OR generate HTML string to write
const html = writeHTML(vnode);
response.write(html);

JavaScript:


// Init HTML render function with all available modules
var writeHTML = require('snabbdom-edge/write-html').init([
  require('snabbdom-edge/write-html/class').default, // makes it support toggled classes
  // the props is only on client-side
  require('snabbdom-edge/write-html/style').default, // handles styling on elements
  require('snabbdom-edge/write-html/dataset').default, // handles data-attributes
  require('snabbdom-edge/write-html/attributes').default // handles all other attributes
]);

// in request handler we generate virtual DOM
var vnode = render_tree();

// write HTML to response body directly to use lesser RAM
writeHTML(vnode, response);

// OR generate HTML string to write
var html = writeHTML(vnode);
response.write(html);

Read DOM

On the client side you may read initial DOM before first patch when initializing your application. The readDOM function does this thing.

This piece of code has been ported from snabbdom-virtualize with key differences:

  1. Event listeners has been removed because this is useless for our purposes.
  2. Decoding HTML entities using html-entities package instead of DOM API calls.
  3. Added support of the single text child in non-text vnode.

Usage example:

TypeScript:


import {init as initDOMReader} from 'snabbdom-edge/read-dom';
import {classModule} from 'snabbdom-edge/read-dom/class';
import {styleModule} from 'snabbdom-edge/read-dom/style';
import {datasetModule} from 'snabbdom-edge/read-dom/dataset';
import {attributesModule} from 'snabbdom-edge/read-dom/attributes';

// Init DOM reader function with all available modules
const readDOM = initDOMReader([
  classModule, // makes it support toggled classes
  // the props is only on client-side
  styleModule, // handles styling on elements
  datasetModule, // handles data-attributes
  attributesModule() // handles all other attributes
]);

//before starting application on client
// we would like to read initial DOM

// now we can read global DOM node (i.e. <html>)
const _vnode = readDOM(document.documentElement);

// OR we can read only <body> as soon
const _vnode = readDOM(document.body);

// render the first virtual DOM
const vnode = render_tree();

// initiate the first patch
patch(_vnode, vnode);

JavaScript:


// Init DOM reader function with all available modules
var readDOM = require('snabbdom-edge/read-dom').init([
  require('snabbdom-edge/read-dom/class'), // makes it support toggled classes
  // the props is only on client-side
  require('snabbdom-edge/read-dom/style'), // handles styling on elements
  require('snabbdom-edge/read-dom/dataset'), // handles data-attributes
  require('snabbdom-edge/read-dom/attributes')() // handles all other attributes
]);

//before starting application on client
// we would like to read initial DOM

// now we can read global DOM node (i.e. <html>)
var _vnode = readDOM(document.documentElement);

// OR we can read only <body> as soon
var _vnode = readDOM(document.body);

// render the first virtual DOM
var vnode = render_tree();

// initiate the first patch
patch(_vnode, vnode);

Attributes module options

As you seen above the attributes module instantiated by calling as function. It implemented so in order to have possibility of transferring options to it. Available options is below:

  • Use style: true to read style attribute to attrs field.
  • Use class: true to read class attribute to attrs field.
  • Use dataset: true to read data- attributes to attrs field.

TypeScript:


import {init as initDOMReader} from 'snabbdom-edge/read-dom';
import {attributesModule} from 'snabbdom-edge/read-dom/attributes';

// Init DOM reader function with all available modules
const readDOM = initDOMReader([
  attributesModule({ // handles all attributes
    style: true,
    class: true,
    dataset: true
  })
]);
  

JavaScript:


// Init DOM reader function with all available modules
const readDOM = require('snabbdom-edge/read-dom').init([
  require('snabbdom-edge/read-dom/attributes').default({ // handles all attributes
    style: true,
    class: true,
    dataset: true
  })
]);