hotkeys icon indicating copy to clipboard operation
hotkeys copied to clipboard

Setting [hotkeys] on a button only activates hotkey if focused on the button

Open gioragutt opened this issue 3 years ago • 9 comments

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[X] Bug report  
[ ] Performance issue
[X] Feature request
[ ] Documentation issue or request
[X] Support request
[ ] Other... Please describe:

Current behavior

<button
	(click)="console.log('click')"
	hotkeys="meta.shift.5"
	(hotkey)="console.log('hotkey')"
>
	CLICK ME
</button>

I have a form with a bunch of buttons, much like the one above. Long as the form is open, I want the hotkey defined on each button to perform the action of the button. At first, I didn't have the (hotkey) handler, since I assumed it would invoke the (click) handler, but then I looked at the docs and added the (hotkey) handler, but it still didn't work.

The culprit is this piece of code setting the element option without being allowed to override it. I would want to set it as "document.documentElement", for example.

Expected behavior

When I set a hotkey on an element, I want to be able to have the hotkey work without having the element be focused. Perhaps it can be an input flag of the directive, changing the ...hotkeys to happen after setting element to be the host element.

Minimal reproduction of the problem with instructions

See example in "Current Behavior"

What is the motivation / use case for changing the behavior?

I want to easily set hotkeys for buttons for the duration the button lives. Moreover, I want to not have to use HotkeysService directly to force the element to be document.

Environment


Angular version: 10.0.0
@ngneat/hotkeys version: 1.1.1



Browser:
- [X] Chrome (desktop) version 75.0.3770.100
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: XX  
- Platform:  

Others:

gioragutt avatar Nov 02 '21 17:11 gioragutt

You're welcome to create a PR

NetanelBasal avatar Nov 02 '21 18:11 NetanelBasal

You're welcome to create a PR

What's your preferred way to solve this from what I suggested?

gioragutt avatar Nov 02 '21 19:11 gioragutt

Can you elaborate on the suggested solutions, please?

NetanelBasal avatar Nov 03 '21 06:11 NetanelBasal

Can you elaborate on the suggested solutions, please?

It comes down to two ways:

  1. Some declarative Input flat, such as <button hotkeys="meta.a" globalHotkey></button> or something similar
  2. Replace this.hotkeysService.addShortcut({ ...hotkey, element: this.elementRef.nativeElement }) with this.hotkeysService.addShortcut({ element: this.elementRef.nativeElement, ...hotkey }) so that users will be able to override element. This is a breaking change as far as I can see.

gioragutt avatar Nov 03 '21 09:11 gioragutt

I'm OK with adding both. The latter isn't a breaking change.

NetanelBasal avatar Nov 03 '21 09:11 NetanelBasal

I'm OK with adding both. The latter isn't a breaking change.

When you say both, do you mean Both or Either? 😅

gioragutt avatar Nov 03 '21 11:11 gioragutt

Both

NetanelBasal avatar Nov 03 '21 11:11 NetanelBasal

NP. Thanks for the availability 👍🏻

gioragutt avatar Nov 03 '21 11:11 gioragutt

Hi!

What is the progress on this one? I would really like this feature. Can I help in some way?

AdamSundberg avatar Aug 01 '23 08:08 AdamSundberg