# FadeThroughNavigationTransitions

> Provides fade through navigation transforms adhering to Material 3 motion specifications.

## API Reference

> Source set: Common

```kotlin
public object FadeThroughNavigationTransitions
```

Provides fade through navigation transforms adhering to Material 3 motion specifications.

Use these animations for transitions between destinations without a strong spatial or directional
relationship, such as switching between tabs in a bottom navigation bar. See
[Material 3 transition patterns](https://m3.material.io/styles/motion/transitions/transition-patterns).

## Functions

### transform

> Source set: Common

```kotlin
public fun transform(): ContentTransform
```

Creates a `ContentTransform` for a fade through transition.

### predictiveBackTransform

> Source set: Common

```kotlin
public fun predictiveBackTransform(
        backEvent: () -> NavigationEvent,
        density: Density,
    ): MutableContentTransform
```

Creates a `MutableContentTransform` configured for the gesture phase of a fade through
predictive back transition.

#### Parameters

| | |
| --- | --- |
| backEvent | provider of the ongoing `NavigationEvent` |
| density | the [Density](https://composables.com/jetpack-compose/androidx.compose.ui/ui-unit/interfaces/Density/api.md) used to convert display bounds margins to pixels |

## Code Examples

### FadeThroughTransitionSample

```kotlin
@Preview
@Sampled
@Composable
fun FadeThroughTransitionSample() {
    var targetTab 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(targetTab) }
    val transition = rememberDeferredTransition(transitionState)

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

    val density = LocalDensity.current

    transition.DeferredAnimatedContent(
        transitionSpec = { FadeThroughNavigationTransitions.transform() },
        mutableTransformSpec = {
            FadeThroughNavigationTransitions.predictiveBackTransform(
                backEvent = { backEvent },
                density = density,
            )
        },
    ) { tab ->
        Surface(
            modifier = Modifier.fillMaxSize(),
            color =
                if (tab == 1) {
                    MaterialTheme.colorScheme.primaryContainer
                } else {
                    MaterialTheme.colorScheme.tertiaryContainer
                },
        ) {
            Box(Modifier.fillMaxSize().padding(16.dp), contentAlignment = Alignment.Center) {
                Text("Tab $tab", style = MaterialTheme.typography.headlineLarge)
            }
        }
    }
}
```
