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

Support animating an overlay out after returning a result #1066

Merged
merged 10 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
// SPDX-License-Identifier: Apache-2.0
package com.slack.circuit.overlay

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.animation.SizeTransform
import androidx.compose.animation.core.snap
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier

/**
Expand All @@ -24,11 +30,30 @@ public fun ContentWithOverlays(
overlayHost: OverlayHost = rememberOverlayHost(),
content: @Composable () -> Unit
) {
val overlayHostData by rememberUpdatedState(overlayHost.currentOverlayData)
CompositionLocalProvider(LocalOverlayHost provides overlayHost) {
val overlayHostData by rememberUpdatedState(overlayHost.currentOverlayData)
Box(modifier) {
content()
key(overlayHostData) { overlayHostData?.let { data -> data.overlay.Content(data::finish) } }
AnimatedContent(
targetState = overlayHostData,
transitionSpec = {
val enter =
(targetState?.overlay as? AnimatedOverlay)?.enterTransition ?: EnterTransition.None
val exit =
(initialState?.overlay as? AnimatedOverlay)?.exitTransition ?: ExitTransition.None
val sizeTransform = if (targetState != null) SizeTransform { _, _ -> snap(0) } else null
(enter togetherWith exit).using(sizeTransform).also {
it.targetContentZIndex = targetState?.let { 1f } ?: -1f
}
},
contentAlignment = Alignment.Center
) { data ->
when (val overlay = data?.overlay) {
null -> Unit
is AnimatedOverlay -> with(overlay) { AnimatedContent(data::finish) }
else -> overlay.Content(data::finish)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
// SPDX-License-Identifier: Apache-2.0
package com.slack.circuit.overlay

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.animation.togetherWith
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ProvidableCompositionLocal
import androidx.compose.runtime.Stable
Expand Down Expand Up @@ -166,6 +171,30 @@ public interface Overlay<Result : Any> {
@Composable public fun Content(navigator: OverlayNavigator<Result>)
}

/**
* An [Overlay] that can be animated in and more interestingly out _after_ it has returned a
* [Result]. The [Overlay] is animated in and out by its [enterTransition] and [exitTransition],
* [AnimatedContent] is executed with with [AnimatedVisibilityScope] so that child animations can be
* coordinated with the overlay's animations.
*/
public abstract class AnimatedOverlay<Result : Any>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should mark this as an experimental API. I can see this API evolving.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though the library itself isn't stable yet anyway?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up to you here. I still think we can add signal to what APIs are more stable than others?

I don't mind either way though. If you'd rather keep experimental annotations out of here (understandably), all good with me.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I think we're fine without it for now, most of the library would probably fall under that annotation's umbrella if we introduced it heh

public val enterTransition: EnterTransition,
public val exitTransition: ExitTransition,
) : Overlay<Result> {
@Composable
final override fun Content(navigator: OverlayNavigator<Result>) {
AnimatedContent(
targetState = Unit,
transitionSpec = { EnterTransition.None togetherWith ExitTransition.None },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we should be using enterTransition and exitTransition here

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh good catch #1090

) {
AnimatedContent(navigator)
}
}

@Composable
public abstract fun AnimatedVisibilityScope.AnimatedContent(navigator: OverlayNavigator<Result>)
}

/** A [ProvidableCompositionLocal] to expose the current [OverlayHost] in the composition tree. */
public val LocalOverlayHost: ProvidableCompositionLocal<OverlayHost> = compositionLocalOf {
error("No OverlayHost provided")
Expand Down
Loading