Vaadin 24.1 introduces the SideNav component; let's add a bit of DSL there. On top of creating items via DSL, let's also add a support for easy-adding routes which would also guess the route title. Use the route{} naming instead of item{} naming, to prevent huge name overload.
private fun getRouteTitle(routeClass: KClass<*>): String {
val title = routeClass.java.getAnnotation(PageTitle::class.java)
return title?.value ?: routeClass.simpleName ?: ""
}
@VaadinDsl
fun (@VaadinDsl HasComponents).sideNav(label: String? = null, block: (@VaadinDsl SideNav).() -> Unit = {}) = init(
SideNav(label), block)
@VaadinDsl
fun (@VaadinDsl SideNav).route(
routeClass: KClass<out Component>,
icon: VaadinIcon,
title: String = getRouteTitle(routeClass),
block: (@VaadinDsl SideNavItem).() -> Unit = {}
): SideNavItem {
val item = SideNavItem(title, routeClass.java, icon.create())
block(item)
addItem(item)
return item
}
Vaadin 24.1 introduces the SideNav component; let's add a bit of DSL there. On top of creating items via DSL, let's also add a support for easy-adding routes which would also guess the route title. Use the
route{}
naming instead ofitem{}
naming, to prevent huge name overload.