Search
MaterialComponentsDocs
Components overviewApp barBadgeButtonsButton groupButtonExtended FABFAB menuFABIcon buttonSplit buttonCardCarouselCheckboxChipsDate & time pickersDate pickerTime pickerDialogDividerListLoading & progressLoading indicatorProgressMenuNavigationNavigation barNavigation drawerNavigation railRadio buttonsSearchSelectSheetsBottom sheetSide sheetSliderSnackbarSwitchTabsText fieldToolbarTooltip

Snackbar

Confirm an action without interrupting. Show a message, offer an undo, and try dismissal behavior.

FactoryWeb ComponentsReactVueSvelteSolidJS
PublishedUpdated

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.

Vanilla
import { createSnackbar } from 'material';

const snackbar = createSnackbar({ message: 'Photo saved' });
document.body.append(snackbar.element);

function tell() {
  snackbar.show();
}
Web Components
<m-snackbar message="Photo saved"></m-snackbar>

<script type="module">
  const snackbar = document.querySelector('m-snackbar');

  function tell() {
    snackbar.show();
  }
</script>
React
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" />
  );
}
Vue
<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>
Svelte
<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" />
SolidJS
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.

Vanilla
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();
}
Web Components
<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>
React
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" />
  );
}
Vue
<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>
Svelte
<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" />
SolidJS
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.

Vanilla
import { createSnackbar } from 'material';

const snackbar = createSnackbar({ message: 'Update available', dismissible: true, duration: 'long' });
document.body.append(snackbar.element);

function tell() {
  snackbar.show();
}
Web Components
<m-snackbar message="Update available" dismissible duration="long"></m-snackbar>

<script type="module">
  const snackbar = document.querySelector('m-snackbar');

  function tell() {
    snackbar.show();
  }
</script>
React
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" />
  );
}
Vue
<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>
Svelte
<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" />
SolidJS
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.

Vanilla
import { createSnackbar } from 'material';

const snackbar = createSnackbar({ message: 'Upload complete', queueBehavior: 'replace' });
document.body.append(snackbar.element);

function tell() {
  snackbar.show();
}
Web Components
<m-snackbar message="Upload complete" queue-behavior="replace"></m-snackbar>

<script type="module">
  const snackbar = document.querySelector('m-snackbar');

  function tell() {
    snackbar.show();
  }
</script>
React
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" />
  );
}
Vue
<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>
Svelte
<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" />
SolidJS
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

message

The text, up to two lines

Typestring
Defaultrequired
action

The action's label

Typestring
Defaultundefined
dismissible

A close button

Typeboolean
Defaultfalse
closeLabel

The close button's accessible name

Typestring
Default'Dismiss'
focusShortcut

One 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>)

Typestring | false
Default'F8'
duration

How long it stays: 4s, 10s, until closed, or milliseconds (0 is indefinite)

Type'short' | 'long' | 'indefinite' | number
Default'indefinite' with an action, else 'short'
position

Where along the bottom edge

Type'center' | 'start' | 'end'
Default'center'
queueBehavior

Whether it waits its turn or replaces the queue

Type'queue' | 'replace'
Default'queue'
layer

Shows it in the top layer, inside the topmost modal dialog while one is open

Type'top'
Defaultundefined
onAction / onOpen / onClose / onFocus

Handlers for action, open, close and focus

Type(event: SnackbarEvent) => void
Defaultundefined
on

Event handlers registered at creation

Type{ open?, close?, action?, dismiss?, focus? }
Defaultundefined
class

Additional CSS classes

Typestring
Defaultundefined
prefix

Prefix for CSS class names

Typestring
Default'mtrl'

Methods

show() / hide()

Queues it to show, or closes it with reason api

Parametersnone
ReturnsSnackbarComponent
isOpen()

Whether it is on screen

Parametersnone
Returnsboolean
getMessage() / setMessage(message)

The text

Parametersmessage: string
Returnsstring / SnackbarComponent
getAction() / setAction(text)

The action's label

Parameterstext: string
Returnsstring / SnackbarComponent
getDuration() / setDuration(duration)

How long it stays, read in milliseconds; a change on screen starts the count again

Parametersduration: SnackbarDuration
Returnsnumber / SnackbarComponent
getPosition() / setPosition(position)

Where it sits

Parametersposition: 'center' | 'start' | 'end'
Returnsstring / SnackbarComponent
on(event, handler) / off(event, handler)

Adds or removes a listener

Parametersevent: string, handler: Function
ReturnsSnackbarComponent
destroy()

Removes it

Parametersnone
Returnsvoid
element

The snackbar

TypeHTMLElement
state

queued between show() and its turn on screen, visible while it is on screen

Type'visible' | 'queued' | 'hidden'
actionButton / closeButton

Its buttons

TypeHTMLElement | undefined

clearSnackbars(), from material, closes the snackbar on screen and drops the waiting ones.

Events

open

It is on screen

Payload{ snackbar, originalEvent }
action

The action was used

Payload{ snackbar, originalEvent }
focus

The shortcut moved focus to the action button

Payload{ snackbar, originalEvent }
close / dismiss

It closed

Payload{ 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