Skip to content
isaac-udyPublic

About

A simple navigation library for Kotlin/Compose Multiplatform 🗺️

Topics

Resources

Stars

271 stars

Watchers

4 watching

Forks

Latest commit

 

History

1,914 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Maven Central

Note

The CHANGELOG lists the changes in each Enro release.

Enro 🗺️

Enro is a navigation library for Kotlin Multiplatform. In Enro, a screen is declared like a function.

A NavigationKey is the signature of a screen. The NavigationKey declares the screen's inputs and, optionally, the type of the screen's result. Code that opens a screen depends on the NavigationKey, not on the screen's implementation.

@Serializable
data class ShowProfile(val userId: String) : NavigationKey

@Serializable
data class SelectDate(
    val minDate: LocalDate? = null,
    val maxDate: LocalDate? = null,
) : NavigationKey.WithResult<LocalDate>

The same two contracts, written as function signatures:

fun showProfile(userId: String): Unit
fun selectDate(minDate: LocalDate? = null, maxDate: LocalDate? = null): LocalDate

Enro targets Android, iOS, Desktop and Web through Compose Multiplatform. On Android, a Fragment or an Activity can be a destination. The enro-compat module provides the Enro 2 API for apps that are migrating to Enro 3.

Gradle dependencies

Enro is published to Maven Central. Add the mavenCentral() repository and these dependencies:

dependencies {
    implementation("dev.enro:enro:3.0.0")
    ksp("dev.enro:enro-processor:3.0.0")
    testImplementation("dev.enro:enro-test:3.0.0")
}

Defining and opening a screen

Declare a NavigationKey for each screen, bind a destination to each key with @NavigationDestination, and open a key with navigation.open(key).

@Serializable
data object Home : NavigationKey

@Serializable
data class Profile(val userId: String) : NavigationKey

@Composable
@NavigationDestination(Home::class)
fun HomeScreen() {
    val navigation = navigationHandle<Home>()
    Button(onClick = { navigation.open(Profile("user-123")) }) {
        Text("View profile")
    }
}

@Composable
@NavigationDestination(Profile::class)
fun ProfileScreen() {
    val navigation = navigationHandle<Profile>()
    Column {
        Text("Profile for ${navigation.key.userId}")
        Button(onClick = { navigation.close() }) { Text("Back") }
    }
}

Declare one NavigationComponent and install the NavigationComponent when the application starts:

@NavigationComponent
object MyComponent : NavigationComponentConfiguration(
    module = createNavigationModule { }
)

// Android
class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        MyComponent.installNavigationController(this)
    }
}

Create a navigation container in a Composable and render the container with NavigationDisplay:

val container = rememberNavigationContainer(
    backstack = backstackOf(Home.asInstance()),
)
NavigationDisplay(state = container)

The documentation at enro.dev covers each of these steps.

Documentation and examples

  • Documentation: enro.dev covers installation, concepts, results, animations, testing, each platform, and migration from Enro 2.
  • Recipes: recipes/ contains runnable examples. Each recipe shows one concept. enro.dev/docs/recipes.html lists the recipes. Run the recipes app with ./gradlew :recipes:app:desktop:run.
  • Sample apps: tests/application contains three larger samples. Run the test application with ./gradlew :tests:application:app:desktop:run, then choose "Samples".
  • API reference: ./gradlew dokkaGenerate writes the API reference to build/dokka/html/index.html. Each artifact on Maven Central also has a -javadoc.jar.
  • Changelog: CHANGELOG.md.

Migrating from Enro 2

Enro 3 is a rewrite of Enro 2 for Kotlin Multiplatform and Compose. The migration guide at enro.dev/docs/migrating-from-v2.html lists every API change. The main changes:

  • NavigationKey.SupportsPush / SupportsPresent → NavigationKey, or NavigationKey.WithResult<T> for a screen with a result
  • @Parcelize → @Serializable (kotlinx.serialization)
  • push() / present() → open(key); destination metadata declares dialog and overlay behaviour
  • closeWithResult(r) → complete(r)
  • NavigationApplication is removed; call installNavigationController from Application.onCreate
  • A Fragment or an Activity can be a destination; the enro-compat module provides the Enro 2 API during migration

Applications using Enro

   


"The novices' eyes followed the wriggling path up from the well as it swept a great meandering arc around the hillside. Its stones were green with moss and beset with weeds. Where the path disappeared through the gate they noticed that it joined a second track of bare earth, where the grass appeared to have been trampled so often that it ceased to grow. The dusty track ran straight from the gate to the well, marred only by a fresh set of sandal-prints that went down, and then up, and ended at the feet of the young monk who had fetched their water." — The Garden Path

About

A simple navigation library for Kotlin/Compose Multiplatform 🗺️

Topics

Resources

Stars

271 stars

Watchers

4 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages