T templcn/ui
Componentmessage

Message

Displays a message in a conversation, with optional avatar, header, footer, and alignment.

Preview

Displays a message in a conversation, with optional avatar, header, footer, and alignment.

R
The build failed during dependency installation.
ME
Can you share the exact error?
R
Here's the error from the logs
Something went wrong with the build. The libraries are not installed correctly. Try running the build again.

Installation

Add this component to your project using the CLI.

templcn add message

Usage

1. Import the component in your Go or templ file:

import "your-module/ui"

2. Use the component in your template:

@ui.Message(ui.MessageProps{Align: "start"}) {
  @ui.MessageAvatar(ui.DOMProps{}) {
    @ui.Avatar(ui.AvatarProps{Class: "size-8"}) {
      @ui.AvatarFallback(ui.DOMProps{}) { R }
    }
  }
  @ui.MessageContent(ui.DOMProps{}) {
    @ui.Bubble(ui.BubbleProps{Variant: "muted"}) {
      @ui.BubbleContent(ui.DOMProps{}) { The build failed during dependency installation. }
    }
  }
}

Composition

Use these parts together to build the component.

Message
├── MessageAvatar
├── Avatar
├── AvatarFallback
├── MessageContent
├── Bubble
├── BubbleContent

Examples

Named examples and common variations.

Avatar

Render incoming and outgoing message rows with sender avatars.

R
The build failed during dependency installation.
ME
Can you share the exact error?
R
Here's the error from the logs
Something went wrong with the build. The libraries are not installed correctly. Try running the build again.

Group

Stack consecutive messages from the same sender using MessageGroup.

R
The build failed during dependency installation.
ME
Can you share the exact error?
R
Here's the error from the logs
Something went wrong with the build. The libraries are not installed correctly. Try running the build again.

Header & Footer

Add sender titles, delivery status, and message-level actions.

R
The build failed during dependency installation.
ME
Can you share the exact error?
R
Here's the error from the logs
Something went wrong with the build. The libraries are not installed correctly. Try running the build again.
API Reference
PropTypeDefaultDescription
Alignstring"start""start" (receiver) or "end" (sender).
View source
package ui

import (
	"context"
	"github.com/a-h/templ"
	"io"
)

type MessageProps struct {
	DOMProps
	Align string
}

func MessageGroup(props DOMProps) templ.Component {
	return messageBlock("message-group", "flex min-w-0 flex-col gap-2", "div", props)
}
func Message(props MessageProps) templ.Component {
	return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
		align := props.Align
		if align == "" {
			align = "start"
		}
		attrs := attrsFromDOMProps(props.DOMProps, "message", "group/message relative flex w-full min-w-0 gap-2 text-sm data-[align=end]:flex-row-reverse")
		attrs["data-align"] = align
		return renderElement(ctx, w, "div", attrs, templ.GetChildren(ctx))
	})
}
func MessageAvatar(props DOMProps) templ.Component {
	return messageBlock("message-avatar", "flex w-fit min-w-8 shrink-0 items-center justify-center self-end overflow-hidden rounded-full bg-muted group-has-data-[slot=message-footer]/message:-translate-y-8", "div", props)
}
func MessageContent(props DOMProps) templ.Component {
	return messageBlock("message-content", "flex w-full min-w-0 flex-col gap-2.5 wrap-break-word group-data-[align=end]/message:*:data-slot:self-end", "div", props)
}
func MessageHeader(props DOMProps) templ.Component {
	return messageBlock("message-header", "flex max-w-full min-w-0 items-center px-3 text-xs font-medium text-muted-foreground group-has-data-[variant=ghost]/message:px-0", "div", props)
}
func MessageFooter(props DOMProps) templ.Component {
	return messageBlock("message-footer", "flex max-w-full min-w-0 items-center px-3 text-xs font-medium text-muted-foreground group-has-data-[variant=ghost]/message:px-0 group-data-[align=end]/message:justify-end", "div", props)
}
func messageBlock(slot, className, tag string, props DOMProps) templ.Component {
	return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
		return renderElement(ctx, w, tag, attrsFromDOMProps(props, slot, className), templ.GetChildren(ctx))
	})
}