https://github.com/conneroisu/twerge
generates tailwind merges and classes from go templ sources
https://github.com/conneroisu/twerge
Last synced: 3 months ago
JSON representation
generates tailwind merges and classes from go templ sources
- Host: GitHub
- URL: https://github.com/conneroisu/twerge
- Owner: conneroisu
- License: mit
- Created: 2025-03-31T17:37:24.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2025-03-31T19:26:06.000Z (3 months ago)
- Last Synced: 2025-03-31T20:22:39.366Z (3 months ago)
- Language: Go
- Size: 85 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# twerge
generates tailwind merges and classes from go templ sources
# twerge
```go
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:
```
Using merged classes directly
Using runtime generated class name@unsafe {
twerge.GetRuntimeClassHTML()
}```
## Index
- [Variables](<#variables>)
- [func Generate\(classes string\) string](<#Generate>)
- [func GenerateClassMapCode\(\) string](<#GenerateClassMapCode>)
- [func GenerateTailwind\(inputPath, outputPath string, classMap map\[string\]string\) error](<#GenerateTailwind>)
- [func WriteClassMapFile\(filepath string\) error](<#WriteClassMapFile>)## Variables
ClassMapStr is a map of class strings to their generated class names This variable can be populated by code generation or manually
```go
var ClassMapStr = make(map[string]string)
``````go
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)
)
``````go
func Generate(classes string) string
```Generate creates a short unique CSS class name from the merged classes
## func [GenerateClassMapCode]()```go
func GenerateClassMapCode() string
```GenerateClassMapCode generates Go code for a variable containing the class mapping
```go
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.
```go
func WriteClassMapFile(filepath string) error
```WriteClassMapFile writes the generated class map to the specified file
Generated by [gomarkdoc]()