Skip to content
← Blocks

Notification Centre

A working notification centre with illustrative data and frontend interactions.

Open ↗

Make it yours.

Install the editable source and its dependencies into your React + Tailwind project.

Scroll horizontally for long lines
pnpm dlx shadcn@4.0.8 add http://localhost:3000/r/notification-centre.json

Use it

Scroll horizontally for long lines
"use client";
import { NotificationCentre } from "@/components/jez-ui/blocks/notification-centre";

export default function Example() {
  return (
    <>
      <NotificationCentre />
    </>
  );
}

Compose your own

These styled parts are included in the same install. Use children to build your layout; add className only when you want an override.

Scroll horizontally for long lines
"use client";
import {
  NotificationCentre,
  NotificationCentreToolbar,
  NotificationCentreFilters,
  NotificationCentreMessages,
  NotificationCentreEmpty,
} from "@/components/jez-ui/blocks/notification-centre";

export default function Example() {
  return (
    <NotificationCentre>
      <NotificationCentreToolbar />
      <NotificationCentreFilters />
      <NotificationCentreMessages />
      <NotificationCentreEmpty />
    </NotificationCentre>
  );
}
Scroll horizontally for long lines
import { NotificationCentre, NotificationCentreHeader, NotificationCentreTitle, NotificationCentreContent, NotificationCentreItem, NotificationCentreToolbar, NotificationCentreFilters, NotificationCentreMessages, NotificationCentreEmpty } from "@/components/jez-ui/blocks/notification-centre";
Read the composition guide →

Props & types

Scroll horizontally for long lines
export type NotificationCentreOptions = {
  className?: string;
  items?: typeof NotificationCentreDefaultItems;
  filters?: typeof NotificationCentreDefaultFilters;
  readIds?: string[];
  defaultReadIds?: string[];
  onReadIdsChange?: (value: string[]) => void;
};

export type NotificationCentreProps = Omit<
  React.ComponentProps<"section">,
  keyof NotificationCentreOptions
> &
  NotificationCentreOptions;

NotificationCentreProps

Source

View 4 source files

registry/blocks/notification-centre.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { CheckCheck, Download, AtSign, CircleCheck } from "lucide-react";
import { useControllable } from "../ui/use-controllable";
import { cn } from "../ui/utils";
import { Button } from "../ui/button";
export type NotificationCentreOptions = {
  className?: string;
  items?: typeof NotificationCentreDefaultItems;
  filters?: typeof NotificationCentreDefaultFilters;
  readIds?: string[];
  defaultReadIds?: string[];
  onReadIdsChange?: (value: string[]) => void;
};
export type NotificationCentreProps = Omit<
  React.ComponentProps<"section">,
  keyof NotificationCentreOptions
> &
  NotificationCentreOptions;
const NotificationCentreDefaultItems = [
  {
    id: "export",
    title: "Your export is ready",
    detail: "September report · CSV",
    time: "2m",
    icon: Download,
  },
  {
    id: "mention",
    title: "Sam mentioned you in a comment",
    detail: "“Can you take a look at the updated brief?”",
    time: "18m",
    icon: AtSign,
  },
  {
    id: "review",
    title: "A project is ready for review",
    detail: "Field notes · Homepage exploration",
    time: "1h",
    icon: CircleCheck,
  },
];
const NotificationCentreDefaultFilters = [false, true];
function useNotificationCentreModel({
  readIds: suppliedValue,
  defaultReadIds = [],
  onReadIdsChange,
  items = NotificationCentreDefaultItems,
  filters = NotificationCentreDefaultFilters,
  className,
  children,
  ...rootProps
}: NotificationCentreProps) {
  const [read, setRead] = useControllable<string[]>(
    suppliedValue,
    defaultReadIds,
    onReadIdsChange,
  );
  const [unreadOnly, setUnreadOnly] = React.useState(false);
  return {
    suppliedValue,
    defaultReadIds,
    onReadIdsChange,
    items,
    filters,
    className,
    children,
    rootProps,
    read,
    setRead,
    unreadOnly,
    setUnreadOnly,
  };
}
const NotificationCentreCompositionContext = React.createContext<ReturnType<
  typeof useNotificationCentreModel
> | null>(null);
function useNotificationCentreComposition() {
  const context = React.useContext(NotificationCentreCompositionContext);
  if (!context)
    throw new Error(
      "NotificationCentre parts must be inside NotificationCentre.",
    );
  return context;
}
export function NotificationCentre(props: NotificationCentreProps) {
  const model = useNotificationCentreModel(props);
  const { className, rootProps, children } = model;
  return (
    <NotificationCentreCompositionContext.Provider value={model}>
      <section
        {...rootProps}
        className={cn(
          "overflow-hidden rounded-xl border border-border",
          className,
        )}
      >
        {children !== undefined ? (
          children
        ) : (
          <>
            <NotificationCentreToolbar />
            <NotificationCentreFilters />
            <NotificationCentreMessages />
            <NotificationCentreEmpty />
          </>
        )}
      </section>
    </NotificationCentreCompositionContext.Provider>
  );
}

export function NotificationCentreHeader({
  className,
  ...props
}: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="notification-centre-header"
      className={cn(
        "flex flex-wrap items-center justify-between gap-3 p-5",
        className,
      )}
      {...props}
    />
  );
}
export function NotificationCentreTitle({
  className,
  ...props
}: React.ComponentProps<"h2">) {
  return (
    <h2
      data-slot="notification-centre-title"
      className={cn("text-lg font-semibold", className)}
      {...props}
    />
  );
}
export function NotificationCentreContent({
  className,
  ...props
}: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="notification-centre-content"
      className={cn("flex gap-5 border-b border-border px-5", className)}
      {...props}
    />
  );
}

export function NotificationCentreItem({
  className,
  ...props
}: React.ComponentProps<"button">) {
  return (
    <button
      type="button"
      data-slot="notification-centre-item"
      className={cn("border-b-2 pb-3 text-sm", className)}
      {...props}
    />
  );
}

export function NotificationCentreToolbar({
  children,
  ...props
}: Partial<React.ComponentProps<typeof NotificationCentreHeader>> & {
  children?: React.ReactNode;
}) {
  const { items, read, setRead } = useNotificationCentreComposition();
  return (
    <NotificationCentreHeader {...props}>
      {children === undefined ? (
        <>
          <NotificationCentreTitle>
            Inbox{" "}
            <span className="ml-1 text-sm text-muted-foreground">
              {items.filter((item) => !read.includes(item.id)).length}
            </span>
          </NotificationCentreTitle>
          <Button
            size="sm"
            variant="ghost"
            onClick={() => setRead(items.map((i) => i.id))}
          >
            <CheckCheck size={15} />
            Mark all read
          </Button>
        </>
      ) : (
        children
      )}
    </NotificationCentreHeader>
  );
}
export function NotificationCentreFilters({
  children,
  ...props
}: Partial<React.ComponentProps<typeof NotificationCentreContent>> & {
  children?: React.ReactNode;
}) {
  const { filters, unreadOnly, setUnreadOnly } =
    useNotificationCentreComposition();
  return (
    <NotificationCentreContent {...props}>
      {children === undefined
        ? filters.map((v) => (
            <NotificationCentreItem
              key={String(v)}
              onClick={() => setUnreadOnly(v)}
              className={cn(
                unreadOnly === v
                  ? "border-primary font-medium"
                  : "border-transparent text-muted-foreground",
              )}
            >
              {v ? "Unread" : "All activity"}
            </NotificationCentreItem>
          ))
        : children}
    </NotificationCentreContent>
  );
}
export function NotificationCentreMessages({
  children,
}: React.PropsWithChildren) {
  const { items, read, setRead, unreadOnly } =
    useNotificationCentreComposition();
  return children === undefined
    ? items
        .filter((i) => !unreadOnly || !read.includes(i.id))
        .map((i) => (
          <button
            key={i.id}
            onClick={() =>
              setRead((r) => (r.includes(i.id) ? r : [...r, i.id]))
            }
            className={cn(
              "flex w-full items-start gap-3 border-b border-border/60 p-5 text-left transition-colors last:border-0 hover:bg-muted/40",
              !read.includes(i.id) && "bg-primary/3",
            )}
          >
            <span className="grid size-9 shrink-0 place-items-center rounded-full border border-border bg-background">
              <i.icon size={16} />
            </span>
            <span className="min-w-0 flex-1">
              <span className="block text-sm font-medium">{i.title}</span>
              <span className="mt-1 block text-xs leading-relaxed text-muted-foreground">
                {i.detail}
              </span>
            </span>
            <span className="grid justify-items-end gap-2 text-xs text-muted-foreground">
              {i.time}
              {!read.includes(i.id) && (
                <span className="size-1.5 rounded-full bg-primary" />
              )}
              <span className="sr-only">
                {read.includes(i.id) ? "Read" : "Unread"}
              </span>
            </span>
          </button>
        ))
    : children;
}
export function NotificationCentreEmpty({ children }: React.PropsWithChildren) {
  const { items, read, unreadOnly } = useNotificationCentreComposition();
  return children === undefined
    ? unreadOnly && items.every((item) => read.includes(item.id)) && (
        <p className="p-10 text-center text-sm text-muted-foreground">
          You’re all caught up.
        </p>
      )
    : children;
}

registry/ui/use-controllable.ts

Scroll horizontally for long lines
"use client";
import { useRef, useState, type SetStateAction } from "react";
/** Controlled or instance-local state. Functional updates also work before a rerender. */
export function useControllable<T>(
  value: T | undefined,
  initial: T,
  onChange?: (value: T) => void,
) {
  const [local, setLocal] = useState(initial);
  const current = useRef(value === undefined ? local : value);
  current.current = value === undefined ? local : value;
  return [
    current.current,
    (action: SetStateAction<T>) => {
      const next =
        typeof action === "function"
          ? (action as (previous: T) => T)(current.current)
          : action;
      current.current = next;
      if (value === undefined) setLocal(next);
      onChange?.(next);
    },
  ] as const;
}

registry/ui/utils.ts

Scroll horizontally for long lines
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...values: ClassValue[]) {
  return twMerge(clsx(values));
}

registry/ui/button.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { Slot } from "radix-ui";
import { LoaderCircle } from "lucide-react";
import { cn } from "./utils";
export type ButtonProps = React.ComponentProps<"button"> & {
  variant?: "primary" | "secondary" | "outline" | "ghost" | "danger";
  size?: "sm" | "md" | "lg";
  loading?: boolean;
  asChild?: boolean;
};
export function Button({
  variant = "primary",
  size = "md",
  loading,
  asChild,
  className,
  children,
  disabled,
  ...props
}: ButtonProps) {
  const Comp = asChild ? Slot.Root : "button";
  return (
    <Comp
      type={asChild ? undefined : "button"}
      disabled={asChild ? undefined : disabled || loading}
      aria-disabled={disabled || loading || undefined}
      aria-busy={loading || undefined}
      className={cn(
        "relative inline-flex shrink-0 items-center justify-center gap-2 whitespace-nowrap rounded-lg border border-transparent text-sm font-medium leading-none transition-[background-color,border-color,box-shadow,transform] duration-150 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary active:scale-[.98] disabled:pointer-events-none disabled:opacity-45 aria-disabled:pointer-events-none [&_svg]:shrink-0",
        {
          "bg-primary text-primary-foreground shadow-[inset_0_1px_0_#ffffff26,0_1px_2px_#00000012] hover:bg-primary/90":
            variant === "primary",
          "border-border/60 bg-muted text-foreground hover:bg-muted/70":
            variant === "secondary",
          "border-border bg-background text-foreground shadow-sm hover:bg-muted/60":
            variant === "outline",
          "text-muted-foreground hover:bg-muted hover:text-foreground":
            variant === "ghost",
          "bg-danger text-danger-foreground hover:bg-danger/90":
            variant === "danger",
        },
        {
          "h-8 px-3 text-xs": size === "sm",
          "h-10 px-4": size === "md",
          "h-12 px-6": size === "lg",
        },
        className,
      )}
      {...props}
    >
      {asChild ? (
        children
      ) : (
        <>
          <span
            className={cn(
              "inline-flex min-w-0 flex-1 items-center justify-center gap-2",
              loading && "invisible",
            )}
          >
            {children}
          </span>
          {loading && (
            <LoaderCircle
              aria-hidden="true"
              className="absolute size-4 animate-spin motion-reduce:animate-none"
            />
          )}
        </>
      )}
    </Comp>
  );
}

Dependencies

lucide-react@0.577.0 · clsx@2.1.1 · tailwind-merge@3.5.0 · radix-ui@1.6.7

Accessibility & behaviour

Provide meaningful labels and preserve keyboard focus styles. Check contrast when customising theme colours.

Read the accessibility and performance guide →