sodapopcan

sodapopcan OP

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? :thinking:

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!

First 8 of 8 Posts Switch mode

mayel

mayel

Well this isn’t an answer but FYI live.html.heex has access to all assigns from your LiveView, so you could stick the nav there.

sodapopcan

sodapopcan OP

No, that is an answer. I did know that but I wanted to avoid having to default all the “special” attrs to nil for the layout. Of course, now that I’m saying that out loud, that’s actually not such a big deal since in my case I only have one—I was really just prematurely optimizing for a scenario I probably will never have. Heh, ok yes, haha, thank you!

cmo

cmo

I was pondering my navbar recently and came to the conclusion that it shall be a function component in live.html.heex and it’s brains shall be contained in an on_mount hook.

sodapopcan

sodapopcan OP

That’s along the lines of one of my thoughts, although I was gonna put stuff in on_mount and still use a live component since my navbar makes database calls using optionally using assigns from the current page (liveview). It felt a little perverse. But ya, I think it’s going to end up having to be in the on_mount callback as since I’ve posted this, I’ve already felt the pain of having to include boilerplate on every liveview I want a navbar on… which is all of them o_O This also means caring about the url in on_mount which is unfortunate but there are worse things in life.

cmo

cmo

Know that you’re not alone in your suffering :wink:. I tried a liveview, a sticky liveview, a layout component that I included in every liveview and a live component. The root level function component + on_mount is the only one that could cater for everything my heart desires. This is indeed how they do it in live_beats.

sodapopcan

sodapopcan OP

That’s interesting—I’ve never defined on_mount right in a liveview before. I see it’s doing to attach hooks but… I’ll have to dig into that later, and really finishing digging more into LiveBeats.

cmo

cmo

They aren’t defining it in a liveview. This module is called from the live_session, as per these lines.

sodapopcan

sodapopcan OP

Oh jeez, yes, I have to stop going on elixirforums at the end of my day. Cool cool, thank you!

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
AstonJ
Just a general thread to post chat/news/info relating to AI/ML stuff that may be relevant for Nx now or in the future. Got anything to sh...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
mudasobwa
While I am working on the Language Agnostic Code Audit SaaS, which uses MetaAST (spoiler: I am expecting it to be in a good shape for ann...
New

We're in Beta

About us Mission Statement