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: Content Configuration - 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.

Where Next?

Popular in Questions Top

Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New

Other popular topics Top

vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement