aptinio
I just published canonical_tailwind, a formatter plugin that canonicalizes Tailwind CSS utility classes in HEEx templates.
- mr-4 custom-btn flex ml-[1rem] flex
+ custom-btn mx-4 flex
It delegates to the tailwindcss CLI’s new canonicalize subcommand — the same engine that powers the Prettier plugin. So you get sorting, normalization, and duplicate collapsing powered directly by the Tailwind CSS engine.
It hooks into mix format via Phoenix LiveView 1.1’s attribute_formatters API and works with LSP format-on-save (tested with Expert).
Setup is two lines — one dep, one formatter config:
# mix.exs
{:canonical_tailwind, "~> 0.1.0", only: [:dev, :test], runtime: false}
# .formatter.exs
[
plugins: [Phoenix.LiveView.HTMLFormatter],
attribute_formatters: %{class: CanonicalTailwind},
]
If you’re already using the :tailwind hex package, it detects your binary and profile automatically. Make sure your tailwind version in config/config.exs is at least 4.2.2 and run mix tailwind.install after changing it.
Requires Elixir ~> 1.18, Phoenix LiveView ~> 1.1, and tailwindcss CLI >= 4.2.2 (the first version with canonicalize). The canonicalize --stream flag that makes this possible was merged in tailwindcss#19796.
Hex: canonical_tailwind | Hex
Docs: canonical_tailwind v0.3.0 — Documentation
Trending in Announcing
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
webuhu
Very cool. Thanks for your contributions.
It’s the formatter I’ve been waiting for.
aptinio
It was a long time coming. I first opened a PR for Phoenix LiveView asking the team to make
HTMLFormatter.tokenize/1public. José Valim came back with a better idea —attribute_formatters(phoenix_live_view#3781) — which gave formatters a proper hook into the template pipeline.The initial implementation parsed Tailwind’s output CSS to derive the sort order. It worked, but I was never happy with it, especially with format-on-save enabled in an editor. The ideal solution was to hand the formatting off to Tailwind itself, and that became possible once the
canonicalizesubcommand landed. I contributed a--streamflag (tailwindcss#19796) so the CLI could stay alive as a long-running process, and after that the library was the easy part.Hope you find it useful!
vicb335
Damn this makes working on liveview even better, thanks so much!!
aptinio
Best part about it for me is you stop thinking about class order — write whatever, hit save, done. On a team it’s even better because diffs stay clean and everyone’s output looks the same.
sevenseacat
Very awesome!
Now we just need Tailwind to expose something to allow merging and overriding of classes…
aptinio
Thanks! Interestingly, Tailwind’s
canonicalizesubcommand already resolves conflicts within a single class string — if you give itbg-blue-500 px-4 bg-red-500, it’ll collapse that down tobg-red-500 px-4. Last one wins.The tricky part is that the interesting merge case is runtime — something like
class={["bg-blue-500 px-4", @class]}where one side is dynamic. A formatter can’t resolve that since it doesn’t know the value of@classat format time. And doing it at runtime would mean either shipping and running the Tailwind binary in prod, or reimplementing its conflict resolution in pure Elixir — both non-trivial.Would be a great project though!
simoncocking
Unsure whether this is
canonical_tailwindor Tailwind’scanonicalizesubcommand, but I just addedcanonical_tailwindto our (quite large) project and ran into a couple of issues:We had one instance where classes were wrapped in a
~s’’’heredoc, and the parser crashed (failing to find the closing'’’).There were many instances where the formatter swapped class strings between nearby but different HTML elements.
There were several instances where multi-line class strings were truncated (the formatter dropped continuation lines) - these in classes such as:
[edit]
That last one may have been in classes such as
aptinio
Thanks for trying this out on a large project and sharing what you ran into!
All three are symptoms of the same bug — newlines in class strings broke the line-based protocol with the
tailwindcssCLI. Multi-line strings got truncated, and subsequent attributes received stale responses (which looked like classes swapping between elements). The heredoc crash was a related issue where trailing newlines got stripped, producing invalid Elixir.Fixed in v0.1.1. One thing to note: multi-line class strings are now collapsed into a single line, since
tailwindcss canonicalizealways returns a single line. If you prefer breaking long class lists across lines, you can use a list instead:Let me know if you’re still seeing issues after upgrading.
simoncocking
Thanks for the quick fix - the new version is certainly much better! I’m not seeing any of the same issues with
mix format, however a couple of things I notice:mix formatno longer crashes and does canonicalize, it collapses these multiple-line strings into a single line every time. This is probably a difficult one to solve - understandably the lines have to be joined in order to canonicalize, so wrapping and formatting them in the same way afterwards is tricky. We can certainly standardize on multiple strings in an array.~s'''delimited class strings withUnable to format: token missing on lib/myapp_web/live/components/thing.ex:8:2: error: missing terminator: ‘’’ (for heredoc starting at line 5), even thoughmix formatworks.aptinio
Glad it’s working better!
On the multi-line collapsing, that’s inherent to how canonicalize works. The classes have to be joined into a single string for the CLI, and there’s no way to know where the original line breaks were meaningful. Using a list of strings is probably the cleanest workaround.
On the
~s'''issue with ElixirLS, I’d need more detail to dig into that one. Could you file an issue on GitHub?