Skip to content

regular/hyperscript-nested-contexts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

hyperscript-nested-contexts

Hyperscript's context() function returns an object that implements the hyperscript API, but it misses a context() function itself. This module fixes this.

require('hyperscript').context().conext()

Example

const hs = require('hyperscript-nested-contexts')(require('hyperscript'))
const ho = require('hyperobj')
const observable = require('observable')

const render = ho(
  function(v) {
    if (typeof v!=='object') return
    const h = this.ctx || hs
    return h('ol',
      Object.keys(v).map( (k)=> {
        const msg = observable()
        return (function(h) {
          msg('clickme')
          return h('li', [
            h('em.key', h('span', msg), this.call(this, k), {
              onclick: function() {
                msg('I was clicked')
                h.cleanup()
              }
            }),
            h('span.value', this.call(this, v[k], k))
          ])
        }).call(this, this.ctx = h.context())
      })
    )
  },
  function(v) {
    const h = this.ctx || hs
    return h('span.string', v)
  }
)

document.body.appendChild(
  render(require('./package.json'))
)

(Clicking on a parent will remove event handlers and observables from children)

About

Create hyperscript contexts within hyperscript contexts and clean them up properly

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published