How to use morphdom - 9 common examples

To help you get started, we’ve selected a few morphdom examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github spectjs / spect / src / html-morph.js View on Github external
// don't override initial tag id
    if (tag.id && !props.id) props.id = tag.id
    if (tag.classList.length && props.class) {
      props.class = [...tag.classList, props.class]
    }

    // keep attributes
    if (tag.attributes) {
      for (let attr of tag.attributes) {
        if (!props[attr.name]) props[attr.name] = attr.value
      }
    }
    let newTag = createElement(tag.tagName, props, children)

    morph(tag, newTag, {
      getNodeKey: (el) => {
        return el.key || el.id
      },
      onBeforeElChildrenUpdated: (fromEl, toEl) => {
        // text-only case is special
        // if ([...fromEl.childNodes].every(node => node.nodeType === 3) && [...toEl.childNodes].every(node => node.nodeType === 3)) {
        //   console.log(fromEl.innerHTML, toEl.innerHTML)
        //   // fromEl.textContent = toEl.textContent
        //   // return false
        // }
      },
      onBeforeElUpdated: (fromEl, toEl) => {
        // FIXME: this blocks updating
        // if (fromEl.isEqualNode(toEl)) {
        //   return false
        // }
github Tencent / omi / packages / omire / src / vdom / diff.js View on Github external
children[j] = undefined
            if (j === childrenLen - 1) childrenLen--
            if (j === min) min++
            break
          }
        }
      }

      // morph the matched/found/created DOM child to match vchild (deep)
      let out = idiff(child, vchild, context, mountAll)
      if (out._reactComponent) {
        let diffIt = false
        for (let k = 0, cl = dom.childNodes.length; k < cl; k++) {
          if (dom.childNodes[i] && dom.childNodes[i]._reactComponent.constructor === out._reactComponent.constructor) {
            diffIt = true
            morphdom(dom.childNodes[i], out)
            break
          }
        }

        !diffIt && dom.appendChild(out)
      } else {
        f = originalChildren[i]
        if (out && out !== dom && out !== f) {
          if (f == null) {
            dom.appendChild(out)
          } else if (out === f.nextSibling) {
            removeNode(f)
          } else {
            dom.insertBefore(out, f)
          }
        }
github jxnblk / hello-color / index.js View on Github external
backgroundColor: result.base,
    ...result
  })
  const cardimg = getCardImage(result)

  const nextHead = h('head')(
    h('title')(`Hello Color ${color}`),
    h('meta')({
      name: 'viewport',
      content: 'width=device-width,initial-scale=1'
    })(),
    h('style')('*{box-sizing:border-box;}body{margin:0}')
  )

  if (tree) {
    update(tree, next)
    update(head, nextHead)
  } else {
    document.body.appendChild(next)
    console.log(document.head, nextHead)
    document.head.parentNode.replaceChild(nextHead, document.head)
    return {
      tree: next,
      head: nextHead
    }
  }
}
github Shopify / buy-button-js / src / view.js View on Github external
updateNode(node, html) {
    const div = document.createElement('div');
    div.innerHTML = html;
    morphdom(node, div.firstElementChild);
  }
github clauderic / virtualized-list / src / VirtualList / index.js View on Github external
const {height, offset = 0} = this.state;
    const {start, stop} = this._sizeAndPositionManager.getVisibleRange({
      containerSize: height,
      offset,
      overscanCount,
    });
    const fragment = document.createDocumentFragment();

    for (let index = start; index <= stop; index++) {
      fragment.appendChild(renderRow(index));
    }

    this.inner.style.height = `${this._sizeAndPositionManager.getTotalSize()}px`;
    this.content.style.top = `${this.getRowOffset(start)}px`;

    morphdom(this.content, fragment, {
      childrenOnly: true,
      getNodeKey: node => node.nodeIndex,
    });

    this.onRowsRendered({
      startIndex: start,
      stopIndex: stop,
    });
  }
}
github jxnblk / hello-color / demo / index.js View on Github external
backgroundColor: result.base,
    ...result
  })
  const cardimg = getCardImage(result)

  const nextHead = h('head')(
    h('title')(`Hello Color ${color}`),
    h('meta')({
      name: 'viewport',
      content: 'width=device-width,initial-scale=1'
    })(),
    h('style')('*{box-sizing:border-box;}body{margin:0}')
  )

  if (tree) {
    update(tree, next)
    update(head, nextHead)
  } else {
    document.body.appendChild(next)
    document.head.parentNode.replaceChild(nextHead, document.head)
    return {
      tree: next,
      head: nextHead
    }
  }
}
github stimulusreflex / cable_ready / javascript / operations.js View on Github external
operate(operation, () => {
        const { childrenOnly, focusSelector } = operation
        morphdom(
          element,
          childrenOnly ? template.content : template.innerHTML,
          {
            childrenOnly: !!childrenOnly,
            onBeforeElUpdated: shouldMorph(operation),
            onElUpdated: didMorph(operation)
          }
        )
        assignFocus(focusSelector)
      })
      after(parent.children[ordinal], callee, operation)
github keeweb / keeweb / app / scripts / framework / views / view.js View on Github external
renderElement(templateData) {
        const html = this.template(templateData);
        if (this.el) {
            const mountRoot = this.options.ownParent ? this.el.firstChild : this.el;
            morphdom(mountRoot, html);
            this.bindElementEvents();
        } else {
            let parent = this.options.parent || this.parent;
            if (parent) {
                if (typeof parent === 'string') {
                    parent = document.querySelector(parent);
                }
                if (!parent) {
                    throw new Error(`Error rendering ${this.constructor.name}: parent not found`);
                }
                if (this.options.replace) {
                    Tip.destroyTips(parent);
                    parent.innerHTML = '';
                }
                const el = document.createElement('div');
                el.innerHTML = html;

morphdom

Morph a DOM tree to another DOM tree (no virtual DOM needed)

MIT
Latest version published 4 months ago

Package Health Score

85 / 100
Full package analysis

Popular morphdom functions