Handling focus with JS commands - is there a way to conditionally trigger the phx-window-keydown only if that modal is open?

I recently had the same problem while migrating my dropdowns from Alipine to JS commands.

I ended up removing the phx-window-keydown and in the hook, adding an event listener on keydown when key is Escape. Then event triggered the JS I had defined for phx-click-away on the dropdown panel, using this.liveSocket.execJS.

If you look at livebeats they click the document body and focus the element in the menu(?) Hook.