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

byo

v2.1.2

Published

byo is like choo without a renderer

Downloads

15

Readme

byo

API stability NPM version Build Status Standard Size

byo or bring your own is like choo without a renderer.

Example

Clone of the choo example, but we bring bel, nanomount, and nanomorph ourselves.

var byo = require('byo')
var html = require('bel')
var nanomount = require('nanomount')
var nanomorph = require('nanomorph')

var app = byo({
  mount: handleMount,
  render: handleRender
})

app.use(logger)
app.use(countStore)
app.route('/', mainView)
app.mount('body')

function handleMount (tree, root) {
  nanomount(root, tree)
  return root
}

function handleRender (tree, newTree, root) {
  return nanomorph(tree, newTree)
}

function mainView (state, emit) {
  return html`
    <body>
      <h1>count is ${state.count}</h1>
      <button onclick=${onclick}>Increment</button>
    </body>
  `

  function onclick () {
    emit('increment', 1)
  }
}

function logger (state, emitter) {
  emitter.on('*', function (messageName, data) {
    console.log('event', messageName, data)
  })
}

function countStore (state, emitter) {
  state.count = 0
  emitter.on('increment', function (count) {
    state.count += count
    emitter.emit('render')
  })
}

API

Under the hood, byo is essentially a fork of choo. At the moment, we'll keep byo in API parity with choo. Please refer to the choo documentation for details on routing, events, and the architecture in general. The only thing we need to document here is...

Additional Options

A couple additional options are provided for defining render methods, alongside all the supported choo options. While we refer to them as options, mount and render are required since byo has no render methods by default.

opts.mount

required | Mount the tree onto the root. Typically you will return the tree or root. Example:

function handleMount (tree, root) {
  nanomount(root, tree)
  return root
}

opts.render

required | Render the new tree. tree, newTree, and root are available for morphing. Example:

function handleRender (tree, newTree, root) {
  return nanomorph(tree, newTree)
}

opts.toString

Convert html (or vdom) to string. You'll usually define this method for server rendering. Example:

function handleToString (html) {
  return html.toString()
}

Why does this exist?

If you like choo use choo, it's rad.

But sometimes bel, nanomount, or nanomorph might not fit the needs of a project. Maybe you like virtual dom but you still want to build your apps choo-style. byo is the back-pocket tool for those scenarios. I'm maintaining this project because I currently have a need for nested components which we can't quite yet pull off with nanocomponent, so I opt for preact + hyperx.

Renderers

Here are some examples of renderers with tagged template string implementations you can byo:

virtual-dom

var vdom = require('virtual-dom')
var hyperx = require('hyperx')
var html = hyperx(vdom.h)
var rootNode

function handleMount (tree, root) {
  rootNode = vdom.create(tree)
  root.appendChild(rootNode)
  return tree
})

function handleRender (tree, newTree, root) {
  var patches = vdom.diff(tree, newTree)
  rootNode = vdom.patch(rootNode, patches)
  return newTree
})

Inferno

var Inferno = require('inferno')
var hyperx = require('hyperx')
var html = hyperx(require('inferno-create-element'))

function handleMount (tree, root) {
  return Inferno.render(tree, root)
})

function handleRender (tree, newTree, root) {
  return Inferno.render(newTree, root)
})

Preact

var preact = require('preact')
var hyperx = require('hyperx')
var html = hyperx(preact.h)

function handleMount (tree, root) {
  return preact.render(tree, root)
})

function handleRender (tree, newTree, root) {
  return preact.render(newTree, root, tree)
})

React

var React = require('react')
var ReactDOM = require('react-dom')
var hyperx = require('hyperx')
var html = hyperx(React.createElement)

function handleMount (tree, root) {
  return ReactDOM.render(tree, root)
})

function handleRender (tree, newTree, root) {
  return ReactDOM.render(newTree, root)
})

Snabby

var html = require('snabby')

function handleMount (tree, root) {
  html.update(root, tree)
  return tree
})

function handleRender (tree, newTree, root) {
  html.update(tree, newTree)
  return newTree
})

Fine Print

Thanks Yoshua Wuyts and the rest of the choo team (s/o dat) for the continual awsm work 🙏