100phlecs

100phlecs

TailwindFormatter - Opinionated tailwind class sorter for HEEx templates

Here’s the repo link: GitHub - 100phlecs/tailwind_formatter: Formats tailwind classes within elixir projects

The idea is to have a consistent ordering of tailwind classes so it is easier to read through.
The solution uses Regex which, at times, feels a little brittle, but so far works in most cases.

It bails if it doesn’t have a clean selection to work with, so if you end up using it you may find sometimes the classes won’t sort. This usually happens due to too many inline elixir functions which I’ve yet to investigate.

Still, it’s ‘alpha’ at the moment, i.e. if there are a lot of inline elixir functions it may not sort.
Been using it in my private repositories without much issue though!

This is my first elixir library package, so feel free to critique/send PRs.
Still rather new to elixir :slight_smile:

If you’re curious on how I gathered all of the classes and variants, I pulled them out of the official tailwindcss library, with plugins attached. Here is the unorganized repo where I generate them: tailwindsort/index.js at d823656f1ffe26519f21fe4e458de482534121ee · 100phlecs/tailwindsort · GitHub

Hope you find it useful!

Most Liked

100phlecs

100phlecs

New release, thanks to those who raised issues & contributed!

Shoutout to @aptinio for ironing out the expression sorting, sorting multiple :class attributes & handling leading-trailing spacing issues between string fragments. :slightly_smiling_face:

I’ve added some highlights in the changelog.

A quick summary is that most if not all of the code fragments within a class={} are supported and sorted.

You can also now load in your custom TailwindCSS configuration instead of using the default ones I’ve generated awhile back.

Note: This release requires Elixir v1.15+. We’ve dropped MultiFormatter as Elixir v.1.15+ now supports multiple formatters.

11
Post #7
100phlecs

100phlecs

New release!

TailwindFormatter now includes TailwindFormatter.MultiFormatter as a .formatter.exs plugin option, which will format your heex templates with Phoenix.Liveview.HTMLFormatter first, then with TailwindFormatter. This makes it so you no longer have to deal with scripts in order to use TailwindFormatter on a Phoenix project.

Also includes a bug fix on preserving original class order for user-defined classes.
Thanks to zachallaun the README is cleaned up!

Thank you to those who suggested these changes :slight_smile:

c4710n

c4710n

Nice work.


For guys don’t like extra deps, and want to sort classes and group classes manually, you can do something like this:

<body class={[
  "h-screen overflow-hidden",
  "bg-gray-300",
  "select-none"
]}>
  <%= @inner_content %>
</body>

A: Why do you group classes?
B: Because I want to seperate the concerns, like:

  1. Positioning
  2. Box model
  3. Typographic
  4. Visual
  5. Misc

This idea comes from Code Guide by @mdo.

100phlecs

100phlecs

New release.

  • Fixed a bug where class lists that had . or # in them were not sorted
  • Allow dynamic classes, i.e. grid-cols-#{@cols}, previously they were broken up
  • Validate any inline elixir with Code.string_to_quoted, raises an error otherwise

I’m using a new sort method by first “placeholding” any elixir functions with $#{num}, sorting, and then putting them back in. This makes the Regex easier, and also makes it easy to support dynamic classes.

Thank you to those who brought these issues up :slight_smile:

100phlecs

100phlecs

This separation of concerns could be a possible formatting option.

The main barrier would be labeling all the tailwind classes with each group. Another issue would be user-defined classes, they would probably just be filed under “Misc”

Where Next?

Popular in Libraries Top

Qqwy
TypeCheck: Fast and flexible runtime type-checking for your Elixir projects. Core ideas Type- and function specifications are const...
336 13801 100
New
kip
Image is an image processing library for Elixir. It is based upon the fabulous vix library that provides a libvips wrapper for Elixir. I...
574 16576 179
New
mindok
What is ContEx? A pure Elixir server-side data plotting/charting library outputting SVG. It has nice barcharts in particular and works g...
New
blatyo
https://www.conduitframework.com/ The best overview for how things are tied together is this presentation. Modules and functions are pre...
New
asiniy
Hey there! I wrote a download elixir package which does exactly what its name about - an easy way to download files. I saw solutions ...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
martinthenth
Hello everybody :wave: Recently, some of my colleagues talked about database ids and uuids and their problems, and I remembered the pain...
New
OvermindDL1
I created a new library (rather I pulled out a couple files from my big project), it manages an operating system PID file for the BEAM. ...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
versilov
Could not wait for the missing Elixir ML libraries to appear, so, I wrote one myself, taking https://github.com/sdwolfz/exlearn as a foun...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New

Sub Categories:

We're in Beta

About us Mission Statement