Skip to content

Svelte 5: Add the ability to bind to snippet parameters #13200

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

Closed
Serator opened this issue Sep 11, 2024 · 2 comments
Closed

Svelte 5: Add the ability to bind to snippet parameters #13200

Serator opened this issue Sep 11, 2024 · 2 comments

Comments

@Serator
Copy link

Serator commented Sep 11, 2024

Describe the problem

In some cases, I would like to be able to bind to snippet parameters similar to how it is already possible with component properties.

Let's say we have a component with a dialog that wraps the button that toggle it: example.

In case you need to activate this dialog from the outside, binding to the toggle parameter would work perfectly, but that option is not available right now. As a workaround, you can use a “component in the middle” that will take in a snippet parameter and return the same: example.

I may have overlooked some cases and there is a better solution.

Probably overlaps with that.

Describe the proposed solution

Instead of adding a “component in the middle” as done in the previous link, it would be great to have the ability to bind directly to a snippet parameter, something like this (pseudocode):

<script lang="ts">
	import Dialog from './Dialog.svelte'
	import BindableSnippet from './BindableSnippet.svelte'
	let toggle: (() => boolean) | undefined = $state()
</script>

<Dialog>
                     <!-- ⬇⬇⬇⬇ -->
	{#snippet trigger({bind:toggle})}
		<button onclick={toggle} type="button">Toggle dialog (inside)</button>
	{/snippet}
</Dialog>

<button onclick={toggle} type="button">Toggle dialog (outside)</button>

Importance

would make my life easier

@brunnerh
Copy link
Member

There should be very few cases where something like this should be necessary or even makes sense.
E.g. here you could just export the function and invoke it on the instance.

// in Dialog
export const toggle = () => (isShown = !isShown)
<script lang="ts">
	import Dialog from './Dialog.svelte'

	let dialog;
</script>

<Dialog bind:this={dialog}>...</Dialog>

<button onclick={() => dialog.toggle()} type="button">Toggle dialog (outside)</button>

REPL

@Serator
Copy link
Author

Serator commented Sep 11, 2024

That solves the original issue, thanks!

@Serator Serator closed this as completed Sep 11, 2024
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