Skip to content

Instantly share code, notes, and snippets.

@igorescodro
Last active January 25, 2025 16:58
Show Gist options
  • Save igorescodro/a52acad5abc0b149ae6a6e4dfb53f81a to your computer and use it in GitHub Desktop.
Save igorescodro/a52acad5abc0b149ae6a6e4dfb53f81a to your computer and use it in GitHub Desktop.
@Composable
fun Modifier.shimmerable(
shape: Shape = RoundedCornerShape(8.dp),
color: Color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.6f)
): Modifier {
if (!LocalShimmerState.current.isLoading) return this
return this
.shimmer()
.background(color = color, shape = shape)
.drawWithContent {
// Do not draw the actual content.
}
}
data class ShimmerState(val isLoading: Boolean)
val LocalShimmerState = compositionLocalOf { ShimmerState(isLoading = false) }
@Composable
fun ShimmerProvider(isLoading: Boolean = true, content: @Composable (Boolean) -> Unit) {
CompositionLocalProvider(
value = LocalShimmerState provides ShimmerState(isLoading = isLoading),
content = { content(isLoading) },
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment