Created
December 24, 2024 01:47
-
-
Save dpaluy/fc5b53139e10c4c1244b1ad3e016413f to your computer and use it in GitHub Desktop.
Keyboard shortcuts for StimulusJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app/javascript/controllers/click_controller.js | |
import { Controller } from "@hotwired/stimulus" | |
export default class extends Controller { | |
click() { | |
this.element.click() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%# app/views/shared/_keyboard_shortcuts.html.erb %> | |
<! -- clicking "n" will click the link --> | |
<%= link_to "New Post", new_post_path, | |
data: { | |
controller: "click", | |
action: "keydown.n@window->click#click" | |
} %> | |
<! -- clicking "Ctrl+K" will click the link --> | |
<button data-controller="click" | |
data-action="keydown.meta+k@window->click#click keydown.ctrl+k@window->click#click"> | |
Search | |
</button> | |
<! -- clicking "Cmd+Enter" will click the link --> | |
<button data-controller="click" | |
data-action="keydown.cmd+enter@window->click#click keydown.ctrl+enter@window->click#click"> | |
Submit | |
</button> |
Author
dpaluy
commented
Dec 24, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment