-
Notifications
You must be signed in to change notification settings - Fork 314
Description
Summary
The goal of this method is to allow for the reordering of child nodes without the need to remove them and re-append them. Currently, reordering child nodes (by re-appending) causes several undesired side effects:
- Reloading of iframes
- Restarting of animations
- Loss of selection which cannot be restored in certain input types
This is a problem for several frameworks, including React, Preact, and Mithril.
Adding a new API vs changing existing APIs
Instead of adding a new API, we could change existing uses of DOM APIs to avoid reparenting. For example:
<div id=parent>
<div id=firstchild></div>
<div id=secondchild></div>
</div>
<script>
parent.appendChild(firstchild);
</script>
In this case, you can see that the script wants to move firstchild
past secondchild
and doesn't necessarily want the browser to remove firstchild
from the DOM and reparent it, so we could try to change appendChild
to keep the parent throughout the DOM modification and avoid the loss of state.
However, I think a new API would be a better solution:
- Changing
appendChild
and other DOM modification methods would get more complicated due to difficulties defining and standardizing the existing behavior. - If there are any sites out there which rely on the full reparenting logic, they could become broken.
API shape
I have some ideas for what this method could look like:
- Provide a full list with all children in desired order.
parentNode.reorderChildren([childOne, childTwo, childThree])
- Move one child one spot forward or one spot backwards.
childNode.movePastNextSibling()
childNode.moveBeforePreviousSibling()
- Move child before or after another child.
childNode.moveBefore(otherChildNode)
childNode.moveAfter(otherChildNode)
I don't want to bikeshed too much on this until it really sounds like we will add a new method, but if any API shape seems particularly good please speak up so I can start prototyping.
Relationship to #808
@annevk is #808 blocking us from having a new way to reorder child nodes? That issue seems more concerned about insertions and mutation events which don't really seem to apply to reordering. Couldn't we just have new spec steps with no special functionality for iframes and scripts and no mutation events?
I made this issue based on feedback in this discussion: whatwg/html#5484