Rich_Morin

Rich_Morin

How to comment Tailwind CSS class soup?

ElixirConf 2022 - Chris McCord - Phoenix + LiveView Updates contains (around 22:20) an example of Tailwind CSS class soup, used to define a button:

"phx-submit-loading:opacity-75 w-full inline-flex justify-center ...

The class names go on for several lines and, aside from making my eyes cross, have no indication of what they’re supposed to be doing.

If I were writing this, I’d want to break the definition onto multiple lines and comment each one with its purpose, caveats, etc. One way to do this would be to code the class names as a list of strings (one per line, with comments) and then concatenate them for actual use. However, I suspect there’s a cleaner way. Help?

-r

Marked As Solved

Rich_Morin

Rich_Morin

OK, I think I get it now. Because of all the layers of syntax, this isn’t just a string of class names. Rather, it’s a list of strings, attributes, etc. So, I could do something like this:

class={
  [
    "phx-submit-loading:opacity-75",
    "w-full",         # foo
    "inline-flex",    # bar
    "justify-center", # baz
    ...
  ]
}

In practice, I don’t think I’d spread things out this much. Rather, I’d group related names, adding comments as needed.

Also Liked

andreaseriksson

andreaseriksson

I have worked with Tailwind since early 2020 and to be honest, this is not a problem (IMO). However, to answer your question. You would solve this with having reusable components and/or a Tailwind framework like DaisyUI

With components you can split up the classes in a list:

        class={[
          input_border(@errors),
          "mt-2 block w-full rounded-lg border-zinc-300 py-[7px] px-[11px]",
          "text-zinc-900 focus:outline-none focus:ring-4 sm:text-sm sm:leading-6",
          "phx-no-feedback:border-zinc-300 phx-no-feedback:focus:border-zinc-400 phx-no-feedback:focus:ring-zinc-800/5"
        ]}

(this is from the new phx 1.7 btw)

However, you can also name it with a custom class name and in the css-file define the Tailwind styes for that class.

.my-class {
  @apply bg-yellow-300 flex items-center;
}

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement