Skip to content

Key and Mouse Button events - modifiers that restrict the action of on:event #5795

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
lukaszpolowczyk opened this issue Dec 16, 2020 · 2 comments

Comments

@lukaszpolowczyk
Copy link

lukaszpolowczyk commented Dec 16, 2020

Is your feature request related to a problem? Please describe.
Svelte greatly simplifies the code.
I miss the simplification of the recognition of a key or a mouse button - to make it even more transparent.
I'm trying on the line (maybe it's a bad idea, but I don't have a better one):

<button on:mouseup={event=>
	event.button===0?doSound(wave)
	:
	event.button===1?saveSound(wave)
	:
	null
}>

but this code is just... not very readable, and ugly.

Describe the solution you'd like

<!-- Mouse button modifiers (and @const in tag) -->
<button
	on:mouseup|firstButton={()=>doSound(wave)}
	on:mouseup|secondButton={()=>saveSound(wave)}
}>
	
<!-- Or maybe like a function? -->
<button
	on:mouseup|button(0)={()=>doSound(wave)}
	on:mouseup|button(1)={()=>saveSound(wave)}
}>
<!-- Key code modifiers -->
<svelte:window on:keydown|KeyA|KeyS|KeyD|KeyW={doSometing}/>
	
<!-- or from an array -->
{@const arr=["KeyA","KeyS","KeyD","KeyW"]}
<svelte:window on:keydown|{arr}={doSometing}/>
	
<!-- or like a function -->
<svelte:window on:keydown|keyCode(KeyA,KeyS,KeyD,KeyW)={doSometing}/>

<!-- or -->
{@const arr=["KeyA","KeyS","KeyD","KeyW"]}
<svelte:window on:keydown|keyCode(arr)={doSometing}/>

Describe alternatives you've considered

<script>
function mouseup (event) {
  event.button===0?doSound(wave)
  event.button===1?saveSound(wave)
  :
  null
}
</script>
<button on:mouseup>

How important is this feature to you?
I use this on a daily basis so quite important.

@Conduitry
Copy link
Member

This came up before in #4427 and in other issues, and I still think that all the extra syntax (and extra documentation) that this would require is not worth it, compared to just doing this in javascript.

@lukaszpolowczyk
Copy link
Author

lukaszpolowczyk commented Dec 16, 2020

@Conduitry How can you say unequivocally "worth it" or "not worth it"? Is it such an arbitrary decision?

I understand the concept of svelte simplicity and support it very much.
But there aren't many places like the concept of modifiers with parameters - it's something simple and general.
on:keydown|keyCode(KeyA,KeyS,KeyD,KeyW)

I will even say that there are a lot of things in svelte that can only be used occasionally, and sometimes you have to use a less neat version.
For example, you can
bind:value
but there are situations where you cannot do without
on:change={e=>value=e.target.value}
But we don't delete bind:value.

Such button and key modifiers would fit in perfectly.

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