import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
connect() {
this.onKeydown = this.onKeydown.bind(this)
document.addEventListener("keydown", this.onKeydown)
}
disconnect() {
document.removeEventListener("keydown", this.onKeydown)
}
onKeydown(e) {
if (e.target && ["INPUT", "TEXTAREA"].includes(e.target.tagName)) return
if (e.key === "n") {
const href = this.element.dataset.newUrl
if (href) Turbo.visit(href)
}
}
}
Keyboard shortcuts improve power-user workflows, but they must survive Turbo navigation. Attach on connect/disconnect, avoid global leaks, and scope shortcuts to the page/component.