Notification Stack

An animated notification stack component with swipe-to-dismiss gestures and smooth stacking animations.

The Notification Stack component provides a visually appealing way to display stacked notifications with interactive swipe-to-dismiss gestures and smooth animations. Built with Framer Motion, it offers a modern UX for notification displays with physics-based interactions.

Installation

npx @ayushmxxn/shadcn add notification-stack

Copy and paste the component source into your project.

File Structure

The component follows a simple structure with the main component file:

notification-stack.tsx

Usage

Import the component and use it with an array of notification data:

import { NotificationStack } from "@/components/craft-blocks/cards/notification-stack/notification-stack";

export default function NotificationsDemo() {
  return <NotificationStack />;
}

Custom Notifications

Pass your own notification data to customize the stack:

const customNotifications = [
  {
    id: 1,
    title: "New Message",
    description: "You have received a new message from John",
    icon: "💬",
    color: "bg-blue-500",
  },
  {
    id: 2,
    title: "Task Completed",
    description: "Your project build has finished successfully",
    icon: "✅",
    color: "bg-green-500",
  },
  {
    id: 3,
    title: "Warning",
    description: "Storage space is running low",
    icon: "⚠️",
    color: "bg-yellow-500",
  },
];

<NotificationStack notifications={customNotifications} />;

Theming

The component uses Tailwind CSS classes and can be customized through:

  • Background Colors: Customize the card backgrounds and icon colors
  • Border Radius: Modify the rounded corners (default: rounded-lg)
  • Shadows: Adjust shadow intensity for depth
  • Typography: Change font sizes and weights for title and description

Example theme customization:

// Modify the notification card styling
<motion.div
  className="relative w-80 rounded-2xl bg-gradient-to-br from-purple-500 to-pink-500 
             p-4 shadow-2xl"
>
  {/* Notification content */}
</motion.div>

Animations

The component features several animation effects powered by Framer Motion:

Stack Animation

  • Each notification is offset vertically to create a stacked appearance
  • Cards scale down slightly as they move back in the stack
  • Smooth transitions between positions when cards are dismissed

Swipe Gesture

  • Drag: Users can drag notifications left or right
  • Velocity Threshold: Fast swipes (velocity > 500) trigger dismissal
  • Distance Threshold: Dragging beyond 100px also triggers dismissal
  • Spring Physics: Cards snap back with spring animation if not dismissed

Entry/Exit Animations

  • New notifications slide in from the top with fade effect
  • Dismissed notifications slide out in the drag direction
  • Stack reorders smoothly when cards are removed

How It Works

The notification stack manages state and animations through several key mechanisms:

  1. State Management: Uses React's useState to track the array of active notifications
  2. Motion Values: Framer Motion's useMotionValue tracks drag position in real-time
  3. Animation Variants: Predefined animation states for enter, center, and exit transitions
  4. Gesture Detection: onDragEnd handler evaluates swipe velocity and distance to determine dismissal
  5. Stack Positioning: Each card's position is calculated based on its index in the array

The dismiss logic evaluates both velocity and position:

const swipeConfidenceThreshold = 100;
const swipePower = (offset: number, velocity: number) => {
  return Math.abs(offset) * velocity;
};

// Dismiss if swiped far enough or fast enough
if (
  Math.abs(info.offset.x) > swipeConfidenceThreshold ||
  swipePower(info.offset.x, info.velocity.x) > 500
) {
  handleDismiss(notification.id);
}

Props

Prop

Type

Notification Object Structure

Prop

Type

Accessibility

The component implements several accessibility features:

  • Keyboard Navigation: Cards can be focused and dismissed with keyboard
  • ARIA Labels: Screen readers announce notification content
  • Motion Preferences: Respects prefers-reduced-motion for users with motion sensitivities
  • Focus Management: Maintains logical focus order as notifications are dismissed
  • Interactive Elements: All interactive areas have proper hit targets (minimum 44x44px)

To enhance accessibility further:

<motion.div
  role="alert"
  aria-live="polite"
  aria-atomic="true"
  className="notification-card"
>
  <button
    aria-label={`Dismiss ${notification.title}`}
    onClick={() => handleDismiss(notification.id)}
  >
    Dismiss
  </button>
</motion.div>

Registry Bundle

This component is available as a registry bundle and can be installed with a single command. The bundle includes all necessary dependencies and configurations.

Contact

If you have any questions or need assistance with this component, please don't hesitate to reach out via email.

Last updated on