An open API service indexing awesome lists of open source software.

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

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)
)
```


## func [Generate]()

```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


## func [GenerateTailwind]()

```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.


## func [WriteClassMapFile]()

```go
func WriteClassMapFile(filepath string) error
```

WriteClassMapFile writes the generated class map to the specified file

Generated by [gomarkdoc]()