Skip to content

Aliases for certain type of key press events #8273

@danielimmke

Description

@danielimmke

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 a on:keydown etc... so if someone is navigating with their keyboard, it will still work.

The problem is the code for this is extremely verbose:

on:click={clickHandler} on:keydown={(e:KeyboardEvent) => { if(e.key === 'Enter') return clickHandler(e) }}

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 do use:enter on:enter={} for each time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions