Currently the Phoenix.LiveView.JS
struct is defined as an @opaque
type, meaning it’s fields are internal api not to be relied upon.
I have the following use case I’d like to support to keep working in elixir land as much as possible:
- I have a toast notifications implementation in JS
- The liveview process sends events to show or dismiss toasts, and the client handles via
window.addEventListener
, so I’m able to fully control the toasts api with liveview - I need to add additional behavior to the toasts: the toasts can have an action, so when the user clicks them some JS will run
- This can be achieved normally with the
phx-click
attribute set on the toast, and it works if the value is a simple event name string and the value is inphx-value
, but it gets ugly to implement when the value should be the result of many chains ofJS.*
functions
This is trivial to solve if I do this:
%{
actions: %{
"Foo" => Jason.encode!(JS.dispatch("foo", detail: %{some: "arg"}).opts)
}
}
However, the opts
field is private, so I can’t just rely on this approach always working.
Is there any chance of this field being public in the near future, or for the JS
struct to be json serializable via some phoenix public api?