button.go 862 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package element
  2. import (
  3. "git.nspix.com/golang/pgenr/internal/pool"
  4. "golang.org/x/net/html/atom"
  5. "html"
  6. )
  7. type (
  8. ButtonOption func(btn *Button)
  9. Button struct {
  10. Url string
  11. Text string
  12. Color string
  13. Style Style
  14. }
  15. )
  16. func (element *Button) Html() string {
  17. return element.String()
  18. }
  19. func (element *Button) String() string {
  20. br := pool.Get()
  21. defer pool.Put(br)
  22. if element.Style == nil {
  23. element.Style = make(map[string]string)
  24. }
  25. if element.Color != "" {
  26. element.Style["color"] = element.Color
  27. }
  28. br.WriteString(renderTag(atom.A.String(), Attrs{"class": "button", "href": element.Url, "style": element.Style.String()}, html.EscapeString(element.Text)))
  29. return br.String()
  30. }
  31. func NewButton(label, link string, opts ...ButtonOption) *Button {
  32. btn := &Button{Text: label, Url: link}
  33. for _, cb := range opts {
  34. cb(btn)
  35. }
  36. return btn
  37. }