DaAnalyst

DaAnalyst

Can function components be made to work properly with `defdelegate` somehow?

Maybe I’m asking too much and maybe this is a language issue, not a LiveView one but still, can it be made for function components to be defdelegated to from another module and work as good when used from that other module?

By work as good I mean those slot's and attr's reporting compile time errors when something that’s required is not specified.

Ex:

defmodule SpecificComponents do
   use Phoenix.Component

   attr :id, :string, required: true
   attr :name, :string, required: true
   attr :role, :string, default: nil
   def account( assign)

   embed_templates "specific_components/*"  # containing `account.html.heex`
end
defmodule OverallComponents do
    defdelegate account( assigns), to: SpecificComponents
end
defmodule Example do
  ...

  import OverallCompoenents

  def render( assigns) do
     ~H"""
     <div>
       <.account id="account" />
     </div>
     """
  end
end

This compiles (on latest Phoenix, LiveView, Elixir) without complaining the otherwise required name attribute is missing.

Most Liked

LostKobrakai

LostKobrakai

defdelegate is just a macro replace typing out the following

@doc delegate_to: {SpecificComponents, :account, 1}
def account(assigns) do
  SpecificComponents.account(assigns)
end

Nothing here creates’s attrs or slots for OverallComponents.account/1. I’m not sure if the live view compiler could make use of the doc metadata.

Personally I’d argue that this seems like useless complexity. Why not use the SpecificComponents one directly. No indirection and no need to search around where the actual code is. The docs the LSP shows will be correct and so on.

sodapopcan

sodapopcan

Well this explains a lot… d’oh me. cc: @dviramontes

But ya, I used a very similar pattern at my last job, mostly so all components could be mass imported.

Where Next?

Popular in Proposals: Ideas Top

sbennett33
When building a component library, it is often useful to give users the ability to customize the underlying element or component to use. ...
New
woylie
We are seeing a lot of warning logs like this: navigate event to "https://someurl" failed because you are redirecting across live_sessio...
New
azyzz228
The slow network is known to be an Achilles heel of LiveView’s architecture. Recently, I was working on creating a fast rendering map wi...
New
andreamancuso
Hey folks, This might sound niche, but I think it’s worth bringing up - especially given Phoenix’s reputation for being lightweight, por...
New
marcandre
I notice that most events have bindings (e.g. phx-keyup) but not the input event. The input event is the preferred way to interact with ...
New
engineeringdept
In 2026 double submit/session tokens are no longer necessary to prevent against CSRF attacks. Instead, we can use the Sec-Fetch-Site head...
New
sevensidedmarble
Hello all, Apologies if this has been proposed before I guess, but I have a very simple one: With the increasing importance of LV, I th...
New
BartOtten
I’d like to propose that we refrain from using the term "DeadView" as the opposite of “LiveView” and instead choose an alternative. A new...
New
spicychickensauce
I’ve previously explored what is possible today with hacks to implement view transitions in our apps: I have since created a fork to im...
New
Flo0807
Hello everyone! Phoenix LiveView v0.18 introduced the special attributes :let, :for and :if. In addition to the :if special attribute, I...
New

Other popular topics Top

Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement