# Notification inbox

> The bell, done properly: its own circle grows into the inbox, new notifications swing it and roll the count, and read ones fold out of Unread.

Most notification bells drop a dropdown under themselves and repaint the list whenever anything changes. Here the bell's own circle grows into the inbox: its size and corners ease, the bell fades into the list, and closing folds it back the same way. A new notification swings the bell from its top and rolls the count; if the inbox is open it fades in on top while the rest glide down. Unread and All are tabs whose highlight slides. Opening a notification reads it: the dot shrinks to an outline, the title settles to grey, and in Unread the row folds away a moment later. The dot is also the read / unread toggle, archive sits beside it, and Mark all read does them together. When there's nothing left, "You're all caught up" draws its check.

## Install

```bash
npx shadcn@latest add @hairline/notification-inbox
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: `lucide-react`
- Hairline UI components it needs (the CLI installs them too): `text-morph`, `number-roll`, `status-button`
- Docs and live preview: https://hairlineui.com/ui/notification-inbox
- Registry item (source): https://hairlineui.com/r/notification-inbox.json

## Usage

```tsx
import { NotificationInbox } from "@/components/notification-inbox";

<NotificationInbox
  defaultValue={notifications}
  subscribe={(push) => feed.on("notification", push)}
  onRead={(ids, read) => api.markRead(ids, read)}
  onArchive={(ids) => api.archive(ids)}
  onOpen={(n) => n.href && router.push(n.href)}
/>
```

## Live from Supabase Realtime

Notifications are rows in your database. The inbox starts with the latest ones from the server, and a Realtime subscription pushes each new row for this user as it's inserted. Read and archive are plain updates. Knock and Novu feeds plug into the same three functions.

```tsx
"use client";
import { useRouter } from "next/navigation";
import { NotificationInbox, type InboxItem } from "@/components/notification-inbox";
import { createClient } from "@/lib/supabase/client";

const supabase = createClient();
const toItem = (r: any): InboxItem => ({ id: r.id, title: r.title, body: r.body, at: r.created_at, read: r.read, href: r.href, actor: r.actor_name ? { name: r.actor_name, image: r.actor_image } : undefined });

export function Inbox({ userId, initial }: { userId: string; initial: InboxItem[] }) {
  const router = useRouter();
  return (
    <NotificationInbox
      defaultValue={initial}
      subscribe={(push) => {
        const channel = supabase
          .channel(`inbox:${userId}`)
          .on("postgres_changes", { event: "INSERT", schema: "public", table: "notifications", filter: `user_id=eq.${userId}` }, (p) => push(toItem(p.new)))
          .subscribe();
        return () => void supabase.removeChannel(channel);
      }}
      onRead={async (ids, read) => {
        const { error } = await supabase.from("notifications").update({ read }).in("id", ids);
        if (error) throw error;
      }}
      onArchive={async (ids) => {
        const { error } = await supabase.from("notifications").update({ archived: true }).in("id", ids);
        if (error) throw error;
      }}
      loadMore={async (before) => {
        const { data } = await supabase.from("notifications").select().eq("archived", false).lt("created_at", before.at).order("created_at", { ascending: false }).limit(20);
        return (data ?? []).map(toItem);
      }}
      onOpen={(n) => n.href && router.push(n.href)}
    />
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `InboxItem[]` | – | Controlled notifications, newest first: { id, title, body?, at, read?, actor?, href? }. |
| `defaultValue` | `InboxItem[]` | – | Starting notifications when uncontrolled. |
| `onValueChange` | `(items) => void` | – | Every arrival, read change and archive. |
| `subscribe` | `(push) => () => void` | – | Feeds new notifications; call push with one or many. Duplicates by id are ignored. |
| `onRead` | `(ids, read) => Promise<unknown>` | – | Saves read state; the change shows at once and is undone if this throws. Without it there is no Mark all read. |
| `onArchive` | `(ids) => Promise<unknown>` | – | Archives; the row leaves at once and comes back if this throws. Without it there is no archive button. |
| `onOpen` | `(item) => void` | – | Runs when a notification is opened, after it's marked read. Items with href are links. |
| `loadMore` | `(before) => Promise<InboxItem[]>` | – | Older notifications for the All tab; an empty list hides Show older. |
| `align` | `"end" \| "start"` | `"end"` | Which edge of the bell the inbox lines up with. |
| `label` | `string` | `"Inbox"` | Names the bell and the dialog. |
| `defaultOpen` | `boolean` | `false` | Starts open. |

## States

- **open**: The bell's 36px circle grows into the inbox (460ms, size and corners together), lined up with the bell's edge and nudged back on screen; the bell fades as the list fades in 100ms behind. Escape, the cross or a click outside folds it back and returns focus to the bell.
- **arrive**: subscribe() pushes a notification: the bell swings from its top (720ms, ±16° settling), the badge springs in or its count rolls, and in an open inbox the row fades in on top while the rest glide down.
- **read**: Opening a notification (or its dot) marks it read: the dot shrinks to an outline, the title eases to grey; in Unread the row stays 900ms, then folds away. onRead failing puts it back.
- **mark all**: Mark all read → Marking → All read with a drawn check; every unread row reads and folds out of Unread together, and the counts roll to zero.
- **archive**: The archive button (shown on hover or focus, always on touch) removes the row: it folds away where it stood and the rest glide up. onArchive failing brings it back.
- **empty**: "You're all caught up" opens by height with a check that draws itself in a hairline circle.

## What it handles

Most notification bells drop a dropdown under themselves and repaint the list whenever anything changes. Here the bell’s own circle grows into the inbox, and new notifications arrive without anything jumping.

- **The bell grows into the inbox**: The bell’s circle eases into the inbox panel, and folds back the same way when it closes.
- **New ones arrive visibly**: subscribe() feeds new notifications. The bell swings, the count rolls, and an open inbox fades the new row in while the rest glide down.
- **Unread and All**: Opening a notification reads it. In Unread, the row lingers for a moment and then folds away.
- **Read and archive save safely**: A change shows at once, and is put back if your onRead or onArchive function throws.
- **Mark all read**: One button reads everything, with a drawn check, and the counts roll to zero.
- **All caught up, and older ones**: An empty inbox says so with a drawn check, and loadMore brings in older notifications for All.

## Replaces

a bell with a badge, a dropdown, Unread and All tabs, mark-as-read logic, archive with rollback, a realtime feed hook

## Where it fits

- **SaaS app headers**: The bell in the top bar, with an unread count.
- **Collaboration tools**: Mentions, comments and assignments for each person.
- **Marketplace and shop dashboards**: New orders, reviews and payouts as they happen.
- **Admin consoles**: Alerts and system events that need to be seen and cleared.

## Notes

- The bell is a button that says how many are unread ("Inbox, 2 unread") and opens a dialog; focus goes to the active tab and returns to the bell on close.
- Unread and All are a tablist (arrows switch). Each row is a button or link with ", unread" read out when it is; the dot is a toggle button named for what it will do.
- New notifications, read changes and archives are announced in a status region outside the dialog.
- The archive button appears on hover and focus with a mouse, and is always visible on touch screens.
- Reduced motion opens the inbox in place, without the swing, glides or folds; the counts still change.

License: MIT (https://hairlineui.com/license#components)
