Skip to content

Material 3

Components

Snackbar

Snackbars show short updates about app processes at the bottom of the screen.

SSandra AdamsPhotos from the summer trip
TTrevor HansenBirthday gift ideas
BBritta HoltRecipe to try on Friday
JJonas MeyerYour order has shipped
MMina ParkNotes from the review
Email archivedUndo

Snackbars show short updates about app processes at the bottom of the screen. They inform people of a process that the app has performed or will perform, such as archiving an email or saving a draft.

  • A snackbar does not interrupt: people can keep using the page without responding to it.
  • One snackbar shows at a time.
  • A snackbar can have a single action, such as Undo. Do not make a snackbar the only way to reach something the app needs to be usable.

To interrupt people with a message that needs a response, use a dialog instead.

import {
  Snackbar,
  SnackbarAction,
  SnackbarCloseButton,
  SnackbarContent,
  SnackbarQueue,
  SnackbarRegion,
} from '@material-web/react'
import { Text } from 'react-aria-components'

Basic snackbar

A snackbar appears for a short time towards the bottom of the window, in front of the page. This one has a text, an action and a close button.

import {
  Button,
  Icon,
  Snackbar,
  SnackbarAction,
  SnackbarCloseButton,
  SnackbarContent,
  SnackbarQueue,
  SnackbarRegion,
} from '@material-web/react'
import { Text } from 'react-aria-components'

const snackbars = new SnackbarQueue<string>()

export default function Basic() {
  return (
    <>
      <Button variant="tonal" onPress={() => snackbars.add('Email archived')}>
        Archive email
      </Button>
      <SnackbarRegion queue={snackbars}>
        {({ toast }) => (
          <Snackbar toast={toast}>
            <SnackbarContent>
              <Text slot="title">{toast.content}</Text>
            </SnackbarContent>
            <SnackbarAction onPress={() => snackbars.close(toast.key)}>Undo</SnackbarAction>
            <SnackbarCloseButton>
              <Icon name="close" />
            </SnackbarCloseButton>
          </Snackbar>
        )}
      </SnackbarRegion>
    </>
  )
}

Create a SnackbarQueue, render a SnackbarRegion for it once, and call the queue's add from anywhere in the app to show a snackbar. The region's children are a function that renders each snackbar: a Snackbar with its text in a SnackbarContent, then its action, a SnackbarAction, and its close button, a SnackbarCloseButton. Use these two parts for a snackbar's buttons, not a Button or an IconButton.

The queue, the region and the snackbar work as the parts of React Aria's Toast do.

Note: The snackbar's API may change in a later version.

Composition

A snackbar is composed of these parts:

<SnackbarRegion queue={snackbars}>
  {({ toast }) => (
    <Snackbar toast={toast}>
      <SnackbarContent>
        <Text slot="title" />
        <Text slot="description" />
      </SnackbarContent>
      <SnackbarAction />
      <SnackbarCloseButton />
    </Snackbar>
  )}
</SnackbarRegion>

These are the parts, where each goes, and what it takes:

PartInUse
SnackbarQueueYour app, created onceThe queue of snackbars to show. It takes two options: maxVisibleToasts, the number of snackbars shown at a time, which is 1 (the default), and wrapUpdate, a function that runs each change to the queue.
SnackbarRegionYour app, rendered onceShows the queue's snackbars. It takes the queue and, as its children, a function that renders each toast. Its className and style can be functions of visibleToasts, isHovered, isFocused and isFocusVisible.
SnackbarThe region's functionOne snackbar. It takes the toast to show. Its className, style and children can be functions of toast, isFocused and isFocusVisible.
SnackbarContentSnackbarHolds the snackbar's text
SnackbarActionSnackbarThe action, after the SnackbarContent. It takes onPress and the other press props of a button. Its children can be a function of isHovered, isPressed, isFocused, isFocusVisible and isDisabled.
SnackbarCloseButtonSnackbarThe close button, last. It takes the same props as a SnackbarAction.

Queue and region

Create one queue for the app, render one region, and add to the queue from anywhere:

const snackbars = new SnackbarQueue<{ text: string; action?: string }>()

function Snackbars() {
  return (
    <SnackbarRegion queue={snackbars}>
      {({ toast }) => (
        <Snackbar toast={toast}>
          <SnackbarContent>
            <Text slot="title">{toast.content.text}</Text>
          </SnackbarContent>
          {toast.content.action && (
            <SnackbarAction
              onPress={() => {
                undo()
                snackbars.close(toast.key)
              }}
            >
              {toast.content.action}
            </SnackbarAction>
          )}
          <SnackbarCloseButton>
            <Icon name="close" />
          </SnackbarCloseButton>
        </Snackbar>
      )}
    </SnackbarRegion>
  )
}

snackbars.add({ text: 'Email archived', action: 'Undo' })
snackbars.add({ text: 'All changes saved' }, { timeout: 5000 })

The content of a snackbar is yours to shape: a string, or an object as above. Type it on the queue, and read it as toast.content in the region's function.

Note: Each example on this page has its own queue and region. All but the one with a dialog show their snackbars in the example's frame, which stands in for the window.

Content

Put the snackbar's text in a SnackbarContent. After it, write the action as a SnackbarAction with its label as its children, and the close button as a SnackbarCloseButton with an icon as its child. Both buttons are optional.

An action that is wider than 128dp together with the close button moves below the text, at the end, and the close button moves with it. The text then takes the whole row. A label wider than that row wraps.

In this example, the region has className="max-w-86", which limits its width to 344dp, so that the longer text takes two lines.

import {
  Button,
  Icon,
  Snackbar,
  SnackbarAction,
  SnackbarCloseButton,
  SnackbarContent,
  SnackbarQueue,
  SnackbarRegion,
} from '@material-web/react'
import { Text } from 'react-aria-components'

type Message = { text: string; action?: string }

const snackbars = new SnackbarQueue<Message>()

const messages: [label: string, message: Message][] = [
  ['Single line', { text: 'Photo archived' }],
  ['With action', { text: 'Photo added to “Natural Light” album', action: 'View' }],
  [
    'Two lines',
    { text: 'The item already has the label “travel.” Add a new label, or choose another one.' },
  ],
  [
    'Longer action',
    { text: 'Can’t add photo to your favorites', action: 'Learn more about storage' },
  ],
]

export default function Configurations() {
  return (
    <div className="box-border flex h-64 w-full flex-wrap content-start justify-center gap-space-100 p-space-300">
      {messages.map(([label, message]) => (
        <Button key={label} variant="outlined" onPress={() => snackbars.add(message)}>
          {label}
        </Button>
      ))}
      <SnackbarRegion queue={snackbars} className="max-w-86">
        {({ toast }) => (
          <Snackbar toast={toast}>
            <SnackbarContent>
              <Text slot="title">{toast.content.text}</Text>
            </SnackbarContent>
            {toast.content.action && (
              <SnackbarAction onPress={() => snackbars.close(toast.key)}>
                {toast.content.action}
              </SnackbarAction>
            )}
            <SnackbarCloseButton>
              <Icon name="close" />
            </SnackbarCloseButton>
          </Snackbar>
        )}
      </SnackbarRegion>
    </div>
  )
}

Slots

  1. title
  2. description
SlotChildUse
titleA TextThe text, which names the snackbar
descriptionA TextDescribes the snackbar

Each takes a separate line.

import {
  Button,
  Icon,
  Snackbar,
  SnackbarCloseButton,
  SnackbarContent,
  SnackbarQueue,
  SnackbarRegion,
} from '@material-web/react'
import { Text } from 'react-aria-components'

const snackbars = new SnackbarQueue<{ title: string; description: string }>()

export default function Description() {
  return (
    <>
      <Button
        variant="tonal"
        onPress={() =>
          snackbars.add({ title: 'Upload paused', description: 'It resumes once you are online.' })
        }
      >
        Pause upload
      </Button>
      <SnackbarRegion queue={snackbars}>
        {({ toast }) => (
          <Snackbar toast={toast}>
            <SnackbarContent>
              <Text slot="title">{toast.content.title}</Text>
              <Text slot="description">{toast.content.description}</Text>
            </SnackbarContent>
            <SnackbarCloseButton>
              <Icon name="close" />
            </SnackbarCloseButton>
          </Snackbar>
        )}
      </SnackbarRegion>
    </>
  )
}

Showing and closing

The queue shows one snackbar at a time. Its maxVisibleToasts option sets how many.

  • Call add(content, options) to show a snackbar at once. It returns the snackbar's key.
  • Call close(key) to close a snackbar, and clear() to close them all.
  • An action does not close its snackbar. Call close(toast.key) in its onPress.
  • A close button closes its snackbar when pressed.

For the queue's other methods, see React Aria's ToastQueue.

Dismissal

Pass a timeout in milliseconds in the options of add to close a snackbar automatically, and an onClose to be called when it closes. Without a timeout, a snackbar stays until it is closed.

  • Give a snackbar with an action no timeout.
  • For a snackbar with no action, use a timeout of at least 5 seconds and at most 10, and show inline feedback as well.
  • The timer pauses while the pointer is on the snackbar or the focus is in it.

In this example, the button's label changes to Saved as the inline feedback, and the snackbar closes after 5 seconds.

import {
  Button,
  Snackbar,
  SnackbarContent,
  SnackbarQueue,
  SnackbarRegion,
} from '@material-web/react'
import { useState } from 'react'
import { Text } from 'react-aria-components'

const snackbars = new SnackbarQueue<string>()

export default function Timeout() {
  const [isSaved, setSaved] = useState(false)
  return (
    <>
      <Button
        isDisabled={isSaved}
        onPress={() => {
          setSaved(true)
          snackbars.add('All changes saved', { timeout: 5000, onClose: () => setSaved(false) })
        }}
      >
        {isSaved ? 'Saved' : 'Save'}
      </Button>
      <SnackbarRegion queue={snackbars}>
        {({ toast }) => (
          <Snackbar toast={toast}>
            <SnackbarContent>
              <Text slot="title">{toast.content}</Text>
            </SnackbarContent>
          </Snackbar>
        )}
      </SnackbarRegion>
    </>
  )
}

Consecutive snackbars

A snackbar added while another shows takes its place, and the earlier one comes back once the newer one has closed. To replace a snackbar for good, close it before you add the next.

import {
  Button,
  Icon,
  Snackbar,
  SnackbarCloseButton,
  SnackbarContent,
  SnackbarQueue,
  SnackbarRegion,
} from '@material-web/react'
import { useState } from 'react'
import { Text } from 'react-aria-components'

const snackbars = new SnackbarQueue<string>()

export default function Consecutive() {
  const [count, setCount] = useState(0)
  return (
    <>
      <Button
        variant="outlined"
        onPress={() => {
          setCount(count + 1)
          snackbars.add(`Message ${count + 1}`)
        }}
      >
        Add a snackbar
      </Button>
      <SnackbarRegion queue={snackbars}>
        {({ toast }) => (
          <Snackbar toast={toast}>
            <SnackbarContent>
              <Text slot="title">{toast.content}</Text>
            </SnackbarContent>
            <SnackbarCloseButton>
              <Icon name="close" />
            </SnackbarCloseButton>
          </Snackbar>
        )}
      </SnackbarRegion>
    </>
  )
}

Placement

The region is fixed to the bottom of the window, centered, 12dp from its edges and up to 600dp wide. It is in front of the page, at z-snackbar. Set a className on the SnackbarRegion to move it:

  • bottom-22 places it above a FAB that is 16dp from the corner. See Snackbar with FAB.
  • mx-0 aligns it to the window's start.
  • static keeps it inside a container that you render it into with UNSAFE_PortalProvider.

In a wide window, a snackbar can be centered or aligned to the start, as long as it always shows in the same place. Avoid placing it in front of navigation or other frequently used targets.

In this example, the region has className="mx-0 max-w-86": it is aligned to the start, and up to 344dp wide.

import {
  Button,
  Icon,
  Snackbar,
  SnackbarCloseButton,
  SnackbarContent,
  SnackbarQueue,
  SnackbarRegion,
} from '@material-web/react'
import { Text } from 'react-aria-components'

const snackbars = new SnackbarQueue<string>()

export default function Aligned() {
  return (
    <>
      <Button variant="outlined" onPress={() => snackbars.add('Photo archived')}>
        Archive photo
      </Button>
      <SnackbarRegion queue={snackbars} className="mx-0 max-w-86">
        {({ toast }) => (
          <Snackbar toast={toast}>
            <SnackbarContent>
              <Text slot="title">{toast.content}</Text>
            </SnackbarContent>
            <SnackbarCloseButton>
              <Icon name="close" />
            </SnackbarCloseButton>
          </Snackbar>
        )}
      </SnackbarRegion>
    </>
  )
}

Snackbar with FAB

Show a snackbar above a FAB, not in front of it. Set className="bottom-22" on the SnackbarRegion to place it above a FAB that is 16dp from the corner.

import {
  Button,
  Fab,
  Icon,
  Snackbar,
  SnackbarAction,
  SnackbarContent,
  SnackbarQueue,
  SnackbarRegion,
} from '@material-web/react'
import { Text } from 'react-aria-components'

const snackbars = new SnackbarQueue<string>()

export default function Position() {
  return (
    <div className="relative flex h-64 w-full items-start justify-center">
      <Button
        variant="outlined"
        className="mt-space-300"
        onPress={() => snackbars.add('Draft saved')}
      >
        Save draft
      </Button>
      <Fab aria-label="Compose" className="absolute end-space-200 bottom-space-200">
        <Icon name="edit" />
      </Fab>
      <SnackbarRegion queue={snackbars} className="bottom-22">
        {({ toast }) => (
          <Snackbar toast={toast}>
            <SnackbarContent>
              <Text slot="title">{toast.content}</Text>
            </SnackbarContent>
            <SnackbarAction onPress={() => snackbars.close(toast.key)}>Undo</SnackbarAction>
          </Snackbar>
        )}
      </SnackbarRegion>
    </div>
  )
}

Snackbar with dialog

A snackbar shows in front of an open dialog, and can be used while the dialog is open. Add it from inside the dialog as from anywhere else.

  • A press on the snackbar does not close the dialog.
  • Escape closes the snackbar while the focus is in it, and the dialog otherwise.
  • F6 moves the focus from the dialog to the snackbar.
import {
  Button,
  Dialog,
  DialogTrigger,
  Icon,
  Snackbar,
  SnackbarCloseButton,
  SnackbarContent,
  SnackbarQueue,
  SnackbarRegion,
} from '@material-web/react'
import { Heading, Text } from 'react-aria-components'

const snackbars = new SnackbarQueue<string>()

export default function InDialog() {
  return (
    <>
      <DialogTrigger>
        <Button variant="tonal">Share album</Button>
        <Dialog>
          <Heading
            slot="title"
            className="m-0 px-space-300 pb-space-200 text-headline-sm text-on-surface"
          >
            Share album
          </Heading>
          <Text slot="description" elementType="p" className="m-0 px-space-300 pb-space-100">
            Anyone with the link can view this album.
          </Text>
          <div className="flex shrink-0 justify-end gap-space-100 px-space-300 pt-space-200">
            <Button variant="text" onPress={() => snackbars.add('Link copied', { timeout: 5000 })}>
              Copy link
            </Button>
            <Button variant="text" slot="close">
              Done
            </Button>
          </div>
        </Dialog>
      </DialogTrigger>
      <SnackbarRegion queue={snackbars}>
        {({ toast }) => (
          <Snackbar toast={toast}>
            <SnackbarContent>
              <Text slot="title">{toast.content}</Text>
            </SnackbarContent>
            <SnackbarCloseButton>
              <Icon name="close" />
            </SnackbarCloseButton>
          </Snackbar>
        )}
      </SnackbarRegion>
    </>
  )
}

Customization

Pass classes in className to restyle a part of a snackbar. A class of yours replaces the part's own class for the same property. See Styling for the classes, the states and the theme.

In this example, the snackbar of a failed upload takes the error color roles. The close button has a color of its own, so it takes the on-color too.

import {
  Button,
  Icon,
  Snackbar,
  SnackbarCloseButton,
  SnackbarContent,
  SnackbarQueue,
  SnackbarRegion,
} from '@material-web/react'
import { Text } from 'react-aria-components'

const snackbars = new SnackbarQueue<{ text: string; isError?: boolean }>()

export default function Custom() {
  return (
    <>
      <Button variant="outlined" onPress={() => snackbars.add({ text: 'Photo uploaded' })}>
        Upload
      </Button>
      <Button
        variant="outlined"
        onPress={() => snackbars.add({ text: 'Upload failed', isError: true })}
      >
        Fail
      </Button>
      <SnackbarRegion queue={snackbars}>
        {({ toast }) => (
          <Snackbar
            toast={toast}
            className={toast.content.isError ? 'bg-error text-on-error' : undefined}
          >
            <SnackbarContent>
              <Text slot="title">{toast.content.text}</Text>
            </SnackbarContent>
            <SnackbarCloseButton className={toast.content.isError ? 'text-on-error' : undefined}>
              <Icon name="close" />
            </SnackbarCloseButton>
          </Snackbar>
        )}
      </SnackbarRegion>
    </>
  )
}
  • Color: Pass a container color and the on-color of that container to the Snackbar, as in bg-error text-on-error. Pass a text color to a SnackbarAction and a SnackbarCloseButton as well: their hover, focus and press states follow it.
  • Functions: On every part but the SnackbarContent, className and style can be functions of the part's state. See Composition for the state of each part, and Children as a function.
  • Ancestors: The region is rendered at the end of the page, not inside the element it is written in. A class or a data-theme on an element around it does not reach the snackbars. Set it on the SnackbarRegion itself, or on the root element.
  • Every snackbar: To restyle all the snackbars at once, change the theme.

Transitions

The queue runs each change in a view transition. Pass a wrapUpdate function in the queue's options to run the changes yourself. A view transition that you start there animates the snackbars only if it has the type snackbar.