goCVA is a Go package inspired by Class Variance Authority for dynamic class name management, ideal for conditional styling.
Install goCVA with Go's package manager:
go get github.com/gungun974/gocva
Import goCVA into your project:
import "github.com/gungun974/gocva"
- Creating a CVA Instance: Initialize
gocva.CVA
to start using the library.
cva := gocva.CVA{}
- Rendering Without Parameters: Without parameters,
Render
returns an empty string.
result := cva.Render(gocva.Variant{})
// result is ""
- Adding a Base Class: Specify a base class for consistent inclusion.
cva := gocva.CVA{
Base: "base-class",
}
result := cva.Render(gocva.Variant{})
// result is "base-class"
- Defining Variants: Set up variants for conditional class names.
cva := gocva.CVA{
Variants: gocva.Variants{
"variant": {
"primary": "variant-primary",
"secondary": "variant-secondary",
},
"size": {
"large": "text-large",
"small": "text-small",
},
},
}
- Rendering Variants: Choose variants to apply during rendering.
result := cva.Render(gocva.Variant{
"variant": "secondary",
"size": "large",
})
// result is "text-large variant-secondary"
Set default variants for automatic application when none are specified.
cva := gocva.CVA{
Variants: gocva.Variants{
"variant": {
"primary": "variant-primary",
"secondary": "variant-secondary",
},
},
DefaultVariants: gocva.Variant{
"variant": "primary",
},
}
result := cva.Render(gocva.Variant{})
// result is "variant-primary"
Define classes based on multiple variant conditions.
cva := gocva.CVA{
CompoundVariants: []gocva.CompoundVariant{
{
Class: "md:ml-24",
Attrs: gocva.Variant{
"direction": "left",
"size": "large",
},
},
},
}
result := cva.Render(gocva.Variant{
"direction": "left",
"size": "large",
})
// result is "md:ml-24"
Manage button styles based on state and size with goCVA.
cva := gocva.CVA{
Base: "btn",
Variants: gocva.Variants{
"state": {
"active": "btn-active",
"disabled": "btn-disabled",
},
"size": {
"large": "btn-large",
"small": "btn-small",
},
},
}
result := cva.Render(gocva.Variant{
"state": "active",
"size": "large",
})
// result is "btn btn-active btn-large"
Use make test