-
-
Save kasem-sm/694fbc82c142ef5b50846c19b8927c01 to your computer and use it in GitHub Desktop.
Animated Placeholder with Jetpack Compose
This file contains 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
@Composable | |
fun AnimatedPlaceholder( | |
hints: List<String>, | |
textStyle: FontFamily = LocalSlimeFont.current.medium, | |
textColor: Color = MaterialTheme.colorScheme.onSurfaceVariant, | |
) { | |
val iterator = hints.listIterator() | |
val target by produceState(initialValue = hints.first()) { | |
iterator.doWhenHasNextOrPrevious { | |
value = it | |
} | |
} | |
AnimatedContent( | |
targetState = target, | |
transitionSpec = { ScrollAnimation() } | |
) { str -> | |
Text( | |
text = str, | |
fontFamily = textStyle, | |
color = textColor, | |
fontSize = 14.withScale(), | |
) | |
} | |
} |
This file contains 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
suspend fun <T> ListIterator<T>.doWhenHasNextOrPrevious( | |
delayMills: Long = 3000, | |
doWork: suspend (T) -> Unit | |
) { | |
while (hasNext() || hasPrevious()) { | |
while (hasNext()) { | |
delay(delayMills) | |
doWork(next()) | |
} | |
while (hasPrevious()) { | |
delay(delayMills) | |
doWork(previous()) | |
} | |
} | |
} |
This file contains 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
object ScrollAnimation { | |
operator fun invoke(): ContentTransform { | |
return slideInVertically( | |
initialOffsetY = { 50 }, | |
animationSpec = tween() | |
) + fadeIn() with slideOutVertically( | |
targetOffsetY = { -50 }, | |
animationSpec = tween() | |
) + fadeOut() | |
} | |
} |
This file contains 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
@Composable | |
fun SearchBar( | |
modifier: Modifier = Modifier, | |
query: String, | |
onQueryChange: (String) -> Unit, | |
) { | |
SlimeTextField( | |
modifier = modifier, | |
input = query, | |
onTextChange = onQueryChange, | |
placeholderContent = { | |
AnimatedPlaceholder( | |
hints = listOf( | |
stringResource(id = R.string.article_search_txt_1), | |
stringResource(id = R.string.article_search_txt_2), | |
"Search your favourite topic", | |
), | |
) | |
}, | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment