Socket
Book a DemoInstallSign in
Socket

nanocomponent-adapters

Package Overview
Dependencies
Maintainers
24
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nanocomponent-adapters

Adapters to make nanocomponent run natively inside frameworks

latest
Source
npmnpm
Version
3.0.0
Version published
Weekly downloads
1
Maintainers
24
Weekly downloads
 
Created
Source

nanocomponent-adapters stability

npm version build status downloads js-standard-style

Adapters to make nanocomponent run natively inside frameworks. This allows you to write highly performant components once, and reuse them between all frameworks.

Table of Contents

Not all languages and frameworks are supported yet; PRs to support more frameworks support are very welcome!

  • React / Preact
  • Choo
  • Angular
  • Ember
  • Cycle
  • Vue
  • Inferno
  • Custom Elements (webcomponents)
  • Elm

React / Preact

var toReact = require('nanocomponent-adapters/react')
var Nanocomponent = require('nanocomponent')
var reactDom = require('react-dom')
var react = require('react')
var html = require('bel')

class Button extends Nanocomponent {
  constructor () {
    super()
    this.color = null
  }

  handleClick () {
    console.log('choo choo!')
  }

  createElement ({color}) {
    this.color = color
    return html`
      <button onclick=${this.handleClick} style="background-color: ${color}">
        Click Me
      </button>
    `
  }

  update ({color}) {
    return color !== this.color
  }
}

var ReactButton = toReact(Button, react)
reactDom.render(<ReactButton color='white' />, mountNode)

It's very similar with Preact, or any other React-like library that exposes a Component base class and a createElement function:

var preact = require('preact')
var PreactButton = toReact(Button, preact)
preact.render(<PreactButton color='hotpink' />, document.body)

Choo

Choo just works™.

var Nanocomponent = require('nanocomponent')
var html = require('choo/html')
var choo = require('choo')

// create new nanocomponent
class Button extends Nanocomponent {
  constructor () {
    super()
    this.color = null
  }

  handleClick (color) {
    console.log('choo choo!')
  }

  createElement (color) {
    this.color = color
    return html`
      <button onclick=${this.handleClick} style="background-color: ${color}">
        Click Me
      </button>
    `
  }

  update (color) {
    return color !== this.color
  }
}

var app = choo()
app.route('/', mainView)
app.mount('body')

var customButton = new Button ()

function mainView (state, emit) {
  return html`
    <section>
      ${customButton.render('blue')}
    </section>
  `
}

See Also

License

MIT

Keywords

nanocomponent

FAQs

Package last updated on 05 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts