Skip to content
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

useHotKeys: Allow unbinding hotkeys within a scope if provided #244

Closed
wants to merge 1 commit into from

Conversation

tkoenig
Copy link
Contributor

@tkoenig tkoenig commented Nov 24, 2022

This partially fixes #177 , as it at least now allows to unbind the shortcuts within a scope if one has been provided.

Example

import { Controller } from "@hotwired/stimulus";
import { useHotkeys, useThrottle } from "stimulus-use";

export default class extends Controller {
  static values = { key: String, scope: { type: String, default: "app" } };
  static throttles = ["fire"];
  connect() {
    useThrottle(this, { wait: 2000 });
    hotkeys = {};
    hotkeys[this.keyValue] = { handler: this.fire, options: { scope: this.scopeValue } };
    useHotkeys(this, { hotkeys: hotkeys });
  }

  fire() {
    this.element.click();
  }
}
<button data-action="dialog#close" data-controller="hotkey" data-hotkey-scope-value="preview" data-hotkey-key-value="Esc" type="button">
  Close
</button>


<button data-action="selection#empty" data-controller="hotkey" data-hotkey-scope-value="selection" data-hotkey-key-value="Esc" type="button">
  Empty selection
</button>

So when Closing the dialog, the Esc key gets unbound correctly, while the Empty selection hotkey will still work.

within a scope if one has been provided (see also stimulus-use#177)
@tkoenig
Copy link
Contributor Author

tkoenig commented Nov 28, 2022

Actually it was easier using hotkeys.js directly and since stimulus will get support for keyboard events (hotwired/stimulus#442) there is probably no more need for that.

@tkoenig tkoenig closed this Nov 28, 2022
@tkoenig tkoenig deleted the use-hotkeys-unbind-scope branch November 28, 2022 09:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant