hyperscript-normalize-args
A hyperscript helper to normalize component arguments, for easier creation of reusable components
Why?
We want a flexible API to our users but an easy to use and consistent API internally.
This helper enables your hyperscript component to be called in a lot of ways:
// selector;// selector, props;// selector, props, child/-ren;// selector, child/-ren;// props, child/-ren;// child/-ren;
And you can code against a consistent API in your component no matter what!
You'll always get a props
object as first argument and a children
array as the second argument.
See module usage for examples.
Installation
Install hyperscript-normalize-args
using npm:
npm install --save hyperscript-normalize-args
Usage
Module usage
Cycle.js, virtual-dom or hyperscript
With;;; const n = ; // no options => DOM mode // Creating a `myButton` component, which wraps `button` and always add "my-button" as css classconst myButton = ; // myButton can now be used in multiple ways: ;// props = {id: 'the-button'}// children = [];// props = {id: 'the-button', className: 'a-class btn'}// children = ['Click me'];// props = {}// children = ['Click me']
snabbdom
With;;;; const button = ;const n = ; // snabbdom mode // Creating a `myButton` component, which wraps `button` and always add "my-button" as css classconst myButton = ; // myButton can now be used in multiple ways: ;// props = {props: {id: 'the-button'}}// children = [];// props = {props: {id: 'the-button', className: 'a-class btn'}}// children = ['Click me'];// props = {}// children = ['Click me']
hyperscript-string
With;;;; const button = ;const n = ; // no dom mode, i.e. string mode // Creating a `myButton` component, which wraps `button` and always add "my-button" as css classconst myButton = ; // myButton can now be used in multiple ways: ;// props = {id: 'the-button'}// children = [];// props = {id: 'the-button', class: 'a-class btn'}// children = ['Click me'];// props = {}// children = ['Click me']
Related packages
hyperscript
- Create HyperText with JavaScript, on client or server.hyperscript-helpers
- Terse syntax for hyperscripthyperscript-string
- Create HTML strings with JavaScriptsnabbdom
- A virtual DOM library with focus on simplicity, modularity, powerful features and performance.virtual-dom
- A batched diff-based DOM rendering strategy@cycle/dom
- The standard DOM Driver for Cycle.js, based on virtual-dom, and other helpers
API
normalize([options])
Name | Type | Description |
---|---|---|
options | Object |
Normalize options |
Returns: function
, a normalize function. See examples above.
options.dom
Type: Boolean
Default: true
The mode to use with Cycle.js, hyperscript or virtual-dom.
When explicitly set to false
it is good to use with hyperscript-string.
options.snabbdom
Type: Boolean
Default: false
The mode to use with snabbdom.
License
MIT © Joakim Carlstein