Skip to content

Snackbar

Brief feedback about an action, with an optional next step.

Unsaved

Installation

Terminal
pnpm dlx shadcn@latest add https://react-toolbox.io/r/snackbar.json

New here? Set up your project first. Styles, fonts, and required components are included.

Usage

Keep the label brief and use action for an optional follow-up such as Undo.

Composition

Show one message at a time in arrival order. useSnackbarQueue can track a promise from pending to success or failure and returns its original result or error. Queueing remains application behavior in editable source.

Terminal
pnpm dlx shadcn@latest add https://react-toolbox.io/r/example-snackbar-queue.json
Show composition code
tsx
'use client';

import { Button } from '@/components/ui/material/button';
import { useSnackbarQueue } from '@/components/ui/examples/snackbar-queue';

export default function SnackbarComposition() {
  const queue = useSnackbarQueue();
  return (
    <div className="flex flex-wrap gap-4">
      <Button
        onClick={() => {
          queue.enqueue({ label: 'First observation saved' });
          queue.enqueue({ label: 'Second observation saved' });
        }}
      >
        Queue messages
      </Button>
      <Button
        primary
        onClick={() => {
          const save = new Promise<void>((resolve) => setTimeout(resolve, 700));
          void queue
            .track(save, {
              pending: 'Saving observations…',
              success: () => 'Observations saved',
              error: () => 'Observations could not be saved',
            })
            .catch(() => undefined);
        }}
      >
        Track a save
      </Button>
      {queue.snackbar}
    </div>
  );
}

State and callbacks

Set active to false in onTimeout to dismiss the snackbar. The default timeout={0} disables that callback. delay controls the exit transition separately; onClick handles the action.

API reference

Key props and defaults. Full types and inline documentation are included with the source in material/snackbar.

Snackbar

Property Type Default
action string —
active boolean false
label ReactNode —
onClick MouseEventHandler<HTMLElement> —
onTimeout () => void —
timeout number 0

Material guidelines

  • Show brief, non-blocking feedback at the bottom of the screen, one snackbar at a time. Let it disappear after an appropriate interval.
  • Offer zero or one useful action, such as Undo. Do not add Dismiss or Cancel actions, icons, or a second action; Undo must actually reverse the change.
  • Keep essential actions available elsewhere after the message expires.

Material Design 1: Snackbars and toasts