innocentoldguy

innocentoldguy

Is it possible to pass partial TailwindCSS values into a LiveView Component Function?

I am seeing this issue in:
Elixir 1.15.0
Phoenix: 1.7.6
LiveView: 0.19.0

I’m having an issue with passing TailwindCSS values into a LiveView Function Component and can’t figure out why it isn’t working as expected. Here’s an example that works:

Function Component:

defmodule Example.Components.SVG do
  use Phoenix.Component
  
  attr :height, :string, default: "h-16"
  attr :width, :string, default: "w-16"

  def image(assigns) do
    ~H"""
    <div class={"#{@height} #{@width} bg-red-500"}></div>
    <%= render_slot(@inner_block) %>
    """
  end
end

HEEX Template

<Example.Components.SVG.image height="h-8" width="h-8">
  This is a test.
</Example.Components.SVG.image>

The code snippets above render this HTML <div class="h-8 w-8 bg-red-500>...</div> and display correctly in the browser.

The above code works, but since I always want this DIV to be a square, it seems more efficient to do this:
Function Component

defmodule Example.Components.SVG do
  use Phoenix.Component
  
  attr :size, :string, default: "16"

  def image(assigns) do
    ~H"""
    <div class={"h-#{@size} w-#{@size} bg-red-300"}></div>
    <%= render_slot(@inner_block) %>
    """
  end
end

HEEX Template

<Example.Components.SVG.image size="8">
  This is a test.
</Example.Components.SVG.image>

The second set of code snippets also render this HTML <div class="h-8 w-8 bg-red-500>...</div> ; however, they do not render correctly in the browser. Instead, the browser behaves as if no CSS exists at all.

This is true for ALL TailwindCSS classes. For example, if I try to use string interpolation to render different shades of red:

This works <.custom_tag background="bg-red-300">class={"#{@background} ..."}

This fails to render <.custom_tag background="300">class={"bg-red-#{@background} ..."}

Both code snippets will produce this HTML: <div class="bg-red-300">...</div>

However, only the first example renders correctly. The browser ignores the second CSS completely, even though it is identical.

Is this just a shortcoming of LiveView in Chrome, or is there something special I need to do to in order to use string interpolation to generate part of a TailwindCSS class?

Most Liked

dkulchenko

dkulchenko

This is a limitation of Tailwind, unrelated to Phoenix or LiveView. Search for the “Dynamic class names” section here: Detecting classes in source files - Core concepts - Tailwind CSS

You have to have the full class name (unbroken) present someplace in the code for the Tailwind JIT to include it in your CSS bundle.

The reason you’re not seeing anything when you construct the class name dynamically is that those classes are not being ”seen” and thus not built by Tailwind.

Last Post!

innocentoldguy

innocentoldguy

Thanks! I remember this about Tailwind now that you say it.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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

We're in Beta

About us Mission Statement