atom / atom-keymap

Atom's selector-based keymap system
MIT License
105 stars 58 forks source link

Question: Allowing simple keymaps to bubble up to native event as well. #215

Closed envex closed 7 years ago

envex commented 7 years ago

This is setup in an electron app (v1.6.7) with react (v15.1).

I have a select input that is set to multiple and I've setup an event in my component.

My event is setup like so in KeymapManager:

'.selectable': {
  up: 'up-event'
}

When I have this event setup, the native up/down arrow events are blocked for the select input. Ideally, I'd like to be able to fire my up-event and have it bubble up so the native event is also fired.

Note: If this is against the guidelines, or irrelevant, please close.

nathansobo commented 7 years ago

Interesting. We currently call preventDefault and stopPropagation to indicate that the keymap system has "consumed" the event by transforming it into the specified command, but I don't actually have a clear picture of exactly what would go wrong if we didn't do that. It's been a long time since that decision was made.

Could you explain the specifics of your situation? Do you have a proposal for how it would work?

envex commented 7 years ago

The example in this situation is I have a searchable item component. In that component is an input and a select dropdown (set to multiple so it acts/looks like a list).

Natively, once the select is focused, you can use the arrow keys to navigate through the list. You can see that functionality here: http://jsbin.com/geremameme/edit?html,output

I've setup a custom up (and down) key event so that if the input is focused, pressing the down arrow would focus the select.

With the key-map attaching the down event, the native down event is blocked and only the key-map down is fired.

Ideally it would be nice if I could either:

// Badly coded example
window.addEventListener('up-event', (event) => {
  // Do something

  event.bubble();
});
nathansobo commented 7 years ago

Could you map the up arrow to the special native! directive or use the .native-key-bindings class on your component and do your own event handling?

envex commented 7 years ago

Yeah, I'm just handling it natively for now. I was just curious if there was a way to use keymap for that event/situation.

nathansobo commented 7 years ago

You can also call abortKeyBinding on the emitted command event to tell the keymap system that even though the binding matched and emitted a command, you still want to continue as if the key binding didn't match. This wouldn't prevent other bindings further up in the document from matching the event however. Does that address your issue?

envex commented 7 years ago

Interesting. That may be the key. Let me test it out and I'll get back to you.

Thanks!

envex commented 7 years ago

That did the trick, thanks!