overcomeoj

overcomeoj

Using view helper with do block in LiveComponent causing "replace all" html updates

I am using tailwind, so I have made myself a few helper functions to stamp common css classes or html. One of those is a container that accepts a block, wrapping that content in a div.

  def container_box(opts \\ [], do: content) do
    %{class: class} = Enum.into(opts, %{class: ""})
    ~E"""
    <section class="classes... <%= class %>">
    <%= content %>
    </section>
    """
  end

One of my (stateful) components (a modal) calls this function at the top of it’s template.

<%= container_box() do %>

    <h1 class="font-bold mb-12">User</h1>

    <%= f = form_for @changeset, "#",
      id: "user-form",
      phx_target: @myself,
      phx_change: "validate",
      phx_submit: "save",
     ....

I noticed that when I interact with the form, my change digests as described in the console are just one fat lump. I just get an object that’s got one key, a huge string book ended by my “container_box” <section> tags.

If I remove the calls, my thin updates are restored and I get update-objects with more than one key.

My understanding is that live view will construct a mapping of all variables used and just send updates to elements where those variables would be effected. I guess because the whole template is wrapped in a call, it just sees it as one ever changing leex var/string/struct/object?

Now it all still works but it certainly feels like an indication that I am doing something wrong.

Is there an easy fix that still lets me DRY up the template?

Should my container be another stateless live component? I actually had it like that some time ago, but then read that it’s a wasteful manner when I just want to wrap some strings around something.

I know I can make a new css class that applies all the tailwind classes, but I’m curious if there is a LiveView trick since I have other helpers which do more structural work beyond pinning some classes so I think I might hit this issue again later.

Marked As Solved

sfusato

sfusato

Have a look at LiveEEx Pitfalls section of the documentation:

When it comes to do/end blocks, change tracking is supported only on blocks given to Elixir’s basic constructs, such as if, case, for, and friends. If the do/end block is given to a library function or user function, such as content_tag, change tracking won’t work.

Having recently started working with Tailwind, I too was looking for a solution to its madness (kidding, I’m a newly convert). If there’s something I would suggest for composing components similar to how you would do it in React/Vue that is Surface.

Last Post!

overcomeoj

overcomeoj

Ah, yes, like I thought. Sometimes you read the docs so often you forget whats in them.

Yeah tailwind is very nice for iteration, not 100% sure how I like it on the next step, at least with LiveView. If youre using something that lets you make (non-LV) components easily I think you can avoid some of the pain.

Surface would have the same problems right? I guess it depends where its compile step fits in the LiveView pipeline.

Where Next?

Popular in Questions Top

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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
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

We're in Beta

About us Mission Statement