x
1
<form class="button_to" method="post" action="/lookbook/home/index"><button class="btn" data-controller="hotkey" data-action="keydown.w@document->hotkey#click" data-turbo-confirm="Are you sure?" type="submit">Click or press w</button><input type="hidden" name="authenticity_token" value="N1AlyJe75x9appteL56XcDsQr1IWiRx8e264QhKI7XUB5K9aBtpJUR_osgPa0k1ff6DGv-Lk0flrR_inxzyFxw" autocomplete="off" /></form>
1
<%= button_to "Click or press w", nil, class: "btn", data: { controller: "hotkey", action: "keydown.w@document->hotkey#click", turbo_confirm: "Are you sure?" } %>
CSS is not required or multiple files are needed, check the notes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
click(event) {
this.#isClickable && !this.#shouldIgnore(event) && this.element.click()
}
#shouldIgnore(event) {
return event.defaultPrevented || event.target.closest("input, textarea")
}
get #isClickable() {
return getComputedStyle(this.element).pointerEvents !== "none"
}
}