tapcn
Components

Snackbar

A bottom ephemeral message bar for brief feedback.

Show Snackbar

Snackbar

A brief message that appears at the bottom of the screen and automatically dismisses after a set duration. Supports an optional action button. Uses a context provider pattern so snackbars can be triggered from anywhere in the tree.

Installation

npx @tapcn/cli add snackbar

Or using shadcn directly:

npx shadcn@latest add https://tapcn.vercel.app/r/snackbar.json

Usage

Wrap your app (or a subtree) with SnackbarProvider, then call snackbar() from the useSnackbar hook.

import { SnackbarProvider, useSnackbar } from '~/components/ui/snackbar';
import { Button } from '~/components/ui/button';
import { Text } from '~/components/ui/text';

function App() {
  return (
    <SnackbarProvider>
      <SnackbarDemo />
    </SnackbarProvider>
  );
}

function SnackbarDemo() {
  const { snackbar } = useSnackbar();

  return (
    <Button
      onPress={() =>
        snackbar({
          message: 'Item deleted.',
          action: { label: 'Undo', onPress: () => console.log('undo') },
          duration: 4000,
        })
      }
    >
      <Text>Show Snackbar</Text>
    </Button>
  );
}

Simple message

snackbar({ message: 'Changes saved.' });

With action and custom duration

snackbar({
  message: 'Message archived.',
  action: { label: 'Undo', onPress: () => restoreMessage() },
  duration: 5000,
});

API

SnackbarProvider

Wrap your component tree with this provider. It renders a SnackbarViewport that displays active snackbars at the bottom of the screen.

PropTypeDescription
childrenReact.ReactNodeThe app content.

useSnackbar()

Returns the snackbar context with the following properties:

PropertyTypeDescription
snackbar(data: Omit<SnackbarData, 'id'>) => voidShow a new snackbar.
dismiss(id: string) => voidManually dismiss a snackbar by its ID.
snackbarsSnackbarData[]The currently visible snackbars.

SnackbarData

PropertyTypeDefaultDescription
messagestringrequiredThe text message to display.
action{ label: string; onPress: () => void }-An optional action button shown on the right side.
durationnumber3000Time in milliseconds before the snackbar is automatically dismissed.

Dependencies

  • react-native-reanimated -- for slide-in/slide-out animations

On this page