Skip to content

Commit a6f87e2

Browse files
committed
make modalsheet main enter/exit animations configurable
1 parent 86beb9f commit a6f87e2

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed

demo/src/main/kotlin/dev/hrach/navigation/demo/NavHost.kt

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
package dev.hrach.navigation.demo
22

3+
import androidx.compose.animation.EnterTransition
4+
import androidx.compose.animation.ExitTransition
5+
import androidx.compose.animation.core.FastOutLinearInEasing
6+
import androidx.compose.animation.core.LinearOutSlowInEasing
7+
import androidx.compose.animation.core.tween
8+
import androidx.compose.animation.fadeIn
9+
import androidx.compose.animation.fadeOut
10+
import androidx.compose.animation.slideInHorizontally
11+
import androidx.compose.animation.slideInVertically
12+
import androidx.compose.animation.slideOutHorizontally
13+
import androidx.compose.animation.slideOutVertically
314
import androidx.compose.foundation.shape.CornerSize
415
import androidx.compose.foundation.shape.RoundedCornerShape
516
import androidx.compose.material3.ExperimentalMaterial3Api
617
import androidx.compose.material3.MaterialTheme
718
import androidx.compose.runtime.Composable
19+
import androidx.compose.ui.platform.LocalDensity
20+
import androidx.compose.ui.unit.Density
821
import androidx.compose.ui.unit.dp
922
import androidx.navigation.NavHostController
1023
import androidx.navigation.compose.NavHost
@@ -29,9 +42,14 @@ internal fun NavHost(
2942
modalSheetNavigator: ModalSheetNavigator,
3043
bottomSheetNavigator: BottomSheetNavigator,
3144
) {
45+
val density = LocalDensity.current
3246
NavHost(
3347
navController = navController,
3448
startDestination = Destinations.Home,
49+
enterTransition = { SharedXAxisEnterTransition(density) },
50+
exitTransition = { SharedXAxisExitTransition(density) },
51+
popEnterTransition = { SharedXAxisPopEnterTransition(density) },
52+
popExitTransition = { SharedXAxisPopExitTransition(density) },
3553
) {
3654
composable<Destinations.Home> { Home(navController) }
3755
composable<Destinations.List> { List() }
@@ -40,14 +58,62 @@ internal fun NavHost(
4058
modalSheet<Destinations.Modal2> { Modal2() }
4159
bottomSheet<Destinations.BottomSheet> { BottomSheet(navController) }
4260
}
43-
ModalSheetHost(modalSheetNavigator, containerColor = MaterialTheme.colorScheme.background)
61+
ModalSheetHost(
62+
modalSheetNavigator = modalSheetNavigator,
63+
containerColor = MaterialTheme.colorScheme.background,
64+
enterTransition = { SharedYAxisEnterTransition(density) },
65+
exitTransition = { SharedYAxisExitTransition(density) },
66+
)
4467
BottomSheetHost(
45-
bottomSheetNavigator,
46-
shape = RoundedCornerShape( // optional, just an example of bottom sheet custom property
68+
navigator = bottomSheetNavigator,
69+
shape = RoundedCornerShape(
70+
// optional, just an example of bottom sheet custom property
4771
topStart = CornerSize(12.dp),
4872
topEnd = CornerSize(12.dp),
4973
bottomStart = CornerSize(0.dp),
5074
bottomEnd = CornerSize(0.dp),
5175
),
5276
)
5377
}
78+
79+
private val SharedXAxisEnterTransition: (Density) -> EnterTransition = { density ->
80+
fadeIn(animationSpec = tween(durationMillis = 210, delayMillis = 90, easing = LinearOutSlowInEasing)) +
81+
slideInHorizontally(animationSpec = tween(durationMillis = 300)) {
82+
with(density) { 30.dp.roundToPx() }
83+
}
84+
}
85+
86+
private val SharedXAxisPopEnterTransition: (Density) -> EnterTransition = { density ->
87+
fadeIn(animationSpec = tween(durationMillis = 210, delayMillis = 90, easing = LinearOutSlowInEasing)) +
88+
slideInHorizontally(animationSpec = tween(durationMillis = 300)) {
89+
with(density) { (-30).dp.roundToPx() }
90+
}
91+
}
92+
93+
private val SharedXAxisExitTransition: (Density) -> ExitTransition = { density ->
94+
fadeOut(animationSpec = tween(durationMillis = 90, easing = FastOutLinearInEasing)) +
95+
slideOutHorizontally(animationSpec = tween(durationMillis = 300)) {
96+
with(density) { (-30).dp.roundToPx() }
97+
}
98+
}
99+
100+
private val SharedXAxisPopExitTransition: (Density) -> ExitTransition = { density ->
101+
fadeOut(animationSpec = tween(durationMillis = 90, easing = FastOutLinearInEasing)) +
102+
slideOutHorizontally(animationSpec = tween(durationMillis = 300)) {
103+
with(density) { 30.dp.roundToPx() }
104+
}
105+
}
106+
107+
private val SharedYAxisEnterTransition: (Density) -> EnterTransition = { density ->
108+
fadeIn(animationSpec = tween(durationMillis = 210, delayMillis = 90, easing = LinearOutSlowInEasing)) +
109+
slideInVertically(animationSpec = tween(durationMillis = 300)) {
110+
with(density) { 30.dp.roundToPx() }
111+
}
112+
}
113+
114+
private val SharedYAxisExitTransition: (Density) -> ExitTransition = { density ->
115+
fadeOut(animationSpec = tween(durationMillis = 210, delayMillis = 90, easing = LinearOutSlowInEasing)) +
116+
slideOutVertically(animationSpec = tween(durationMillis = 300)) {
117+
with(density) { 30.dp.roundToPx() }
118+
}
119+
}

modalsheet/src/main/kotlin/dev/hrach/navigation/modalsheet/ModalSheetHost.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,17 @@ public fun ModalSheetHost(
176176
.background(if (transition.targetState == null || transition.currentState == null) Color.Transparent else containerColor),
177177
contentAlignment = Alignment.TopStart,
178178
transitionSpec = block@{
179+
@Suppress("UNCHECKED_CAST")
179180
val initialState = initialState ?: return@block ContentTransform(
180-
fadeIn(),
181+
enterTransition(this as AnimatedContentTransitionScope<NavBackStackEntry>),
181182
fadeOut(), // irrelevant
182183
0f,
183184
)
185+
186+
@Suppress("UNCHECKED_CAST")
184187
val targetState = targetState ?: return@block ContentTransform(
185188
fadeIn(), // irrelevant
186-
fadeOut(),
189+
exitTransition(this as AnimatedContentTransitionScope<NavBackStackEntry>),
187190
0f,
188191
)
189192

0 commit comments

Comments
 (0)