commonMain.cafe.adriel.voyager.transitions.ScreenTransition.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of voyager-transitions-desktop Show documentation
Show all versions of voyager-transitions-desktop Show documentation
A pragmatic navigation library for Jetpack Compose
package cafe.adriel.voyager.transitions
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedContentScope
import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ContentTransform
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.core.stack.StackEvent
import cafe.adriel.voyager.navigator.Navigator
public typealias ScreenTransitionContent = @Composable AnimatedVisibilityScope.(Screen) -> Unit
@ExperimentalAnimationApi
@Composable
public fun ScreenTransition(
navigator: Navigator,
enterTransition: AnimatedContentScope.() -> ContentTransform,
exitTransition: AnimatedContentScope.() -> ContentTransform,
modifier: Modifier = Modifier,
content: ScreenTransitionContent = { it.Content() }
) {
ScreenTransition(
navigator = navigator,
modifier = modifier,
content = content,
transition = {
when (navigator.lastEvent) {
StackEvent.Pop -> exitTransition()
else -> enterTransition()
}
}
)
}
@ExperimentalAnimationApi
@Composable
public fun ScreenTransition(
navigator: Navigator,
transition: AnimatedContentScope.() -> ContentTransform,
modifier: Modifier = Modifier,
content: ScreenTransitionContent = { it.Content() }
) {
AnimatedContent(
targetState = navigator.lastItem,
transitionSpec = transition,
modifier = modifier
) { screen ->
navigator.saveableState("transition", screen) {
content(screen)
}
}
}