Skip to content

lukwol/navigation

Repository files navigation

navigation

maven-central check

Tiny library for easy navigation in Compose Multiplatform applications.

Provides:

  • Screens navigation - multiplatform
  • Optional ViewModel with work cancellation support - multiplatform
  • Windows navigation - desktop only

Versions

For each version of navigation specific version of Compose Multiplatform is required

navigation compose-multiplatform
1.4.0 1.6.1
1.3.2 1.5.12
1.3.1 1.5.11
1.3.0 1.5.10
1.2.0 1.5.3
1.1.0 1.5.2
1.0.0 1.5.1

Installation

Add mavenCentral and google repositories:

repositories {
    mavenCentral()
    google()
}

Declare dependencies in build.gradle.kts:

dependencies {
    // Screens navigation - multiplatform
    implementation("io.github.lukwol:navigation-screens:1.4.0")
    // Screens navigation with ViewModel support - multiplatform
    implementation("io.github.lukwol:navigation-screens-viewmodel:1.4.0")
    // Windows navigation - desktop application only
    implementation("io.github.lukwol:navigation-windows:1.4.0")
}

Usage

Bootstrap new project with handy app-template.

Build screens navigation

Basic screens navigation:

ScreensNavigation(
    startRoute = AppRoutes.FirstScreenRoute,
) {
    screen(AppRoutes.FirstScreenRoute) {
        FirstScreen()
    }

    screen(AppRoutes.SecondScreenRoute) { args: String? ->
        SecondScreen(args)
    }
}

Screens navigation with ViewModel and custom animations:

ScreensNavigation(
    startRoute = AppRoutes.FirstScreenRoute,
    enterTransition = {
        slideIntoContainer(AnimatedContentTransitionScope.SlideDirection.Left)
    },
    exitTransition = {
        slideOutOfContainer(AnimatedContentTransitionScope.SlideDirection.Left)
    },
    popEnterTransition = {
        slideIntoContainer(AnimatedContentTransitionScope.SlideDirection.Right)
    },
    popExitTransition = {
        slideOutOfContainer(AnimatedContentTransitionScope.SlideDirection.Right)
    },
) {
    screen(
        route = AppRoutes.FirstScreenRoute,
        viewModelFactory = {
            FirstScreenViewModel()
        }
    ) { viewModel ->
        FirstScreen(viewModel)
    }

    screen(
        route = AppRoutes.SecondScreenRoute,
        viewModelWithArgs = { args: SomeArgs? ->
            SecondScreenViewModel(args)
        }
    ) { viewModel ->
        SecondScreen(viewModel)
    }
}

Build windows navigation if needed (desktop)

WindowsNavigation(
    startRoute = AppRoutes.FirstWindowRoute
) {
    window(
        route = AppRoutes.FirstWindowRoute,
        title = "First Window"
    ) {
        ScreensNavigation(
            startRoute = AppRoutes.FirstScreenRoute
        ) {
            // ...
        }
    }

    window(
        route = AppRoutes.SecondWindowRoute,
        title = "Second Window"
    ) {
        ScreensNavigation(
            startRoute = AppRoutes.SecondScreenRoute
        ) {
            // ...
        }
    }
}

Navigate to screen

// Obtain LocalScreensController in your view
val screensController = LocalScreensController.current

// Push screen
screensController.push(AppRoutes.SecondScreenRoute)

// Optionally pass arguments if needed
screensController.push(AppRoutes.SecondScreenRoute, SomeArguments)

// Pop screen to navigate back
screensController.pop()

// Optionally pass route, up to which screens should be dismissed
screensController.pop(AppRoutes.FirstScreenRoute)

Navigate to window (desktop)

// Obtain LocalWindowController in your view
val windowsController = LocalWindowController.current

// Open window
windowsController.open(AppRoutes.SecondWindowRoute)

// Optionally pass arguments if needed
windowsController.open(AppRoutes.SecondWindowRoute, SomeArguments)

// Close window
windowsController.close(AppRoutes.SecondWindowRoute)

Documentation

API Reference is available at https://lukwol.github.io/navigation/

Licensing

Project is available under MIT License.