Skip to content

Snackbar

A brief message with an optional action and dismiss, queued through a host.

import { Snackbar, SnackbarHost, useSnackbarHostState } from '@windmorph/react';

Props

By name or description, across the 18 props below

Snackbar 11 props

  • childrentype ReactNodeRequired

    The message.

  • actionLabeltype string

    Label of the action button.

  • actionOnNewLinetype booleandefault false

    Put the actions under the message, for long action labels.

  • reftype Ref<HTMLDivElement>

    Ref to the root element.

Events

  • onActiontype (() => void)

    Called when the action button is pressed.

  • onClosetype (() => void)

    Shows a close (×) button that calls this. Escape calls it too.

  • onEscapetype (() => void)default onClose

    Called on Escape while focus is inside, which dismisses a snackbar with or without the × button.

Text

  • labelstype Partial<SnackbarLabels>

    Words the snackbar announces, with English defaults. A LabelsProvider sets them for the whole app.

Also accepts the root element's HTML attributes (id, data-*, event handlers).

SnackbarHost 7 props

  • statetype SnackbarHostStateRequired

    The queue of snackbars to show, from useSnackbarHostState().

  • snackbartype ((data: SnackbarData) => ReactNode)

    Renders each snackbar; defaults to Snackbar.

  • focusShortcuttype string | falsedefault "F6"

    The key combination that moves focus to a snackbar with actions, as the spec asks for on the web. F6 by default, the key React Aria's toasts take (browsers use it to move between the window's panes): while a snackbar with a control shows, F6 goes to it from anywhere on the page, and pressed again there it stays. Document it where your app lists its shortcuts. false turns it off.

  • reftype Ref<HTMLDivElement>

    Ref to the root element.

Text

  • labelstype Partial<SnackbarLabels>

    Words each default snackbar announces, with English defaults. A LabelsProvider sets them for the whole app.

Also accepts the root element's HTML attributes (id, data-*, event handlers).

Tokens

M3's component tokens as CSS variables. Each is unset until you set it, on :root, a theme or any ancestor, and falls back to the default shown. The root carries data-variant (and data-size where it has sizes), which picks the variables that apply. See Component tokens in the Customisation guide.

Snackbar tokens (md-comp-snackbar)
VariableDefaultPaintsCompose
--md-comp-snackbar-container-colorvar(--md-sys-color-inverse-surface)The container.SnackbarTokens.ContainerColor
--md-comp-snackbar-container-shapevar(--md-sys-shape-corner-extra-small)The corners.SnackbarTokens.ContainerShape
--md-comp-snackbar-container-elevationvar(--md-sys-elevation-level-3)The shadow.SnackbarTokens.ContainerElevation
--md-comp-snackbar-supporting-text-colorvar(--md-sys-color-inverse-on-surface)The message.SnackbarTokens.SupportingTextColor
--md-comp-snackbar-action-label-text-colorvar(--md-sys-color-inverse-primary)The action, a text button.SnackbarTokens.ActionLabelTextColor
--md-comp-snackbar-icon-colorvar(--md-sys-color-inverse-on-surface)The dismiss icon button.SnackbarTokens.IconColor

Hooks

useSnackbarHostState

useSnackbarHostState signature
useSnackbarHostState(): SnackbarHostState

A SnackbarHostState that lives as long as the component.

Returns SnackbarHostState

  • currentSnackbarDatatype SnackbarData | nullRequired

    The snackbar being shown, if any.

  • showSnackbartype (visuals: string | SnackbarVisuals, { replace }?: ShowSnackbarOptions) => Promise<SnackbarResult>Required

    Shows a snackbar once any current one has gone (or at once with replace), and resolves with how it ended.

  • subscribetype (listener: () => void) => () => voidRequired
  • getSnapshottype () => SnackbarData | nullRequired

Spec status

Built to the M3 Expressive snackbar spec. Each item of its variants and configurations links to a live example; where the M3 Expressive column of the spec differs from the original M3 one, it wins.

  • Single line

    Built

    Configuration · Layouts

  • Single line with action

    Built

    Configuration · Layouts

  • Two lines

    Built

    Configuration · Two lines

  • Two lines with action

    Built

    Configuration · Two lines

  • Two lines with longer action

    Built

    Configuration · Two lines, Layouts

  • Close affordance (optional icon)

    Built

    Configuration · Layouts

  • Queued messages, one at a time

    Built

    Use case · Host

  • Replacing an outdated snackbar

    Built

    Use case · Host

  • Pause on hover and focus

    Extension

    The auto-dismiss timer pauses while the pointer or focus is on the snackbar, so nobody loses the message (WCAG 2.2.1).

  • focusShortcut

    Extension

    F6 (configurable, or false) moves focus to a snackbar with a control, the keyboard path the spec asks for on the web.

Bundle size

Gzipped, as published: the component and the library code it imports. React, React Aria, Motion and the other dependencies aren't counted, and your bundler's minifier makes it smaller still. CI fails a change that grows it past its budget.

  • Snackbar, SnackbarHost: 31.9 kB

Differences from Compose

  • SnackbarHostState ports Compose's queue: showSnackbar resolves 'action-performed' or 'dismissed', so you can react to Undo.
  • Durations match Compose: short 4s and long 10s. A snackbar with an action is always indefinite, as the spec requires; Compose lets you time one out. There is no platform accessibility multiplier.
  • showSnackbar(visuals, { replace: true }) replaces the snackbar on screen with updated information instead of queueing behind it.
  • The container is inverse-surface at elevation 3 with 4px corners, up to 600px wide; the action is a text Button in inverse-primary and the dismiss is a standard IconButton.
  • A leaving snackbar stays inert while the next one fades in, both sharing one grid cell, matching Compose's cross-fade. A snackbar expands upward from the bottom edge rather than scaling, since the spec avoids scale motion. The pause on hover or focus is an addition on top of Compose.