GithubHelp home page GithubHelp logo

fragment-node's Introduction

fragment-node

Why

fragment-node is an experiment to see if there's a hack way of creating a fragment DOM node, similar to DocumentFragment that exists in the DOM tree rather than as an abstraction. By having such a node, it allows us to mutate the DOM tree easily using the existing DOM API whilst ensuring childNodes on the fragment remain. DocumentFragments lose their childNodes once the DocumentFragment is appended to another node.

The core reasoning behind this experiment is to see if React or Inferno can use fragment-node to describe common problems that affect virtual DOM node trees. Such problems include:

const Rows = () => [
    <tr />,
    <tr />,
    <tr />
];

const Table = () => (
    <table>
        <Rows />
    </table>
);

The above example shows a component returning an array of <tr /> nodes from the <Rows /> component. If we were to wrap the output in a <div /> this would break the page as the contents of <table> elements are strict.

Instead libraries like React and Inferno push this problem into userland and educate the user to instead structure their components to have a wrapping root node. In the above example, this would mean they'd have to completely rethink how the <Table /> and ` components work.

How

fragment-node attempts to deal with this problem by providing an alternative to DocumentFragment that works like a typical DOM node, in that it actually gets inserted into the DOM tree and its childNodes remain with it.

It does this by using a placeholder base node (in this implementation it uses an empty CommentNode). It then uses defineProperty and MutationObservers to proxy in features and API methods that you'd typically use on a DocumentFragment.

fragment-node's People

Contributors

trueadm avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

leeoniya

fragment-node's Issues

Some questions...

Hey @trueadm this is interesting. I have some questions. Haven't tested yet cause on phone...

  • would it be better to use comment nodes since adjacent fragment nodes would not get merged during rendering to and html string (for ssr).
  • can this accomodate fragments-in-fragments? adjacent fragments?

a more useful demo might be

table
  tbody
    tr
    tr
    tr
    tr
  tbody
    tr
    tr
    tr
    tr
  tbody
    tr
    tr
    tr
    tr
  tbody
    tr
    tr
    tr
    tr

where every 2 adjacent tbody elems are a fragment and every 2 adjacent tr elems are a fragment.

this would go a long way towards demonstrating general utility.

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.