Skip to content
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

Svelte component nested inside a <svelte:element> with dynamic 'this' breaks attribute reactivity #9742

Open
ranjan-purbey opened this issue Dec 2, 2023 · 2 comments

Comments

@ranjan-purbey
Copy link

ranjan-purbey commented Dec 2, 2023

Describe the bug

Nesting a svelte component inside a svelte:element with dynamic this like this:

<svelte:element this={"div"}>
  <MyComponent />
</svelte:element>

breaks attribute reactivity for the entire svelte block. Though content reactivity is not affected.

This issue doesn't pop up when either the this attr is static (like this="div" instead of this={"div"})
or, when the svelte:element doesn't have any other svelte components as children.

The bug affects all svelte versions that support svelte:element including v3.47.0 (except svelte 5 preview).

Reproduction

  1. Go to example repl.
  2. Try toggling the checkbox and notice the border style of the following div change.
  3. Uncomment line 12 (<MyComponent/> inside <svelte:element>)
  4. Repeat step 2 but border style doesn't change now.
  5. Make the this attribute of svelte:element static by removing the curly braces.
  6. Repeat step 2 and this time border style does change.

Logs

No response

System Info

Svelte repl

Severity

blocking all usage of svelte

@ranjan-purbey
Copy link
Author

Idk if this change makes sense, but it seems to solve the issue as well as passes all existing tests

diff --git a/packages/svelte/src/compiler/compile/render_dom/Block.js b/packages/svelte/src/compiler/compile/render_dom/Block.js
index 7f0f37aa7..ed076ea42 100644
--- a/packages/svelte/src/compiler/compile/render_dom/Block.js
+++ b/packages/svelte/src/compiler/compile/render_dom/Block.js
@@ -265,6 +265,8 @@ export default class Block {
       this.add_variable({ type: 'Identifier', name: '#current' });
       if (this.chunks.intro.length > 0) {
         this.chunks.intro.push(b`#current = true;`);
+      }
+      if (this.chunks.mount.length > 0) {
         this.chunks.mount.push(b`#current = true;`);
       }
       if (this.chunks.outro.length > 0) {

@ranjan-purbey ranjan-purbey changed the title Svelte component nested inside a <svelte:element> with dynamic this breaks attribute reactivity Svelte component nested inside a <svelte:element> with dynamic 'this' breaks attribute reactivity Dec 2, 2023
@jweston491
Copy link

jweston491 commented Apr 26, 2024

Ran into this issue as well, using svelte@4.2.9

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

No branches or pull requests

2 participants