11package 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
314import androidx.compose.foundation.shape.CornerSize
415import androidx.compose.foundation.shape.RoundedCornerShape
516import androidx.compose.material3.ExperimentalMaterial3Api
617import androidx.compose.material3.MaterialTheme
718import androidx.compose.runtime.Composable
19+ import androidx.compose.ui.platform.LocalDensity
20+ import androidx.compose.ui.unit.Density
821import androidx.compose.ui.unit.dp
922import androidx.navigation.NavHostController
1023import 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+ }
0 commit comments