raulrpearson

raulrpearson

Yatm - Yet Another Tailwind Merge

Yatm provides a Tailwind class merging utility to resolve conflicts, i.e. classes targeting the same CSS property.

When using Tailwind with component abstractions, authors usually provide some styling out of the box and consumers want to override some of that styling. The naive solution is to replace or append those extra classes:

def button(assigns) do
  ~H"""
  <button type={@type} class={[button_classes(), @class]} {@rest}>
    {render_slot(@inner_block)}
  </button>
  """
end

But this easily ends up in conflicts. The purpose of this package is to offer a utility to solve these. When provided a sequence of Tailwind utility classes with conflicts, later instances override earlier instances.

Calling Yatm.merge("text-sm text-xl") will return "text-xl" and calling Yatm.merge("text-xl text-sm") will return "text-sm".

In the JavaScript ecosystem, tailwind-merge is likely the most popular solution. In Elixir, several packages have been created for this already (tails, twix, tailwind_merge, turboprop). As far as I can tell, tw_merge is the most recent and alive attempt, a fork of the Tailwind-related code in Turboprop.

Yatm is that, yet another attempt. At this point it’s a bit of an experiment and learning project. I’m not entirely sure if it’ll pan out and whether it’ll be maintainable or performant or if the approach I have in mind is sound.

It uses nimble_parsec to parse the strings of Tailwind classes and annotate each class with the CSS property it targets. It then reduces the list of annotated nodes into a map with (loosely) CSS properties as keys. This effectively achieves the conflict resolution (values of keys get overwritten). Finally, it concatenates the classes that remain to produce the merged conflict-free class string.

It exports a merge/1 function at the moment, but I’d like to see if this can be built into an EEx engine so that the merging of class strings can happen transparently as part of the compilation of templates.

The library is in very early stages and doesn’t yet cover many Tailwind features. You’re welcome to try it out. Feedback appreciated!

Where Next?

Popular in Libraries Top

josevalim
Hi everyone, We would like to announce that Plataformatec is working on a new MySQL driver called MyXQL. Our goal is to eventually integ...
New
mhanberg
I just released the first version of Temple: an HTML DSL for Elixir and Phoenix! You can read this blog post or the docs for more info...
New
tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
OvermindDL1
Been making an MLElixir thing (not released yet…) for fun in spare time in the past day. I’m just trying to see how much I can get an ML...
132 13487 106
New
archan937
It is a well-know topic within the Elixir community: “To mock or not to mock? :)” Every alchemist probably has his / her own opinion con...
New
Crowdhailer
I have been updating a library that allows you to pipe between functions that use the erlang result tuple convention. Assuming you have...
New
josevalim
Yes, yet another parser combinator library! Most of the parser combinators in the ecosystem are either compile-time, often using AST tra...
159 18262 141
New
wmnnd
Hi there, for my project DBLSQD, I needed a file storage solution that is a bit more flexible than Arc. Because I thought others might f...
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
Qqwy
While not as prevalent as in imperative languages, arrays (collections with efficient random element access) are still very useful in Eli...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
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
Tee
can someone please explain to me how Enum.reduce works with maps
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

Sub Categories:

We're in Beta

About us Mission Statement