├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── example.js
├── index.js
├── package.json
└── test.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 | coverage/
3 | tmp/
4 | dist/
5 | npm-debug.log*
6 | .DS_Store
7 | .nyc_output
8 | yarn.lock
9 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | node_js:
2 | - "4"
3 | - "5"
4 | - "6"
5 | - "7"
6 | sudo: false
7 | language: node_js
8 | script: "npm run test"
9 | # after_success: "npm i -g codecov && npm run coverage && codecov"
10 | addons:
11 | apt:
12 | packages:
13 | - xvfb
14 | install:
15 | - export DISPLAY=':99.0'
16 | - Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
17 | - npm install
18 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 Yoshua Wuyts
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # microcomponent [![stability][0]][1]
2 | [![npm version][2]][3] [![build status][4]][5]
3 | [![downloads][8]][9] [![js-standard-style][10]][11]
4 |
5 | Smol event based component library. Syntactic sugar around [nanocomponent][nc].
6 | Adds logging through [nanologger](https://github.com/yoshuawuyts/nanologger).
7 |
8 | ## Usage
9 | ```js
10 | var microcomponent = require('microcomponent')
11 | var html = require('bel')
12 |
13 | var component = createComponent()
14 | document.body.appendChild(component.render())
15 |
16 | function createComponent () {
17 | var component = microcomponent({
18 | props: {
19 | text: null
20 | }
21 | })
22 | component.on('render', render)
23 | component.on('update', update)
24 | component.on('load', load)
25 | component.on('unload', unload)
26 | return component
27 |
28 | function render () {
29 | return html`
${this.props.text}
`
30 | }
31 |
32 | function update (props) {
33 | return props.text !== this.props.text
34 | }
35 |
36 | function load () {
37 | console.log('mounted on DOM')
38 | }
39 |
40 | function unload () {
41 | console.log('removed from DOM')
42 | }
43 | }
44 | ```
45 |
46 | ## API
47 | ### `component = Component([{ name, props, state, pure }])`
48 | Create a new Microcomponent instance. Takes a name string that's used for
49 | logging data. Logging is logged on log level `'debug'`. You can set the log
50 | level through `localStorage.logLevel = 'debug|info|warn|error|fatal'`. Also
51 | takes objects that will be initialized as `this.state` and `this.props`. For
52 | reference, a `this.oldProps` will always contain the state of the previous
53 | render iteration.
54 |
55 | Set `pure = true` to activate the default `on('update')` handler, which will
56 | shallow diff `props` with `this.props`.
57 |
58 | ### `component.on(eventname, handler)`
59 | Register a new handler for an eventname. Can register any custom event,
60 | built-in lifecycle events are:
61 | - `render`: (required) create a new DOMNode. If there's already been an DOMNode
62 | rendered it'll be diffed instead. Must always return an DOMNode of the same
63 | type.
64 | - `update`: (required) determine if `update` should be called
65 | - `load`: called when the element is mounted on the DOM
66 | - `unload`: called when the element is removed from the DOM
67 |
68 | ### `component.emit(eventname, [props])`
69 | Trigger a handler on the component.
70 |
71 | ### `DOMNode = component.render([props])`
72 | Render an element.
73 |
74 | ## See Also
75 | - [yoshuawuyts/nanocomponent][nc]
76 |
77 | ## License
78 | [MIT](https://tldrlegal.com/license/mit-license)
79 |
80 | [0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square
81 | [1]: https://nodejs.org/api/documentation.html#documentation_stability_index
82 | [2]: https://img.shields.io/npm/v/microcomponent.svg?style=flat-square
83 | [3]: https://npmjs.org/package/microcomponent
84 | [4]: https://img.shields.io/travis/yoshuawuyts/microcomponent/master.svg?style=flat-square
85 | [5]: https://travis-ci.org/yoshuawuyts/microcomponent
86 | [6]: https://img.shields.io/codecov/c/github/yoshuawuyts/microcomponent/master.svg?style=flat-square
87 | [7]: https://codecov.io/github/yoshuawuyts/microcomponent
88 | [8]: http://img.shields.io/npm/dm/microcomponent.svg?style=flat-square
89 | [9]: https://npmjs.org/package/microcomponent
90 | [10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
91 | [11]: https://github.com/feross/standard
92 | [nc]: https://github.com/yoshuawuyts/nanocomponent
93 |
--------------------------------------------------------------------------------
/example.js:
--------------------------------------------------------------------------------
1 | var microcomponent = require('.')
2 | var morph = require('nanomorph')
3 | var css = require('yo-css')
4 | var html = require('bel')
5 | var shallowEqual = require('juliangruber-shallow-equal/objects')
6 | var move = require('array-move').mut
7 |
8 | function createComponent () {
9 | var component = microcomponent({
10 | name: 'example',
11 | state: {
12 | first: null,
13 | last: null
14 | }
15 | })
16 | component.on('render', render)
17 | component.on('update', update)
18 | return component
19 |
20 | function render () {
21 | this.state.first = this.props.first()
22 | this.state.last = this.props.last()
23 | return html`
24 |
25 |