package components import ( "fmt" "github.com/ryanhamamura/games/chat" "github.com/starfederation/datastar-go/datastar" ) // ColorFunc resolves a player slot to a CSS color string. type ColorFunc func(slot int) string // Config holds the game-specific settings for rendering a chat component. type Config struct { // CSSPrefix is used for element IDs and CSS classes (e.g. "c4" or "snake"). CSSPrefix string // PostURL is the URL to POST chat messages to (e.g. "/games/{id}/chat"). PostURL string // Color resolves a player slot to a CSS color string. Color ColorFunc // StopKeyPropagation adds data-on:keydown.stop="" to the input to prevent // key events from propagating (needed for snake to avoid steering while typing). StopKeyPropagation bool } templ Chat(messages []chat.Message, cfg Config) {
for _, m := range messages {
{ m.Nickname + ": " } { m.Message }
}
if cfg.StopKeyPropagation { } else { }
@chatAutoScroll(cfg.CSSPrefix)
} script chatAutoScroll(cssPrefix string) { var el = document.querySelector('.' + cssPrefix + '-chat-history'); if (!el) return; el.scrollTop = el.scrollHeight; new MutationObserver(function(){ el.scrollTop = el.scrollHeight; }) .observe(el, {childList:true, subtree:true}); }