DeclarativeSnackbar
0.2.1indexedOffers a declarative API for managing Snackbars, supporting global and local messages, type-safe custom messages, flexible positioning, custom UI, and animations.
84
Stars
—
Used by
dependents
17
Health
/ 100
Offers a declarative API for managing Snackbars, supporting global and local messages, type-safe custom messages, flexible positioning, custom UI, and animations.
A declarative API for Snackbars in Compose Multiplatform (Android, iOS, Desktop) that makes it easy to display and manage snackbar messages in your application.
| Global and local | Custom content |
|---|---|
Add the dependency to your app's build.gradle.kts file:
dependencies {
implementation("io.github.ajiekcx:declarative-snackbar-compose:0.2.0")
implementation("io.github.ajiekcx:declarative-snackbar-core:0.2.0") // Optional, for modules without compose dependency
}
⚠️ For demonstration purposes only, use DI in real applications!
object GlobalSnackbarComponent: SnackbarComponent<String> by SnackbarComponent()
SnackbarBox(
component = GlobalSnackbarComponent,
snackbarContent = { message ->
Snackbar { Text(message) }
},
) {
// Your app content here
}
GlobalSnackbarComponent.show(
SnackbarMessage(
content = "This is a global message",
duration = SnackbarDuration.Short
)
)
SnackbarBox() {
Scaffold(
topBar = {
TopAppBar(
title = { Text("Title") },
modifier = Modifier.noOverlapTopContentBySnackbar()
)
},
bottomBar = {
NavigationBar(
modifier = Modifier.noOverlapBottomContentBySnackbar()
)
}
)
}
Use the noOverlapTopContentBySnackbar modifier to prevent the snackbar (with top alignment) from
overlapping top UI content, and noOverlapBottomContentBySnackbar for bottom-aligned snackbars.
sealed interface MySnackbarMessage {
val message: String
class Text(override val message: String) : MySnackbarMessage
class TextWithAction(
override val message: String,
val actionTitle: String,
val onActionClick: () -> Unit
) : MySnackbarMessage
}
class MyViewModel : ViewModel() {
val snackbarComponent: SnackbarComponent<MySnackbarMessage> = SnackbarComponent()
fun showMessage() {
snackbarComponent.show(
SnackbarMessage(
content = MySnackbarMessage.Text("This is a local message"),
duration = SnackbarDuration.Short
)
)
}
override fun {
.onCleared()
snackbarComponent.onDestroy()
}
}
SnackbarBox(
component = viewModel.snackbarComponent,
alignment = Alignment.TopCenter,
snackbarContent = { message ->
Snackbar(
modifier = Modifier.padding(horizontal = 16.dp),
action = {
if (message is MySnackbarMessage.TextWithAction) {
TextButton(onClick = { message.onActionClick() }) {
Text(message.actionTitle)
}
}
}
) {
Text(message.message)
}
},
) {
// Your screen content here
}
See the sample app for more examples.
Surfaced from shared tags and platforms — no rankings paid for.