-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtailwind.go
40 lines (33 loc) · 1.38 KB
/
tailwind.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package merge
import (
"fmt"
"github.com/will-lol/merge/twMergeGoja"
)
type tailwindMerge struct {
Merger twMergeGoja.TwMerge
}
type TailwindMerge interface {
TailwindMergeFunc(existing any, incoming any) (remaining any, committed any)
}
// NewTailwindMerge creates an instance of twMergeGoja, the provider of the tailwind-merge library in JavaScript. It runs in [github.com/dop251/goja], making it far slower than the other included Merge functions. It is recommended that you run the NewTailwindMerge function in a seperate goroutine as it can have a runtime of up to 14ms. Subsequent calls of TailwindMergeFunc are much faster, and do not need to run in a goroutine.
func NewTailwindMerge() (TailwindMerge, error) {
merger, err := twMergeGoja.NewTwMerge()
if err != nil {
return nil, err
}
return &tailwindMerge{
Merger: merger,
}, nil
}
// TailwindCSS is a CSS framework. Its classes can be intelligently merged by this MergeFunc.
// This MergeFunc uses https://github.com/dcastil/tailwind-merge and as such follows its rules.
// It will never leave classes unmerged returned as 'remaining'.
func (t tailwindMerge) TailwindMergeFunc(existing any, incoming any) (remaining any, committed any) {
existingString := fmt.Sprint(existing)
incomingString := fmt.Sprint(incoming)
res, err := t.Merger.Merge(existingString, incomingString)
if err != nil {
panic(err)
}
return nil, *res
}