A snackbar tells people about something the app has done, at the bottom of the screen: a message archived, a photo saved. It doesn't take focus or ask for anything, and has one action at most, such as Undo. For a decision, use a dialog.
Usage
show() puts it in a queue shared by every snackbar on the page: one shows at a time, the
others wait their turn. Without an action it goes after 4 seconds.
import { createSnackbar } from 'material';
const snackbar = createSnackbar({ message: 'Photo saved' });
document.body.append(snackbar.element);
function tell() {
snackbar.show();
}
<m-snackbar message="Photo saved"></m-snackbar>
<script type="module">
const snackbar = document.querySelector('m-snackbar');
function tell() {
snackbar.show();
}
</script>
import { useState } from 'react';
import { Snackbar } from 'material/react';
export function Example() {
const [open, setOpen] = useState(false);
const tell = () => setOpen(true);
return (
<Snackbar open={open} onClose={() => setOpen(false)} message="Photo saved" />
);
}
<script setup lang="ts">
import { ref } from 'vue';
import { MSnackbar } from 'material/vue';
const open = ref(false);
function tell() {
open.value = true;
}
</script>
<template>
<MSnackbar :open="open" @close="open = false" message="Photo saved" />
</template>
<script lang="ts">
import { Snackbar } from 'material/svelte';
let open = $state(false);
function tell() {
open = true;
}
</script>
<Snackbar open={open} onclose={() => (open = false)} message="Photo saved" />
import { createSignal } from 'solid-js';
import { Snackbar } from 'material/solid';
export function Example() {
const [open, setOpen] = createSignal(false);
const tell = () => setOpen(true);
return (
<Snackbar open={open()} onClose={() => setOpen(false)} message="Photo saved" />
);
}
Examples
Undo
A snackbar with an action stays until it is acted on or dismissed, so there is time to reach it. The action closes it.
import { createSnackbar } from 'material';
const snackbar = createSnackbar({ message: 'Message archived', action: 'Undo' });
snackbar.on('action', () => undoArchive());
document.body.append(snackbar.element);
function archive() {
snackbar.show();
}
<m-snackbar message="Message archived" action="Undo"></m-snackbar>
<script type="module">
const snackbar = document.querySelector('m-snackbar');
snackbar.addEventListener('action', () => undoArchive());
function archive() {
snackbar.show();
}
</script>
import { useState } from 'react';
import { Snackbar } from 'material/react';
import { undoArchive } from './app';
export function Example() {
const [open, setOpen] = useState(false);
const archive = () => setOpen(true);
return (
<Snackbar onAction={() => undoArchive()} open={open} onClose={() => setOpen(false)} message="Message archived" action="Undo" />
);
}
<script setup lang="ts">
import { ref } from 'vue';
import { MSnackbar } from 'material/vue';
import { undoArchive } from './app';
const open = ref(false);
function archive() {
open.value = true;
}
</script>
<template>
<MSnackbar @action="undoArchive()" :open="open" @close="open = false" message="Message archived" action="Undo" />
</template>
<script lang="ts">
import { Snackbar } from 'material/svelte';
import { undoArchive } from './app';
let open = $state(false);
function archive() {
open = true;
}
</script>
<Snackbar onaction={() => undoArchive()} open={open} onclose={() => (open = false)} message="Message archived" action="Undo" />
import { createSignal } from 'solid-js';
import { Snackbar } from 'material/solid';
import { undoArchive } from './app';
export function Example() {
const [open, setOpen] = createSignal(false);
const archive = () => setOpen(true);
return (
<Snackbar onAction={() => undoArchive()} open={open()} onClose={() => setOpen(false)} message="Message archived" action="Undo" />
);
}
A close button, and how long it stays
dismissible adds a close button. duration is short (4s), long (10s), indefinite, or
milliseconds.
import { createSnackbar } from 'material';
const snackbar = createSnackbar({ message: 'Update available', dismissible: true, duration: 'long' });
document.body.append(snackbar.element);
function tell() {
snackbar.show();
}
<m-snackbar message="Update available" dismissible duration="long"></m-snackbar>
<script type="module">
const snackbar = document.querySelector('m-snackbar');
function tell() {
snackbar.show();
}
</script>
import { useState } from 'react';
import { Snackbar } from 'material/react';
export function Example() {
const [open, setOpen] = useState(false);
const tell = () => setOpen(true);
return (
<Snackbar open={open} onClose={() => setOpen(false)} message="Update available" dismissible duration="long" />
);
}
<script setup lang="ts">
import { ref } from 'vue';
import { MSnackbar } from 'material/vue';
const open = ref(false);
function tell() {
open.value = true;
}
</script>
<template>
<MSnackbar :open="open" @close="open = false" message="Update available" dismissible duration="long" />
</template>
<script lang="ts">
import { Snackbar } from 'material/svelte';
let open = $state(false);
function tell() {
open = true;
}
</script>
<Snackbar open={open} onclose={() => (open = false)} message="Update available" dismissible duration="long" />
import { createSignal } from 'solid-js';
import { Snackbar } from 'material/solid';
export function Example() {
const [open, setOpen] = createSignal(false);
const tell = () => setOpen(true);
return (
<Snackbar open={open()} onClose={() => setOpen(false)} message="Update available" dismissible duration="long" />
);
}
Replacing the one on screen
When only the newest message matters, queueBehavior: 'replace' closes the snackbar on screen
and drops the waiting ones.
import { createSnackbar } from 'material';
const snackbar = createSnackbar({ message: 'Upload complete', queueBehavior: 'replace' });
document.body.append(snackbar.element);
function tell() {
snackbar.show();
}
<m-snackbar message="Upload complete" queue-behavior="replace"></m-snackbar>
<script type="module">
const snackbar = document.querySelector('m-snackbar');
function tell() {
snackbar.show();
}
</script>
import { useState } from 'react';
import { Snackbar } from 'material/react';
export function Example() {
const [open, setOpen] = useState(false);
const tell = () => setOpen(true);
return (
<Snackbar open={open} onClose={() => setOpen(false)} message="Upload complete" queueBehavior="replace" />
);
}
<script setup lang="ts">
import { ref } from 'vue';
import { MSnackbar } from 'material/vue';
const open = ref(false);
function tell() {
open.value = true;
}
</script>
<template>
<MSnackbar :open="open" @close="open = false" message="Upload complete" queue-behavior="replace" />
</template>
<script lang="ts">
import { Snackbar } from 'material/svelte';
let open = $state(false);
function tell() {
open = true;
}
</script>
<Snackbar open={open} onclose={() => (open = false)} message="Upload complete" queueBehavior="replace" />
import { createSignal } from 'solid-js';
import { Snackbar } from 'material/solid';
export function Example() {
const [open, setOpen] = createSignal(false);
const tell = () => setOpen(true);
return (
<Snackbar open={open()} onClose={() => setOpen(false)} message="Upload complete" queueBehavior="replace" />
);
}
position places it at the start, center or end of the bottom edge. layer: 'top'
shows it in the browser's top layer; while a modal dialog is open, it moves inside the dialog,
so it can still be read and used. The web component always is in the top layer.
clearSnackbars() closes the one on screen and drops the queue, for when what they were about
goes away.
API
Options
messageThe text, up to two lines
stringactionThe action's label
stringundefineddismissibleA close button
booleanfalsecloseLabelThe close button's accessible name
string'Dismiss'focusShortcutOne non-character KeyboardEvent.key name (such as 'F6', 'F8' or 'F9', with no modifiers held) that moves focus to the action button while on screen; false, 'off', '' or a single-character string disables it (<m-snackbar focus-shortcut>)
string | false'F8'durationHow long it stays: 4s, 10s, until closed, or milliseconds (0 is indefinite)
'short' | 'long' | 'indefinite' | number'indefinite' with an action, else 'short'positionWhere along the bottom edge
'center' | 'start' | 'end''center'queueBehaviorWhether it waits its turn or replaces the queue
'queue' | 'replace''queue'layerShows it in the top layer, inside the topmost modal dialog while one is open
'top'undefinedonAction / onOpen / onClose / onFocusHandlers for action, open, close and focus
(event: SnackbarEvent) => voidundefinedonEvent handlers registered at creation
{ open?, close?, action?, dismiss?, focus? }undefinedclassAdditional CSS classes
stringundefinedprefixPrefix for CSS class names
string'mtrl'Methods
show() / hide()Queues it to show, or closes it with reason api
SnackbarComponentisOpen()Whether it is on screen
booleangetMessage() / setMessage(message)The text
message: stringstring / SnackbarComponentgetAction() / setAction(text)The action's label
text: stringstring / SnackbarComponentgetDuration() / setDuration(duration)How long it stays, read in milliseconds; a change on screen starts the count again
duration: SnackbarDurationnumber / SnackbarComponentgetPosition() / setPosition(position)Where it sits
position: 'center' | 'start' | 'end'string / SnackbarComponenton(event, handler) / off(event, handler)Adds or removes a listener
event: string, handler: FunctionSnackbarComponentdestroy()Removes it
voidelementThe snackbar
HTMLElementstatequeued between show() and its turn on screen, visible while it is on screen
'visible' | 'queued' | 'hidden'actionButton / closeButtonIts buttons
HTMLElement | undefinedclearSnackbars(), from material, closes the snackbar on screen and drops the waiting ones.
Events
openIt is on screen
{ snackbar, originalEvent }actionThe action was used
{ snackbar, originalEvent }focusThe shortcut moved focus to the action button
{ snackbar, originalEvent }close / dismissIt closed
{ snackbar, reason, originalEvent }reason is timeout, action, close-button, escape, api or queue (replaced or
cleared). The web component takes focus-shortcut (focus-shortcut="F6",
focus-shortcut="false") and dispatches open, action, focus and close; its close
carries { reason }, and its open property is true from show() on, while it waits too.
Styling
Its colors are the inverse roles, so it reads as a surface of the opposite theme.
.mtrl-snackbar, .mtrl-snackbar--visible { }
.mtrl-snackbar--center, .mtrl-snackbar--start, .mtrl-snackbar--end { }
.mtrl-snackbar--with-action, .mtrl-snackbar--dismissible { }
.mtrl-snackbar--action-below { } /* an action wider than 128dp, on its own line */
.mtrl-snackbar__text, .mtrl-snackbar__action, .mtrl-snackbar__close { }
Measurements
| Attribute | Value |
|---|---|
| Container | inverse-surface, 4dp corners, elevation 3 |
| Height | 48dp, 68dp for two lines |
| Text | Body Medium, inverse-on-surface, two lines at most |
| Action | inverse-primary; below the text when wider than 128dp |
| Padding | 16dp at the start, 8dp between the text and the action |
| Motion | Fades on the fast effects spring, and scales from 0.8 on the fast spatial one |