# File-Based & Typed Routing (https://crow.build/docs/stack/routing)

`crow-router` (paired with the `crow.routes` Gradle plugin) turns your `routes/` folder into a compile-time, type-safe navigation graph — inspired by Expo Router and Next.js App Router, built natively for Kotlin and Android.

## Folder Conventions [#folder-conventions]

Every Kotlin file under your `routes/` directory becomes a strongly typed route on the generated `Routes` object:

<Files>
<Folder name="routes" defaultOpen>
<File name="_layout.kt" />

<File name="_not_found.kt" />

<File name="log_brew.kt" />

<Folder name="_group_main" defaultOpen>
<File name="_layout.kt" />

<File name="brews.kt" />

<File name="beans.kt" />

<File name="studio.kt" />
</Folder>

<Folder name="brew" defaultOpen>
<File name="_id.kt" />
</Folder>
</Folder>
</Files>

| File / Folder | Generated Route | Behavior |
| --- | --- | --- |
| `brews.kt` | `Routes.Brews` | Standard page |
| `brew/_id.kt` | `Routes.Brew.Id(id)` | Dynamic path parameter (`_id` = path identity) |
| `files/__path.kt` | `Routes.Files.Path(path)` | Catch-all wildcard segment |
| `_group_main/_layout.kt` | Tab shell (`@TabsLayout`) | Independent back stack & scroll state per tab |
| `log_brew.kt` | `Routes.LogBrew(method, dose)` | Optional function params become live query params |
| `_not_found.kt` | Fallback route | Unmatched deep links |

## Declaring Pages & Sheets [#declaring-pages--sheets]

A page's parameters are simply its `@Page @Ui` function parameters. Path parameters define entry identity; query parameters arrive as **live props** updated in place without remounting the screen:

```kotlin title="routes/log_brew.kt"
val logBrewConfig = routeConfig<Routes.LogBrew> {
    presentation = Presentation.Sheet
}

@Page
@Ui
fun LogBrewScreen(
    method: BrewMethod = BrewMethod.PourOver,
    dose: Int = 15,
    water: Int = 240,
) {
    val router = router()
    // ...
    Button(onClick = { router.dismiss() }) { Text("Done") }
}
```

Navigate from anywhere with full compile-time type safety:

```kotlin
val router = router()
router.navigate(Routes.Brew.Id(id = brew.id))
router.navigate(Routes.LogBrew(method = BrewMethod.Espresso, dose = 18))
```

## Tab Shells with Independent Histories [#tab-shells-with-independent-histories]

Annotate a group layout with `@TabsLayout` and render `SelectedTab(tabs)`:

```kotlin title="routes/_group_main/_layout.kt"
@TabsLayout("brews", "beans", "studio")
@Ui
fun MainTabs(@Once tabs: TabsScope) {
    Box(Modifier.fillMaxSize()) {
        SelectedTab(tabs)
        TabBar(
            items = tabItems,
            selected = tabs.selected,
            onSelect = tabs::select,
        )
    }
}
```

Switching tabs keeps covered tabs **dormant** (no layout, draw, or effects) while preserving their scroll position and sub-stack.

## Low-Level `BackStack` & `NavHost` [#low-level-backstack--navhost]

If you prefer programmatic routing without file generation, `crow-runtime` provides `BackStack<Route>` and `NavHost(stack)` directly — complete with `keepAlive` dormant screens, `ModalRoute`, typed `pushForResult`, held data transitions (up to 150 ms so destination screens never flash empty), route transitions, shared elements, and 1:1 predictive back.