-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Aliases for certain type of key press events #8273
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
Comments
This has come up before in #4427 and in some other issues. I'm not that much more enthusiastic about the idea than I was then. I'm definitely against a syntax like |
I see where you're coming from. Would it be better to use more verbose names like The only other approach I can think of would be scoping. Maybe like |
Just use the correct elements, see: |
What about letting people create custom event modifiers? function enter (fn: (ev: KeyboardEvent) => void) {
return function (this: HTMLElement, ev: KeyboardEvent) {
if (ev.key === "Enter") {
fn.call(this, ev);
}
}
}
on:keypress|enter={handler} |
This really isn't always practical, especially when you have more complex UI elements. Wrapping everything in an anchor tag or button brings a lot of additional potential issues. SvelteKit does its own magic with anchors, and a lot of times you aren't trying to navigate to a different route but affect a state change on the current route. The existence of aria attributes in the HTML spec acknowledges that you can't always use the correct elements, so I think it's worth discussing further here too. |
I would agree that there are edge cases where you may need to use other elements, but this does not justify adding obscure magic to Svelte that will lead to problems elsewhere. Often what people try to do as a workaround via events does not even work; the warning you get from Svelte also confirms that, as it fails to mention that enabling focus is also required to make keyboard events trigger.
That is why most of the time you should just use |
Describe the problem
I get this a11y message all the time when I put a
on:click
on an element that isn't normally clickable. It says I also need to have aon:keydown
etc... so if someone is navigating with their keyboard, it will still work.The problem is the code for this is extremely verbose:
Yes, it is possible to write an abstraction that would cut down on it a bit, but then everybody's creating different solutions to the same issue.
Describe the proposed solution
Navigation by keyboard is usually only a few keys: Tab, Shift + Tab, Enter, Arrow Keys.
The same way Svelte has a lot of things like conditional attributes to tighten up the render markup, I think it would be cool if there were aliases that could be used. For example:
on:click={clickHandler} on:enter={clickHandler}
I feel like this is the actual intent 99% of the time, and if this alias existed and satisfied the a11y warning I bet people would use it.
Alternatives considered
Trying to combine the event handlers like
on:click:enter
- but I think that would be messy looking, and they are still two different events (the functions would receive a MouseEvent for click and a KeyboardEvent for enter, so should be handled separately.Importance
would make my life easier
Additional Information
I know I can create my own use actions. I will try to do
<svelte:window use:enter />
and see if that lets me implement this on all elements. I hope I don't have to douse:enter on:enter={}
for each time.The text was updated successfully, but these errors were encountered: