Created
July 27, 2024 17:05
-
-
Save iamhariomsharma/913303d5804087b6c6e9034a60dcebc8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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