Skip to content

motorcyclejs/dom

Repository files navigation

Motorcycle.js DOM Driver Build Status

Motorcycle.jsisolate

The Standard DOM Driver for Motorcycle. Built using Snabbdom for it's modularity and it's faster virtual-dom implementation. This library exports the fantastic hyperscript-helpers library for ease-of-use.

Installing

$ npm install @motorcycle/dom

Want to Contribute?

If you found an issue or want to contribute code, please read the contributing guidelines.

Examples

Basic usage

import most from 'most'
import {run} from '@motorcycle/core'
import {makeDOMDriver, h} from '@motorcycle/dom'

function main(sources) {
  ...
  return {
    DOM: view$,
  }
}

run(main, {
  DOM: makeDOMDriver('#app')
})

More examples can be found here.

API

makeDOMDriver(container, modules)

Arguments

container :: Element|CSS-Selector - A DOM node or a CSS-Selector which points to an existing DOM node that will be used as the initial place to patch the DOM.

modules :: Array - An array of Snabbdom modules which will be used by Snabbdom to add/remove behaviors that are available to you from the h() or hyperscript-helpers functions.

import {makeDOMDriver} from '@motorcycle/dom'

makeDOMDriver('#app')
// or
makeDOMDriver(document.querySelector('#app'))

/* with modules */
/* these are the default modules used */
makeDOMDriver('#app', [
  require(`snabbdom/modules/class`),
  require(`snabbdom/modules/props`),
  require(`snabbdom/modules/attributes`),
  require(`snabbdom/modules/style`),
])

snabbdom - h() / thunk()

Importing
import {h, thunk} from '@motorcycle/dom'

For more information on how to use h(), please refer to the original documentation.

For more information on how to use thunk), please refer to the original documentation

hyperscript-helpers

Importing
import {div, h1, p} from '@motorcycle/dom'

For more information on how to use hyperscript-helpers.

mockDOMSource()

A testing utility which aids in creating a queryable collection of Observables. Call mockDOMSource giving it an object specifying selectors, eventTypes and their Observables, and get as output an object following the same format as the DOM Driver's source.

Example:

const userEvents = mockDOMSource({
 '.foo': {
   'click': most.just({target: {}}),
   'mouseover': most.just({target: {}})
 },
 '.bar': {
   'scroll': most.just({target: {}})
 }
});

// Usage
const click$ = userEvents.select('.foo').events('click')

Arguments:

mockedSelectors :: Object an object where keys are selector strings and values are objects. Those nested objects have eventType strings as keys and values are Observables you created. Return:

(Object) fake DOM source object, containing a function select() which can be used just like the DOM Driver's source. Call select(selector).events(eventType) on the source object to get the Observable you defined in the input of mockDOMSource.