rhcarvalho
The Phoenix template for new apps generated with phx.new sets an alias that allows using JS commands in HEEx templates and other places conveniently without the full module name. For instance, most of the time we can write
<button phx-click={JS.show(to: "#modal", transition: "fade-in")}>
show modal
</button>
instead of
<button phx-click={Phoenix.LiveView.JS.show(to: "#modal", transition: "fade-in")}>
show modal
</button>
ColocatedHook (and ColocatedJS) would benefit from a similar alias, making colocated hooks less verbose to use.
With the alias we could write:
<script :type={ColocatedHook} name=".PhoneNumber">
// ...
</script>
Considerations
- The impact on the existing eco-system: ColocatedJS and ColocatedHook were released recently in Phoenix LiveView 1.1. Adding the alias would simplify future projects, with no impact to existing usage with the fully qualified module path.
- The impact on learning: It would make ColocatedJS and ColocatedHook as easy to use as JS commands in HEEx templates.
- Whether your suggestion is easily implemented or inline with the state of Phoenix as it is today: The implementation is a simple template update. New apps already depend on LiveView 1.1, so the new modules are guaranteed to be available.
- Whether your suggestion would benefit Phoenix or the community as a whole: I’m biased. But having setup the alias in my current project, I find the less verbose template code clear enough, no ambiguity. In fact, I had once copied the example in the release notes just to realize it didn’t compile (when I didn’t have the alias).
Implementation
If accepted, I can provide a PR. The diff:
diff --git a/installer/templates/phx_single/lib/app_name_web.ex b/installer/templates/phx_single/lib/app_name_web.ex
index 06c762714..e90288a8c 100644
--- a/installer/templates/phx_single/lib/app_name_web.ex
+++ b/installer/templates/phx_single/lib/app_name_web.ex
@@ -88,6 +88,8 @@ defmodule <%= @web_namespace %> do
import <%= @web_namespace %>.CoreComponents
# Common modules used in templates
+ alias Phoenix.LiveView.ColocatedHook
+ alias Phoenix.LiveView.ColocatedJS
alias Phoenix.LiveView.JS
alias <%= @web_namespace %>.Layouts
diff --git a/installer/templates/phx_umbrella/apps/app_name_web/lib/app_name.ex b/installer/templates/phx_umbrella/apps/app_name_web/lib/app_name.ex
index d8b1f9543..87a06fc35 100644
--- a/installer/templates/phx_umbrella/apps/app_name_web/lib/app_name.ex
+++ b/installer/templates/phx_umbrella/apps/app_name_web/lib/app_name.ex
@@ -88,6 +88,8 @@ defmodule <%= @web_namespace %> do
import <%= @web_namespace %>.CoreComponents
# Common modules used in templates
+ alias Phoenix.LiveView.ColocatedHook
+ alias Phoenix.LiveView.ColocatedJS
alias Phoenix.LiveView.JS
alias <%= @web_namespace %>.Layouts
Trending in Proposals: Ideas
We are seeing a lot of warning logs like this:
navigate event to "https://someurl" failed because you are redirecting across live_sessio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
rhcarvalho
An optional accompanying change would be updating the LiveView docs to use the non-fully qualified module name in examples.
steffend
I was thinking about this, but decided to not alias by default because I don’t think it’s used so frequently (especially ColocatedJS). Maybe I’m wrong, but I’d like to get more feedback on this.
What we can definitely do is add a tip to the docs to set the alias in case people use it more often in their projects!
rhcarvalho
I agree regarding frequency. I certainly have way more use of
JScommands than I ever expect to haveColocatedHook(andColocatedJS).What I realized was that even for a single use the fully qualified module name was verbose and harder to remember. Considering those names are so specific (because of the “colocated” prefix), I decided they are too unlikely to conflict with anything in my project and went with the aliases in
myapp_web.ex.Let’s hear who else is using the new goodies in their projects