Skip to content

Instantly share code, notes, and snippets.

@muhammedesadcomert
Last active January 1, 2024 16:46
Show Gist options
  • Save muhammedesadcomert/9460bfc62d764013d1f839c9027c904f to your computer and use it in GitHub Desktop.
Save muhammedesadcomert/9460bfc62d764013d1f839c9027c904f to your computer and use it in GitHub Desktop.
@SuppressLint("ComposableModifierFactory")
@Composable
fun Modifier.bouncyClick(enabled: Boolean = true): Modifier {
return if (enabled) {
var clickState by remember { mutableStateOf(ClickState.Idle) }
val scale by animateFloatAsState(
targetValue = if (clickState == ClickState.Pressed) 0.925f else 1f,
animationSpec = spring(
dampingRatio = Spring.DampingRatioNoBouncy,
stiffness = Spring.StiffnessMedium
),
label = "bouncyClick"
)
graphicsLayer {
scaleX = scale
scaleY = scale
}.pointerInput(clickState) {
awaitPointerEventScope {
clickState = if (clickState == ClickState.Pressed) {
waitForUpOrCancellation()
ClickState.Idle
} else {
awaitFirstDown(false)
ClickState.Pressed
}
}
}
} else {
this
}
}
enum class ClickState {
Pressed,
Idle
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment