Snackbar
A brief message with an optional action and dismiss, queued through a host.
import { Snackbar, SnackbarHost, useSnackbarHostState } from '@windmorph/react';Props
Snackbar 11 props
The message.
Label of the action button.
Put the actions under the message, for long action labels.
Ref to the root element.
Events
Called when the action button is pressed.
Shows a close (×) button that calls this. Escape calls it too.
Called on Escape while focus is inside, which dismisses a snackbar with or without the × button.
Text
Words the snackbar announces, with English defaults. A
LabelsProvidersets them for the whole app.
Classes for each part, by slot.
classNames.rootis merged withclassName.Inline styles for the root element.
Classes for the root element, merged with the component's own (yours win).
Also accepts the root element's HTML attributes (id, data-*, event handlers).
SnackbarHost 7 props
The queue of snackbars to show, from
useSnackbarHostState().Renders each snackbar; defaults to
Snackbar.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.
falseturns it off.Ref to the root element.
Text
Words each default snackbar announces, with English defaults. A
LabelsProvidersets 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.
| Variable | Default | Paints | Compose |
|---|---|---|---|
--md-comp-snackbar-container-color | var(--md-sys-color-inverse-surface) | The container. | SnackbarTokens.ContainerColor |
--md-comp-snackbar-container-shape | var(--md-sys-shape-corner-extra-small) | The corners. | SnackbarTokens.ContainerShape |
--md-comp-snackbar-container-elevation | var(--md-sys-elevation-level-3) | The shadow. | SnackbarTokens.ContainerElevation |
--md-comp-snackbar-supporting-text-color | var(--md-sys-color-inverse-on-surface) | The message. | SnackbarTokens.SupportingTextColor |
--md-comp-snackbar-action-label-text-color | var(--md-sys-color-inverse-primary) | The action, a text button. | SnackbarTokens.ActionLabelTextColor |
--md-comp-snackbar-icon-color | var(--md-sys-color-inverse-on-surface) | The dismiss icon button. | SnackbarTokens.IconColor |
Hooks
useSnackbarHostState
A SnackbarHostState that lives as long as the component.
Returns SnackbarHostState
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.
| Item | Status | Details |
|---|---|---|
| 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. |
Single line
BuiltConfiguration · Layouts
Single line with action
BuiltConfiguration · Layouts
Two lines
BuiltConfiguration · Two lines
Two lines with action
BuiltConfiguration · Two lines
Close affordance (optional icon)
BuiltConfiguration · Layouts
Queued messages, one at a time
BuiltUse case · Host
Replacing an outdated snackbar
BuiltUse case · Host
Pause on hover and focus
ExtensionThe auto-dismiss timer pauses while the pointer or focus is on the snackbar, so nobody loses the message (WCAG 2.2.1).
ExtensionfocusShortcutF6 (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
SnackbarHostStateports Compose's queue:showSnackbarresolves'action-performed'or'dismissed', so you can react to Undo.- Durations match Compose:
short4s andlong10s. A snackbar with an action is alwaysindefinite, 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-surfaceat elevation 3 with 4px corners, up to 600px wide; the action is a textButtonininverse-primaryand the dismiss is a standardIconButton. - A leaving snackbar stays
inertwhile 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.