Oudwins / tailwind-merge-go

Merge Tailwind CSS classes without style conflicts in Go
MIT License
35 stars 0 forks source link

tailwind-merge-go

tailwind-merge-go - Tailwind Merge For Golang

Go Reference License: MIT Go Report Card Coverage Status

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"
}

Why use it?

[Watch this introduction video from Simon Vrachliotis (@simonswiss) ↓ The "why" behind tailwind-merge](https://www.youtube.com/watch?v=tfgLd5ZSNPc (Watch YouTube video "Tailwind-Merge Is Incredibly Useful — And Here's Why!"))

Limitations

Advanced Examples

You might also want to check out the advanced example at /cmd/examples/advanced

Provide Your Own or Extend Default Config

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"
}

Provide your own Cache

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"
}

Contributing

Checkout the contributing docs

Roadmap

Acknowledgments