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

let-binding fails when multiple elements are assigned to the same slot #4135

Closed
mdempsky opened this issue Dec 20, 2019 · 3 comments
Closed

Comments

@mdempsky
Copy link
Contributor

Describe the bug
If two elements are assigned to the same slot, let bindings on the first element are syntactically accepted, but don't actually introduce any bindings.

To Reproduce
https://svelte.dev/repl/193a784e481947979ee20f97a8fc8200?version=3.16.5

Expected behavior
The output should be:

A X
X
X
B X

but instead it's

undefined X
X
X
B X

Alternatively, a compiler error would be acceptable.

Severity
Low.

@JazielGuerrero
Copy link

Interesting,

If you swap the elements of slot A, it expose the value. REPL

But of course, the output will be different:

X
A X
X
B X

On the other hand, if you expose the variable 'a' to both slot="a" elements it produce the output you expected: REPL.

A X
X
X
B X

Continuing the test, if you create another element of slot="a" and don't expose the variable to that given slot, it will made the first element of slot="a" undefined again. REPL. Producing the next output:

undefined X
X
X
X
B X

It seems like, for all slot elements of the same type you need to expose all the variables. But if you expose the variables in the components itself, and not on the slot, it works as we expect. REPL.

A X
X
X
B X

@Crisfole
Copy link
Contributor

Crisfole commented Jan 2, 2020

Huh, to be honest, I'd expect assigning multiple elements to the same slot to fail at compile time unless the multiples were inside mutually exclusive conditionals.

If the multiples were outside statically resolvable mutually exclusive conditionals I'd expect to see a runtime error.

In your REPL's case I'm super surprised by the fact that the let:x works at all in <Nested let:x>...there's no default slot. If this works it could address #4173, but in an unexpected way...not one I'm a huge fan of...

Oof. Slots are complicated

@tanhauhau
Copy link
Member

If you want to assign multiple elements into the same slot, you could use <svelte:fragment> introduced in 3.35.0. You could declare your let bindings on the <svelte:fragment>.

<script>
	import Nested from './Nested.svelte';
</script>

<Nested let:x>
	<svelte:fragment slot="a" let:a>
		<p>{a} {x}</p>
		<p>{x}</p>
	</svelte:fragment>

	<svelte:fragment slot="b" let:b>
		<p>{x}</p>
		<p>{b} {x}</p>
	</svelte:fragment>
</Nested>

https://svelte.dev/repl/10435c582a1541caa0555037bc336440?version=3.35.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants