Componentalert
Alert
Displays a prominent message to call attention to important information.
Preview
Displays a prominent message to call attention to important information.
Heads up!
You can add components to your app using the templcn CLI.
@ui.Alert(ui.AlertProps{}) {
@ui.AlertTitle(ui.DOMProps{}) { Heads up! }
@ui.AlertDescription(ui.DOMProps{}) { You can add components to your app using the CLI. }
}Installation
Add this component to your project using the CLI.
templcn add alertUsage
1. Import the component in your Go or templ file:
import "your-module/ui"2. Use the component in your template:
@ui.Alert(ui.AlertProps{}) {
@ui.AlertTitle(ui.DOMProps{}) { Heads up! }
@ui.AlertDescription(ui.DOMProps{}) { You can add components to your app using the CLI. }
}Composition
Use these parts together to build the component.
Alert
├── AlertTitle
├── AlertDescriptionExamples
Named examples and common variations.
Default
A default informational alert.
Heads up!
You can add components to your app using the templcn CLI.
@ui.Alert(ui.AlertProps{}) {
@ui.AlertTitle(ui.DOMProps{}) { Heads up! }
@ui.AlertDescription(ui.DOMProps{}) { You can add components using the CLI. }
}Destructive
A destructive alert for errors.
Error
Your session has expired. Please log in again.
@ui.Alert(ui.AlertProps{Variant: ui.AlertVariantDestructive}) {
@ui.AlertTitle(ui.DOMProps{}) { Error }
@ui.AlertDescription(ui.DOMProps{}) { Your session has expired. Please log in again. }
}API Reference
| Prop | Type | Default | Description |
|---|---|---|---|
| Variant | AlertVariant | "default" | "default" or "destructive". |
View source
package ui
import (
"context"
"io"
"github.com/a-h/templ"
)
type AlertVariant string
const (
AlertVariantDefault AlertVariant = "default"
AlertVariantDestructive AlertVariant = "destructive"
)
type AlertProps struct {
DOMProps
Variant AlertVariant
}
func alertClasses(variant AlertVariant, className string) string {
if variant == "" {
variant = AlertVariantDefault
}
base := "relative grid w-full grid-cols-[0_1fr] items-start gap-y-0.5 rounded-lg border px-4 py-3 text-sm has-[>svg]:grid-cols-[calc(var(--spacing)*4)_1fr] has-[>svg]:gap-x-3 [&>svg]:size-4 [&>svg]:translate-y-0.5 [&>svg]:text-current"
switch variant {
case AlertVariantDestructive:
return cn(base, "bg-card text-destructive *:data-[slot=alert-description]:text-destructive/90 [&>svg]:text-current", className)
default:
return cn(base, "bg-card text-card-foreground", className)
}
}
func Alert(props AlertProps) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
attrs := attrsFromDOMProps(props.DOMProps, "alert", alertClasses(props.Variant, ""))
attrs["role"] = "alert"
return renderElement(ctx, w, "div", attrs, templ.GetChildren(ctx))
})
}
func AlertTitle(props DOMProps) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
return renderElement(ctx, w, "div", attrsFromDOMProps(props, "alert-title", "col-start-2 line-clamp-1 min-h-4 font-medium tracking-tight"), templ.GetChildren(ctx))
})
}
func AlertDescription(props DOMProps) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
return renderElement(ctx, w, "div", attrsFromDOMProps(props, "alert-description", "col-start-2 grid justify-items-start gap-1 text-sm text-muted-foreground [&_p]:leading-relaxed"), templ.GetChildren(ctx))
})
}