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 Show documentation
Show all versions of voyager-transitions Show documentation
A pragmatic navigation library for Jetpack Compose
The newest version!
package cafe.adriel.voyager.transitions
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedContentTransitionScope
import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ContentTransform
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
@Composable
public fun ScreenTransition(
navigator: Navigator,
enterTransition: AnimatedContentTransitionScope.() -> ContentTransform,
exitTransition: AnimatedContentTransitionScope.() -> ContentTransform,
modifier: Modifier = Modifier,
content: ScreenTransitionContent = { it.Content() }
) {
ScreenTransition(
navigator = navigator,
modifier = modifier,
content = content,
transition = {
when (navigator.lastEvent) {
StackEvent.Pop -> exitTransition()
else -> enterTransition()
}
}
)
}
@Composable
public fun ScreenTransition(
navigator: Navigator,
transition: AnimatedContentTransitionScope.() -> ContentTransform,
modifier: Modifier = Modifier,
content: ScreenTransitionContent = { it.Content() }
) {
AnimatedContent(
targetState = navigator.lastItem,
transitionSpec = transition,
modifier = modifier
) { screen ->
navigator.saveableState("transition", screen) {
content(screen)
}
}
}