Notifications List
Notification card with an unread count badge and a Mark all read action, grouped into Today and Earlier. Unread rows keep a ringed timeline mark and full-strength text; read rows fade to muted.
"use client"
import { useState } from "react"
import {
Timeline,
TimelineContent,
TimelineDescription,
TimelineIndicator,
TimelineItem,
TimelineLabel,
TimelineTitle,
} from "@/components/timeline/timeline"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import {
Card,
CardAction,
CardContent,
CardHeader,
CardTitle,
} from "@/components/ui/card"
// The copy: change text here, not in the markup.
const COPY = {
title: "Notifications",
unread: (count: number) => `${count} unread`,
markAll: "Mark all read",
groups: { today: "Today", earlier: "Earlier" },
} as const
const GROUPS = [
{
label: COPY.groups.today,
items: [
{
actor: "Megan McArthur",
action: "approved your request",
target: "Three design seats",
time: "9 min",
unread: true,
},
{
actor: "Alexei Leonov",
action: "mentioned you in",
target: "#482",
time: "1h",
unread: true,
},
{
actor: "Deploys",
action: "api-gateway is live",
target: "e43ddee",
time: "2h",
unread: true,
},
],
},
{
label: COPY.groups.earlier,
items: [
{
actor: "Raja Chari",
action: "assigned you",
target: "Rate limiter review",
time: "Yesterday",
},
{
actor: "Billing",
action: "your invoice is ready",
target: "October",
time: "Yesterday",
},
{
actor: "Sian Proctor",
action: "replied to your comment on",
target: "#311",
time: "2 days",
},
],
},
] as const
const UNREAD = GROUPS.flatMap((g) =>
g.items.filter((n) => "unread" in n && n.unread).map((n) => n.actor + n.time)
)
// Unread rows carry the ring and dark ink; read rows fade. Groups head their
// day; the rail runs through each group. Mark all read clears the set, the
// count and the rings together.
export default function NotificationsList01() {
const [unreadIds, setUnreadIds] = useState<string[]>(UNREAD)
return (
<Card className="mx-auto w-full max-w-sm">
<CardHeader>
<CardTitle className="flex items-center gap-2">
{COPY.title}
{unreadIds.length > 0 && (
<Badge variant="secondary">{COPY.unread(unreadIds.length)}</Badge>
)}
</CardTitle>
<CardAction>
<Button
variant="ghost"
size="sm"
disabled={unreadIds.length === 0}
onClick={() => setUnreadIds([])}
>
{COPY.markAll}
</Button>
</CardAction>
</CardHeader>
<CardContent className="grid gap-5">
{GROUPS.map((g) => (
<div key={g.label}>
<TimelineLabel className="mb-3">{g.label}</TimelineLabel>
<Timeline className="[--timeline-lh:1.25rem]">
{g.items.map((n) => {
const unread = unreadIds.includes(n.actor + n.time)
return (
<TimelineItem key={n.actor + n.time}>
<TimelineIndicator variant={unread ? undefined : "faint"} />
<TimelineContent className="pb-4">
<div className="flex items-baseline justify-between gap-3">
<TimelineTitle
className={
unread
? "text-sm/5 font-normal"
: "text-sm/5 font-normal text-muted-foreground"
}
>
<span className="font-medium">{n.actor}</span>{" "}
{n.action}
</TimelineTitle>
<TimelineLabel className="shrink-0">
{n.time}
</TimelineLabel>
</div>
<TimelineDescription className="mt-0 text-xs">
{n.target}
</TimelineDescription>
</TimelineContent>
</TimelineItem>
)
})}
</Timeline>
</div>
))}
</CardContent>
</Card>
)
}
import type { ComponentProps } from "react"
import { mergeProps } from "@base-ui/react/merge-props"
import { useRender } from "@base-ui/react/use-render"
import { cn } from "cn"
/**
* A vertical timeline, as an ordered list. Holds `TimelineItem` entries,
* each with a mark and a line down to the next.
*
* Three CSS variables, set on it through `className` or `style`, tune the
* marks. `--timeline-lh` is the line height of the first line in
* `TimelineContent`, so the mark lines up with it: the default 1rem suits a
* leading `TimelineLabel`; set 1.5rem when a title or body text comes first.
* `--timeline-dot` is the mark's diameter (1rem). `--timeline-nudge` (1px)
* lowers the mark slightly, because a mark centered on a line of text looks
* high.
*/
function Timeline({ className, ...props }: ComponentProps<"ol">) {
return (
<ol
role="list"
data-slot="timeline"
className={cn(
"relative [--timeline-dot:1rem] [--timeline-lh:1rem] [--timeline-nudge:1px]",
"[--timeline-shift:calc((var(--timeline-lh)-var(--timeline-dot))/2+var(--timeline-nudge))]",
className
)}
{...props}
/>
)
}
/**
* One entry, an `li` by default. Put a `TimelineIndicator` and a
* `TimelineContent` inside it. It draws the line from under its mark to the
* next entry's mark; the last entry has none. A `Timeline` nested inside
* `TimelineContent` keeps its own lines.
*/
function TimelineItem({
className,
children,
render,
...props
}: useRender.ComponentProps<"li">) {
return useRender({
defaultTagName: "li",
render,
state: { slot: "timeline-item" },
props: mergeProps<"li">(
{
className: cn(
"group/timeline-item relative flex items-start gap-3",
className
),
children: (
<>
<div
aria-hidden
data-slot="timeline-line"
className={cn(
// Runs from under this mark to the top of the next, never through it. The
// last-item check looks at the direct parent only, so a Timeline nested in
// TimelineContent keeps its own rails.
"pointer-events-none absolute top-[calc(var(--timeline-shift)+var(--timeline-dot))]",
"left-[calc(var(--timeline-dot)/2)] w-px -translate-x-1/2",
"h-[calc(100%-var(--timeline-dot))]",
// A 1px rail can't get thinner, so it is lighter than the type.
"bg-muted-foreground/45 [[data-slot=timeline-item]:last-child>&]:hidden"
)}
/>
{children}
</>
),
},
props
),
})
}
const indicatorVariants = {
default: { ring: "", dot: "bg-foreground" },
muted: { ring: "border-border", dot: "bg-muted-foreground/60" },
plain: { ring: "border-0", dot: "bg-muted-foreground/60" },
faint: { ring: "border-0", dot: "bg-border" },
line: { ring: "border-border", dot: "bg-border" },
empty: { ring: "border-border bg-transparent", dot: "hidden" },
solid: { ring: "border-0 bg-muted-foreground/60", dot: "hidden" },
inverted: { ring: "border-0 bg-foreground", dot: "bg-background" },
primary: { ring: "border-primary", dot: "bg-primary" },
}
/**
* The mark beside an entry. With no children it shows a small dot in a ring.
* Pass an element, such as an icon, to show your own mark instead; the ring
* and fill are then removed. Bare text and `false` are not supported.
*
* `variant` picks how the default mark looks, so a finished, current or
* upcoming entry can read differently without overriding the dot:
* `default` is a foreground ring and dot (the current entry); `muted` a
* soft ring with a muted dot; `plain` a muted dot with no ring; `faint` a
* pale dot with no ring; `line` a soft ring with a pale dot; `empty` an
* empty soft ring (upcoming); `solid` a filled muted disc; `inverted` a
* foreground disc with a background-colored dot; `primary` a primary ring
* and dot. It has no effect on a custom child.
*/
function TimelineIndicator({
className,
children,
variant = "default",
...props
}: ComponentProps<"div"> & {
/** Which look the default mark takes. See the part's description. */
variant?: keyof typeof indicatorVariants
}) {
const look = indicatorVariants[variant]
return (
<div
data-slot="timeline-indicator"
data-variant={variant}
className={cn(
"relative z-10 mt-(--timeline-shift) flex shrink-0",
"size-(--timeline-dot) items-center justify-center rounded-full",
// No children: the default dot in its ring. An element child: your mark,
// no ring. false, [] and bare text are not supported forms.
"border-[1.5px] border-foreground bg-background",
"has-[*:not([data-slot=timeline-dot])]:border-0",
"has-[*:not([data-slot=timeline-dot])]:bg-transparent",
look.ring,
className
)}
{...props}
>
{children ?? (
<div
data-slot="timeline-dot"
className={cn("size-1.5 rounded-full", look.dot)}
/>
)}
</div>
)
}
/**
* The text of an entry, beside its mark. It leaves 32px of space before the
* next entry, and none after the last.
*/
function TimelineContent({ className, ...props }: ComponentProps<"div">) {
return (
<div
data-slot="timeline-content"
className={cn(
"min-w-0 flex-1 pb-8 [[data-slot=timeline-item]:last-child>&]:pb-0",
className
)}
{...props}
/>
)
}
/** The entry's title, in medium weight. It renders a `div`, not a heading. */
function TimelineTitle({ className, ...props }: ComponentProps<"div">) {
return (
<div
data-slot="timeline-title"
className={cn("font-medium", className)}
{...props}
/>
)
}
/** Supporting text for an entry, small and muted, under the title. */
function TimelineDescription({ className, ...props }: ComponentProps<"p">) {
return (
<p
data-slot="timeline-description"
className={cn("mt-1 text-sm text-muted-foreground", className)}
{...props}
/>
)
}
/**
* A small muted line for an entry, such as a date. Its line height matches
* the default `--timeline-lh`, so a label that comes first lines up with the
* mark.
*/
function TimelineLabel({ className, ...props }: ComponentProps<"p">) {
return (
<p
data-slot="timeline-label"
className={cn("text-xs/4 text-muted-foreground", className)}
{...props}
/>
)
}
export {
Timeline,
TimelineItem,
TimelineIndicator,
TimelineContent,
TimelineTitle,
TimelineDescription,
TimelineLabel,
}
Installation
npx shadcn@latest add @01orbital/notifications-list-01Make it yours
- Its words are in
COPYat the top ofcomponents/blocks/notifications/notifications-list-01/index.tsx: change them there, not in the markup. GROUPSandUNREAD, also at the top of the file, hold its lists and settings.
Built from
Built from these parts. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:
The shadcn parts here are their Base UI versions, and this piece uses Base UI's API. A Radix version you already have won't work with it.
Last updated on