button.go 772 B

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