Describe the bug
I have an action that requires knowledge about parentNode of an element. A simplified version looks like this:
<script>
function onMountAction(node) {
console.log(!!node.parentElement);
}
</script>
<h1 use:onMountAction>Hello!</h1>
To Reproduce
Expected behavior
I expect that the action will be called after the node has been attached to the DOM. Because this is how it worked in the past versions.
Additional context
I am using this action to implement draggable element. It needs to know the parent node to properly compute positioning. Real code can be found here: https://github.com/just-boris/components-graph/blob/master/src/utils/drag.js#L2
I suspect that the issue was introduced by this PR: https://github.com/sveltejs/svelte/pull/4156
@tanhauhau Was there a reason in #4156 you switched attaching actions to be during creation rather than mounting? I don't think the docs explicitly say whether the element will be mounted in the DOM by the time the action is called, but it does seem like there are situations where it would be useful.
The reason was to have event listeners and attaching actions in the same phase, so we can order them.
so, a way to fix this is to move the event listeners + attaching actions to mounting? 馃
I was just hit by this confusion while trying to apply a scroll top to an element on mount. The following example doesn't work because the action is called before the node is mounted.
<script>
const scrollToCenter = node => {
node.scrollTop = 500;
};
</script>
<div use:scrollToCenter>
<slot></slot>
</div>
The docs are misleading https://svelte.dev/docs#use_action
Here the code example contains a comment inside the action that says
// the node has been mounted in the DOM
Fixed in 3.17.0 - https://svelte.dev/repl/b280b938304247a5ae891af8c62ce7d6?version=3.17.0
Most helpful comment
Fixed in 3.17.0 - https://svelte.dev/repl/b280b938304247a5ae891af8c62ce7d6?version=3.17.0