Move SearchGlobalShortcut to a colocated hook

This commit is contained in:
Claudio Ortolina
2025-07-17 17:32:24 +01:00
parent bad6247c69
commit 5e73231767
5 changed files with 27 additions and 24 deletions
@@ -10,6 +10,8 @@ defmodule MusicLibraryWeb.Layouts do
"""
use MusicLibraryWeb, :html
alias Phoenix.LiveView.ColocatedHook
embed_templates "layouts/*"
attr :current_section, :atom, required: true
@@ -38,13 +38,33 @@
</div>
</div>
<div class="flex space-x-2 md:space-x-4 items-center">
<script :type={ColocatedHook} name=".SearchGlobalShortcut">
export default {
mounted() {
const universalSearchButton = document.querySelector("#universal-search-button");
document.addEventListener("keydown", (event) => {
switch (event.key) {
case "k":
if (event.metaKey || event.ctrlKey) {
event.preventDefault();
universalSearchButton.click();
}
break;
default:
break;
}
});
},
};
</script>
<.button
id="universal-search-button"
variant="ghost"
title={gettext("Search (Ctrl+K)")}
phx-click="open_modal"
phx-target="#universal-search"
phx-hook="SearchGlobalShortcut"
phx-hook=".SearchGlobalShortcut"
>
<span class="sr-only">{gettext("Search (Ctrl+K)")}</span>
<.icon name="hero-magnifying-glass" class="h-5 w-5" />