I am trying to display different live components depending on the device kind, so I have this in my template
<div phx-hook="SpecialScreenSize" id="screen-size"></div>
<div>
<%= @device_kind %>
<%= if @device_kind == :mobile do %>
<.live_component
module={NextlairWeb.ListingLive.ShowComponents.ShowMobile}
id="listing-mobile"
current_user={@current_user}
page_title={@page_title}
/>
<% else %>
<.live_component
module={NextlairWeb.ListingLive.ShowComponents.ShowDesktop}
id="listing-mobile"
current_user={@current_user}
page_title={@page_title}
/>
<% end %>
</div>
Unfortunately, the show_desktop live component is the one that's shown regardless of the device kind. I noticed when I remove the markup to display the live components and conditionally show the device kind, it works as expected. What could I be doing wrong?
You are likely running a hook
SpecialScreenSizeto decide whether the screen size is mobile or desktop. The hook is executed later and you have to trigger a rerender from phoenix to updateThis is better done with CSS. The screen size is a client parameter and CSS will help you do that.
My suggestion is to apply a hide CSS to the mobile component using a CSS @media selector. The CSS can be
Now the components
If you are using tailwind css