innocentoldguy

innocentoldguy

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?

Showing Posts 1 to 2

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.

innocentoldguy

innocentoldguy OP

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews