Johnny
07/11/2024, 7:53 AMsharedElement
modifier and ended up creating a wrapper modifier for easier usage. It looks like this:
@Composable
fun Modifier.sharedElementModifier(
key: String,
): Modifier {
val sharedTransitionScope = LocalSharedTransitionScope.current
val animatedVisibilityScope = LocalAnimatedContentScope.current
with(sharedTransitionScope) {
return animatedVisibilityScope?.let {
return this@sharedElementModifier then Modifier.sharedElement(
rememberSharedContentState(key = key),
animatedVisibilityScope = animatedVisibilityScope,
)
} ?: this@sharedElementModifier
}
}
Is something like that possible with the new Modifier.Node
api? And do you see any performance issues by using this wrapper?Zach Klippenstein (he/him) [MOD]
07/11/2024, 8:02 AMcomposed
was for. But now a modifier node can read composition locals so you don’t need to read them ahead of time.Stylianos Gakis
07/11/2024, 8:06 AMfun Modifier.sharedElement(
sharedTransitionScope: SharedTransitionScope,
animatedVisibilityScope: AnimatedVisibilityScope,
state: SharedContentState,
boundsTransform: BoundsTransform = DefaultBoundsTransform,
placeHolderSize: PlaceHolderSize = contentSize,
renderInOverlayDuringTransition: Boolean = true,
zIndexInOverlay: Float = 0f,
clipInOverlayDuringTransition: OverlayClip = ParentClip,
): Modifier = with(sharedTransitionScope) {
this@sharedElement.sharedElement(
state = state,
animatedVisibilityScope = animatedVisibilityScope,
boundsTransform = boundsTransform,
placeHolderSize = placeHolderSize,
renderInOverlayDuringTransition = renderInOverlayDuringTransition,
zIndexInOverlay = zIndexInOverlay,
clipInOverlayDuringTransition = clipInOverlayDuringTransition,
)
}
where on the call site you do this
.sharedElement(
LocalSharedTransitionScope.current,
LocalNavAnimatedVisibilityScope.current,
rememberSharedContentState(contract.id),
)
Honestly I prefer this one since sometimes you will in fact not want to grab the visibility or transitionscope from your CompositionLocals. You might have a different instance you want to pass in for transitions tied perhaps to something other than your navigation, or to some transitions that are in a smaller scoped transitionScope.Johnny
07/11/2024, 8:44 AMeygraber
07/11/2024, 10:10 PMrememberSharedContentState
is a function on SharedTransitionScope
Stylianos Gakis
07/11/2024, 10:55 PMeygraber
07/11/2024, 11:12 PMStylianos Gakis
07/12/2024, 8:33 AM@Composable
fun rememberSharedContentState(key: Any): SharedContentState {
return LocalSharedTransitionScope.current.rememberSharedContentState(key)
}
So in the call site when you write
.sharedElement(
LocalSharedTransitionScope.current,
LocalNavAnimatedVisibilityScope.current,
rememberSharedContentState(id),
)
You practically write
.sharedElement(
LocalSharedTransitionScope.current,
LocalNavAnimatedVisibilityScope.current,
LocalSharedTransitionScope.current.rememberSharedContentState(id),
)
So you use the same scope by default for these two.
Of course it's possible to forget about this, and pass a different scope on the first parameter, and use a different scope for the rememberSharedContentState
call, if that is what you were worried about.
Otherwise these are both composition local reads next to each other, they should always properly resolve to the current local which must be the same in the same function call. If you hold the result of rememberSharedContentState
and pass it further down in some way where the local may have changed, sure that could be a problem too.Thomas Hormes
07/25/2024, 12:12 PMThomas Hormes
07/25/2024, 12:12 PMStylianos Gakis
07/25/2024, 12:22 PMStylianos Gakis
07/25/2024, 12:23 PMThomas Hormes
07/25/2024, 2:58 PMStylianos Gakis
07/25/2024, 3:23 PMStylianos Gakis
07/25/2024, 3:50 PMThomas Hormes
07/26/2024, 5:39 AM