generates tailwind merges and classes from go templ sources
import "github.com/conneroisu/twerge"
Package twerge provides a tailwind merger for go-templ with class generation and runtime static hashmap.
It performs four key functions: 1. Merges TailwindCSS classes intelligently (resolving conflicts) 2. Generates short unique CSS class names from the merged classes 3. Creates a mapping from original class strings to generated class names 4. Provides a runtime static hashmap for direct class name lookup
Basic Usage:
import "github.com/conneroisu/twerge"
// Merge TailwindCSS classes from a space-delimited string
merged := twerge.Merge("text-red-500 bg-blue-500 text-blue-700")
// Returns: "bg-blue-500 text-blue-700"
// Generate a short unique class name
className := twerge.Generate("text-red-500 bg-blue-500")
// Returns something like: "tw-Ab3F5g7"
Runtime Static Map Usage:
// Pre-register common classes
twerge.RegisterClasses(map[string]string{
"bg-blue-500 text-white": "tw-btn-blue",
"bg-red-500 text-white": "tw-btn-red",
})
// Generate a class name at runtime
className := twerge.RuntimeGenerate("p-4 m-2")
// Returns a deterministic class name, stored in the runtime map
// Generate CSS for all registered classes
css := twerge.GetRuntimeClassHTML()
// Returns CSS like: ".tw-btn-blue { @apply bg-blue-500 text-white; }"
For templ users:
<div class={ twerge.Merge("bg-blue-500 p-4 bg-red-500") }>
Using merged classes directly
</div>
<div class={ twerge.RuntimeGenerate("bg-blue-500 p-4") }>
Using runtime generated class name
</div>
<style>
@unsafe {
twerge.GetRuntimeClassHTML()
}
</style>
- Variables
- func Generate(classes string) string
- func GenerateClassMapCode() string
- func GenerateTailwind(inputPath, outputPath string, classMap map[string]string) error
- func WriteClassMapFile(filepath string) error
ClassMapStr is a map of class strings to their generated class names This variable can be populated by code generation or manually
var ClassMapStr = make(map[string]string)
var (
// Merge is the default template merger
// It takes a space-delimited string of TailwindCSS classes and returns a merged string
// It also adds the merged class to the ClassMapStr when used
// It will quickly return the generated class name from ClassMapStr if available
Merge = createTwMerge(nil, nil)
)
func Generate
func Generate(classes string) string
Generate creates a short unique CSS class name from the merged classes
func GenerateClassMapCode
func GenerateClassMapCode() string
GenerateClassMapCode generates Go code for a variable containing the class mapping
func GenerateTailwind
func GenerateTailwind(inputPath, outputPath string, classMap map[string]string) error
GenerateTailwind creates an input CSS file for the Tailwind CLI that includes all the @apply directives from the provided class map.
This is useful for building a production CSS file with Tailwind's CLI.
The marker is used to identify the start and end of the @apply directives generated by Twerge.
func WriteClassMapFile
func WriteClassMapFile(filepath string) error
WriteClassMapFile writes the generated class map to the specified file
Generated by gomarkdoc