opensourceui
GitHubTwitter/X
  1. Home
  2. Components
  3. Dropdowns
  4. Notification
Back to Dropdowns

Components / Dropdowns

Notification

Free Dropdowns React component — NotificationDropdown. MIT licensed, copy-paste ready for Next.js and Tailwind CSS.

Square bell trigger with a gray inset tray — each notification is a small bordered card inside the panel.

Preview

Setup

How to use

Free for personal and commercial use. No UI attribution required. Include the MIT copyright notice when copying component source into your project. Read the MIT license.

  1. 1

    Run in your terminal:

    $npm install clsx tailwind-merge lucide-react
  2. 2

    Copy lib/cn.ts below. Skip if you already have cn().

  3. 3

    Copy the code below and create components/dropdowns/notification-dropdown.tsx in your project.

  4. 4

    Import and render:

    Example

    import { NotificationDropdown } from "@/components/dropdowns/notification-dropdown";

    <NotificationDropdown onNotificationClick={(n) => console.log(n.id)} />

lib/cn.ts

1
2
3
4
5
6
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
 
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}

components/dropdowns/notification-dropdown.tsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"use client";
 
import {
forwardRef,
useEffect,
useId,
useRef,
useState,
type ComponentPropsWithoutRef,
} from "react";
 
import { cn } from "@/lib/cn";
 
import { Bell } from "lucide-react";
 
export type NotificationItem = Readonly<{
id: string;
title: string;
body: string;
time: string;
unread?: boolean;
}>;
 
export type NotificationDropdownProps = Readonly<
{
triggerAriaLabel?: string;
menuAriaLabel?: string;
headerTitle?: string;
markAllLabel?: string;
emptyLabel?: string;
notifications?: readonly NotificationItem[];
onNotificationClick?: (item: NotificationItem) => void;
onMarkAllRead?: () => void;
} & ComponentPropsWithoutRef<"div">
>;
 
const defaultNotifications: readonly NotificationItem[] = [
{
id: "comment",
title: "New comment",
body: "Sarah replied on your post",
time: "2m",
unread: true,
},
{
id: "deploy",
title: "Deploy succeeded",
body: "Production build finished",
time: "1h",
unread: true,
},
{
id: "invite",
title: "Invite accepted",
body: "Alex joined your workspace",
time: "1d",
unread: false,
},
];
 
type NotificationRowProps = Readonly<{
item: NotificationItem;
onSelect: (item: NotificationItem) => void;
}>;
 
function NotificationRow({ item, onSelect }: NotificationRowProps) {
return (
<button
type="button"
role="menuitem"
aria-label={`${item.title}. ${item.body}`}
onClick={() => onSelect(item)}
className={cn(
"flex w-full cursor-pointer items-start justify-between gap-3 rounded-lg border px-2.5 py-2 text-left transition-colors",
item.unread
? "border-neutral-200 bg-white hover:border-neutral-300"
: "border-transparent bg-transparent hover:bg-white",
)}
>
<span className="min-w-0">
<span className="block truncate text-[12px] font-medium text-neutral-800">
{item.title}
</span>
<span className="mt-0.5 block truncate text-[10px] text-neutral-400">
{item.body}
</span>
</span>
<span className="shrink-0 font-mono text-[9px] text-neutral-400">
{item.time}
</span>
</button>
);
}
 
// Notification panel — square trigger, inset gray tray with card rows.
export const NotificationDropdown = forwardRef<
HTMLDivElement,
NotificationDropdownProps
>(
(
{
triggerAriaLabel = "Notifications",
menuAriaLabel = "Notification list",
headerTitle = "Inbox",
markAllLabel = "Clear",
emptyLabel = "Nothing new",
notifications = defaultNotifications,
onNotificationClick,
onMarkAllRead,
className,
...props
},
ref,
) => {
const [open, setOpen] = useState(false);
const rootRef = useRef<HTMLDivElement>(null);
const menuId = useId();
 
const unreadCount = notifications.filter((n) => n.unread).length;
 
useEffect(() => {
const closeOnOutside = (event: globalThis.MouseEvent) => {
if (!rootRef.current?.contains(event.target as Node)) {
setOpen(false);
}
};
 
const closeOnEscape = (event: KeyboardEvent) => {
if (event.key === "Escape") setOpen(false);
};
 
document.addEventListener("mousedown", closeOnOutside);
document.addEventListener("keydown", closeOnEscape);
return () => {
document.removeEventListener("mousedown", closeOnOutside);
document.removeEventListener("keydown", closeOnEscape);
};
}, []);
 
const handleNotificationSelect = (item: NotificationItem) => {
setOpen(false);
onNotificationClick?.(item);
};
 
const toggleOpen = () => setOpen((prev) => !prev);
 
return (
<div
ref={ref}
data-slot="notification-dropdown"
className={cn("relative inline-block font-sans", className)}
{...props}
>
<div ref={rootRef} className="relative">
<button
type="button"
aria-label={
unreadCount > 0
? `${triggerAriaLabel}, ${unreadCount} unread`
: triggerAriaLabel
}
aria-expanded={open}
aria-haspopup="menu"
aria-controls={open ? menuId : undefined}
onClick={toggleOpen}
onKeyDown={(event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
toggleOpen();
}
}}
className={cn(
"relative inline-flex size-10 cursor-pointer items-center justify-center rounded-lg border bg-white transition-colors",
open ? "border-neutral-200" : "border-neutral-100 hover:border-neutral-200",
)}
>
<Bell size={16} strokeWidth={2} className="text-neutral-500" />
{unreadCount > 0 ? (
<span
aria-hidden
className="absolute -top-1 -right-1 flex size-4 items-center justify-center rounded-full bg-neutral-800 text-[8px] font-bold text-white"
>
{unreadCount > 9 ? "9+" : unreadCount}
</span>
) : null}
</button>
 
{open ? (
<div
id={menuId}
role="menu"
aria-label={menuAriaLabel}
className="absolute top-[calc(100%+8px)] right-0 z-100 w-60 rounded-xl bg-neutral-50 p-2 md:w-64"
>
<div className="mb-2 flex items-center justify-between px-1">
<p className="text-[11px] font-semibold text-neutral-700">
{headerTitle}
</p>
{unreadCount > 0 ? (
<button
type="button"
onClick={() => onMarkAllRead?.()}
className="cursor-pointer text-[10px] font-medium text-neutral-400 hover:text-neutral-600"
>
{markAllLabel}
</button>
) : null}
</div>
 
<div className="flex max-h-52 flex-col gap-1 overflow-y-auto">
{notifications.length > 0 ? (
notifications.map((item) => (
<NotificationRow
key={item.id}
item={item}
onSelect={handleNotificationSelect}
/>
))
) : (
<p className="py-6 text-center text-[11px] text-neutral-400">
{emptyLabel}
</p>
)}
</div>
</div>
) : null}
</div>
</div>
);
},
);
 
NotificationDropdown.displayName = "NotificationDropdown";
PreviousNext

On this

page

Jump to a section on this page.

  • lib/cn.ts
  • components/dropdowns/notification-dropdown.tsx

Sponsor spot · demo preview

Logo

Your brand name

Your tagline or category

A short pitch about your product or service goes here. This is a preview of how sponsor cards will look in this sidebar.

Your call to action

This slot is available. Sponsor Opensource UI and reach developers browsing components every day.