Snackbar with Action
Brief message carrying one action, such as Undo, shown without taking focus. Because focus never moves to it, its action is hard to reach from a keyboard and easy to miss with TalkBack, so the action must also be available somewhere else and the message must stay until dismissed.
Selection
The criteria an agent checks before retrieving this component.
Use when
- Use when a brief message reports something the user did and offers one optional action on it (e.g., "Message archived" with "Undo", "Upload failed" with "Retry").
- Use when the user can safely ignore both the message and the action.
Try a different component when
- Do not use when the message needs no action (use
snackbar.basic). - Do not use when the user must decide before continuing (use
dialog.alert). - Do not use when the action is the only way to do something important, such as the only route to recover deleted work. Put it where the user can find it again.
Must Haves
Non-negotiable structure. Every generated instance must satisfy these rules.
- The message is announced without moving focus, can be dismissed without a gesture, and stays until the user dismisses it. Material's
SnackbarHostis the reference implementation of that contract, withshowSnackbar's defaultSnackbarDuration.Indefinitefor a message that has an action (global.native-first). - Make the action available somewhere else in the interface too, such as an Undo control on the screen itself, an entry in the screen's overflow menu, or a restore option on the archived item. Focus never moves to the snackbar, so the user may never reach its button.
- Show the message through
SnackbarHostState.showSnackbarwith anactionLabel, and keep the default duration, which isSnackbarDuration.Indefinitefor a message with an action. - Pass
withDismissAction = true, so a message that stays until dismissed has a visible close control as well as its accessibilitydismissaction. - Label the action with what it does (e.g., "Undo", "Retry", "View"), not with its outcome.
- Carry one action. A second command belongs somewhere the user navigates to, not in a message they may not reach.
- Handle both results of
showSnackbar: run the action onSnackbarResult.ActionPerformed, and treatSnackbarResult.Dismissedas the user declining it. - Leave focus where it is when the message appears (
global.focus-management).
Donts
Avoid these accessibility and UX barriers.
- Do not set
SnackbarDuration.ShortorSnackbarDuration.Longon an action snackbar. The message can leave before a TalkBack or switch user gets to the action. - Do not move focus to the snackbar to make the action reachable. It interrupts the user, and the action belongs elsewhere as well anyway.
- Do not make the snackbar the only route to an action. A user who missed it cannot get it back.
- Do not render a
Snackbardirectly with its ownactionslot. OutsideSnackbarHostit is not announced and has no dismiss action. - Do not word the message so it only makes sense with the action (e.g., "Undo?"). The message is announced on its own.
Customizable
Alternatives and options that give the AI agent some room to move.
- The action may also be offered through a keyboard shortcut, such as Ctrl+Z for Undo, in addition to the other route in the interface.
- The message may sit in the default position at the bottom of the
Scaffold, or in aSnackbarHostplaced elsewhere, provided it is still aSnackbarHost. Snackbarcolors and shape may follow the brand through the host'ssnackbarslot, which keeps the host's semantics.
Golden Pattern
The tested reference implementation. Agents start from this shape and adapt to the developer’s codebase and context.
@Composable
fun SnackbarActionExamples(onArchive: () -> Unit, onUnarchive: () -> Unit) {
val snackbarHostState = remember { SnackbarHostState() }
val scope = rememberCoroutineScope()
var canUndo by remember { mutableStateOf(false) }
Scaffold(
snackbarHost = { SnackbarHost(hostState = snackbarHostState) }
) { padding ->
Column(Modifier.padding(padding)) {
Button(onClick = {
onArchive()
canUndo = true
scope.launch {
val result = snackbarHostState.showSnackbar(
message = "Message archived",
actionLabel = "Undo",
withDismissAction = true
)
if (result == SnackbarResult.ActionPerformed) {
onUnarchive()
canUndo = false
}
}
}) {
Text("Archive")
}
if (canUndo) {
TextButton(onClick = {
onUnarchive()
canUndo = false
snackbarHostState.currentSnackbarData?.dismiss()
}) {
Text("Undo archive")
}
}
}
}
}