Primary
Badge
package showcase
import "github.com/axzilla/templui/internal/components/badge"
templ BadgeDefault() {
@badge.Badge() {
Badge
}
}
Badge component is used to display a small amount of information in a visual style.
package showcase
import "github.com/axzilla/templui/internal/components/badge"
templ BadgeDefault() {
@badge.Badge() {
Badge
}
}
templui add badge
Copy and paste the following code into your project:
package badge
import "github.com/axzilla/templui/internal/utils"
type Variant string
const (
VariantDefault Variant = "default"
VariantSecondary Variant = "secondary"
VariantDestructive Variant = "destructive"
VariantOutline Variant = "outline"
)
type Props struct {
ID string
Class string
Attributes templ.Attributes
Variant Variant
}
templ Badge(props ...Props) {
{{ var p Props }}
if len(props) > 0 {
{{ p = props[0] }}
}
<div
if p.ID != "" {
id={ p.ID }
}
class={
utils.TwMerge(
"inline-flex items-center gap-2",
"rounded-md border px-2.5 py-0.5 text-xs font-semibold transition-colors",
"focus:outline-hidden focus:ring-2 focus:ring-ring focus:ring-offset-2",
p.variantClasses(),
p.Class,
),
}
{ p.Attributes... }
>
{ children... }
</div>
}
func (p Props) variantClasses() string {
switch p.Variant {
case VariantDestructive:
return "border-transparent bg-destructive text-destructive-foreground"
case VariantOutline:
return "text-foreground border-border"
case VariantSecondary:
return "border-transparent bg-secondary text-secondary-foreground"
default:
return "border-transparent bg-primary text-primary-foreground"
}
}
Update the import paths to match your project setup.
Primary
package showcase
import "github.com/axzilla/templui/internal/components/badge"
templ BadgeDefault() {
@badge.Badge() {
Badge
}
}
Secondary
package showcase
import "github.com/axzilla/templui/internal/components/badge"
templ BadgeSecondary() {
@badge.Badge(badge.Props{
Variant: badge.VariantSecondary,
}) {
Secondary
}
}
Outline
package showcase
import "github.com/axzilla/templui/internal/components/badge"
templ BadgeOutline() {
@badge.Badge(badge.Props{
Variant: badge.VariantOutline,
}) {
Outline
}
}
Destructive
package showcase
import "github.com/axzilla/templui/internal/components/badge"
templ BadgeDestructive() {
@badge.Badge(badge.Props{
Variant: badge.VariantDestructive,
}) {
Destructive
}
}
With Icon
package showcase
import (
"github.com/axzilla/templui/internal/components/badge"
"github.com/axzilla/templui/internal/components/icon"
)
templ BadgeWithIcon() {
@badge.Badge(badge.Props{
Class: "flex gap-1 items-center",
}) {
@icon.Rocket(icon.Props{Size: 14})
With Icon
}
}