Skip to content

Conversation

@theengineear
Copy link
Collaborator

@theengineear theengineear commented Dec 6, 2025

Previously, both “connectedCallback” and “disconnectedCallback” would be called when simply attempting to reshuffle elements in a mapping within the same DOM tree.

With the addition of the “moveBefore” element API, we can fix this! Now, such children are simply moved and remain connected.

Additionally, a new test was added to confirm that browsers are calling the “connectedMoveCallback” method on moves. And, another test added to ensure that we are testing all conditions / ternaries in our code to maintain 100% coverage.

Closes #254.

  • Works in Chrome
  • Works in FireFox (nightly)
  • Works in Safari

and `disconnectedCallback` are no longer called because elements are only
moved in the DOM rather than being disconnected and reconnected. Added test
coverage for `connectedMoveCallback` and reference tracking during element
reordering (#254).
Copy link
Collaborator Author

@theengineear theengineear Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case this issue doesn’t ring a bell — we specifically have an outstanding bug related to shuffling tabs for an editor which can currently cause monaco to be disposed and then recreated if moving tabs around!

}

connectedMoveCallback() {
super.connectedMoveCallback();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only line in our test suite that exercises this callback — just for coverage.

assert(container.querySelector('#target').children[2].classList.contains('true'));
});

// This specifically tests out cases where we _cannot_ reuse DOM on shuffle.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was tricky! A bit harder to hit this case now that we do more DOM re-use here. Again, just making sure we maintain 100% coverage.

});

// TODO: #254: See https://chromestatus.com/feature/5135990159835136.
it.todo('native map does not cause disconnectedCallback on list shuffle', () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very satisfying to delete this “.todo” here and see it just work ❤️

/**
* Extends HTMLElement.prototype.connectedMoveCallback.
*/
connectedMoveCallback() {}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor interface addition here for discoverability (matches our other patterns — i.e., you should be able to rely on super existing).

// Note that passing “null” as the reference node moves nodes to the end.
for (let iii = nodes.length - 1; iii >= 0; iii--) {
const node = nodes[iii];
parentNode.moveBefore(node, referenceNode);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really the key change. We leverage the new moveBefore API to move these items without disconnecting / reconnecting them!

@theengineear
Copy link
Collaborator Author

@klebba — Here’s that moveBefore change you were looking for. It feels like we’re just inches away. The first time I looked into this, it was just in Chrome canary. It’s now readily available in Chrome and FireFox Nightly. Sill waiting for Safari…

Previously, both “connectedCallback” and “disconnectedCallback” would be
called when simply attempting to reshuffle elements in a mapping within
the same DOM tree.

With the addition of the “moveBefore” element API, we can fix this! Now,
such children are simply _moved_ and remain connected.

Additionally, a new test was added to confirm that browsers are calling
the “connectedMoveCallback” method on moves. And, another test added to
ensure that we are testing all conditions / ternaries in our code to
maintain 100% coverage.

Closes #254.
@theengineear
Copy link
Collaborator Author

Update — going to convert this to a draft PR until we see support in Safari. It’ll sorta just be ready to roll, but no need to act too hastily here.

@theengineear theengineear marked this pull request as draft December 8, 2025 01:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Neither connectedCallback nor disconnectedCallback should be called when re-mapping causes a shuffle.

2 participants