Skip to content

Commit

Permalink
Move keyUp handling to JS hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
errantsky committed Nov 19, 2024
1 parent 5557337 commit 241d0b8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
18 changes: 17 additions & 1 deletion assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,23 @@ let Hooks = {}
Hooks.FocusSearch = {
mounted() {
this.handleEvent("focus_search_input", () => {
document.getElementById("search-input").focus()
document.getElementById("search-input").focus();
});

window.addEventListener("keydown", (event) => {
const activeElement = document.activeElement;

if (
activeElement.tagName === "INPUT" ||
activeElement.tagName == "TEXTAREA" ||
activeElement.isContentEditable
) {
return;
}

if (["/", "n"].includes(event.key)) {
this.pushEvent("keyboard-shortcut", { key: event.key })
}
});
},
};
Expand Down
5 changes: 0 additions & 5 deletions lib/phoenix_vault_web/live/snapshot_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,6 @@ defmodule PhoenixVaultWeb.SnapshotLive.Index do
end
end

@impl true
def handle_event("keyboard-shortcut", _, socket) do
{:noreply, socket}
end

@impl true
def handle_event("refresh_snapshot", %{"sid" => snapshot_id}, socket) do
Logger.debug("Triggering a refresh event.")
Expand Down
1 change: 0 additions & 1 deletion lib/phoenix_vault_web/live/snapshot_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<form phx-submit="search" phx-change="change_query" class="search-form">
<input
id="search-input"
phx-window-keyup="keyboard-shortcut"
phx-hook="FocusSearch"
type="text"
name="query"
Expand Down

0 comments on commit 241d0b8

Please sign in to comment.