@papernote/ui 1.8.3 → 1.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,560 @@
1
+ import React, { useState, useCallback, useMemo } from 'react';
2
+ import { Bell, Check, ExternalLink } from 'lucide-react';
3
+ import Popover from './Popover';
4
+ import Button from './Button';
5
+ import Stack from './Stack';
6
+ import Text from './Text';
7
+ import Badge from './Badge';
8
+ import { Skeleton } from './Loading';
9
+
10
+ /**
11
+ * Represents a single notification item
12
+ */
13
+ export interface NotificationItem {
14
+ /** Unique identifier for the notification */
15
+ id: string;
16
+ /** Title of the notification */
17
+ title: string;
18
+ /** Message body of the notification */
19
+ message: string;
20
+ /** Type determines the color coding */
21
+ type: 'info' | 'success' | 'warning' | 'error';
22
+ /** Priority affects visual treatment */
23
+ priority: 'low' | 'normal' | 'high' | 'urgent';
24
+ /** When the notification was created */
25
+ createdAt: string | Date;
26
+ /** Whether the notification has been read */
27
+ isRead: boolean;
28
+ /** Optional URL to navigate to when clicked */
29
+ actionUrl?: string;
30
+ /** Optional custom label for the type badge (used in detailed variant) */
31
+ typeLabel?: string;
32
+ }
33
+
34
+ /** Dropdown position options */
35
+ export type NotificationBellPosition =
36
+ | 'bottom-left'
37
+ | 'bottom-right'
38
+ | 'top-left'
39
+ | 'top-right'
40
+ | 'left' // Alias for bottom-left (legacy)
41
+ | 'right'; // Alias for bottom-right (legacy)
42
+
43
+ /** Bell button style options */
44
+ export type NotificationBellStyle = 'ghost' | 'outlined';
45
+
46
+ /**
47
+ * NotificationBell component props
48
+ */
49
+ export interface NotificationBellProps {
50
+ /** List of notifications to display */
51
+ notifications: NotificationItem[];
52
+ /** Number of unread notifications (shown in badge). If not provided, calculated from notifications */
53
+ unreadCount?: number;
54
+ /** Callback when marking a single notification as read */
55
+ onMarkAsRead?: (id: string) => void;
56
+ /** Callback when marking all notifications as read */
57
+ onMarkAllRead?: () => void;
58
+ /** Callback when clicking a notification */
59
+ onNotificationClick?: (notification: NotificationItem) => void;
60
+ /** Callback when clicking "View All" */
61
+ onViewAll?: () => void;
62
+ /** Show loading state */
63
+ loading?: boolean;
64
+ /** Position of the dropdown relative to the bell */
65
+ dropdownPosition?: NotificationBellPosition;
66
+ /** Maximum height of notification list before scrolling */
67
+ maxHeight?: string;
68
+ /** Size of the bell button */
69
+ size?: 'sm' | 'md' | 'lg';
70
+ /** Empty state message */
71
+ emptyMessage?: string;
72
+ /** "View All" button text */
73
+ viewAllText?: string;
74
+ /** Disabled state */
75
+ disabled?: boolean;
76
+ /** Additional class name for the bell button */
77
+ className?: string;
78
+ /**
79
+ * Visual variant for notification items
80
+ * - 'compact': Dot indicator, stacked layout (default)
81
+ * - 'detailed': Labeled type badge, time aligned right
82
+ */
83
+ variant?: 'compact' | 'detailed';
84
+ /** Show unread count in header (e.g., "Notifications (2 unread)") */
85
+ showUnreadInHeader?: boolean;
86
+ /**
87
+ * Style of the bell button
88
+ * - 'ghost': Transparent background, just the icon (default)
89
+ * - 'outlined': Visible border/container with subtle background
90
+ */
91
+ bellStyle?: NotificationBellStyle;
92
+ }
93
+
94
+ /**
95
+ * Format a date to a relative time string
96
+ */
97
+ function formatTimeAgo(date: string | Date): string {
98
+ const now = new Date();
99
+ const then = new Date(date);
100
+ const diffMs = now.getTime() - then.getTime();
101
+ const diffSeconds = Math.floor(diffMs / 1000);
102
+ const diffMinutes = Math.floor(diffSeconds / 60);
103
+ const diffHours = Math.floor(diffMinutes / 60);
104
+ const diffDays = Math.floor(diffHours / 24);
105
+
106
+ if (diffSeconds < 60) {
107
+ return 'Just now';
108
+ } else if (diffMinutes < 60) {
109
+ return `${diffMinutes}m ago`;
110
+ } else if (diffHours < 24) {
111
+ return `${diffHours}h ago`;
112
+ } else if (diffDays < 7) {
113
+ return `${diffDays}d ago`;
114
+ } else {
115
+ return then.toLocaleDateString();
116
+ }
117
+ }
118
+
119
+ /**
120
+ * Map notification type to Badge variant
121
+ */
122
+ const typeToBadgeVariant: Record<NotificationItem['type'], 'info' | 'success' | 'warning' | 'error'> = {
123
+ info: 'info',
124
+ success: 'success',
125
+ warning: 'warning',
126
+ error: 'error',
127
+ };
128
+
129
+ /**
130
+ * Default labels for notification types
131
+ */
132
+ const defaultTypeLabels: Record<NotificationItem['type'], string> = {
133
+ info: 'Info',
134
+ success: 'Success',
135
+ warning: 'Warning',
136
+ error: 'Alert',
137
+ };
138
+
139
+ /**
140
+ * Map dropdown position to Popover placement
141
+ * - bottom-right: Below bell, dropdown's right edge aligns with bell
142
+ * - bottom-left: Below bell, dropdown's left edge aligns with bell
143
+ * - top-right: Above bell, dropdown's right edge aligns with bell
144
+ * - top-left: Above bell, dropdown's left edge aligns with bell
145
+ */
146
+ function getPopoverPlacement(position: NotificationBellPosition): 'bottom-start' | 'bottom-end' | 'top-start' | 'top-end' {
147
+ switch (position) {
148
+ case 'bottom-right':
149
+ case 'right':
150
+ return 'bottom-start'; // Below, extends to the right
151
+ case 'bottom-left':
152
+ case 'left':
153
+ return 'bottom-end'; // Below, extends to the left
154
+ case 'top-right':
155
+ return 'top-start'; // Above, extends to the right
156
+ case 'top-left':
157
+ return 'top-end'; // Above, extends to the left
158
+ default:
159
+ return 'bottom-start';
160
+ }
161
+ }
162
+
163
+ /**
164
+ * NotificationBell - A bell icon with badge and dropdown for displaying notifications
165
+ *
166
+ * Displays a bell icon with an optional unread count badge. When clicked, shows a
167
+ * dropdown panel with recent notifications, mark as read actions, and a link to
168
+ * view all notifications.
169
+ *
170
+ * @example Basic usage (compact variant)
171
+ * ```tsx
172
+ * <NotificationBell
173
+ * notifications={notifications}
174
+ * onMarkAsRead={(id) => markRead(id)}
175
+ * onMarkAllRead={() => markAllRead()}
176
+ * onNotificationClick={(n) => navigate(n.actionUrl)}
177
+ * onViewAll={() => navigate('/notifications')}
178
+ * />
179
+ * ```
180
+ *
181
+ * @example Detailed variant with labeled badges
182
+ * ```tsx
183
+ * <NotificationBell
184
+ * notifications={notifications}
185
+ * variant="detailed"
186
+ * showUnreadInHeader
187
+ * dropdownPosition="bottom-left"
188
+ * />
189
+ * ```
190
+ */
191
+ export default function NotificationBell({
192
+ notifications,
193
+ unreadCount: providedUnreadCount,
194
+ onMarkAsRead,
195
+ onMarkAllRead,
196
+ onNotificationClick,
197
+ onViewAll,
198
+ loading = false,
199
+ dropdownPosition = 'bottom-right',
200
+ maxHeight = '400px',
201
+ size = 'md',
202
+ emptyMessage = 'No notifications',
203
+ viewAllText = 'View all notifications',
204
+ disabled = false,
205
+ className = '',
206
+ variant = 'compact',
207
+ showUnreadInHeader = false,
208
+ bellStyle = 'ghost',
209
+ }: NotificationBellProps) {
210
+ const [isOpen, setIsOpen] = useState(false);
211
+
212
+ // Calculate unread count if not provided
213
+ const unreadCount = useMemo(() => {
214
+ if (providedUnreadCount !== undefined) {
215
+ return providedUnreadCount;
216
+ }
217
+ return notifications.filter((n) => !n.isRead).length;
218
+ }, [providedUnreadCount, notifications]);
219
+
220
+ // Handle notification click
221
+ const handleNotificationClick = useCallback(
222
+ (notification: NotificationItem) => {
223
+ onNotificationClick?.(notification);
224
+ },
225
+ [onNotificationClick]
226
+ );
227
+
228
+ // Handle mark as read
229
+ const handleMarkAsRead = useCallback(
230
+ (e: React.MouseEvent, id: string) => {
231
+ e.stopPropagation();
232
+ onMarkAsRead?.(id);
233
+ },
234
+ [onMarkAsRead]
235
+ );
236
+
237
+ // Handle mark all as read
238
+ const handleMarkAllRead = useCallback(() => {
239
+ onMarkAllRead?.();
240
+ }, [onMarkAllRead]);
241
+
242
+ // Handle view all
243
+ const handleViewAll = useCallback(() => {
244
+ onViewAll?.();
245
+ setIsOpen(false);
246
+ }, [onViewAll]);
247
+
248
+ // Icon sizes based on button size
249
+ const iconSizes = {
250
+ sm: 'h-4 w-4',
251
+ md: 'h-5 w-5',
252
+ lg: 'h-6 w-6',
253
+ };
254
+
255
+ // Dropdown width based on size
256
+ const dropdownWidths = {
257
+ sm: 'w-72',
258
+ md: 'w-80',
259
+ lg: 'w-96',
260
+ };
261
+
262
+ // Outlined bell style classes
263
+ const outlinedSizeClasses = {
264
+ sm: 'p-2',
265
+ md: 'p-3',
266
+ lg: 'p-4',
267
+ };
268
+
269
+ // Trigger button
270
+ const triggerButton = bellStyle === 'outlined' ? (
271
+ <div className="relative inline-block">
272
+ <button
273
+ className={`
274
+ ${outlinedSizeClasses[size]}
275
+ bg-white border-2 border-paper-300 rounded-xl
276
+ hover:bg-paper-50 hover:border-paper-400
277
+ focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-accent-400
278
+ transition-all duration-200
279
+ disabled:opacity-40 disabled:cursor-not-allowed
280
+ ${className}
281
+ `}
282
+ disabled={disabled}
283
+ aria-label={
284
+ unreadCount > 0
285
+ ? `Notifications - ${unreadCount} unread`
286
+ : 'Notifications'
287
+ }
288
+ >
289
+ <Bell className={`${iconSizes[size]} text-ink-600`} />
290
+ </button>
291
+ {unreadCount > 0 && (
292
+ <span
293
+ className={`
294
+ absolute -top-1 -right-1
295
+ flex items-center justify-center
296
+ min-w-[18px] h-[18px] px-1.5
297
+ rounded-full text-white font-semibold text-[11px]
298
+ bg-error-500 shadow-sm
299
+ pointer-events-none
300
+ `}
301
+ aria-label={`${unreadCount > 99 ? '99+' : unreadCount} notifications`}
302
+ >
303
+ {unreadCount > 99 ? '99+' : unreadCount}
304
+ </span>
305
+ )}
306
+ </div>
307
+ ) : (
308
+ <Button
309
+ variant="ghost"
310
+ iconOnly
311
+ size={size}
312
+ disabled={disabled}
313
+ badge={unreadCount > 0 ? unreadCount : undefined}
314
+ badgeVariant="error"
315
+ aria-label={
316
+ unreadCount > 0
317
+ ? `Notifications - ${unreadCount} unread`
318
+ : 'Notifications'
319
+ }
320
+ className={className}
321
+ >
322
+ <Bell className={iconSizes[size]} />
323
+ </Button>
324
+ );
325
+
326
+ // Header title with optional unread count
327
+ const headerTitle = showUnreadInHeader && unreadCount > 0
328
+ ? `Notifications (${unreadCount} unread)`
329
+ : 'Notifications';
330
+
331
+ // Render compact notification item
332
+ const renderCompactItem = (notification: NotificationItem) => (
333
+ <div className="flex gap-3">
334
+ {/* Type indicator */}
335
+ <div className="flex-shrink-0 pt-1">
336
+ <Badge
337
+ dot
338
+ variant={typeToBadgeVariant[notification.type]}
339
+ size="sm"
340
+ />
341
+ </div>
342
+
343
+ {/* Content */}
344
+ <div className="flex-1 min-w-0">
345
+ <Text
346
+ size="sm"
347
+ weight={
348
+ notification.priority === 'high' ||
349
+ notification.priority === 'urgent' ||
350
+ !notification.isRead
351
+ ? 'medium'
352
+ : 'normal'
353
+ }
354
+ truncate
355
+ >
356
+ {notification.title}
357
+ </Text>
358
+ <Text size="xs" color="muted" lineClamp={2} className="mt-0.5">
359
+ {notification.message}
360
+ </Text>
361
+ <Text size="xs" color="muted" className="mt-1">
362
+ {formatTimeAgo(notification.createdAt)}
363
+ </Text>
364
+ </div>
365
+
366
+ {/* Mark as read button */}
367
+ {!notification.isRead && onMarkAsRead && (
368
+ <button
369
+ className="flex-shrink-0 p-1 text-ink-400 hover:text-ink-600 hover:bg-paper-100 rounded transition-colors"
370
+ onClick={(e) => handleMarkAsRead(e, notification.id)}
371
+ aria-label="Mark as read"
372
+ title="Mark as read"
373
+ >
374
+ <Check className="h-4 w-4" />
375
+ </button>
376
+ )}
377
+ </div>
378
+ );
379
+
380
+ // Render detailed notification item
381
+ const renderDetailedItem = (notification: NotificationItem) => (
382
+ <div className="flex gap-3">
383
+ {/* Content */}
384
+ <div className="flex-1 min-w-0">
385
+ {/* Title row with time */}
386
+ <div className="flex items-start justify-between gap-2">
387
+ <Text
388
+ size="sm"
389
+ weight={
390
+ notification.priority === 'high' ||
391
+ notification.priority === 'urgent' ||
392
+ !notification.isRead
393
+ ? 'semibold'
394
+ : 'medium'
395
+ }
396
+ className="flex-1"
397
+ >
398
+ {notification.title}
399
+ </Text>
400
+ <Text size="xs" color="muted" className="flex-shrink-0 whitespace-nowrap">
401
+ {formatTimeAgo(notification.createdAt)}
402
+ </Text>
403
+ </div>
404
+
405
+ {/* Message */}
406
+ <Text size="xs" color="muted" lineClamp={2} className="mt-1">
407
+ {notification.message}
408
+ </Text>
409
+
410
+ {/* Type badge */}
411
+ <div className="mt-2">
412
+ <Badge
413
+ variant={typeToBadgeVariant[notification.type]}
414
+ size="sm"
415
+ >
416
+ {notification.typeLabel || defaultTypeLabels[notification.type]}
417
+ </Badge>
418
+ </div>
419
+ </div>
420
+
421
+ {/* Mark as read button */}
422
+ {!notification.isRead && onMarkAsRead && (
423
+ <button
424
+ className="flex-shrink-0 p-1 text-ink-400 hover:text-ink-600 hover:bg-paper-100 rounded transition-colors self-center"
425
+ onClick={(e) => handleMarkAsRead(e, notification.id)}
426
+ aria-label="Mark as read"
427
+ title="Mark as read"
428
+ >
429
+ <Check className="h-4 w-4" />
430
+ </button>
431
+ )}
432
+ </div>
433
+ );
434
+
435
+ // Dropdown content
436
+ const dropdownContent = (
437
+ <div className={`${dropdownWidths[size]} bg-white`}>
438
+ {/* Header */}
439
+ <div className="flex items-center justify-between px-4 py-3 border-b border-paper-200">
440
+ <Text weight="semibold" size="sm">
441
+ {headerTitle}
442
+ </Text>
443
+ {unreadCount > 0 && onMarkAllRead && (
444
+ <button
445
+ className="flex items-center gap-1.5 text-xs text-ink-600 hover:text-ink-800 transition-colors"
446
+ onClick={handleMarkAllRead}
447
+ >
448
+ <Check className="h-3.5 w-3.5" />
449
+ Mark all read
450
+ </button>
451
+ )}
452
+ </div>
453
+
454
+ {/* Notification List */}
455
+ <div
456
+ className="overflow-y-auto"
457
+ style={{ maxHeight }}
458
+ role="list"
459
+ aria-label="Notifications"
460
+ >
461
+ {loading ? (
462
+ // Loading state
463
+ <div className="p-4">
464
+ <Stack spacing="sm">
465
+ {[1, 2, 3].map((i) => (
466
+ <div key={i} className="flex gap-3">
467
+ {variant === 'compact' && (
468
+ <Skeleton className="h-4 w-4 rounded-full flex-shrink-0" />
469
+ )}
470
+ <div className="flex-1">
471
+ <div className="flex justify-between">
472
+ <Skeleton className="h-4 w-3/4 mb-2" />
473
+ {variant === 'detailed' && (
474
+ <Skeleton className="h-3 w-12" />
475
+ )}
476
+ </div>
477
+ <Skeleton className="h-3 w-full mb-1" />
478
+ {variant === 'compact' ? (
479
+ <Skeleton className="h-3 w-1/4" />
480
+ ) : (
481
+ <Skeleton className="h-5 w-16 mt-2" />
482
+ )}
483
+ </div>
484
+ </div>
485
+ ))}
486
+ </Stack>
487
+ </div>
488
+ ) : notifications.length === 0 ? (
489
+ // Empty state
490
+ <div className="py-8 px-4 text-center">
491
+ <Bell className="h-10 w-10 text-ink-300 mx-auto mb-3" />
492
+ <Text color="muted" size="sm">
493
+ {emptyMessage}
494
+ </Text>
495
+ </div>
496
+ ) : (
497
+ // Notification items
498
+ notifications.map((notification) => (
499
+ <div
500
+ key={notification.id}
501
+ role="listitem"
502
+ className={`
503
+ px-4 py-3 border-b border-paper-100 last:border-b-0
504
+ hover:bg-paper-50 transition-colors cursor-pointer
505
+ ${!notification.isRead ? 'bg-primary-50/30' : ''}
506
+ ${notification.priority === 'urgent' ? 'border-l-2 border-l-error-500' : ''}
507
+ `}
508
+ onClick={() => handleNotificationClick(notification)}
509
+ onKeyDown={(e) => {
510
+ if (e.key === 'Enter' || e.key === ' ') {
511
+ e.preventDefault();
512
+ handleNotificationClick(notification);
513
+ }
514
+ }}
515
+ tabIndex={0}
516
+ >
517
+ {variant === 'compact'
518
+ ? renderCompactItem(notification)
519
+ : renderDetailedItem(notification)}
520
+ </div>
521
+ ))
522
+ )}
523
+ </div>
524
+
525
+ {/* Footer */}
526
+ {onViewAll && notifications.length > 0 && (
527
+ <div className="px-4 py-3 border-t border-paper-200">
528
+ <Button
529
+ variant="ghost"
530
+ size="sm"
531
+ fullWidth
532
+ onClick={handleViewAll}
533
+ icon={<ExternalLink className="h-3.5 w-3.5" />}
534
+ iconPosition="right"
535
+ >
536
+ {viewAllText}
537
+ </Button>
538
+ </div>
539
+ )}
540
+ </div>
541
+ );
542
+
543
+ return (
544
+ <Popover
545
+ trigger={triggerButton}
546
+ placement={getPopoverPlacement(dropdownPosition)}
547
+ triggerMode="click"
548
+ showArrow={false}
549
+ offset={4}
550
+ open={isOpen}
551
+ onOpenChange={setIsOpen}
552
+ closeOnClickOutside
553
+ closeOnEscape
554
+ disabled={disabled}
555
+ className="p-0 overflow-hidden"
556
+ >
557
+ {dropdownContent}
558
+ </Popover>
559
+ );
560
+ }
@@ -316,6 +316,10 @@ export type { SearchBarProps } from './SearchBar';
316
316
  export { default as NotificationIndicator } from './NotificationIndicator';
317
317
  export type { NotificationIndicatorProps } from './NotificationIndicator';
318
318
 
319
+ // Notification Bell (dropdown with notification list)
320
+ export { default as NotificationBell } from './NotificationBell';
321
+ export type { NotificationBellProps, NotificationItem, NotificationBellPosition, NotificationBellStyle } from './NotificationBell';
322
+
319
323
  // Data Table
320
324
  export { default as DataTable } from './DataTable';
321
325
  export type {