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

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

Other popular topics Top

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
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
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement