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

fix: don't hoist snippets with bind:group #15040

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

paoloricciuti
Copy link
Member

Closes #15037

This is a bit of weird one...the point is that the snippet was technically not accessing anything from the instance so it was hoisted but it was using bind:group so it needed the binding_group...i tried to think it it make sense to export a snippet with a bind group but i don't think it does (especially since we specify in the docs that for bind:group to work inputs needs to be in the same component) and i don't even think we could do it because even if we would accept the binding_group as an argument for the snippet a component that renders that snippet should know that it needs a binding_group in some way and i don't think it's doable.

However while trying to come up with various situations where the snippet could've been exported i noticed that we can technically "kinda" export snippets by passing them as prop. So a user could do something like this

App.svelte

<script>
    import Component from "./Component.svelte";
</script>

{#snippet radio(group, value)}
    <input type="radio", bind:group={group.selected} {value} />
{/snippet}

<Component {radio} />
<Component {radio} />

Component.svelte

<script>
    let { radio } = $props();
    let group = $state({ selected: undefined });
</script>

{@render radio(group, "A")}
{@render radio(group, "B")}

this is a bit weird because now App has the binding_group array but the state is inside Component. So i was wondering...should we disallow bind:group in snippets all togheter? Technically we can't do it now since it would be a breaking change but i wonder if we should do something about this weirdness.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Jan 16, 2025

🦋 Changeset detected

Latest commit: f380b34

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Rich-Harris
Copy link
Member

preview: https://svelte-dev-git-preview-svelte-15040-svelte.vercel.app/

this is an automated message

Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@15040

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

Successfully merging this pull request may close these issues.

binding_group is not defined
2 participants