Skip to content

Instantly share code, notes, and snippets.

@psttf
Created December 27, 2021 08:23
Show Gist options
  • Save psttf/f9cc0a85f35b2699ddda63961cc3dd85 to your computer and use it in GitHub Desktop.
Save psttf/f9cc0a85f35b2699ddda63961cc3dd85 to your computer and use it in GitHub Desktop.
TabsComponent.scala
// Bootstrap 5 tabs Scala.js + Outwatch wrapper
final class Tab(
tabId: String,
selected: Boolean,
label: String,
content: VDomModifier,
) {
def navLi: HtmlVNode =
li(
cls := "nav-item",
role := "presentation",
button(
cls := ("nav-link" :: selected.option("active").toList).mkString(" "),
idAttr := s"$tabId-tab",
attr("data-bs-toggle") := "tab",
attr("data-bs-target") := s"#$tabId",
`type` := "button",
role := "tab",
attr("aria-controls") := tabId,
attr("aria-selected") := selected,
label,
),
)
def paneDiv: HtmlVNode =
div(
cls := ("tab-pane" :: selected.option("active").toList).mkString(" "),
idAttr := tabId,
role := "tabpanel",
attr("aria-labelledby") := s"$tabId-tab",
content,
)
}
final class TabsComponent(tabs: Tab*) {
def modifier: VDomModifier =
VDomModifier(
ul(cls := "nav nav-tabs", role := "tablist", tabs.map(_.navLi)),
div(cls := "tab-content my-4", tabs.map(_.paneDiv)),
)
}
// example:
private val tabs = new TabsComponent(
new Tab(
"item-general-data",
true,
"General",
/* tab content */ ??? : VDomModifier,
),
new Tab(
"item-other-data",
false,
"Other",
/* tab content */ ??? : VDomModifier,
),
)
// and then somwhere in the UI code: tabs.modifier
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment