I have a navbar component that I include at the top of all my live views. Since it reacts differently based on certain attributes passed to it that are loaded in live views, I can’t just stick it in live.html.heex
.
So the top of a typical live view render function looks like this:
def render(assigns) do
~H"""
<.live_component
id="navbar"
module="MyApp.SomeNamespace.NavBar"
logged_in?={@logged_in?}
some_decision_based_on_liveview_assigns={@some_assign_unavailable_in_the_layout}
/>
"""
end
While I’m the type who loves some pretty code, I don’t strive for it at all costs if it hurts clarity. But I also hate noise and this started to feel really noisy. I realized that id
and module
are always static, so then I had the idea to wrap the live component in a functional one:
def navbar(assigns) do
~H"""
<.live_component
id="navbar"
module="MyApp.SomeNamespace.NavBar"
logged_in?={@logged_in?}
some_decision_based_on_liveview_assigns={@some_decision_based_on_liveview_assigns}
"""
end
So now in my live views I can simply do:
def render(assigns) do
~H"""
<.navbar logged_in?={@logged_in?} some_attr={@some_attr} />
"""
end
Other than that it’s hiding away a live component and that it maybe feels a little icky, I’m wondering: are there any drawbacks?
In anticipation of someone quoting that last sentence back to me saying, “That’s what’s wrong with it!”, my thoughts are that I would only use it in this one case where it’s going to be on basically every live view in my app. Maybe calling it <.live_navbar />
would it clear that it’s lying about being functional?
I realize this is a bit of a bike shed, but I’m interested in opinions if they are out there, especially if there is some real danger I’m not thinking of.
Thanks!