55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
// templui util templui.go - version: v0.84.0 installed by templui v0.84.0
|
|
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"crypto/rand"
|
|
|
|
"github.com/a-h/templ"
|
|
|
|
twmerge "github.com/Oudwins/tailwind-merge-go"
|
|
)
|
|
|
|
// TwMerge combines Tailwind classes and resolves conflicts.
|
|
// Example: "bg-red-500 hover:bg-blue-500", "bg-green-500" → "hover:bg-blue-500 bg-green-500"
|
|
func TwMerge(classes ...string) string {
|
|
return twmerge.Merge(classes...)
|
|
}
|
|
|
|
// TwIf returns value if condition is true, otherwise an empty value of type T.
|
|
// Example: true, "bg-red-500" → "bg-red-500"
|
|
func If[T comparable](condition bool, value T) T {
|
|
var empty T
|
|
if condition {
|
|
return value
|
|
}
|
|
return empty
|
|
}
|
|
|
|
// TwIfElse returns trueValue if condition is true, otherwise falseValue.
|
|
// Example: true, "bg-red-500", "bg-gray-300" → "bg-red-500"
|
|
func IfElse[T any](condition bool, trueValue T, falseValue T) T {
|
|
if condition {
|
|
return trueValue
|
|
}
|
|
return falseValue
|
|
}
|
|
|
|
// MergeAttributes combines multiple Attributes into one.
|
|
// Example: MergeAttributes(attr1, attr2) → combined attributes
|
|
func MergeAttributes(attrs ...templ.Attributes) templ.Attributes {
|
|
merged := templ.Attributes{}
|
|
for _, attr := range attrs {
|
|
for k, v := range attr {
|
|
merged[k] = v
|
|
}
|
|
}
|
|
return merged
|
|
}
|
|
|
|
// RandomID generates a random ID string.
|
|
// Example: RandomID() → "id-1a2b3c"
|
|
func RandomID() string {
|
|
return fmt.Sprintf("id-%s", rand.Text())
|
|
}
|