-
Notifications
You must be signed in to change notification settings - Fork 22.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Editorial review: Add moveBefore() docs #39002
base: main
Are you sure you want to change the base?
Conversation
Preview URLs (7 pages)Flaws (14)Note! 4 documents with no flaws that don't need to be listed. 🎉 URL:
URL:
URL:
(comment last updated: 2025-04-09 13:47:52) |
Thanks! A few technical comments:
|
Cool, thanks for the tip; I've updated the list of preserved state things to account for this. Note that, in light of your comments, I've moved this content to a new "Description" section on the page and added the other bits you wanted me to include there.
I did a bit of testing on this but couldn't get the selection to preserve. I've just removed the bullet about preserving selection, as this doesn't seem to me to be a case that will come up often or behavior that can be particularly relied upon. What do you think?
I've added a section to cover this inside the "Description" section, and added some info about
I've added a section about constraints to the "Description" section, and also tried to move sure the relevant exceptions are represented in the "Exceptions" section.
Good call; I've added a note about this. |
@noamr ^ |
|
||
- [Animation](/en-US/docs/Web/CSS/CSS_animations) and [transition](/en-US/docs/Web/CSS/CSS_transitions) state. | ||
- {{htmlelement("iframe")}} loading state. | ||
- Interactive node {{cssxref(":focus")}} and {{cssxref(":active")}} states. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps reword to "interactivity"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to
- Interactivity states (for example, {{cssxref(":focus")}} and {{cssxref(":active")}}).
> [!NOTE] | ||
> When observing changes to the DOM using a {{domxref("MutationObserver")}}, nodes moved with `moveBefore()` will be recorded with a [removed node](/en-US/docs/Web/API/MutationRecord/removedNodes) and an [added node](/en-US/docs/Web/API/MutationRecord/addedNodes). | ||
|
||
### `moveBefore()` constaints |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constraints
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops! Fixed.
|
||
### Moving custom elements | ||
|
||
When moving a [custom element](/en-US/docs/Web/API/Web_components/Using_custom_elements), care needs to be taken to handle the `connectedCallback()` and `disconnectedCallback()` [lifecycle callbacks](/en-US/docs/Web/API/Web_components/Using_custom_elements#custom_element_lifecycle_callbacks) appropriately, if any are defined in the custom element's constructor. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"care needs to be taken" is not the right framing here.
By default, custom elements don't support state-preserving move, and their disconnected
and connected
callbacks are fired.
Authors of web components can opt in to state-preserving move behavior, by implementing the connectedMoveCallback
, which would be called instead of the disconnected+connected default.
Note: this default behavior is introduced in order to maintain backwards compatibility with existing custom elements who might rely on being disconnected and reconnected when moved around the DOM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I get this now; thanks for the explanation. I've reworded the section as follows:
Moving custom elements
Each time a custom element is moved via moveBefore()
(or similar methods such as {{domxref("Node.insertBefore()")}}), the disconnectedCallback()
and connectedCallback()
lifecycle callbacks are fired.
Consider this minimal example:
class MyCustomElement extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
console.log("Custom element added to page.");
}
disconnectedCallback() {
console.log("Custom element removed from page.");
}
}
customElements.define("my-custom-element", MyCustomElement);
In this case, "Custom element removed from page." and "Custom element added to page." are logged to the console with each move. This might be your intended behavior. However, if you use the callbacks to run initialization and cleanup code, it might cause problems with the state of the moved element.
Custom elements can be opted in to state-preserving moves via the connectedMoveCallback()
lifecycle callback. If defined in the constructor, this will run instead of connectedCallback()
and disconnectedCallback()
when an element instance is moved via moveBefore()
. You could add an empty connectedMoveCallback()
to stop the other two callbacks running, or include some custom logic to handle the move:
connectedMoveCallback() {
console.log("Custom element moved with moveBefore()");
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect thanks!
@@ -57,6 +57,7 @@ Custom element lifecycle callbacks include: | |||
|
|||
- `connectedCallback()`: called each time the element is added to the document. The specification recommends that, as far as possible, developers should implement custom element setup in this callback rather than the constructor. | |||
- `disconnectedCallback()`: called each time the element is removed from the document. | |||
- `connectedMoveCallback()`: called _instead_ of `connectedCallback()` and `disconnectedCallback()` each time the element is moved to a different place in the DOM via {{domxref("Element.moveBefore()")}}. This can be used to overcome functionality problems encountered as a result of initialization/cleanup code being run multiple times when the element is not actually being added to or removed from the DOM. See [Moving custom elements](/en-US/docs/Web/API/Element/moveBefore#moving_custom_elements) for more details. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- When defined, called _instead etc..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
Thanks a lot for the review, @noamr. I think this is ready for editorial review now, so I will move it on to that stage, but feel free to chime in if you spot anything else. |
Description
Chrome 133 supports the
moveBefore()
method; see https://chromestatus.com/feature/5135990159835136.This PR documents the new method on
Document
,DocumentFragment
, andElement
.Motivation
Additional details
Related issues and pull requests