package element import ( "golang.org/x/net/html/atom" "html" ) const ( TextThemeSuccess = "success" TextThemeDanger = "danger" ) type ( TextOption func(t *Text) Text struct { Tag atom.Atom Content string Theme string Color string Style Style } ) func WithTextTheme(s string) TextOption { return func(t *Text) { t.Theme = s } } func WithTextStyle(ms map[string]string) TextOption { return func(t *Text) { t.Style = ms } } func WithTextTag(tag atom.Atom) TextOption { return func(t *Text) { t.Tag = tag } } func WithTextColor(color string) TextOption { return func(t *Text) { t.Color = color } } func (text *Text) Html() string { return text.String() } func (text *Text) String() string { var className string if text.Style == nil { text.Style = make(map[string]string) } if text.Color != "" { text.Style["color"] = text.Color } if text.Tag == 0 { text.Tag = atom.Span } if text.Theme != "" { className = "text-" + text.Theme } return renderTag(text.Tag.String(), Attrs{ "class": className, "style": text.Style.String(), }, html.EscapeString(text.Content)) } func NewText(s string, opts ...TextOption) *Text { txt := &Text{Content: s, Tag: atom.Span} for _, cb := range opts { cb(txt) } return txt }