Coder Social home page Coder Social logo

DocumentFragment support about incremental-dom HOT 5 CLOSED

dy avatar dy commented on June 30, 2024
DocumentFragment support

from incremental-dom.

Comments (5)

sparhami avatar sparhami commented on June 30, 2024 1

The elementOpen function is expected to create an Element, and be inserted as an Element into the parent at the current location. As a result, a DocumentFragment won't work in its place. You can create a nested patch if you need to work with a DocumentFragment however. For example:

function render() {
  elementOpen('div');
  renderFragment();
  elementClose('div');
}

function renderFragment() {
  const frag = patch(document.createDocumentFragment(), () => {
    text('Hello world');
  });

  // Do something with frag.
  // Note: If you try to append the content to the current parent, the content
  // will get removed by `elementClose`, unless you call `skip()`.
} 

const el = document.createElement('div');
patch(el, render);

from incremental-dom.

sparhami avatar sparhami commented on June 30, 2024 1

Actually, that's also relevant for <template> case:

function renderPart() {
  let el = elementOpen(tpl);
  text('Hello world');
  elementClose(tpl);
}
function tpl () {
  return document.importNode(templateEl.content)
}
let templateEl = document.createElement('template')

let el = document.createElement('div')
patch(el, renderPart);

That gives same error.

So raising back same questions:

  • Would that be considered a valuable feature?
  • Is there a workaround now for this?

I'm not sure I quite understand the goal of this particular example with respect to rendering "Hello World". Since a template's contents are a tree, it isn't clear to me where in the template's cloned content "Hello world" would be rendered.

If you do need to clone a template as a static subtree, you will want to make sure you also tell Incremental DOM to skip the contents so it does not remove it via a diff. For example:

function renderTemplate(template) {
// Incremental DOM currently requires you have an extra container for rendering
// content into.
const el = elementOpen('div', '<use a key to avoid conflicts>');
// If already rendered the template, skip this part and just reuse it.
if (!el._templateRendered) {
  el.appendChild(document.importNode(template.content, true));
  el._templateRendered = true;
}
// Tell Incremental DOM to ignore all the content of the element so it isn't
// removed.
skip();
elementClose('div');
}

from incremental-dom.

dy avatar dy commented on June 30, 2024

Actually, that's also relevant for <template> case:

function renderPart() {
  let el = elementOpen(tpl);
  text('Hello world');
  elementClose(tpl);
}
function tpl () {
  return document.importNode(templateEl.content)
}
let templateEl = document.createElement('template')

let el = document.createElement('div')
patch(el, renderPart);

That gives same error.

So raising back same questions:

  • Would that be considered a valuable feature?
  • Is there a workaround now for this?

from incremental-dom.

sgammon avatar sgammon commented on June 30, 2024

we use idom with DocumentFragments regularly - am i missing something?

from incremental-dom.

dy avatar dy commented on June 30, 2024

Thank you for the solutions, patching internal fragment worked.

from incremental-dom.

Related Issues (20)

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.