button.go 762 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package element
  2. import (
  3. "golang.org/x/net/html/atom"
  4. "html"
  5. )
  6. type (
  7. ButtonOption func(btn *Button)
  8. Button struct {
  9. Url string
  10. Text string
  11. Color string
  12. Style Style
  13. }
  14. )
  15. func (element *Button) Html() string {
  16. return element.String()
  17. }
  18. func (element *Button) String() string {
  19. if element.Style == nil {
  20. element.Style = make(map[string]string)
  21. }
  22. if element.Color != "" {
  23. element.Style["color"] = element.Color
  24. }
  25. return renderTag(atom.A.String(), Attrs{
  26. "class": "button",
  27. "href": element.Url,
  28. "style": element.Style.String(),
  29. }, html.EscapeString(element.Text))
  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. }