Snackbar.kt
package com.louisfn.somovie.ui.component
import androidx.compose.foundation.layout.padding
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Snackbar
import androidx.compose.material.SnackbarData
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
@Composable
fun DefaultSnackbar(
snackbarData: SnackbarData,
modifier: Modifier = Modifier,
actionOnNewLine: Boolean = false,
shape: Shape = MaterialTheme.shapes.small,
backgroundColor: Color = MaterialTheme.colors.background,
contentColor: Color = MaterialTheme.colors.surface,
actionColor: Color = MaterialTheme.colors.secondary,
elevation: Dp = 6.dp,
) {
DefaultSnackbar(
message = snackbarData.message,
actionLabel = snackbarData.actionLabel,
onActionClick = { snackbarData.performAction() },
modifier = modifier,
actionOnNewLine = actionOnNewLine,
shape = shape,
backgroundColor = backgroundColor,
contentColor = contentColor,
actionColor = actionColor,
elevation = elevation,
)
}
@Composable
fun DefaultSnackbar(
message: String,
actionLabel: String?,
onActionClick: () -> Unit,
modifier: Modifier = Modifier,
actionOnNewLine: Boolean = false,
shape: Shape = MaterialTheme.shapes.small,
backgroundColor: Color = MaterialTheme.colors.background,
contentColor: Color = MaterialTheme.colors.surface,
actionColor: Color = MaterialTheme.colors.secondary,
elevation: Dp = 6.dp,
) {
val actionComposable: (@Composable () -> Unit)? =
if (actionLabel != null) {
@Composable {
TextButton(
onClick = onActionClick,
content = { Text(text = actionLabel, color = actionColor) },
)
}
} else {
null
}
Snackbar(
modifier = modifier.padding(12.dp),
content = { Text(message) },
action = actionComposable,
actionOnNewLine = actionOnNewLine,
shape = shape,
backgroundColor = backgroundColor,
contentColor = contentColor,
elevation = elevation,
)
}