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

Allow user of component to specify event and bind ordering. #7415

Closed
shaun-wild opened this issue Apr 2, 2022 · 1 comment
Closed

Allow user of component to specify event and bind ordering. #7415

shaun-wild opened this issue Apr 2, 2022 · 1 comment

Comments

@shaun-wild
Copy link

shaun-wild commented Apr 2, 2022

Describe the problem

The order of event and bind attributes matters in svelte, (See #4616).

It is currently possible to forward events from components. It is not possible to specify the order in which these events are fired.

Describe the proposed solution

Allow a component to specify the order of its events and binds are to be set by the caller.

The exact implementation of this, I'm not sure.

Maybe it could be an option?

<svelte:options preserveOrder />

<script>
export let value;
</script>

<input bind:value on:change />

Then the order of bind:value and on:change will be set on the parent component instead.

Alternatives considered

Making use of sensible defaults, it's probable a user will want the bind before the events.

Importance

would make my life easier

@mrkishi
Copy link
Member

mrkishi commented Apr 2, 2022

It is not possible to specify the order in which these events are fired.

As mentioned in the other issue, components can expose these kinds of options through their API:

<script>
    export let value;
    export let prebinding = false;
</script>

{#if prebinding}
    <input on:change bind:value />
{:else}
    <input bind:value on:change />
{/if}

Components are compiled individually, so even with the proposed compiler option we wouldn't be able to decide at build-time what order to pick and it'd have a similar runtime hit (at minimum).

The exact implementation of this, I'm not sure.

Maybe it could be an option?

I get the intention, and it sounds sensible to want a way for component attribute order to behave similarly to tag attribute order. On the other hand, it'd cause confusion as to why the order matters for some components but not others.

Implementation-wise, I'm not even convinced it's possible to come up with a satisfying behavior that handles all cases. For example, what should we do when we have forwarded events and bindings amongst actions and non-forwarded events?

<!-- Input.svelte -->
<input use:foo on:change use:bar bind:value />

<!-- Component.svelte -->
<Input bind:value on:change={handle}/>

Would this be bind, change, foo, bar, bind, foo, change, bar, foo, bar, bind, change, or foo, bind, change, bar? This would be surprising regardless of our choice, and it'd still be invisible to users of the component, so the component creator would have to be sure it worked regardless of order. And if some actions came from users, all bets would be off since Svelte wouldn't be able to tell and they'd appear to be executed in a random order.

Making use of sensible defaults, it's probable a user will want the bind before the events.

I don't think it's that clear. Anecdotally, I've personally never used both a binding and an event at the same time while giving the binding priority.

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

3 participants