API Reference Item

HorizontalSharedAxisNavigationTransitions

Provides horizontal shared axis navigation transforms adhering to Material 3 motion specifications.

HorizontalSharedAxisTransitionSample

@Preview
@Sampled
@Composable
fun HorizontalSharedAxisTransitionSample() {
    var targetScreen by remember { mutableIntStateOf(1) }
    var isGestureInProgress by remember { mutableStateOf(false) }
    var backEvent by remember { mutableStateOf(NavigationEvent()) }

    // TODO(b/561731740): Replace this manual navigation setup with NavDisplay once it adopts
    //  MutableContentTransform, which provides built-in transition and back gesture handling.
    val transitionState = remember { DeferredTransitionState(targetScreen) }
    val transition = rememberDeferredTransition(transitionState)

    LaunchedEffect(isGestureInProgress, targetScreen) {
        if (isGestureInProgress) {
            transitionState.defer(targetScreen)
        } else {
            transitionState.animateTo(targetScreen)
        }
    }

    val scrimColor = HorizontalSharedAxisNavigationTransitions.scrimColor
    val density = LocalDensity.current

    transition.DeferredAnimatedContent(
        transitionSpec = {
            if (targetState > initialState) {
                HorizontalSharedAxisNavigationTransitions.forwardTransform()
            } else {
                HorizontalSharedAxisNavigationTransitions.backwardTransform()
            }
        },
        mutableTransformSpec = {
            if (initialState > targetState) {
                HorizontalSharedAxisNavigationTransitions.predictiveBackTransform(
                    backEvent = { backEvent },
                    density = density,
                    scrimColor = scrimColor,
                )
            } else {
                null
            }
        },
    ) { screen ->
        Surface(
            modifier = Modifier.fillMaxSize(),
            color =
                if (screen == 1) {
                    MaterialTheme.colorScheme.primaryContainer
                } else {
                    MaterialTheme.colorScheme.secondaryContainer
                },
        ) {
            Box(Modifier.fillMaxSize().padding(16.dp), contentAlignment = Alignment.Center) {
                Text("Screen $screen", style = MaterialTheme.typography.headlineLarge)
            }
        }
    }
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: