rhcarvalho

rhcarvalho

Hi,

In the past when I needed to set the background image on an element I gave up Verified Routes and wrote something like bg-[url(/images/background.svg)] in my template. The Tailwind compiler then generates a CSS class that sets the background to the image.

I found myself in need of doing it again, and since I couldn’t find an existing thread on using TailwindCSS + VerifiedRoutes I decided to ask – is there a way to use both together?

I tried this but it doesn’t work, because I understand the Tailwind compiler sees Elixir code as is and not the compiled string after the ~p sigil is processed:

<div class={"bg-[url(#{~p"/images/background.svg"})]"}>...</div>

Another idea, which I did not try, would be to add some kind of plugin in tailwind.config.js similar to how Heroicons are made available. I thought perhaps someone in the community has an easier way to offer?

Showing Posts 1 to 9

codeanpeace

codeanpeace

Hmm, wonder if the nested double quotes are the problem…

class={"bg-[url(" + ~p"/images/background.svg" + ")]"}

class={"bg-[url(#{~p'/images/background.svg'})]"}

Just to rule that out, I’d give those a shot ¯\_(ツ)_/¯

rhcarvalho

rhcarvalho OP

Tailwind only looks at the text of the files, without any further interpretation: Detecting classes in source files - Core concepts - Tailwind CSS, so quoting differences don’t matter.

rhcarvalho

rhcarvalho OP

There are a few possibilities listed in Detecting classes in source files - Core concepts - Tailwind CSS.

I could:

Considering that Verified Routes for static paths don’t really verify that the full path exists in disk, but only that the path is well-formed, then I think the possibilities above are not worth it – simply don’t use a verified route for cases like in my OP.

I will feel good if other people share that’s what they do :slight_smile:

harmon25

harmon25

My solution to a similar problem was to use css variables and to extend tailwind background image:

{
 extend: {
    backgroundImage: (theme) => ({
      custom: "var(--bg-image)"
    })
  }
}

this requires setting the --bg-image css variable to url(/some/image.png) which can be done with an inline style, kinda like this:

<div style={"--bg-image: #{url}"} class="bg-custom">
 something here
</div>
rhcarvalho

rhcarvalho OP

A bit of going on a tangent, but something I learned recently is that [...] can also be used to set CSS variables:

This can be useful for things like CSS variables as well, especially when they need to change under different conditions:

<div class="[--scroll-offset:56px] lg:[--scroll-offset:44px]">
 <!-- ... -->
</div>

So that could be used to set --bg-image conditionally, but if the value would be url(...) then again the URL needs to appear as-is within the parentheses or back to Tailwind not picking up the value to generate CSS classes.

arcanemachine

arcanemachine

Just spitballing here, but how about just adding it to the safelist since it’s just the one URL?

LostKobrakai

LostKobrakai

<div class="bg-[url:var(--bg-image)]" style="--bg-image: url(…)">

That one works. The style can be dynamically set in that case.

More details: Adding custom styles - Core concepts - Tailwind CSS

rhcarvalho

rhcarvalho OP

:clap:

I like how you put those ideas together :heart_decoration:

Did you actually reach out for that in existing projects? I’m still finding verified routes may not be worth the trouble in that case.

LostKobrakai

LostKobrakai

I did use that approach before, though with colors not urls.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
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
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
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
widianto
I think I’ve found a small improvement I could contribute to &lt;%= web_namespace %&gt;.CoreComponents (installer/templates/phx_web/compo...
New

Other Trending Topics Top

pckrishnadas88
Hey everyone! :waving_hand: I’ve published Part 7 of the Building Distributed Systems in Elixir series, where we build core distributed ...
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