All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jsMain.app.softwork.routingcompose.NavLink.kt Maven / Gradle / Ivy

Go to download

A multiplatform library for routing to use with JetPack Compose Web and Desktop

There is a newer version: 0.4.0
Show newest version
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