[
  {
    "path": ".gitignore",
    "content": "node_modules/\ncoverage/\ntmp/\nnpm-debug.log*\n.DS_Store\ndist\n.source*\n.vscode"
  },
  {
    "path": ".npmrc",
    "content": "package-lock=false\n"
  },
  {
    "path": ".travis.yml",
    "content": "dist: trusty\nlanguage: node_js\nnode_js:\n  - 'node'\nsudo: false\naddons:\n  apt:\n    packages:\n      - xvfb\ncache:\n  directories:\n    - ~/.npm\ninstall:\n  - export DISPLAY=':99.0'\n  - Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &\n  - npm i\nscript:\n  - npm test\n"
  },
  {
    "path": "CHANGELOG.md",
    "content": "# nanocomponent Change Log\nAll notable changes to this project will be documented in this file.\nThis project adheres to [Semantic Versioning](http://semver.org/).\n\n## 6.6.0 - 2021-04-12\n- You can modify makeID (https://github.com/choojs/nanocomponent/pull/96)\n\n## 6.5.3 - 2019-11-12\n- Fix `onclick` event name in readme example (https://github.com/choojs/nanocomponent/pull/82)\n- Add doc note about maintaining control of a component (https://github.com/choojs/nanocomponent/pull/89)\n- Point out the `createElement()` return value should be a single DOM node (https://github.com/choojs/nanocomponent/pull/93)\n\n## 6.5.2 - 2018-04-20\n- Allow SVGs as the root node (https://github.com/choojs/nanocomponent/pull/79)\n- Update deps\n\n## 6.5.1 - 2018-02-11\n- Update nanotiming@7.2.0\n- Update devdeps: tap-run, dependency-check, browserify, bankai\n\n## 6.4.6 - 2017-12-05\n- Proxy elements are created matching the root node returned from the `createElement` method. (🙏@tornqvist🙏)\n\n## 6.4.5 - 2017-12-03\n- Pin `on-load` to v3.3.4 to fix node import.\n\n## 6.4.4 - 2017-12-03\n- Pin `on-load` to v3.3.3 to fix unbundled electron import.\n\n## 6.4.3 - 2017-12-02\n- Pin `on-load` to 3.3.2 to fix unbundled electron import.\n\n## 6.4.1 - 2017-09-11\n- Fixed `afterreorder` hook typo.\n- Update `on-load` to handle `<head>` loading and for addded assertions.\n\n## 6.4.0 - 2017-09-04\n\n- **Added**: `.rerender()` method to allow re-rendering with the last rendered arguments if internal state changes.\n- Updated docs for `rerender`.\n- Add a few more pitfall pointers in the lifecycle API docs around rerendering in `beforerender`.\n\n## 6.3.0 - 2017-08-24\n\n- **Added**: Use [`nanoassert`](https://github.com/emilbayes/nanoassert) in the browser.\n\n## 6.2.0 - 2017-08-18\n\n- **Added**: `afterreorder` event which is called after your component is remounted on sibbling reorders.\n\n## 6.1.0 - 2017-08-14\n\n- **Added**: [nanotiming](https://github.com/choojs/nanotiming) timings.  You can name component instances and it will emit timing information. See [nanocomponent/pull/47](https://github.com/choojs/nanocomponent/pull/47)\n\n## 6.0.1 - 2017-08-09\n\n- **Fixed**: [[`f9f7540415`](https://github.com/choojs/nanocomponent/commit/f9f7540415)] - load & unload callbacks should be passed el (timwis)\n\n## 6.0.0 - 2017-08-09\n\n🎉 nanocomponent and [cache-component][cc] are merged into one module: `nanocomponent@6.0.0` 🎉.\n\nBe sure to read the README so that you get an understanding of the new API, but here is a quick summary of what has changed from the perspective of both modules:\n\n### Changes since `cache-component@5`\n\n`nanocomponent@6` is mostly the same as `cache-component@5` except a few methods are renamed and everything you interact with has had the `_` prefix removed.\n\n- **Breaking**: The `_element` [getter][getter] is renamed to `element`.\n- **Breaking**: `_willMount` is renamed to `beforerender` because DOM mounting can't be guaranteed from the perspective of a component.\n- **Breaking**: `_didMount` is removed.  Consider using `load` instead now.\n- **Breaking**: `_update` is renamed to `update` and should always be implemented.  Instead of the old default shallow compare, not implementing `update` throws.  You can `require('nanocomponent/compare')` to implement the shallow compare if you want that still.  See below.\n- **Breaking**: `_args` is removed.  `arguments` in `createElement` and `update` are already \"sliced\", so you can simply capture a copy in `update` and `createElement` and use it for comparison at a later time.\n- **Breaking**: `_willUpdate` is removed.  Anything you could do in `_willUpdate` you can just move to `update`.\n- **Changed**: `_didUpdate` is renamed to `afterupdate`.  It also receives an element argument `el` e.g. `afterupdate(el)`.  This makes its argument signature consistent with the other life-cycle methods.\n- **Added**: Added [on-load][ol] hooks `load` and `unload`.  [on-load][ol] listeners only get added when one or both of the hooks are implemented on a component making the mutation observers optional.\n\n\n#### `cache-component@5` to `nanocomponent@6` upgrade guide:\n\n- Renamed `_render` to `createElement`.\n- You must implement `update` now.  Rename existing `_update` method to `update`.  Here is an example of doing shallow compare on components that didn't implement their own update function previously:\n\n```js\nvar html = require('choo/html')\nvar Component = require('nanocomponent')\nvar compare = require('nanocomponent/compare')\n\nclass Meta extends Component {\n  constructor () {\n    super()\n\n    this.arguments = []\n  }\n\n  createElement (title, artist, album) {\n    this.arguments = arguments // cache a copy of arguments\n\n    return html`\n      <div>\n        <p>${title}</p>\n        <p>\n          ${artist} - ${album}\n        </p>\n      </div>\n    `\n  }\n\n  // Implement this to recreate cache-component@5\n  // behavior when update was not implemented\n  update () {\n    return compare(arguments, this.arguments)\n  }\n}\n\n```\n\n- Rename components with `_willMount` to `beforerender`\n- Move any `_didMount` implementations into `load` or a `window.requestAnmimationFrame` inside of `beforerender`.\n- Move any `_willUpdate` implementations into `update`.\n- Rename `_didUpdate` to `afterupdate`.\n- Take advantage of `load` and `unload` for DOM insertion aware node interactions 🙌\n\n### Changes since `nanocomponent@5`\n\n`nanocomponent@6` has some subtle but important differences from `nanocompnent@5`.  Be sure to read the README and check out the examples to get an understanding of the new API.\n\n- **Breaking**: The `_element` property is removed.  A [getter][getter] called `element` is now used instead.  Since this is now a read-only getter, you must not assign anything to this property or else bad things will happen.  The `element` getter returns the component's DOM node if mounted in the page, and `undefined` otherwise.  You are allowed to mutate that DOM node by hand however.  Just don't reassign the property on the component instance.\n- **Fixed**: Components can gracefully be removed, re-ordered and remounted between views.  You can even mutate the same component over individual instances.  This is an improvement over `nanocomponent@5`.\n- **Breaking**: `_render` is renamed to `createElement` and must now return a DOM node always.  In earlier versions you could get away with not returning from `_render` and assigning nodes to `_element`.  No longer!  Also, you should move your DOM mutations into `update`.\n- **Changed**: Update still works the same way: return true to run `createElement` or return false to skip a call to `createElement` when `render` is called.  If you decide to mutate `element` \"by hand\" on updates, do that here (rather than conditional paths inside `createElement`).\n- **Changed**: `_load` and `_unload` renamed to `load` and `unload`. They have always been optional, but now the mutation observers are only added if at least one of these methods are implemented prior to component instantiation.\n- **Added**: `beforerender` lifecycle hook.  Its similar to `load` but runs before the function call to `render` returns on unmounted component instances.  This is where the [on-load][ol] listeners are added and is a good opportunity to add any other lifecycle hooks.\n- **Added**: `afterupdate` runs after `update` returns true and the results of `createElement` is mutated over the mounted component.  Useful for adjusting scroll position.\n\n#### `nanocomponent@5` to `nanocomponent@6` upgrade guide:\n\n- Read through the new leaflet example to get an idea of the differences between the old and new API. 🗺\n- Renamed `_render` to `createElement` and `_update` to `update`.\n- Move any DOM mutation code from `createElement` into `update`.\n- Ensure `createElement` returns a DOM node always. (You will get warnings if you don't and it probably won't work)\n- Rename `_load` and `_unload` to `load` and `unload`.\n- Consider moving any `load` actions into `beforerender` if they don't depend on the newly rendered node being mounted in a DOM tree yet.\n- Take advantage of `afterupdate` allowing you to interact with your component after `createElement` is called on mounted components 🙌\n\n[ol]: https://github.com/shama/on-load\n[cc]: https://github.com/hypermodules/cache-component\n[nanohtml]: http://ghub.io/nanohtml\n[nm]: http://ghub.io/nanomorph\n[getter]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get\n"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2017 Yoshua Wuyts\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# nanocomponent [![stability][0]][1]\n\n[![npm version][2]][3] [![build status][4]][5]\n[![downloads][8]][9] [![js-standard-style][10]][11]\n\nNative DOM components that pair nicely with DOM diffing algorithms.\n\n## Features\n\n- Isolate native DOM libraries from DOM diffing algorithms\n- Makes rendering elements _very fast™_ by avoiding unnecessary rendering\n- Component nesting and state update passthrough\n- Implemented in only a few lines\n- Only uses native DOM methods\n- Class based components offering a familiar component structure\n- Works well with [nanohtml][nanohtml] and [yoyoify][yoyoify]\n- Combines the best of `nanocomponent@5` and [`cache-component@5`][cc].\n\n## Usage\n\n```js\n// button.js\nvar Nanocomponent = require('nanocomponent')\nvar html = require('nanohtml')\n\nclass Button extends Nanocomponent {\n  constructor () {\n    super()\n    this.color = null\n  }\n\n  createElement (color) {\n    this.color = color\n    return html`\n      <button style=\"background-color: ${color}\">\n        Click Me\n      </button>\n    `\n  }\n\n  // Implement conditional rendering\n  update (newColor) {\n    return newColor !== this.color\n  }\n}\n\nmodule.exports = Button\n```\n\n```js\n// index.js\nvar choo = require('choo')\nvar html = require('nanohtml')\n\nvar Button = require('./button.js')\nvar button = new Button()\n\nvar app = choo()\napp.route('/', mainView)\napp.mount('body')\n\nfunction mainView (state, emit) {\n  return html`\n    <body>\n      ${button.render(state.color)}\n    </body>\n  `\n}\n\napp.use(function (state, emitter) {\n  state.color = 'green'\n})\n```\n\n## Patterns\n\nThese are some common patterns you might encounter when writing components.\n\n### Standalone\n\nNanocomponent is part of the choo ecosystem, but works great standalone!\n\n```js\nvar Button = require('./button.js')\nvar button = new Button()\n\n// Attach to DOM\ndocument.body.appendChild(button.render('green'))\n\n// Update mounted component\nbutton.render('green')\nbutton.render('red')\n\n// Log a reference to the mounted dom node\nconsole.log(button.element)\n```\n\n### Binding event handlers as component methods\n\nSometimes it's useful to pass around prototype methods into other functions.\nThis can be done by binding the method that's going to be passed around:\n\n```js\nvar Nanocomponent = require('nanocomponent')\nvar html = require('nanohtml')\n\nclass Component extends Nanocomponent {\n  constructor () {\n    super()\n\n    // Bind the method so it can be passed around\n    this.handleClick = this.handleClick.bind(this)\n  }\n\n  handleClick (event) {\n    console.log('element is', this.element)\n  }\n\n  createElement () {\n    return html`<button onclick=${this.handleClick}>\n      My component\n    </button>`\n  }\n\n  update () {\n    return false // Never re-render\n  }\n}\n```\n\n### ES5 Syntax\n\nNanocomponent can be written using prototypal inheritance too:\n\n```js\nvar Nanocomponent = require('nanocomponent')\nvar html = require('nanohtml')\n\nfunction Component () {\n  if (!(this instanceof Component)) return new Component()\n  Nanocomponent.call(this)\n  this.color = null\n}\n\nComponent.prototype = Object.create(Nanocomponent.prototype)\n\nComponent.prototype.createElement = function (color) {\n  this.color = color\n  return html`\n    <div style=\"background-color: ${color}\">\n      Color is ${color}\n    </div>\n  `\n}\n\nComponent.prototype.update = function (newColor) {\n  return newColor !== this.color\n}\n```\n\n### Mutating the components instead of re-rendering\n\nSometimes you might want to mutate the element that's currently mounted, rather\nthan performing DOM diffing. Think cases like third party widgets that manage\nthemselves.\n\n```js\nvar Nanocomponent = require('nanocomponent')\nvar html = require('nanohtml')\n\nclass Component extends Nanocomponent {\n  constructor () {\n    super()\n    this.text = ''\n  }\n\n  createElement (text) {\n    this.text = text\n    return html`<h1>${text}</h1>`\n  }\n\n  update (text) {\n    if (text !== this.text) {\n      this.text = text\n      this.element.innerText = this.text   // Directly update the element\n    }\n    return false                           // Don't call createElement again\n  }\n\n  unload (text) {\n    console.log('No longer mounted on the DOM!')\n  }\n}\n```\n\nPlease note that if you remove a component from the DOM, it will be unloaded, and when reinserted into the DOM, `createElement` will be fired again. If you want to maintain control of a component's rendering, it has to stay mounted! See [issue #88](https://github.com/choojs/nanocomponent/issues/88) for a more detailed discussion.\n\n### Nested components and component containers\n\nComponents nest and can skip renders at intermediary levels.  Components can\nalso act as containers that shape app data flowing into view specific\ncomponents.\n\n```js\nvar Nanocomponent = require('nanocomponent')\nvar html = require('nanohtml')\nvar Button = require('./button.js')\n\nclass Component extends Nanocomponent {\n  constructor () {\n    super()\n    this.button1 = new Button()\n    this.button2 = new Button()\n    this.button3 = new Button()\n  }\n\n  createElement (state) {\n    var colorArray = shapeData(state)\n    return html`\n      <div>\n        ${this.button1.render(colorArray[0])}\n        ${this.button2.render(colorArray[1])}\n        ${this.button3.render(colorArray[2])}\n      </div>\n    `\n  }\n\n  update (state) {\n    var colorArray = shapeData(state) // process app specific data in a container\n    this.button1.render(colorArray[0]) // pass processed data to owned children components\n    this.button2.render(colorArray[1])\n    this.button3.render(colorArray[2])\n    return false // always return false when mounted\n  }\n}\n\n// Some arbitrary data shaping function\nfunction shapeData (state) {\n  return [state.colors.color1, state.colors.color2, state.colors.color3]\n}\n```\n\n## FAQ\n\n### What order do lifecycle events run in?\n\n![Lifecycle diagram](lifecycle.jpg)\n\n**Note:** `aftercreate` should actually say `afterupdate`.\n\nShoutout to [@lrlna](https://github.com/lrlna) for the excellent diagram.\n\n### Where does this run?\n\nNanocomponent was written to work well with [choo][choo], but it also works well\nwith DOM diffing engines that check `.isSameNode()` like [nanomorph][nm] and\n[morphdom][md].  It is designed and documented in isolation however, so it also\nworks well on it's own if you are careful.  You can even embed it in other SPA\nframeworks like React or Preact with the use of [nanocomponent-adapters][nca] which\nenable framework-free components! 😎\n\n### What's a proxy node?\n\nIt's a node that overloads `Node.isSameNode()` to compare it to another node.\nThis is needed because a given DOM node can only exist in one DOM tree at the\ntime, so we need a way to reference mounted nodes in the tree without actually\nusing them. Hence the proxy pattern, and the recently added support for it in\ncertain diffing engines:\n\n```js\nvar html = require('nanohtml')\n\nvar el1 = html`<div>pink is the best</div>`\nvar el2 = html`<div>blue is the best</div>`\n\n// let's proxy el1\nvar proxy = html`<div></div>`\nproxy.isSameNode = function (targetNode) {\n  return (targetNode === el1)\n}\n\nel1.isSameNode(el1)   // true\nel1.isSameNode(el2)   // false\nproxy.isSameNode(el1) // true\nproxy.isSameNode(el2) // false\n```\n\n### How does it work?\n\n[`nanomorph`][nm] is a diffing engine that diffs real DOM trees. It runs a series\nof checks between nodes to see if they should either be replaced, removed,\nupdated or reordered. This is done using a series of property checks on the\nnodes.\n\n[`nanomorph`][nm] runs `Node.isSameNode(otherNode)` when diffing two DOM trees. This\nallows us to override the function and replace it with a custom function that\nproxies an existing node. Check out the code to see how it works. The result is\nthat if every element in our tree uses `nanocomponent`, only elements that have\nchanged will be recomputed and re-rendered making things very fast.\n\n`nanomorph`, which saw first use in choo 5, has supported `isSameNode` since\nits conception. [`morphdom`][md] has supported `.isSameNode` since [v2.1.0][210].\n\n### Is this basically `react-create-class`?\n\n`nanocomponent` is very similar to `react-create-class`, but it leaves more decisions up\nto you.  For example, there is no built in `props` or `state` abstraction in `nanocomponent`\nbut you can do something similar with `arguments` (perhaps passing a single `props` object\nto `.render` e.g. `.render({ foo, bar })` and assigning internal state to `this` however\nyou want (perhaps `this.state = { fizz: buzz }`).\n\n## API\n\n### `component = Nanocomponent([name])`\n\nCreate a new Nanocomponent instance. Additional methods can be set on the\nprototype. Takes an optional name which is used when emitting timings.\n\n### `component.render([arguments…])`\n\nRender the component. Returns a proxy node if already mounted on the DOM. Proxy\nnodes make it so DOM diffing algorithms leave the element alone when diffing.  Call this when `arguments` have changed.\n\n### `component.rerender()`\n\nRe-run `.render` using the last `arguments` that were passed to the `render` call.  Useful for triggering component renders if internal state has changed.  Arguments are automatically cached under `this._arguments` (🖐 hands off, buster! 🖐).  The `update` method is bypassed on re-render.\n\n### `component.element`\n\nA [getter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get)\nproperty that returns the component's DOM node if its mounted in the page and\n`null` when its not.\n\n### `DOMNode = Nanocomponent.prototype.createElement([arguments…])`\n\n__Must be implemented.__ Component specific render function.  Optionally cache\nargument values here.  Run anything here that needs to run along side node\nrendering.  Must return a DOMNode. Use `beforerender` to run code after\n`createElement` when the component is unmounted.  Previously named `_render`.  Arguments passed to `render` are passed to `createElement`.  Elements returned from `createElement` must always return the same root node type.\n\n### `Boolean = Nanocomponent.prototype.update([arguments…])`\n\n__Must be implemented.__ Return a boolean to determine if\n`prototype.createElement()` should be called.  The `update` method is analogous to\nReact's `shouldComponentUpdate`. Called only when the component is mounted in\nthe DOM tree.  Arguments passed to `render` are passed to `update`.\n\n### `Nanocomponent.prototype.beforerender(el)`\n\nA function called right after `createElement` returns with `el`, but before the fully rendered\nelement is returned to the `render` caller. Run any first render hooks here. The `load` and\n`unload` hooks are added at this stage.  Do not attempt to `rerender` in `beforerender` as the component may not be in the DOM yet.\n\n### `Nanocomponent.prototype.load(el)`\n\nCalled when the component is mounted on the DOM. Uses [on-load][onload] under\nthe hood.\n\n### `Nanocomponent.prototype.unload(el)`\n\nCalled when the component is removed from the DOM. Uses [on-load][onload] under\nthe hood.\n\n### `Nanocomponent.prototype.afterupdate(el)`\n\nCalled after a mounted component updates (e.g. `update` returns true).  You can use this hook to call\n`element.scrollIntoView` or other dom methods on the mounted component.\n\n### `Nanocomponent.prototype.afterreorder(el)`\n\nCalled after a component is re-ordered.  This method is rarely needed, but is handy when you have a component\nthat is sensitive to temorary removals from the DOM, such as externally controlled iframes or embeds (e.g. embedded tweets).\n\n## Installation\n\n```sh\n$ npm install nanocomponent\n```\n\n## Optional lifecycle events\n\nYou can add even more lifecycle events to your components by attatching the following modules\nin the `beforerender` hook.\n\n- [yoshuawuyts/observe-resize](https://github.com/yoshuawuyts/observe-resize)\n- [bendrucker/document-ready](https://github.com/bendrucker/document-ready)\n- [yoshuawuyts/on-intersect](https://github.com/yoshuawuyts/on-intersect)\n- [yoshuawuyts/on-idle](https://github.com/yoshuawuyts/on-idle)\n\n## See also\n\n- [component-box][cb] - Dynamic component instance caching\n- [nanomap][nanomap] - Functional mapping into keyed component instances\n- [choojs/choo][choo]\n- [choojs/nanocomponent-adapters][nca]\n- [choojs/nanohtml](https://github.com/choojs/nanohtml)\n- [shama/on-load](https://github.com/shama/on-load)\n\n## Examples\n\n- [Bloomberg: What’s Inside All the iPhones](https://www.bloomberg.com/features/apple-iphone-guts/) (👏 [@jongacnik](https://github.com/jongacnik) 👏)\n- [twitter-component](https://github.com/bcomnes/twitter-component)\n- [youtube-component](https://github.com/bcomnes/youtube-component)\n- [Ara File Manager](https://ara.one/app) (Decentralized application built atop Electron)\n\n## Similar Packages\n\n- [shama/base-element](https://github.com/shama/base-element)\n- [yoshuawuyts/cache-element](https://github.com/yoshuawuyts/cache-element)\n- [yoshuawuyts/microcomponent](https://github.com/yoshuawuyts/microcomponent)\n- [hypermodules/cache-component](https://github.com/hypermodules/cache-component)\n- [rafaelrinaldi/data-components](https://github.com/rafaelrinaldi/data-components)\n\n## License\n\n[MIT](https://tldrlegal.com/license/mit-license)\n\n[0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square\n[1]: https://nodejs.org/api/documentation.html#documentation_stability_index\n[2]: https://img.shields.io/npm/v/nanocomponent.svg?style=flat-square\n[3]: https://npmjs.org/package/nanocomponent\n[4]: https://img.shields.io/travis/choojs/nanocomponent/master.svg?style=flat-square\n[5]: https://travis-ci.org/choojs/nanocomponent\n[8]: http://img.shields.io/npm/dm/nanocomponent.svg?style=flat-square\n[9]: https://npmjs.org/package/nanocomponent\n[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square\n[11]: https://github.com/feross/standard\n[nanohtml]: https://github.com/choojs/nanohtml\n[yoyoify]: https://github.com/shama/yo-yoify\n[md]: https://github.com/patrick-steele-idem/morphdom\n[210]: https://github.com/patrick-steele-idem/morphdom/pull/81\n[nm]: https://github.com/yoshuawuyts/nanomorph\n[ce]: https://github.com/yoshuawuyts/cache-element\n[class]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes\n[isSameNode]: https://github.com/choojs/nanomorph#caching-dom-elements\n[onload]: https://github.com/shama/on-load\n[choo]: https://github.com/choojs/choo\n[nca]: https://github.com/choojs/nanocomponent-adapters\n[cc]: https://github.com/hypermodules/cache-component\n[nanomap]: https://github.com/bcomnes/nanomap\n[cb]: https://github.com/jongacnik/component-box\n"
  },
  {
    "path": "compare.js",
    "content": "module.exports = compare\n\n// included for compatibility reasons only\n// implements a simple shallow compare for simple values in arrays\n\nfunction compare (array1, array2) {\n  var length = array1.length\n  if (length !== array2.length) return true\n\n  for (var i = 0; i < length; i++) {\n    if (array1[i] !== array2[i]) return true\n  }\n  return false\n}\n"
  },
  {
    "path": "example/index.js",
    "content": "// adapted from https://github.com/timwis/choo-leaflet-demo/blob/master/src/index.js\nvar microbounce = require('microbounce')\nvar html = require('choo/html')\nvar css = require('sheetify')\nvar log = require('choo-log')\nvar choo = require('choo')\nvar Leaflet = require('./leaflet.js')\n\ncss('leaflet')\n\nvar leaflet = new Leaflet()\nvar app = choo()\n\napp.use(log())\napp.use(store)\napp.route('/', mainView)\napp.mount('body')\n\nvar debounce = microbounce(128)\nfunction mainView (state, emit) {\n  return html`\n    <body>\n      <header>\n        <h1>${state.title}</h1>\n      </header>\n      <nav>\n        <input value=${state.title} oninput=${updateTitle}/>\n        <button onclick=${toPhiladelphia}>Philadelphia</button>\n        <button onclick=${toSeattle}>Seattle</button>\n      </nav>\n      <main>\n        ${leaflet.render(state.coords)}\n      </main>\n    </body>\n  `\n\n  function updateTitle (evt) {\n    var value = evt.target.value\n    debounce(function () {\n      emit('update-title', value)\n    })\n  }\n\n  function toPhiladelphia () {\n    emit('set-coords', [39.9526, -75.1652])\n  }\n\n  function toSeattle () {\n    emit('set-coords', [47.6062, -122.3321])\n  }\n}\n\nfunction store (state, emitter) {\n  state.coords = [39.9526, -75.1652]\n  state.title = 'Hello, World'\n\n  emitter.on('DOMContentLoaded', function () {\n    emitter.on('set-coords', setCoords)\n    emitter.on('update-title', updateTitle)\n  })\n\n  function setCoords (newCoords) {\n    state.coords = newCoords\n    emitter.emit('render')\n  }\n\n  function updateTitle (newTitle) {\n    state.title = newTitle\n    emitter.emit('render')\n  }\n}\n"
  },
  {
    "path": "example/leaflet.js",
    "content": "// // adapted from https://github.com/timwis/choo-leaflet-demo/blob/master/src/map.js\nvar Nanocomponent = require('../')\nvar nanologger = require('nanologger')\nvar leaflet = require('leaflet')\nvar onIdle = require('on-idle')\nvar html = require('nanohtml')\n\nmodule.exports = Leaflet\n\nfunction Leaflet () {\n  if (!(this instanceof Leaflet)) return new Leaflet()\n  Nanocomponent.call(this)\n\n  this._log = nanologger('leaflet')\n  this.map = null // capture leaflet\n  this.coords = [0, 0] // null island\n}\n\nLeaflet.prototype = Object.create(Nanocomponent.prototype)\n\nLeaflet.prototype.createElement = function (coords) {\n  this.coords = coords\n  return html`\n    <div style=\"height: 500px\">\n      <div id=\"map\"></div>\n    </div>\n  `\n}\n\nLeaflet.prototype.update = function (coords) {\n  if (!this.map) return this._log.warn('missing map', 'failed to update')\n  if (coords[0] !== this.coords[0] || coords[1] !== this.coords[1]) {\n    var self = this\n    onIdle(function () {\n      self.coords = coords\n      self._log.info('update-map', coords)\n      self.map.setView(coords, 12)\n    })\n  }\n  return false\n}\n\nLeaflet.prototype.beforerender = function (el) {\n  var coords = this.coords\n  this._log.info('create-map', coords)\n\n  var map = leaflet.map(el).setView(coords, 12)\n  leaflet.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/toner/{z}/{x}/{y}.{ext}', {\n    attribution: 'Map tiles by <a href=\"https://stamen.com\">Stamen Design</a>, <a href=\"http://creativecommons.org/licenses/by/3.0\">CC BY 3.0</a> &mdash; Map data &copy; <a href=\"http://www.openstreetmap.org/copyright\">OpenStreetMap</a>',\n    subdomains: 'abcd',\n    minZoom: 0,\n    maxZoom: 20,\n    ext: 'png'\n  }).addTo(map)\n  this.map = map\n}\n\nLeaflet.prototype.load = function () {\n  this._log.info('load')\n  this.map.invalidateSize()\n}\n\nLeaflet.prototype.unload = function () {\n  this._log.info('unload')\n\n  this.map.remove()\n  this.map = null\n  this.coords = [0, 0]\n}\n"
  },
  {
    "path": "index.js",
    "content": "const document = require('global/document')\nconst nanotiming = require('nanotiming')\nconst morph = require('nanomorph')\nconst onload = require('on-load')\nconst assert = require('assert')\n\nconst OL_KEY_ID = onload.KEY_ID\nconst OL_ATTR_ID = onload.KEY_ATTR\n\nmodule.exports = Nanocomponent\n\nfunction makeID () {\n  return 'ncid-' + Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1)\n}\n\nNanocomponent.makeID = makeID\n\nfunction Nanocomponent (name) {\n  this._hasWindow = typeof window !== 'undefined'\n  this._id = null // represents the id of the root node\n  this._ncID = null // internal nanocomponent id\n  this._olID = null\n  this._proxy = null\n  this._loaded = false // Used to debounce on-load when child-reordering\n  this._rootNodeName = null\n  this._name = name || 'nanocomponent'\n  this._rerender = false\n\n  this._handleLoad = this._handleLoad.bind(this)\n  this._handleUnload = this._handleUnload.bind(this)\n\n  this._arguments = []\n\n  const self = this\n\n  Object.defineProperty(this, 'element', {\n    get: function () {\n      const el = document.getElementById(self._id)\n      if (el) return el.dataset.nanocomponent === self._ncID ? el : undefined\n    }\n  })\n}\n\nNanocomponent.prototype.render = function () {\n  const renderTiming = nanotiming(this._name + '.render')\n  const self = this\n  const args = new Array(arguments.length)\n  let el\n\n  for (let i = 0; i < arguments.length; i++) args[i] = arguments[i]\n  if (!this._hasWindow) {\n    const createTiming = nanotiming(this._name + '.create')\n    el = this.createElement.apply(this, args)\n    createTiming()\n    renderTiming()\n    return el\n  } else if (this.element) {\n    el = this.element // retain reference, as the ID might change on render\n    const updateTiming = nanotiming(this._name + '.update')\n    const shouldUpdate = this._rerender || this.update.apply(this, args)\n    updateTiming()\n    if (this._rerender) this._rerender = false\n    if (shouldUpdate) {\n      const desiredHtml = this._handleRender(args)\n      const morphTiming = nanotiming(this._name + '.morph')\n      morph(el, desiredHtml)\n      morphTiming()\n      if (this.afterupdate) this.afterupdate(el)\n    }\n    if (!this._proxy) { this._proxy = this._createProxy() }\n    renderTiming()\n    return this._proxy\n  } else {\n    this._reset()\n    el = this._handleRender(args)\n    if (this.beforerender) this.beforerender(el)\n    if (this.load || this.unload || this.afterreorder) {\n      onload(el, self._handleLoad, self._handleUnload, self._ncID)\n      this._olID = el.dataset[OL_KEY_ID]\n    }\n    renderTiming()\n    return el\n  }\n}\n\nNanocomponent.prototype.rerender = function () {\n  assert(this.element, 'nanocomponent: cant rerender on an unmounted dom node')\n  this._rerender = true\n  this.render.apply(this, this._arguments)\n}\n\nNanocomponent.prototype._handleRender = function (args) {\n  const createElementTiming = nanotiming(this._name + '.createElement')\n  const el = this.createElement.apply(this, args)\n  createElementTiming()\n  if (!this._rootNodeName) this._rootNodeName = el.nodeName\n  assert(el instanceof window.Element, 'nanocomponent: createElement should return a single DOM node')\n  assert(this._rootNodeName === el.nodeName, 'nanocomponent: root node types cannot differ between re-renders')\n  this._arguments = args\n  return this._brandNode(this._ensureID(el))\n}\n\nNanocomponent.prototype._createProxy = function () {\n  const proxy = document.createElement(this._rootNodeName)\n  const self = this\n  this._brandNode(proxy)\n  proxy.id = this._id\n  proxy.setAttribute('data-proxy', '')\n  proxy.isSameNode = function (el) {\n    return (el && el.dataset.nanocomponent === self._ncID)\n  }\n  return proxy\n}\n\nNanocomponent.prototype._reset = function () {\n  this._ncID = Nanocomponent.makeID()\n  this._olID = null\n  this._id = null\n  this._proxy = null\n  this._rootNodeName = null\n}\n\nNanocomponent.prototype._brandNode = function (node) {\n  node.setAttribute('data-nanocomponent', this._ncID)\n  if (this._olID) node.setAttribute(OL_ATTR_ID, this._olID)\n  return node\n}\n\nNanocomponent.prototype._ensureID = function (node) {\n  if (node.id) this._id = node.id\n  else node.id = this._id = this._ncID\n  // Update proxy node ID if it changed\n  if (this._proxy && this._proxy.id !== this._id) this._proxy.id = this._id\n  return node\n}\n\nNanocomponent.prototype._handleLoad = function (el) {\n  if (this._loaded) {\n    if (this.afterreorder) this.afterreorder(el)\n    return // Debounce child-reorders\n  }\n  this._loaded = true\n  if (this.load) this.load(el)\n}\n\nNanocomponent.prototype._handleUnload = function (el) {\n  if (this.element) return // Debounce child-reorders\n  this._loaded = false\n  if (this.unload) this.unload(el)\n}\n\nNanocomponent.prototype.createElement = function () {\n  throw new Error('nanocomponent: createElement should be implemented!')\n}\n\nNanocomponent.prototype.update = function () {\n  throw new Error('nanocomponent: update should be implemented!')\n}\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"nanocomponent\",\n  \"version\": \"6.6.0\",\n  \"description\": \"Native DOM components that pair nicely with DOM diffing algorithms\",\n  \"main\": \"index.js\",\n  \"browser\": {\n    \"assert\": \"nanoassert\"\n  },\n  \"scripts\": {\n    \"test\": \"run-s test:*\",\n    \"test:deps\": \"dependency-check . --no-dev -i nanoassert\",\n    \"test:node\": \"NODE_ENV=test node test/node.js | tap-format-spec\",\n    \"test:browser\": \"browserify test/browser/index.js | tape-run --render='tap-format-spec'\",\n    \"test:lint\": \"standard *.js\",\n    \"start\": \"bankai start example\",\n    \"build\": \"rm -rf example/dist && rm -rf dist && bankai build example && mv example/dist .\"\n  },\n  \"repository\": {\n    \"type\": \"git\",\n    \"url\": \"git+https://github.com/choojs/nanocomponent.git\"\n  },\n  \"keywords\": [\n    \"nanohtml\",\n    \"bel\",\n    \"choo\",\n    \"element\",\n    \"thunk\",\n    \"cache\",\n    \"perf\",\n    \"nanomorph\",\n    \"morphdom\",\n    \"nanocomponent\",\n    \"cache-component\"\n  ],\n  \"author\": \"Trainspotters\",\n  \"contributors\": [\n    \"Bret Comnes <bcomnes@gmail.com> (http://bret.io)\"\n  ],\n  \"license\": \"MIT\",\n  \"bugs\": {\n    \"url\": \"https://github.com/choojs/nanocomponent/issues\"\n  },\n  \"homepage\": \"https://github.com/choojs/nanocomponent#readme\",\n  \"dependencies\": {\n    \"global\": \"^4.3.1\",\n    \"nanoassert\": \"^2.0.0\",\n    \"nanomorph\": \"^5.1.2\",\n    \"nanotiming\": \"^7.2.0\",\n    \"on-load\": \"^4.0.2\"\n  },\n  \"devDependencies\": {\n    \"@tap-format/spec\": \"^0.2.0\",\n    \"bankai\": \"^9.5.1\",\n    \"browserify\": \"^16.0.0\",\n    \"choo\": \"^7.1.0\",\n    \"choo-log\": \"^8.0.0\",\n    \"dependency-check\": \"^4.1.0\",\n    \"envify\": \"^4.0.0\",\n    \"leaflet\": \"^1.1.0\",\n    \"microbounce\": \"^1.0.0\",\n    \"nanobus\": \"^4.2.0\",\n    \"nanohtml\": \"^1.2.3\",\n    \"nanologger\": \"^2.0.0\",\n    \"npm-run-all\": \"^4.0.2\",\n    \"on-idle\": \"^3.1.0\",\n    \"standard\": \"^14.3.4\",\n    \"tape\": \"^5.0.0\",\n    \"tape-run\": \"^7.0.0\"\n  }\n}\n"
  },
  {
    "path": "test/browser/blog-section.js",
    "content": "var Nanocomponent = require('../../')\nvar html = require('nanohtml')\n\nclass BlogSection extends Nanocomponent {\n  constructor (name = 'BlogSection') {\n    super(name)\n    this.entries = null\n  }\n\n  createElement (entries) {\n    this.entries = entries\n    return html`\n      <section>\n        ${entries && entries.length > 0 ? entries.map(e => html`<p>${e}</p>`) : 'No entries'}\n      </section>\n    `\n  }\n\n  update (entries) {\n    if (entries !== this.entries || this.entries.some((e, i) => e !== entries[i])) return true\n    return false\n  }\n}\n\nmodule.exports = BlogSection\n"
  },
  {
    "path": "test/browser/index.js",
    "content": "var test = require('tape')\nvar SimpleComponent = require('./simple')\nvar BlogSection = require('./blog-section')\nvar Nanocomponent = require('../../')\nvar html = require('nanohtml')\nvar compare = require('../../compare')\nvar nanobus = require('nanobus')\n\nfunction makeID () {\n  return 'testid-' + Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1)\n}\n\nfunction createTestElement () {\n  var testRoot = document.createElement('div')\n  testRoot.id = makeID()\n  document.body.appendChild(testRoot)\n  return testRoot\n}\n\ntest('can create a simple component', function (t) {\n  var testRoot = createTestElement()\n\n  // Create instance and mount\n  var comp = new SimpleComponent('yosh')\n  testRoot.appendChild(comp.render('green'))\n  t.ok(comp.element, 'component created and mounted in page')\n  t.equal(comp.element.querySelector('.name').innerText, 'yosh', 'instance options correctly rendered')\n  t.equal(comp.element.querySelector('.color').innerText, 'green', 'arguments correctly rendered')\n  t.equal(comp.element.dataset.proxy, undefined, 'not a proxy element')\n\n  // Update mounted component and inspect proxy\n  var proxy = comp.render('red')\n  t.ok(proxy.dataset.proxy != null, 'proxy is returned on mounted component')\n  t.equal(proxy.dataset.nanocomponent, comp._ncID, 'proxy is tagged with the correct ncID')\n  t.equal(proxy.nodeName, comp.element.nodeName, 'proxy is of same type')\n  t.ok(proxy.isSameNode(comp.element), 'isSameNode works')\n  t.ok(comp.element, 'component is still mounted in page')\n  t.equal(comp.element.querySelector('.color').innerText, 'red', 'arguments correctly rendered')\n  t.equal(comp.element.dataset.proxy, undefined, 'mounted node isn\\'t a proxy')\n\n  comp.render('red')\n  t.ok(comp.element, 'component is still mounted in page')\n  t.equal(comp.element.querySelector('.color').innerText, 'red', 'arguments correctly rendered')\n  t.equal(comp.element.dataset.proxy, undefined, 'mounted node isn\\'t a proxy')\n\n  comp.name = 'lrlna' // Update internal state\n  comp.rerender()\n  t.ok(comp.element, 'component is still mounted in page')\n  t.equal(comp.element.querySelector('.name').innerText, 'lrlna', 'instance options correctly rerendered')\n  t.equal(comp.element.querySelector('.color').innerText, 'red', 'internal state reflected in rerender')\n  t.equal(comp.element.dataset.proxy, undefined, 'mounted node isn\\'t a proxy')\n\n  t.end()\n})\n\ntest('proxy node types match the root node returned from createElement', function (t) {\n  var testRoot = createTestElement()\n  var comp = new BlogSection()\n  testRoot.appendChild(comp.render(['hey', 'hi', 'howdy']))\n  t.ok(comp.element, 'component created and mounted in page')\n  t.equal(comp.element.nodeName, 'SECTION', 'correctly rendered')\n  t.equal(comp.element.dataset.proxy, undefined, 'not a proxy element')\n\n  var proxy = comp.render(['by', 'bye', 'cya'])\n  t.equal(proxy.nodeName, comp.element.nodeName, 'proxy is of same type as the root node of createElement')\n\n  t.end()\n})\n\ntest('missing createElement', function (t) {\n  function Missing () {\n    if (!(this instanceof Missing)) return new Missing()\n    Nanocomponent.call(this)\n  }\n\n  Missing.prototype = Object.create(Nanocomponent.prototype)\n\n  var badMissing = new Missing()\n  t.throws(badMissing.render.bind(badMissing), new RegExp(/createElement should be implemented/), 'call to render throws if createElement is missing')\n  t.end()\n})\n\ntest('missing update', function (t) {\n  function Missing () {\n    if (!(this instanceof Missing)) return new Missing()\n    Nanocomponent.call(this)\n  }\n\n  Missing.prototype = Object.create(Nanocomponent.prototype)\n\n  Missing.prototype.createElement = function () { return html`<div>hey</div>` }\n\n  var badMissing = new Missing()\n  var testRoot = createTestElement()\n  testRoot.appendChild(badMissing.render())\n  t.throws(badMissing.render.bind(badMissing), new RegExp(/update should be implemented/), 'call to update throws if update is missing')\n  t.end()\n})\n\ntest('lifecycle tests', function (t) {\n  var testRoot = createTestElement()\n  class LifeCycleComp extends Nanocomponent {\n    constructor () {\n      super()\n      this.bus = nanobus()\n      this.testState = {\n        'create-element': 0,\n        update: 0,\n        beforerender: 0,\n        afterupdate: 0,\n        load: 0,\n        unload: 0\n      }\n    }\n\n    createElement (text) {\n      this.arguments = arguments\n      this.testState['create-element']++\n      return html`<div>${text}</div>`\n    }\n\n    update (text) {\n      var shouldUpdate = compare(this.arguments, arguments)\n      this.testState.update++\n      return shouldUpdate\n    }\n\n    beforerender () {\n      this.testState.beforerender++\n    }\n\n    afterupdate () {\n      this.testState.afterupdate++\n    }\n\n    load () {\n      this.testState.load++\n      this.bus.emit('load')\n    }\n\n    unload () {\n      this.testState.unload++\n      this.bus.emit('unload')\n    }\n  }\n\n  var comp = new LifeCycleComp()\n  comp.bus.on('load', () => window.requestAnimationFrame(onLoad))\n  comp.bus.on('unload', () => window.requestAnimationFrame(onUnload))\n\n  t.deepEqual(comp.testState, {\n    'create-element': 0,\n    update: 0,\n    beforerender: 0,\n    afterupdate: 0,\n    load: 0,\n    unload: 0\n  }, 'no lifecycle methods run on instantiation')\n  var el = comp.render('hey')\n  t.deepEqual(comp.testState, {\n    'create-element': 1,\n    update: 0,\n    beforerender: 1,\n    afterupdate: 0,\n    load: 0,\n    unload: 0\n  }, 'create-element and beforerender is run on first render')\n\n  testRoot.appendChild(el)\n\n  function onLoad () {\n    t.deepEqual(comp.testState, {\n      'create-element': 1,\n      update: 0,\n      beforerender: 1,\n      afterupdate: 0,\n      load: 1,\n      unload: 0\n    }, 'component loaded')\n\n    comp.render('hi')\n\n    t.deepEqual(comp.testState, {\n      'create-element': 2,\n      update: 1,\n      beforerender: 1,\n      afterupdate: 1,\n      load: 1,\n      unload: 0\n    }, 'component re-rendered')\n\n    comp.render('hi')\n\n    t.deepEqual(comp.testState, {\n      'create-element': 2,\n      update: 2,\n      beforerender: 1,\n      afterupdate: 1,\n      load: 1,\n      unload: 0\n    }, 'component cache hit')\n\n    testRoot.removeChild(comp.element)\n  }\n\n  function onUnload () {\n    t.equal(comp.element, undefined, 'component unmounted')\n    t.end()\n  }\n})\n"
  },
  {
    "path": "test/browser/simple.js",
    "content": "var Nanocomponent = require('../../')\nvar html = require('nanohtml')\n\nmodule.exports = SimpleComponent\n\nfunction SimpleComponent (name) {\n  if (!(this instanceof SimpleComponent)) return new SimpleComponent(name)\n  this.name = name\n  this.color = null\n  Nanocomponent.call(this)\n}\n\nSimpleComponent.prototype = Object.create(Nanocomponent.prototype)\n\nSimpleComponent.prototype.createElement = function (color) {\n  this.color = color || 'blue'\n  return html`\n      <div>\n        <p class=\"name\">${this.name}</p>\n        <p class=\"color\">${this.color}</p>\n      </div>\n    `\n}\n\nSimpleComponent.prototype.update = function (color) {\n  return this.color !== color\n}\n"
  },
  {
    "path": "test/node.js",
    "content": "var Nanocomponent = require('../')\nvar test = require('tape')\nvar html = require('nanohtml')\n\ntest('should validate input types', (t) => {\n  t.plan(1)\n  var comp = new Nanocomponent()\n  t.throws(comp.render.bind(comp), /createElement should be implemented/)\n})\n\ntest('should render elements', (t) => {\n  t.plan(2)\n\n  function MyComp () {\n    if (!(this instanceof MyComp)) return new MyComp()\n    Nanocomponent.call(this)\n  }\n  MyComp.prototype = Object.create(Nanocomponent.prototype)\n\n  MyComp.prototype.createElement = function (name) {\n    return html`<div>${name}</div>`\n  }\n\n  MyComp.prototype.update = function (name) {\n    return false\n  }\n\n  var myComp = new MyComp()\n\n  var el1 = myComp.render('mittens')\n  t.equal(String(el1), '<div>mittens</div>', 'init render success')\n\n  var el3 = myComp.render('scruffles')\n  t.equal(String(el3), '<div>scruffles</div>', 're-render success')\n})\n"
  }
]