Utility function to efficiently merge Tailwind CSS classes in Golang without style conflicts. This library aims to be as close as possible to a 1:1 copy of the original dcastil/tailwind-merge library written in javascript.
import (
"fmt"
twmerge "github.com/Oudwins/tailwind-merge-go"
)
func main() {
// example usage
c := twmerge.Merge("px-4 px-10 p-1")
fmt.Println(c) // "p-1"
}
[Watch this introduction video from Simon Vrachliotis (@simonswiss) ↓ ](https://www.youtube.com/watch?v=tfgLd5ZSNPc (Watch YouTube video "Tailwind-Merge Is Incredibly Useful — And Here's Why!"))
You might also want to check out the advanced example at /cmd/examples/advanced
import (
// Note the import path here is different from the default path. This is so you have access to all the custom functions, structs, etc that are used to build the twmerge config
twmerge "github.com/Oudwins/tailwind-merge-go/pkg/twmerge"
)
var TwMerger twmerge.TwMergeFn
func main() {
// get the default config
config := twmerge.MakeDefaultConfig()
// do your modifications here
// create the merger
TwMerger = twmerge.CreateTwMerge(config, nil) // config, cache (if nil default will be used)
// example usage
m := TwMerger("px-4 px-10", "p-20")
fmt.Println(m) // output: "p-20"
}
The default cache is a LRU Cache and should be acceptable for most use cases. However, you might want to provide your own cache or modify the default creation parameters. Your cache must implement the interface defined at /pkg/cache/cache.go
type ICache interface {
Get(string) string
Set(string, string) // key, value
}
Here is an example of manually creating the default cache with a custom max capacity
import (
twmerge "github.com/Oudwins/tailwind-merge-go/pkg/twmerge"
lru "github.com/Oudwins/tailwind-merge-go/pkg/lru"
)
var TwMerger twmerge.TwMergeFn
func main() {
customCapacity := 10000
cache := lru.make(customCapacity)
// create the merger
TwMerger = twmerge.CreateTwMerge(nil, cache) // config, cache (if nil default will be used)
// example usage
m := TwMerger("px-4 px-10", "p-20")
fmt.Println(m) // output: "p-20"
}
Checkout the contributing docs