Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transitions in between Tab Navigator #265

Open
ShivamKumarJha opened this issue Nov 29, 2023 · 2 comments
Open

Transitions in between Tab Navigator #265

ShivamKumarJha opened this issue Nov 29, 2023 · 2 comments
Labels
bug Something isn't working tabnavigator transitions

Comments

@ShivamKumarJha
Copy link

Hi, Is there any way to have transitions such as slide or fade in between tab navigation?
Thanks

@ShivamKumarJha
Copy link
Author

ShivamKumarJha commented Nov 29, 2023

I'm able to achieve transitions in between tabs with some source mode modifications.
However this is not restoring the previous saved state.

class TabNavigator internal constructor(
    internal val navigator: Navigator
) {

    var current: Tab
        get() = navigator.lastItem as Tab
        set(tab) {
            lastEvent =
                if (tab.options.index >= current.options.index) StackEvent.Push else StackEvent.Pop
            navigator.replaceAll(tab)
        }

    var lastEvent: StackEvent = StackEvent.Push


    @Composable
    fun saveableState(
        key: String,
        tab: Tab = current,
        content: @Composable () -> Unit
    ) {
        navigator.saveableState(key, tab, content = content)
    }
}

@Composable
fun SlideTransition(
    navigator: TabNavigator,
    modifier: Modifier = Modifier,
    orientation: SlideOrientation = SlideOrientation.Horizontal,
    animationSpec: FiniteAnimationSpec<IntOffset> = spring(
        stiffness = Spring.StiffnessMediumLow,
        visibilityThreshold = IntOffset.VisibilityThreshold
    ),
    content: TabNavigatorContent = { it.current.Content() }
) {
    ScreenTransition(
        navigator = navigator,
        modifier = modifier,
        content = content,
        transition = {
            val (initialOffset, targetOffset) = when (navigator.lastEvent) {
                StackEvent.Pop -> ({ size: Int -> -size }) to ({ size: Int -> size })
                else -> ({ size: Int -> size }) to ({ size: Int -> -size })
            }

            when (orientation) {
                SlideOrientation.Horizontal ->
                    slideInHorizontally(animationSpec, initialOffset) togetherWith
                            slideOutHorizontally(animationSpec, targetOffset)

                SlideOrientation.Vertical ->
                    slideInVertically(animationSpec, initialOffset) togetherWith
                            slideOutVertically(animationSpec, targetOffset)
            }
        }
    )
}

@Composable
fun ScreenTransition(
    navigator: TabNavigator,
    transition: AnimatedContentTransitionScope<Tab>.() -> ContentTransform,
    modifier: Modifier = Modifier,
    content: TabNavigatorContent = { it.current.Content() }
) {
    AnimatedContent(
        targetState = navigator.current,
        transitionSpec = transition,
        modifier = modifier
    ) { tab ->
        navigator.saveableState("transition", tab) {
            content(navigator)
        }
    }
}

@ShivamKumarJha ShivamKumarJha changed the title Transitions in between Tabs Transitions in between Tab Navigator Nov 29, 2023
@cedrickcooke
Copy link

I was able to get state-restoration working with a small modification:

@Composable
fun ScreenTransition(
    navigator: TabNavigator,
    transition: AnimatedContentTransitionScope<Tab>.() -> ContentTransform,
    modifier: Modifier = Modifier,
-    content: TabNavigatorContent = { it.current.Content() }
) {
    AnimatedContent(
        targetState = navigator.current,
        transitionSpec = transition,
        modifier = modifier
    ) { tab ->
        navigator.saveableState("transition", tab) {
-            content(navigator)
+            tab.Content()
        }
    }
}

@DevSrSouza DevSrSouza added tabnavigator transitions bug Something isn't working labels Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working tabnavigator transitions
Projects
None yet
Development

No branches or pull requests

3 participants