Giter Site home page Giter Site logo

snabbdom-merge's Introduction

snabbdom-merge

This is a function to merge two snabbdom vnodes, vnode1 and vnode2, with the following behavior:

  • Do a standard object merge on the 'props', 'class', 'style', 'attrs', and 'dataset', with the data in vnode2 getting precedence
  • Chain event listener and hook functions together, with the function from vnode1 getting called first, then the function in vnode 2
    • Eg if vnode1 is h('a', {on: {click: fn1}}) and vnode2 is h('a', {on: {click: fn2}}), then the merged vnode will call fn1, then fn2
  • Concat together selector strings, preserving classnames.
    • Eg if vnode1 is h('div.x') and vnode2 is h('div.y.z') then the merged vnode will be h('div.x.y.z')
  • Concatenate the children of vnode1 with the children of vnode2

merge(vnode1, vnode2)

This function returns a brand new, merged vnode with the above behavior. vnode1 and vnode2 should have the same tag names.

Example

var h = require('snabbdom/h').default
var merge = require('snabbdom-merge')

var sayhi = function(ev) { console.log('hi', ev) }
var saybye = function(ev) { console.log('bye', ev) }

var vnode1 = h('button.x', {
  attrs: { 'data-x': 'x' },
  on: { click: sayhi }
}, h('span', 'x'))

var vnode2 = h('button.y', {
  attrs: { 'data-y': 'y' },
  on: { click: saybye }
}, h('span', 'y'))


var merged = merge(vnode1, vnode2)

// Result:
// 
// h('button.x.y', {
//   attrs: {'data-x': 'x', 'data-y': 'y'}
// , on: {click: function(ev) { sayhi(ev); saybye(ev) }}
// }, [h('span', 'x'), h('span', 'y')])

snabbdom-merge's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.