Skip to main content
Stacked Notification Deck

Stacked Notification Deck

Apple-style kinetic notification stack that fans out on hover or click with swipe-to-dismiss spring physics.

Feedback
2/16
3 notifications

Production build deployed

Edge bundles verified across 35 static routes.

Just now

API rate limit at 85%

Workspace token throughput nearing burst quota.

2m ago

Database snapshot verified

Automated backup checksum completed cleanly.

14m ago

1. Install Dependencies

$npm install framer-motion lucide-react

2. Copy Component Code

components/ui/StackedNotificationDeck.tsx
1"use client";
2
3import React, { useEffect, useId, useRef, useState } from "react";
4import {
5 AnimatePresence,
6 motion,
7 useReducedMotion,
8 type PanInfo,
9} from "framer-motion";
10import { AlertTriangle, Check, ChevronDown, Terminal, X } from "lucide-react";
11
12const APPLE_EASE = [0.22, 1, 0.36, 1] as const;
13const STACK_SPRING = {
14 type: "spring" as const,
15 stiffness: 480,
16 damping: 36,
17 mass: 0.85,
18};
19
20export type NotificationVariant = "success" | "warning" | "system";
21
22export interface StackedNotificationItem {
23 id: string;
24 title: string;
25 description: string;
26 timestamp: string;
27 variant: NotificationVariant;
28}
29
30export interface StackedNotificationDeckProps {
31 /** Controlled notification list. */
32 items?: StackedNotificationItem[];
33 /** Initial notification items when running uncontrolled. */
34 defaultItems?: StackedNotificationItem[];
35 /** Controlled expanded state. */
36 expanded?: boolean;
37 /** Initial expanded state when running uncontrolled. */
38 defaultExpanded?: boolean;
39 /** Callback fired when expanded state changes. */
40 onExpandedChange?: (expanded: boolean) => void;
41 /** Callback fired when a notification card is dismissed. */
42 onDismiss?: (id: string) => void;
43 /** Hide the top expand/collapse count toggle pill. */
44 hideHeaderToggle?: boolean;
45 /** Extra classes on outer deck wrapper. */
46 className?: string;
47}
48
49export const DEFAULT_NOTIFICATIONS: StackedNotificationItem[] = [
50 {
51 id: "notif-deploy",
52 title: "Production build deployed",
53 description: "Edge bundles verified across 35 static routes.",
54 timestamp: "Just now",
55 variant: "success",
56 },
57 {
58 id: "notif-rate",
59 title: "API rate limit at 85%",
60 description: "Workspace token throughput nearing burst quota.",
61 timestamp: "2m ago",
62 variant: "warning",
63 },
64 {
65 id: "notif-snapshot",
66 title: "Database snapshot verified",
67 description: "Automated backup checksum completed cleanly.",
68 timestamp: "14m ago",
69 variant: "system",
70 },
71];
72
73const FALLBACK_CARD_HEIGHT = 76;
74const COLLAPSED_PEEK_OFFSET = 12;
75const EXPANDED_GAP = 8;
76
77function VariantIcon({ variant }: { variant: NotificationVariant }) {
78 if (variant === "success") {
79 return (
80 <span className="flex h-8 w-8 shrink-0 items-center justify-center rounded-[8px] bg-[#10B981]/10 text-[#10B981]">
81 <Check className="h-3.5 w-3.5" strokeWidth={1.75} aria-hidden="true" />
82 </span>
83 );
84 }
85
86 if (variant === "warning") {
87 return (
88 <span className="flex h-8 w-8 shrink-0 items-center justify-center rounded-[8px] bg-[#F3F3F5] text-black dark:bg-[#1A1A1E] dark:text-white">
89 <AlertTriangle
90 className="h-3.5 w-3.5"
91 strokeWidth={1.75}
92 aria-hidden="true"
93 />
94 </span>
95 );
96 }
97
98 return (
99 <span className="flex h-8 w-8 shrink-0 items-center justify-center rounded-[8px] bg-[#F3F3F5] text-black/75 dark:bg-[#1A1A1E] dark:text-white/75">
100 <Terminal
101 className="h-3.5 w-3.5"
102 strokeWidth={1.75}
103 aria-hidden="true"
104 />
105 </span>
106 );
107}
108
109/** Apple-style kinetic notification stack that fans out on hover/click with swipe-to-dismiss. */
110export function StackedNotificationDeck({
111 items,
112 defaultItems = DEFAULT_NOTIFICATIONS,
113 expanded,
114 defaultExpanded = false,
115 onExpandedChange,
116 onDismiss,
117 hideHeaderToggle = false,
118 className = "",
119}: StackedNotificationDeckProps) {
120 const prefersReducedMotion = useReducedMotion();
121 const deckRegionId = useId();
122
123 const [internalItems, setInternalItems] =
124 useState<StackedNotificationItem[]>(defaultItems);
125 const [internalExpanded, setInternalExpanded] = useState(defaultExpanded);
126 const [hovered, setHovered] = useState(false);
127 const [measuredHeights, setMeasuredHeights] = useState<
128 Record<string, number>
129 >({});
130
131 const cardNodesRef = useRef<Record<string, HTMLDivElement | null>>({});
132
133 const activeItems = items !== undefined ? items : internalItems;
134 const effectiveHovered = hovered && activeItems.length > 0;
135 const isExpanded =
136 expanded !== undefined ? expanded : internalExpanded || effectiveHovered;
137
138 useEffect(() => {
139 const observer = new ResizeObserver((entries) => {
140 setMeasuredHeights((prev) => {
141 let next: Record<string, number> | null = null;
142 for (const entry of entries) {
143 const id = (entry.target as HTMLElement).dataset.notifId;
144 if (!id) continue;
145 const height = Math.round(entry.target.getBoundingClientRect().height);
146 if (height > 0 && prev[id] !== height) {
147 if (!next) next = { ...prev };
148 next[id] = height;
149 }
150 }
151 return next ?? prev;
152 });
153 });
154
155 for (const item of activeItems) {
156 const el = cardNodesRef.current[item.id];
157 if (el) observer.observe(el);
158 }
159
160 return () => observer.disconnect();
161 }, [activeItems]);
162
163 function setExpandedState(next: boolean) {
164 if (expanded === undefined) {
165 setInternalExpanded(next);
166 if (!next) setHovered(false);
167 }
168 onExpandedChange?.(next);
169 }
170
171 function handleDismiss(id: string) {
172 if (items === undefined) {
173 setInternalItems((prev) => prev.filter((item) => item.id !== id));
174 }
175 if (activeItems.length <= 1) {
176 setHovered(false);
177 }
178 onDismiss?.(id);
179 }
180
181 function handleDragEnd(
182 id: string,
183 _event: MouseEvent | TouchEvent | PointerEvent,
184 info: PanInfo
185 ) {
186 if (Math.abs(info.offset.x) > 80 || Math.abs(info.velocity.x) > 500) {
187 handleDismiss(id);
188 }
189 }
190
191 const getHeight = (id: string) => measuredHeights[id] ?? FALLBACK_CARD_HEIGHT;
192
193 const frontHeight = activeItems[0] ? getHeight(activeItems[0].id) : 64;
194 const visibleCollapsedCount = Math.min(3, activeItems.length);
195 const collapsedTotalHeight =
196 activeItems.length > 0
197 ? frontHeight + Math.max(0, visibleCollapsedCount - 1) * COLLAPSED_PEEK_OFFSET
198 : 56;
199
200 const expandedOffsets: number[] = [];
201 let runningY = 0;
202 for (let i = 0; i < activeItems.length; i++) {
203 expandedOffsets.push(runningY);
204 runningY += getHeight(activeItems[i].id) + EXPANDED_GAP;
205 }
206 const expandedTotalHeight =
207 activeItems.length > 0 ? runningY - EXPANDED_GAP : 56;
208
209 const containerHeight = isExpanded
210 ? expandedTotalHeight
211 : collapsedTotalHeight;
212
213 return (
214 <div
215 onMouseEnter={() => {
216 if (expanded === undefined && activeItems.length > 0) {
217 setHovered(true);
218 }
219 }}
220 onMouseLeave={() => {
221 if (expanded === undefined) {
222 setHovered(false);
223 }
224 }}
225 className={`w-full max-w-[380px] min-w-0 select-none ${className}`}
226 >
227 {!hideHeaderToggle && activeItems.length > 0 && (
228 <div className="mb-3 flex items-center justify-between px-1">
229 <span className="font-mono text-xs leading-4 tabular-nums text-black/45 dark:text-white/45">
230 {activeItems.length}{" "}
231 {activeItems.length === 1 ? "notification" : "notifications"}
232 </span>
233
234 <button
235 type="button"
236 onClick={() => setExpandedState(!isExpanded)}
237 aria-expanded={isExpanded}
238 aria-controls={deckRegionId}
239 className="inline-flex min-h-7 items-center gap-1 rounded-[8px] bg-white px-2 py-1 text-xs leading-4 font-medium tracking-[-0.01em] text-[#0A0A0A] shadow-[0_2px_8px_rgba(0,0,0,0.04)] transition-colors duration-150 hover:bg-[#F7F7F9] active:scale-[0.97] dark:bg-[#111113] dark:text-white dark:shadow-[inset_0_1px_0_rgba(255,255,255,0.08)] dark:hover:bg-[#161618]"
240 >
241 <span>{isExpanded ? "Collapse" : "Expand"}</span>
242 <ChevronDown
243 className={`h-3.5 w-3.5 shrink-0 text-black/50 transition-transform duration-200 dark:text-white/50 ${
244 isExpanded ? "rotate-180" : "rotate-0"
245 }`}
246 strokeWidth={1.75}
247 aria-hidden="true"
248 />
249 </button>
250 </div>
251 )}
252
253 <motion.div
254 id={deckRegionId}
255 animate={{ height: containerHeight }}
256 transition={prefersReducedMotion ? { duration: 0 } : STACK_SPRING}
257 className="relative w-full"
258 >
259 <AnimatePresence initial={false}>
260 {activeItems.length === 0 ? (
261 <motion.div
262 key="empty"
263 initial={prefersReducedMotion ? false : { opacity: 0, y: 8 }}
264 animate={{ opacity: 1, y: 0 }}
265 exit={prefersReducedMotion ? undefined : { opacity: 0, y: -8 }}
266 transition={
267 prefersReducedMotion
268 ? { duration: 0 }
269 : { duration: 0.18, ease: APPLE_EASE }
270 }
271 className="flex h-14 w-full items-center justify-center rounded-[20px] border-0 bg-white px-4 text-xs leading-4 font-medium tracking-[-0.01em] text-black/55 shadow-[0_4px_20px_rgba(0,0,0,0.05)] dark:border-0 dark:bg-[#111113] dark:text-white/50 dark:shadow-[inset_0_1px_0_rgba(255,255,255,0.09),0_12px_32px_rgba(0,0,0,0.55)]"
272 >
273 All caught up
274 </motion.div>
275 ) : (
276 activeItems.map((item, index) => {
277 const targetY = isExpanded
278 ? expandedOffsets[index]
279 : Math.min(index, 2) * COLLAPSED_PEEK_OFFSET;
280 const targetScale = isExpanded
281 ? 1
282 : index === 0
283 ? 1
284 : index === 1
285 ? 0.95
286 : 0.9;
287 const targetOpacity = isExpanded
288 ? 1
289 : index === 0
290 ? 1
291 : index === 1
292 ? 0.85
293 : index === 2
294 ? 0.65
295 : 0;
296 const isVisible = isExpanded || index < 3;
297
298 return (
299 <motion.div
300 key={item.id}
301 data-notif-id={item.id}
302 aria-hidden={!isVisible}
303 inert={!isVisible ? true : undefined}
304 ref={(el) => {
305 cardNodesRef.current[item.id] = el;
306 }}
307 style={{
308 zIndex: activeItems.length - index,
309 transformOrigin: "top center",
310 }}
311 initial={
312 prefersReducedMotion
313 ? false
314 : { opacity: 0, y: -16, scale: 0.96 }
315 }
316 animate={{
317 opacity: targetOpacity,
318 y: targetY,
319 scale: targetScale,
320 }}
321 exit={
322 prefersReducedMotion
323 ? undefined
324 : {
325 opacity: 0,
326 x: 48,
327 scale: 0.96,
328 transition: { duration: 0.18, ease: APPLE_EASE },
329 }
330 }
331 transition={
332 prefersReducedMotion ? { duration: 0 } : STACK_SPRING
333 }
334 drag={isExpanded && !prefersReducedMotion ? "x" : false}
335 dragConstraints={{ left: 0, right: 0 }}
336 dragElastic={0.7}
337 onDragEnd={(event, info) =>
338 handleDragEnd(item.id, event, info)
339 }
340 onClick={() => {
341 if (!isExpanded) setExpandedState(true);
342 }}
343 className={`absolute inset-x-0 top-0 w-full rounded-[20px] border-0 bg-white p-3 dark:border-0 dark:bg-[#111113] dark:shadow-[inset_0_1px_0_rgba(255,255,255,0.09),0_12px_32px_rgba(0,0,0,0.65)] ${
344 index === 0 || isExpanded
345 ? "shadow-[0_4px_20px_rgba(0,0,0,0.06)]"
346 : "shadow-[0_6px_24px_rgba(0,0,0,0.07)]"
347 } ${
348 isExpanded
349 ? "cursor-grab active:cursor-grabbing"
350 : isVisible
351 ? "cursor-pointer"
352 : "pointer-events-none"
353 }`}
354 >
355 <div className="flex items-start gap-3">
356 <VariantIcon variant={item.variant} />
357
358 <div className="min-w-0 flex-1 space-y-1">
359 <p className="truncate text-xs leading-4 font-medium tracking-[-0.01em] text-[#0A0A0A] dark:text-white">
360 {item.title}
361 </p>
362 <p className="text-xs leading-[18px] font-normal text-black/55 dark:text-white/55">
363 {item.description}
364 </p>
365 </div>
366
367 <div className="flex shrink-0 flex-col items-end gap-1">
368 <button
369 type="button"
370 tabIndex={!isVisible ? -1 : undefined}
371 onClick={(e) => {
372 e.stopPropagation();
373 handleDismiss(item.id);
374 }}
375 aria-label={`Dismiss ${item.title}`}
376 className="flex h-6 w-6 shrink-0 items-center justify-center rounded-[8px] text-black/55 transition-colors duration-150 hover:bg-[#F3F3F5] hover:text-black active:scale-[0.97] dark:text-white/50 dark:hover:bg-[#1A1A1E] dark:hover:text-white"
377 >
378 <X
379 className="h-3.5 w-3.5"
380 strokeWidth={1.75}
381 aria-hidden="true"
382 />
383 </button>
384 <span className="font-mono text-xs leading-4 tabular-nums whitespace-nowrap text-black/55 dark:text-white/50">
385 {item.timestamp}
386 </span>
387 </div>
388 </div>
389 </motion.div>
390 );
391 })
392 )}
393 </AnimatePresence>
394 </motion.div>
395 </div>
396 );
397}

Usage Example

import { StackedNotificationDeck } from "@/components/ui/StackedNotificationDeck";
export default function NotificationTray() {
return <StackedNotificationDeck />;
}

3. Props Reference

Stacked Notification Deck props API reference
PropTypeDefaultDescription
itemsStackedNotificationItem[]undefinedControlled array of notifications rendered in the stack.
defaultItemsStackedNotificationItem[]DEFAULT_NOTIFICATIONSInitial notifications displayed when running uncontrolled.
expandedbooleanundefinedControlled fan-out expansion state.
onExpandedChange(expanded: boolean) => voidundefinedCallback fired when clicking Expand or Collapse.
onDismiss(id: string) => voidundefinedFired when a notification is swiped horizontally or dismissed via click.