composite-components.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package e00_basics
  2. // @snippet_begin(CompositeComponentSample1)
  3. import (
  4. "fmt"
  5. "github.com/qor5/web"
  6. . "github.com/theplant/htmlgo"
  7. )
  8. func Navbar(title string, activeIndex int, items ...HTMLComponent) HTMLComponent {
  9. ul := Ul().Class("navbar-nav mr-auto")
  10. for i, item := range items {
  11. ul.AppendChildren(
  12. Li(
  13. item,
  14. ).Class("nav-item").ClassIf("active", activeIndex == i),
  15. )
  16. }
  17. return Nav(
  18. A(Text(title)).Class("navbar-brand").
  19. Href("#"),
  20. Button("").Class("navbar-toggler").
  21. Type("button").
  22. Attr("data-toggle", "collapse").
  23. Attr("data-target", "#navbarNav").
  24. Attr("aria-controls", "navbarNav").
  25. Attr("aria-expanded", "false").
  26. Attr("aria-label", "Toggle navigation").
  27. Children(
  28. Span("").Class("navbar-toggler-icon"),
  29. ),
  30. Div(
  31. ul,
  32. Form(
  33. Input("").Class("form-control mr-sm-2").
  34. Type("search").
  35. Placeholder("Search").
  36. Attr("aria-label", "Search"),
  37. Button("Search").Class("btn btn-outline-light my-2 my-sm-0").
  38. Type("submit"),
  39. ).Class("form-inline my-2 my-lg-0"),
  40. ).Class("collapse navbar-collapse").
  41. Id("navbarNav"),
  42. ).Class("navbar navbar-expand-lg navbar-dark bg-primary")
  43. }
  44. type CarouselItem struct {
  45. ImageSrc string
  46. ImageAlt string
  47. }
  48. func Carousel(carouselId string, activeIndex int, items []*CarouselItem) HTMLComponent {
  49. var indicators = Ol().Class("carousel-indicators")
  50. var carouselInners = Div().Class("carousel-inner")
  51. for i, item := range items {
  52. indicators.AppendChildren(
  53. Li().Attr("data-target", "#"+carouselId).
  54. Attr("data-slide-to", fmt.Sprint(i)).
  55. ClassIf("active", activeIndex == i),
  56. )
  57. carouselInners.AppendChildren(
  58. Div(
  59. fakeImage(item.ImageAlt),
  60. ).Class("carousel-item").ClassIf("active", activeIndex == i).Style("font-size: 3.5rem;"),
  61. )
  62. }
  63. return Div(
  64. indicators,
  65. carouselInners,
  66. A(
  67. Span("").Class("carousel-control-prev-icon").
  68. Attr("aria-hidden", "true"),
  69. Span("Previous").Class("sr-only"),
  70. ).Class("carousel-control-prev").
  71. Href("#"+carouselId).
  72. Role("button").
  73. Attr("data-slide", "prev"),
  74. A(
  75. Span("").Class("carousel-control-next-icon").
  76. Attr("aria-hidden", "true"),
  77. Span("Next").Class("sr-only"),
  78. ).Class("carousel-control-next").
  79. Href("#"+carouselId).
  80. Role("button").
  81. Attr("data-slide", "next"),
  82. ).Id(carouselId).
  83. Class("carousel slide").
  84. Attr("data-ride", "carousel")
  85. }
  86. func CompositeComponentSample1Page(ctx *web.EventContext) (pr web.PageResponse, err error) {
  87. pr.Body = Div(
  88. Navbar(
  89. "Hello",
  90. 1,
  91. A(
  92. Text("Home"),
  93. ).Class("nav-link").
  94. Href("#"),
  95. A(
  96. Text("Features"),
  97. ).Class("nav-link").
  98. Href("#"),
  99. A(
  100. Text("Pricing"),
  101. ).Class("nav-link").
  102. Href("#"),
  103. A(
  104. Text("Disabled"),
  105. ).Class("nav-link disabled").
  106. Href("#").
  107. TabIndex(-1).
  108. Attr("aria-disabled", "true"),
  109. ),
  110. Div(
  111. Div(
  112. Div(
  113. Carousel("hello1", 1, []*CarouselItem{
  114. {
  115. ImageAlt: "First slide",
  116. },
  117. {
  118. ImageAlt: "Second slide",
  119. },
  120. {
  121. ImageAlt: "Third slide",
  122. },
  123. }),
  124. ).Class("col-12 py-md-3 pl-md-3"),
  125. ).Class("row"),
  126. ).Class("container-fluid"),
  127. )
  128. return
  129. }
  130. var CompositeComponentSample1PagePB = web.Page(CompositeComponentSample1Page)
  131. const CompositeComponentSample1PagePath = "/samples/composite-component-sample1"
  132. // @snippet_end
  133. func fakeImage(title string) HTMLComponent {
  134. return RawHTML(fmt.Sprintf(`
  135. <svg class="bd-placeholder-img bd-placeholder-img-lg d-block w-100" width="800" height="400" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: %s"><title>Placeholder</title><rect width="100%%" height="100%%" fill="#666"></rect><text x="40%%" y="50%%" fill="#444" dy=".3em">%s</text></svg>
  136. `, title, title))
  137. }