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

benkimpel
Background I work at a hedge fund and our traders need highly dynamic UIs (think splitters, tabbed panels, tree lists, enormous data grid...
New
beno
There appears to be no way to escape whitespace in word lists, so sigil_w (and sigil_W) can only be used for lists of single words. Of co...
New
MUSTDOS
Hello all! assign(socket, :name, “Elixir”) Why can’t we have assign/stream take a group of atoms and maps/structs as a default to r...
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
davydog187
I’ve been enjoying the new Streams API, but I keep needing to work around the lack of access to the underlying items. One issue that cons...
New
mxgrn
As one edits a Phoenix LiveView form in a modal, it’s very easy to accidentally press ‘Esc’ and lose all the edits. Similar to data-conf...
New
nunobernardes99
On 1.8+, when we generate an authentication system with mix phx.gen.auth we can make use of magic link login which is amazing and a great...
New
MatinDevs
Currently, there are ongoing discussions about enhancing Phoenix LiveView, particularly focusing on improving performance and user experi...
New
rmoorman
Current situation Currently, the structure of the HTML returned by phoenix is determined by the layouts (components/layouts/[root,app].ht...
New
zachdaniel
One thing I find is that I typically “start” with a LiveView even for static pages (in cases where I know I’m likely to add interactivity...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31194 112
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New

We're in Beta

About us Mission Statement