@@ -57,13 +57,13 @@ See the [](compose-lifecycle.md) page for details of the current implementation
57
57
## Support for browser navigation in web apps
58
58
<secondary-label ref =" Experimental " />
59
59
60
- Compose Multiplatform for web fully supports the common Navigation library APIs,
61
- and on top of that allows your apps to receive navigational input from the browser.
60
+ Compose Multiplatform for web fully supports the common Navigation library APIs
61
+ and allows your apps to receive navigational input from the browser.
62
62
Users can use the ** Back** and ** Forward** buttons in the browser to move between navigation routes reflected in the browser history,
63
63
as well as use the address bar to understand where they are and get to a destination directly.
64
64
65
65
To bind the web app to the navigation graph defined in your common code,
66
- you can use the ` window.bindToNavigation ()` method in your Kotlin/Wasm code.
66
+ you can use the ` NavController.bindToBrowserNavigation ()` method in your Kotlin/Wasm code.
67
67
You can use the same method in Kotlin/JS, but wrap it in the ` onWasmReady {} ` block to ensure
68
68
that the Wasm application is initialized and Skia is ready to render graphics.
69
69
Here is an example of how to set this up:
@@ -90,7 +90,7 @@ fun main() {
90
90
val body = document.body ? : return
91
91
ComposeViewport (body) {
92
92
App (
93
- onNavHostReady = { window.bindToNavigation(it ) }
93
+ onNavHostReady = { it.bindToBrowserNavigation( ) }
94
94
)
95
95
}
96
96
}
@@ -103,14 +103,14 @@ fun main() {
103
103
val body = document.body ? : return @onWasmReady
104
104
ComposeViewport (body) {
105
105
App (
106
- onNavHostReady = { window.bindToNavigation(it ) }
106
+ onNavHostReady = { it.bindToBrowserNavigation( ) }
107
107
)
108
108
}
109
109
}
110
110
}
111
111
```
112
112
113
- After a ` window.bindToNavigation(navController) ` call :
113
+ After calling ` navController.bindToBrowserNavigation() ` :
114
114
* The URL displayed in the browser reflects the current route (in the URL fragment, after the ` # ` character).
115
115
* The app parses URLs entered manually to translate them into destinations within the app.
116
116
@@ -141,7 +141,7 @@ you can assign the name to a screen directly or develop fully custom processing
141
141
142
142
To implement a fully custom route to URL transformation:
143
143
144
- 1 . Pass the optional `getBackStackEntryRoute` lambda to the `window.bindToNavigation ()` function
144
+ 1 . Pass the optional `getBackStackEntryRoute` lambda to the `navController.bindToBrowserNavigation ()` function
145
145
to specify how routes should be converted into URL fragments when necessary.
146
146
2 . If needed, add code that catches URL fragments in the address bar (when someone clicks or pastes your app' s URL)
147
147
and translates URLs into routes to navigate users accordingly.
@@ -191,7 +191,7 @@ internal fun App(
191
191
```
192
192
{default- state= " collapsed" collapsible= " true" collapsed- title= " NavHost(navController = navController, startDestination = StartScreen)" }
193
193
194
- In `wasmJsMain/ kotlin/ main.kt`, add the lambda to the `.bindToNavigation ()` call:
194
+ In `wasmJsMain/ kotlin/ main.kt`, add the lambda to the `.bindToBrowserNavigation ()` call:
195
195
196
196
```kotlin
197
197
@OptIn(
@@ -204,7 +204,7 @@ fun main() {
204
204
ComposeViewport (body) {
205
205
App (
206
206
onNavHostReady = { navController ->
207
- window.bindToNavigation(navController ) { entry ->
207
+ navController.bindToBrowserNavigation( ) { entry ->
208
208
val route = entry.destination.route.orEmpty()
209
209
when {
210
210
// Identifies the route using its serial descriptor
@@ -239,7 +239,7 @@ fun main() {
239
239
}
240
240
}
241
241
```
242
- < ! -- {default- state= " collapsed" collapsible= " true" collapsed- title= " window.bindToNavigation(navController ) { entry ->" }-- >
242
+ < ! -- {default- state= " collapsed" collapsible= " true" collapsed- title= " navController.bindToBrowserNavigation( ) { entry ->" }-- >
243
243
244
244
> Make sure that every string that corresponds to a route starts with the `#` character to keep the data
245
245
> within URL fragments.
@@ -250,8 +250,8 @@ fun main() {
250
250
251
251
If your URLs have custom formatting, you should add the reverse processing
252
252
to match manually entered URLs to destination routes.
253
- The code that does the matching needs to run before the `window.bindToNavigation ()` call binds
254
- `window. location` to the navigation graph:
253
+ The code that does the matching needs to run before the `navController.bindToBrowserNavigation ()` call binds
254
+ the browser location to the navigation graph:
255
255
256
256
< tabs>
257
257
< tab title= " Kotlin/Wasm" >
@@ -284,7 +284,7 @@ The code that does the matching needs to run before the `window.bindToNavigation
284
284
navController.navigate(Patient (name, id))
285
285
}
286
286
}
287
- window.bindToNavigation(navController ) { .. . }
287
+ navController.bindToBrowserNavigation( ) { .. . }
288
288
}
289
289
)
290
290
}
@@ -322,7 +322,7 @@ The code that does the matching needs to run before the `window.bindToNavigation
322
322
navController.navigate(Patient (name, id))
323
323
}
324
324
}
325
- window.bindToNavigation(navController ) { .. . }
325
+ navController.bindToBrowserNavigation( ) { .. . }
326
326
}
327
327
)
328
328
}
0 commit comments