jsMain.app.softwork.routingcompose.NavLink.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of routing-compose Show documentation
Show all versions of routing-compose Show documentation
A multiplatform library for routing to use with JetPack Compose Web and Desktop
package app.softwork.routingcompose
import androidx.compose.runtime.*
import org.jetbrains.compose.web.attributes.*
import org.jetbrains.compose.web.dom.*
import org.w3c.dom.*
/**
* Routing navigation Composable that will navigate to the provided path leveraging
* the top-most Router implementation.
* The Boolean parameter of [attrs] is true, if the path of the [current][Router.current] starts with [to].
*/
@Composable
public fun NavLink(
to: String,
attrs: (AttrsScope.(Boolean) -> Unit)? = null,
content: ContentBuilder? = null
) {
val router = Router.current
A(
href = to,
attrs = {
val currentPath = router.currentPath.path
val selected = if (to == "/") {
currentPath == to
} else currentPath.startsWith(to)
onClick {
if (it.ctrlKey || it.metaKey) return@onClick
router.navigate(to)
it.preventDefault()
}
attrs?.invoke(this, selected)
},
content = content
)
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy