I have two proposals - not sure if I should split them into different threads but I think they’re related:
- Add a new
JS.call/2
function that allows us to invoke native DOM methods. - Support meta key bindings (e.g.
cmd + k
) either throughphx-key
or by adding a newphx-meta-key
binding.
Calling DOM methods
Today, to invoke native DOM methods (e.g. dialog.showModal()
, dialog.close()
, etc.) in LiveView, we must either create a custom hook or dispatch an event.
Proposal
Introduce a new function:
JS.call("method_name", to: "#selector", args: [...])
This would allow us to easily call DOM methods without having to write any JavaScript. A common use case would be opening a dialog
element:
<dialog id="my_dialog">...</dialog>
<button phx-click={JS.call("show_modal", to: "#my_dialog")}>
open dialog
</button>
Meta key bindings
Similarly, Phoenix LiveView has some useful key bindings like phx-keydown
, phx-keyup
, etc. When working on complex UIs, we often need to provide keyboard shortcuts that use meta keys.
Today, we can either have a custom hook or a combination of phx-window-keydown
and handle_event/3
to accomplish this.
Proposal
Either extend phx-key
or add a new phx-meta-key
binding for shortcuts:
<dialog id="my_dialog">...</dialog>
<button phx-meta-key="k" phx-window-keydown={JS.call("show_modal", to: "#my_dialog")}>
open dialog
</button>
The example above would allow to open the dialog
element using cmd+k
or ctrl+k
without the need to write any JavaScript nor handle_event/3
callbacks.
I think those additions would simplify a large class of UI behaviors and further reduce the need for custom hooks or imperative JS for native interactions. Happy to help with a PR or implementation sketch if needed.