Notification bell
A bell that rings when something new arrives and opens a panel with All/Unread tabs, grouped notifications and mark-all-as-read.
"use client";
import { useEffect, useRef, useState, type CSSProperties } from "react";
import "./notification-bell.css";
type Note = {
id: number;
who: string;
tone: string;
text: string;
when: string;
group: "Today" | "Earlier";
unread: boolean;
};
const START: Note[] = [
{
id: 1,
who: "Maya Chen",
tone: "#ff5a1f",
text: "commented on Homepage v3: “Love the new hero.”",
when: "4m",
group: "Today",
unread: true,
},
{
id: 2,
who: "Leo Park",
tone: "#2b35ff",
text: "invited you to Brand refresh",
when: "1h",
group: "Today",
unread: true,
},
{
id: 3,
who: "Billing",
tone: "#0e0e10",
text: "Invoice #2041 was paid",
when: "3h",
group: "Today",
unread: false,
},
{
id: 4,
who: "Sam Ortiz",
tone: "#d7ff3a",
text: "assigned you “Fix mobile nav”",
when: "Mon",
group: "Earlier",
unread: false,
},
];
const INCOMING: Omit<Note, "id"> = {
who: "Ana Silva",
tone: "#ff5a1f",
text: "mentioned you in Launch checklist",
when: "now",
group: "Today",
unread: true,
};
/**
* NotificationBell
* - A button with aria-expanded controls a popover panel. Escape and outside clicks close it.
* - A new notification arrives 2.5s after load to show the ring animation.
*/
export function NotificationBell({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const [notes, setNotes] = useState(START);
const [open, setOpen] = useState(false);
const [tab, setTab] = useState<"all" | "unread">("all");
const [ring, setRing] = useState(0);
const root = useRef<HTMLDivElement>(null);
const unread = notes.filter((n) => n.unread).length;
useEffect(() => {
const t = setTimeout(() => {
setNotes((list) => [{ ...INCOMING, id: Date.now() }, ...list]);
setRing((r) => r + 1);
}, 2500);
return () => clearTimeout(t);
}, []);
useEffect(() => {
if (!open) return;
const onDown = (e: PointerEvent) =>
!root.current?.contains(e.target as Node) && setOpen(false);
const onKey = (e: KeyboardEvent) => e.key === "Escape" && setOpen(false);
addEventListener("pointerdown", onDown);
addEventListener("keydown", onKey);
return () => {
removeEventListener("pointerdown", onDown);
removeEventListener("keydown", onKey);
};
}, [open]);
const shown = notes.filter((n) => tab === "all" || n.unread);
const groups = (["Today", "Earlier"] as const)
.map((g) => ({ g, items: shown.filter((n) => n.group === g) }))
.filter((x) => x.items.length);
return (
<div ref={root} className="gu-bell" data-theme={theme}>
<button
type="button"
className="gu-bell__btn"
aria-expanded={open}
aria-controls="gu-bell-panel"
aria-label={`Notifications${unread ? `, ${unread} unread` : ""}`}
onClick={() => setOpen((o) => !o)}
>
<svg
key={ring}
viewBox="0 0 24 24"
aria-hidden="true"
data-ring={ring ? "" : undefined}
>
<path d="M6 9a6 6 0 0 1 12 0c0 6 2.5 7.5 2.5 7.5h-17S6 15 6 9" />
<path d="M10 19.5a2 2 0 0 0 4 0" className="gu-bell__clapper" />
</svg>
{unread > 0 && (
<span key={unread} className="gu-bell__count">
{unread}
</span>
)}
</button>
{open && (
<div
className="gu-bell__panel"
id="gu-bell-panel"
role="region"
aria-label="Notifications"
>
<header>
<b>Notifications</b>
<button
type="button"
className="gu-bell__mark"
disabled={!unread}
onClick={() =>
setNotes((l) => l.map((n) => ({ ...n, unread: false })))
}
>
Mark all as read
</button>
</header>
<div className="gu-bell__tabs" role="group" aria-label="Show">
<button
type="button"
aria-pressed={tab === "all"}
onClick={() => setTab("all")}
>
All
</button>
<button
type="button"
aria-pressed={tab === "unread"}
onClick={() => setTab("unread")}
>
Unread <span>{unread}</span>
</button>
</div>
<div className="gu-bell__list">
{groups.length ? (
groups.map(({ g, items }) => (
<section key={g}>
<h4>{g}</h4>
<ul>
{items.map((n) => (
<li key={n.id}>
<button
type="button"
data-unread={n.unread ? "" : undefined}
onClick={() =>
setNotes((l) =>
l.map((x) =>
x.id === n.id ? { ...x, unread: false } : x,
),
)
}
>
<span
className="gu-bell__avatar"
style={{ "--tone": n.tone } as CSSProperties}
aria-hidden="true"
>
{n.who[0]}
</span>
<span className="gu-bell__text">
{n.unread && (
<span className="gu-sr">Unread: </span>
)}
<b>{n.who}</b> {n.text}
<small>{n.when}</small>
</span>
{n.unread && (
<i className="gu-bell__dot" aria-hidden="true" />
)}
</button>
</li>
))}
</ul>
</section>
))
) : (
<p className="gu-bell__empty">Nothing unread. Nice.</p>
)}
</div>
</div>
)}
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Notification bell
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
An app header notification bell. When a new notification arrives the bell swings and the orange count pops. Clicking opens a panel with All/Unread tabs, notifications grouped by Today/Earlier, unread highlights, and "Mark all as read".
## Layout and look
- Bell: 48px rounded-square button (radius 16px, hairline border) with an outline bell icon; an orange count badge (22px, Ink text, card-colored ring) on the top-right corner.
- Panel: 380px, radius 22px, white (dark #1f1f23), deep soft shadow, anchored under the bell on the right.
- Header: "Notifications" (bold 18px) and an underlined muted "Mark all as read". Tabs: pill buttons, "Unread" with an orange count.
- Rows: 36px initial avatar, "**Maya Chen** commented on…", time below in muted; unread rows have a faint orange tint and an orange dot.
## Behavior and motion
1. Ring: the bell rotates 22° → −18° → 12° → −8° → 4° → 0 around its top (900ms), the clapper swings opposite. The badge pops with an overshoot.
2. Panel opens from top-right with scale 0.92 → 1 and fade (300ms cubic-bezier(0.16, 1, 0.3, 1)). Escape and outside clicks close it.
3. Clicking a row marks it read. Empty Unread tab: "Nothing unread. Nice."
## Accessibility
- The bell's label includes the unread count; aria-expanded/aria-controls link it to the panel region. Tabs use aria-pressed.
- prefers-reduced-motion: no swing, pop or scale.
## Props
theme. Feed your own notifications into START.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### NotificationBell.tsx
```tsx
"use client";
import { useEffect, useRef, useState, type CSSProperties } from "react";
import "./notification-bell.css";
type Note = {
id: number;
who: string;
tone: string;
text: string;
when: string;
group: "Today" | "Earlier";
unread: boolean;
};
const START: Note[] = [
{
id: 1,
who: "Maya Chen",
tone: "#ff5a1f",
text: "commented on Homepage v3: “Love the new hero.”",
when: "4m",
group: "Today",
unread: true,
},
{
id: 2,
who: "Leo Park",
tone: "#2b35ff",
text: "invited you to Brand refresh",
when: "1h",
group: "Today",
unread: true,
},
{
id: 3,
who: "Billing",
tone: "#0e0e10",
text: "Invoice #2041 was paid",
when: "3h",
group: "Today",
unread: false,
},
{
id: 4,
who: "Sam Ortiz",
tone: "#d7ff3a",
text: "assigned you “Fix mobile nav”",
when: "Mon",
group: "Earlier",
unread: false,
},
];
const INCOMING: Omit<Note, "id"> = {
who: "Ana Silva",
tone: "#ff5a1f",
text: "mentioned you in Launch checklist",
when: "now",
group: "Today",
unread: true,
};
/**
* NotificationBell
* - A button with aria-expanded controls a popover panel. Escape and outside clicks close it.
* - A new notification arrives 2.5s after load to show the ring animation.
*/
export function NotificationBell({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const [notes, setNotes] = useState(START);
const [open, setOpen] = useState(false);
const [tab, setTab] = useState<"all" | "unread">("all");
const [ring, setRing] = useState(0);
const root = useRef<HTMLDivElement>(null);
const unread = notes.filter((n) => n.unread).length;
useEffect(() => {
const t = setTimeout(() => {
setNotes((list) => [{ ...INCOMING, id: Date.now() }, ...list]);
setRing((r) => r + 1);
}, 2500);
return () => clearTimeout(t);
}, []);
useEffect(() => {
if (!open) return;
const onDown = (e: PointerEvent) =>
!root.current?.contains(e.target as Node) && setOpen(false);
const onKey = (e: KeyboardEvent) => e.key === "Escape" && setOpen(false);
addEventListener("pointerdown", onDown);
addEventListener("keydown", onKey);
return () => {
removeEventListener("pointerdown", onDown);
removeEventListener("keydown", onKey);
};
}, [open]);
const shown = notes.filter((n) => tab === "all" || n.unread);
const groups = (["Today", "Earlier"] as const)
.map((g) => ({ g, items: shown.filter((n) => n.group === g) }))
.filter((x) => x.items.length);
return (
<div ref={root} className="gu-bell" data-theme={theme}>
<button
type="button"
className="gu-bell__btn"
aria-expanded={open}
aria-controls="gu-bell-panel"
aria-label={`Notifications${unread ? `, ${unread} unread` : ""}`}
onClick={() => setOpen((o) => !o)}
>
<svg
key={ring}
viewBox="0 0 24 24"
aria-hidden="true"
data-ring={ring ? "" : undefined}
>
<path d="M6 9a6 6 0 0 1 12 0c0 6 2.5 7.5 2.5 7.5h-17S6 15 6 9" />
<path d="M10 19.5a2 2 0 0 0 4 0" className="gu-bell__clapper" />
</svg>
{unread > 0 && (
<span key={unread} className="gu-bell__count">
{unread}
</span>
)}
</button>
{open && (
<div
className="gu-bell__panel"
id="gu-bell-panel"
role="region"
aria-label="Notifications"
>
<header>
<b>Notifications</b>
<button
type="button"
className="gu-bell__mark"
disabled={!unread}
onClick={() =>
setNotes((l) => l.map((n) => ({ ...n, unread: false })))
}
>
Mark all as read
</button>
</header>
<div className="gu-bell__tabs" role="group" aria-label="Show">
<button
type="button"
aria-pressed={tab === "all"}
onClick={() => setTab("all")}
>
All
</button>
<button
type="button"
aria-pressed={tab === "unread"}
onClick={() => setTab("unread")}
>
Unread <span>{unread}</span>
</button>
</div>
<div className="gu-bell__list">
{groups.length ? (
groups.map(({ g, items }) => (
<section key={g}>
<h4>{g}</h4>
<ul>
{items.map((n) => (
<li key={n.id}>
<button
type="button"
data-unread={n.unread ? "" : undefined}
onClick={() =>
setNotes((l) =>
l.map((x) =>
x.id === n.id ? { ...x, unread: false } : x,
),
)
}
>
<span
className="gu-bell__avatar"
style={{ "--tone": n.tone } as CSSProperties}
aria-hidden="true"
>
{n.who[0]}
</span>
<span className="gu-bell__text">
{n.unread && (
<span className="gu-sr">Unread: </span>
)}
<b>{n.who}</b> {n.text}
<small>{n.when}</small>
</span>
{n.unread && (
<i className="gu-bell__dot" aria-hidden="true" />
)}
</button>
</li>
))}
</ul>
</section>
))
) : (
<p className="gu-bell__empty">Nothing unread. Nice.</p>
)}
</div>
</div>
)}
</div>
);
}
```
### notification-bell.css
```css
/* NotificationBell: change these colors to match your brand. */
.gu-bell {
--panel: #ffffff;
--btn: #fbf8f2;
--text: #0e0e10;
--muted: #57534b;
--line: rgb(14 14 16 / 0.1);
--soft: rgb(14 14 16 / 0.05);
--accent: #ff5a1f;
--focus: #2b35ff;
--ease: cubic-bezier(0.16, 1, 0.3, 1);
position: relative;
display: flex;
justify-content: flex-end;
width: min(100%, 380px);
min-height: 470px;
color: var(--text);
font-family: system-ui, sans-serif;
}
.gu-bell[data-theme="dark"] {
--panel: #1f1f23;
--btn: #18181b;
--text: #f3eee3;
--muted: #aea89c;
--line: rgb(243 238 227 / 0.1);
--soft: rgb(243 238 227 / 0.05);
--focus: #d7ff3a;
}
.gu-bell button {
font: inherit;
cursor: pointer;
}
.gu-bell button:focus-visible {
outline: 3px solid var(--focus);
outline-offset: 2px;
}
.gu-bell__btn {
position: relative;
display: grid;
place-items: center;
width: 48px;
height: 48px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--btn);
color: inherit;
}
.gu-bell__btn[aria-expanded="true"] {
border-color: var(--text);
}
.gu-bell__btn svg {
width: 22px;
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
transform-origin: 50% 8%;
}
.gu-bell__btn svg[data-ring] {
animation: gu-bell-ring 0.9s cubic-bezier(0.36, 0.07, 0.19, 0.97);
}
.gu-bell__btn svg[data-ring] .gu-bell__clapper {
animation: gu-bell-clap 0.9s;
}
@keyframes gu-bell-ring {
10% {
rotate: 22deg;
}
25% {
rotate: -18deg;
}
40% {
rotate: 12deg;
}
55% {
rotate: -8deg;
}
70% {
rotate: 4deg;
}
}
@keyframes gu-bell-clap {
20% {
translate: -2px 0;
}
45% {
translate: 2px 0;
}
}
.gu-bell__count {
position: absolute;
top: -6px;
right: -6px;
display: grid;
place-items: center;
min-width: 22px;
height: 22px;
padding: 0 6px;
border: 2px solid var(--btn);
border-radius: 999px;
background: var(--accent);
color: #0e0e10;
font-size: 0.72rem;
font-weight: 900;
animation: gu-bell-pop 0.45s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes gu-bell-pop {
from {
scale: 0;
}
}
.gu-bell__panel {
position: absolute;
top: 60px;
right: 0;
z-index: 5;
width: min(380px, calc(100vw - 24px));
overflow: hidden;
border: 1px solid var(--line);
border-radius: 22px;
background: var(--panel);
box-shadow: 0 30px 60px -24px rgb(0 0 0 / 0.35);
transform-origin: top right;
animation: gu-bell-open 0.3s var(--ease);
}
@keyframes gu-bell-open {
from {
opacity: 0;
transform: scale(0.92) translateY(-6px);
}
}
.gu-bell__panel header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 16px 16px 8px 18px;
}
.gu-bell__panel header b {
font-size: 1.1rem;
font-weight: 900;
}
.gu-bell__mark {
min-height: 36px;
padding: 0 10px;
border: 0;
border-radius: 999px;
background: none;
color: var(--muted);
font-size: 0.82rem !important;
font-weight: 700 !important;
text-decoration: underline;
}
.gu-bell__mark:disabled {
opacity: 0.4;
cursor: default;
}
.gu-bell__tabs {
display: flex;
gap: 4px;
padding: 0 14px 10px;
border-bottom: 1px solid var(--line);
}
.gu-bell__tabs button {
display: inline-flex;
align-items: center;
gap: 6px;
min-height: 36px;
padding: 0 12px;
border: 0;
border-radius: 999px;
background: none;
color: var(--muted);
font-weight: 700 !important;
font-size: 0.85rem !important;
}
.gu-bell__tabs [aria-pressed="true"] {
background: var(--soft);
color: var(--text);
}
.gu-bell__tabs span {
padding: 0 6px;
border-radius: 999px;
background: var(--accent);
color: #0e0e10;
font-size: 0.7rem;
}
.gu-bell__list {
max-height: 340px;
overflow: auto;
padding: 4px 8px 10px;
}
.gu-bell h4 {
margin: 10px 10px 4px;
color: var(--muted);
font-size: 0.75rem;
font-weight: 800;
}
.gu-bell ul {
margin: 0;
padding: 0;
list-style: none;
}
.gu-bell__list li button {
display: grid;
grid-template-columns: 36px 1fr 10px;
align-items: start;
gap: 12px;
width: 100%;
padding: 10px;
border: 0;
border-radius: 14px;
background: none;
color: inherit;
font-size: 0.88rem !important;
line-height: 1.4;
text-align: left;
}
.gu-bell__list li button:hover {
background: var(--soft);
}
.gu-bell__list li button[data-unread] {
background: color-mix(in srgb, var(--accent) 7%, transparent);
}
.gu-bell__avatar {
display: grid;
place-items: center;
width: 36px;
height: 36px;
border-radius: 50%;
background: var(--tone);
color: #f3eee3;
font-weight: 900;
}
.gu-bell__avatar[style*="ff5a1f"],
.gu-bell__avatar[style*="d7ff3a"] {
color: #0e0e10;
}
.gu-bell__text small {
display: block;
margin-top: 2px;
color: var(--muted);
}
.gu-bell__dot {
width: 9px;
height: 9px;
margin-top: 6px;
border-radius: 50%;
background: var(--accent);
}
.gu-bell__empty {
margin: 0;
padding: 36px 12px;
color: var(--muted);
text-align: center;
}
@media (prefers-reduced-motion: reduce) {
.gu-bell__btn svg[data-ring],
.gu-bell__btn svg[data-ring] .gu-bell__clapper,
.gu-bell__count,
.gu-bell__panel {
animation: none;
}
}
.gu-sr {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
}
```
You might also want
Next piece: One-page résuméAvatar stack
Components, beginner
Liquid glass controls
Components, advanced
Mega menu
Components, advanced