Skip to content

Commit 35a7e57

Browse files
authored
Add Boolean parameter to attrs scope of NavLink (#201)
Co-authored-by: hfhbd <[email protected]>
1 parent cba9ebe commit 35a7e57

File tree

2 files changed

+57
-43
lines changed
  • integrationTest/src/jsMain/kotlin
  • src/jsMain/kotlin/app/softwork/routingcompose

2 files changed

+57
-43
lines changed

integrationTest/src/jsMain/kotlin/demo.kt

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,17 @@ fun RouteBuilder.Routing() {
8888
}
8989

9090
Clock()
91-
NavLink(to = "/") { Text("Main") }
91+
NavLink(to = "/", attrs = { selected ->
92+
if (selected) {
93+
classes("active")
94+
}
95+
}) { Text("Main") }
9296
Br()
93-
NavLink(to = "/users") { Text("Users") }
97+
NavLink(to = "/users", attrs = { selected ->
98+
if (selected) {
99+
classes("active")
100+
}
101+
}) { Text("Users") }
94102

95103
P {
96104
Text("Parameters: ${this@Routing.parameters?.map}")
@@ -103,46 +111,53 @@ fun RouteBuilder.Routing() {
103111
answer()
104112
}
105113
noMatch {
106-
P {
107-
Text("Hello Routing")
114+
Hello(enableAnswer) {
115+
enableAnswer = it
108116
}
109-
if (enableAnswer) {
110-
NavLink("answer") {
111-
Text("Click to navigate to /answer")
112-
}
113-
} else {
114-
Button({
115-
onClick {
116-
enableAnswer = true
117-
}
118-
}) {
119-
Text("Enable answer")
120-
}
117+
}
118+
}
119+
120+
@Composable
121+
private fun Hello(enableAnswer: Boolean, updateEnableAnswer: (Boolean) -> Unit) {
122+
P {
123+
Text("Hello Routing")
124+
}
125+
if (enableAnswer) {
126+
NavLink("answer") {
127+
Text("Click to navigate to /answer")
121128
}
122-
Hr()
123-
P {
124-
Text("Custom router usage")
129+
} else {
130+
Button({
131+
onClick {
132+
updateEnableAnswer(true)
133+
}
134+
}) {
135+
Text("Enable answer")
125136
}
137+
}
138+
Hr()
139+
P {
140+
Text("Custom router usage")
141+
}
126142

127-
Code {
128-
Text("val router = Router.current")
129-
Br()
130-
Text("router.navigate(to = /users)")
131-
}
143+
Code {
144+
Text("val router = Router.current")
145+
Br()
146+
Text("router.navigate(to = /users)")
147+
}
132148

133-
val router = Router.current
149+
val router = Router.current
134150

135-
P {
136-
Input(type = InputType.Button) {
137-
onClick {
138-
router.navigate("users")
139-
}
140-
value("Navigate to /users")
151+
P {
152+
Input(type = InputType.Button) {
153+
onClick {
154+
router.navigate("users")
141155
}
156+
value("Navigate to /users")
142157
}
143-
Hr()
144-
RedirectButton()
145158
}
159+
Hr()
160+
RedirectButton()
146161
}
147162

148163
@Routing

src/jsMain/kotlin/app/softwork/routingcompose/NavLink.kt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
package app.softwork.routingcompose
22

33
import androidx.compose.runtime.*
4+
import org.jetbrains.compose.web.attributes.*
45
import org.jetbrains.compose.web.dom.*
56
import org.w3c.dom.*
67

78
/**
89
* Routing navigation Composable that will navigate to the provided path leveraging
910
* the top-most Router implementation.
10-
* This implementation will use the `active` class if the current route starts with this link.
11+
* The Boolean parameter of [attrs] is true, if the path of the [current][Router.current] starts with [to].
1112
*/
1213
@Composable
1314
public fun NavLink(
1415
to: String,
15-
attrs: AttrBuilderContext<HTMLAnchorElement>? = null,
16+
attrs: (AttrsScope<HTMLAnchorElement>.(Boolean) -> Unit)? = null,
1617
content: ContentBuilder<HTMLAnchorElement>? = null
1718
) {
1819
val router = Router.current
1920
A(
2021
href = to,
2122
attrs = {
22-
if (to == "/") {
23-
if (router.currentPath.path == to) {
24-
classes("active")
25-
}
26-
} else if (router.currentPath.path.startsWith(to)) {
27-
classes("active")
28-
}
23+
val currentPath = router.currentPath.path
24+
val selected = if (to == "/") {
25+
currentPath == to
26+
} else currentPath.startsWith(to)
27+
2928
onClick {
3029
if (it.ctrlKey || it.metaKey) return@onClick
3130
router.navigate(to)
3231
it.preventDefault()
3332
}
34-
attrs?.invoke(this)
33+
attrs?.invoke(this, selected)
3534
},
3635
content = content
3736
)

0 commit comments

Comments
 (0)