sveltejs / svelte

Cybernetically enhanced web apps
https://svelte.dev
MIT License
76.8k stars 3.98k forks source link

Svelte 5: Include event modifiers previously found pre svelte 5 as functions available from the svelte library #11458

Open JacobSilasBentley opened 2 weeks ago

JacobSilasBentley commented 2 weeks ago

Describe the problem

I would like for the functionality of event modifiers previously available before svelte 5 for example 'click', 'once' and 'preventDefault' (shown below) to be readily available in svelte 5.

<button on:click|once|preventDefault={handler}>...</button>

Describe the proposed solution

In the preview documentation it suggests creating functions to replace event modifiers.

So the following:

<button on:once|preventDefault={handler}>...</button>

would be rewritten as:

<button onclick={once(preventDefault(handler))}>...</button>

In my opinion, it would be nice if these functions were available from the svelte library itself rather than having to write them independently. This would bring parity between svelte 4 in and svelte 5 for this feature and help minimise work needed for migration.

I understand the addition of these functions needs to be considered against the trade-off of additional bloat to the library, especially as these are relatively quick and simple functions to write (though ensuring the typing is correct adds some difficulty in my experience).

I would be happy to put in a PR for the change if it is deemed a worthwhile addition.

Importance

nice to have