Skip to content

Instantly share code, notes, and snippets.

@iamhariomsharma
Created July 27, 2024 17:05
Show Gist options
  • Save iamhariomsharma/913303d5804087b6c6e9034a60dcebc8 to your computer and use it in GitHub Desktop.
Save iamhariomsharma/913303d5804087b6c6e9034a60dcebc8 to your computer and use it in GitHub Desktop.
fun Modifier.bouncingClickable(
enabled: Boolean = true,
onLongClick: () -> Unit = {},
onClick: () -> Unit = {},
) = composed {
val interactionSource = remember { MutableInteractionSource() }
val isPressed by interactionSource.collectIsPressedAsState()
val haptic = LocalHapticFeedback.current
val animationTransition = updateTransition(isPressed, label = "BouncingClickableTransition")
val scaleFactor by animationTransition.animateFloat(
targetValueByState = { pressed -> if (pressed) 0.94f else 1f },
label = "BouncingClickableScaleFactorTransition",
transitionSpec = {
spring(
dampingRatio = Spring.DampingRatioMediumBouncy,
stiffness = Spring.StiffnessLow
)
}
)
this
.graphicsLayer {
this.scaleX = scaleFactor
this.scaleY = scaleFactor
}
.combinedClickable(
interactionSource = interactionSource,
indication = null,
enabled = enabled,
onClick = {
haptic.performHapticFeedback(HapticFeedbackType.TextHandleMove)
onClick()
},
onLongClick = {
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
onLongClick()
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment