Blocks Design System

Notification center

A bell in an app bar, beside the product’s sections and the account, with an unread count on the bell’s corner. It opens a Popover with Tabs for All, Mentions and Unread, notifications with an initials avatar, text, time and an unread dot, a Mark all as read button and a link to notification settings. Each tab has its own empty state.

Source · recipes/notification-center.tsx
recipes/notification-center.tsx
'use client'

import { Badge, Button, Link, Popover, Tabs, Text } from '@blocks/design-system'
import { Bell, BellOff, Check, Settings } from 'lucide-react'
import { useRef, useState } from 'react'
import styles from './notification-center.module.css'

interface Notification {
  id: string
  actor: string
  initials: string
  action: string
  target: string
  time: string
  mention: boolean
  unread: boolean
}

const initialNotifications: Notification[] = [
  { id: 'n1', actor: 'Maya Okafor', initials: 'MO', action: 'mentioned you in', target: 'Venue shortlist', time: '4 min ago', mention: true, unread: true },
  { id: 'n2', actor: 'Tomás Reyes', initials: 'TR', action: 'assigned you', target: 'Fix CSV export encoding', time: '22 min ago', mention: false, unread: true },
  { id: 'n3', actor: 'Priya Natarajan', initials: 'PN', action: 'replied to your comment on', target: 'Search filters spec', time: '1 h ago', mention: true, unread: true },
  { id: 'n4', actor: 'Jun Park', initials: 'JP', action: 'approved', target: 'Release 3.2.0', time: 'Yesterday', mention: false, unread: false },
  { id: 'n5', actor: 'Elena Vasquez', initials: 'EV', action: 'invited you to', target: 'Q1 planning', time: 'Nov 28', mention: false, unread: false },
]

const tabs = [
  { value: 'all', label: 'All', empty: 'No notifications yet.', show: () => true },
  { value: 'mentions', label: 'Mentions', empty: 'No one has mentioned you yet.', show: (item: Notification) => item.mention },
  { value: 'unread', label: 'Unread', empty: 'You’re all caught up.', show: (item: Notification) => item.unread },
]

// A bell in an app bar. The count lives in the button's accessible name, so
// the visual Badge is hidden from screen readers.
export default function NotificationCenter() {
  const [items, setItems] = useState(initialNotifications)
  const [tab, setTab] = useState('all')
  const tabList = useRef<HTMLDivElement>(null)
  const unread = items.filter((item) => item.unread).length

  function toggleRead(id: string) {
    // On Unread the row is about to leave the list, so focus moves to the tab first.
    if (tab === 'unread') tabList.current?.querySelector<HTMLElement>('[aria-selected="true"]')?.focus()
    setItems((current) => current.map((item) => (item.id === id ? { ...item, unread: !item.unread } : item)))
  }

  const markAllRead = () => setItems((current) => current.map((item) => ({ ...item, unread: false })))

  return (
    <header className={styles.bar}>
      <a href="#fieldnote-home" className={styles.brand}>
        <span className={styles.logo} aria-hidden>
          F
        </span>
        Fieldnote
      </a>
      <nav aria-label="Main" className={styles.nav}>
        <a href="#fieldnote-projects" aria-current="page">
          Projects
        </a>
        <a href="#fieldnote-docs">Docs</a>
        <a href="#fieldnote-reports">Reports</a>
      </nav>
      <Popover>
        <Popover.Trigger
          render={<Button type="button" variant="ghost" borderRadius="circle" dangerouslySetClassName={styles.bell} />}
          aria-label={unread > 0 ? `Notifications, ${unread} unread` : 'Notifications'}
        >
          <Bell aria-hidden size={18} />
          {unread > 0 && (
            <Badge tone="danger" variant="solid" aria-hidden dangerouslySetClassName={styles.count}>
              {unread}
            </Badge>
          )}
        </Popover.Trigger>
        <Popover.Content side="bottom" align="end" dangerouslySetClassName={styles.panel}>
          <div className={styles.head}>
            <Popover.Title>Notifications</Popover.Title>
            <Button type="button" variant="link" size="sm" disabled={unread === 0} onClick={markAllRead}>
              <Check aria-hidden size={14} />
              Mark all as read
            </Button>
          </div>
          <Tabs value={tab} onValueChange={setTab}>
            <Tabs.List ref={tabList} aria-label="Filter notifications" dangerouslySetClassName={styles.tabs}>
              {tabs.map((tab) => (
                <Tabs.Tab key={tab.value} value={tab.value}>
                  {tab.label}
                  {tab.value === 'unread' && unread > 0 && <Badge tone="accent">{unread}</Badge>}
                </Tabs.Tab>
              ))}
            </Tabs.List>
            {tabs.map((tab) => {
              const shown = items.filter(tab.show)
              return (
                <Tabs.Panel key={tab.value} value={tab.value} dangerouslySetClassName={styles.tabPanel}>
                  {shown.length === 0 ? (
                    <div className={styles.empty}>
                      <BellOff aria-hidden size={20} />
                      <Text fontSize="sm" color="text-muted">
                        {tab.empty}
                      </Text>
                    </div>
                  ) : (
                    <ul className={styles.list}>
                      {shown.map((item) => (
                        <li key={item.id} className={styles.item} data-unread={item.unread || undefined}>
                          <span className={styles.avatar} aria-hidden>
                            {item.initials}
                          </span>
                          <div className={styles.text}>
                            <Text fontSize="sm">
                              {item.unread && <span className={styles.srOnly}>Unread: </span>}
                              <strong>{item.actor}</strong> {item.action} <Link href={`#${item.id}`}>{item.target}</Link>
                            </Text>
                            <Text fontSize="xs" color="text-muted">
                              {item.time}
                            </Text>
                          </div>
                          <button
                            type="button"
                            className={styles.toggle}
                            aria-label={`Mark “${item.target}” as ${item.unread ? 'read' : 'unread'}`}
                            onClick={() => toggleRead(item.id)}
                          >
                            <span className={styles.dot} />
                          </button>
                        </li>
                      ))}
                    </ul>
                  )}
                </Tabs.Panel>
              )
            })}
          </Tabs>
          <div className={styles.foot}>
            <Link href="#notification-settings" fontSize="sm" dangerouslySetClassName={styles.settings}>
              <Settings aria-hidden size={14} />
              Notification settings
            </Link>
          </div>
        </Popover.Content>
      </Popover>
      <a href="#fieldnote-account" className={styles.me} aria-label="Your account, Ana Lima">
        AL
      </a>
    </header>
  )
}
Fieldnote notifications

How it works

  • The count is part of the bell’s accessible name (“Notifications, 3 unread”), so the Badge on the bell is aria-hidden rather than read twice.
  • The unread dot is a button: it marks an item read, or unread again, and its label says which. On the Unread tab a read item leaves the list, so focus goes back to the tab instead of being lost.
  • All three tabs filter one list held in state, so marking something read updates every tab, the count on the Unread tab and the bell at once.
  • On a phone the bar drops its section links with a container query and keeps the brand, the bell and the account.
  • The panel is Popover.Content with its padding moved inside, so the list rows and the settings footer can run edge to edge.

App-owned CSS

recipes/notification-center.module.css
.bar {
  container-type: inline-size;
  display: flex;
  align-items: center;
  gap: var(--ds-space-md);
  padding: var(--ds-space-sm) var(--ds-space-md) var(--ds-space-sm) var(--ds-space-lg);
  background-color: var(--ds-color-bg-subtle);
  border: var(--ds-border-width-sm) solid var(--ds-color-border-subtle);
  border-radius: var(--ds-border-radius-xl);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: var(--ds-space-md);
  font-weight: var(--ds-font-weight-semibold);
  color: var(--ds-color-text);
  text-decoration: none;
}

.logo {
  display: grid;
  place-items: center;
  inline-size: var(--ds-control-height-sm);
  block-size: var(--ds-control-height-sm);
  font-size: var(--ds-font-size-sm);
  font-weight: var(--ds-font-weight-bold);
  color: var(--ds-color-accent-fg);
  background-color: var(--ds-color-accent);
  border-radius: var(--ds-border-radius-md);
}

/* The nav takes the free space, which pushes the bell and avatar to the end */
.nav {
  display: flex;
  flex: 1;
  gap: var(--ds-space-xs);
  margin-left: var(--ds-space-md);

  & a {
    padding: var(--ds-space-sm) var(--ds-space-md);
    font-size: var(--ds-font-size-sm);
    color: var(--ds-color-text-muted);
    text-decoration: none;
    border-radius: var(--ds-border-radius-md);

    &:hover {
      color: var(--ds-color-text);
      background-color: var(--ds-color-surface-hover);
    }

    &[aria-current='page'] {
      font-weight: var(--ds-font-weight-medium);
      color: var(--ds-color-text);
      background-color: var(--ds-color-surface);
    }
  }
}

/* On a phone the bar keeps the brand, the bell and the account */
@container (width < 30rem) {
  .nav {
    display: none;
  }

  .brand {
    margin-right: auto;
  }
}

.brand,
.nav a,
.me {
  &:focus-visible {
    outline: var(--ds-border-width-md) solid var(--ds-color-focus-ring);
    outline-offset: var(--ds-space-xs);
  }
}

.me {
  display: grid;
  place-items: center;
  inline-size: var(--ds-control-height-md);
  block-size: var(--ds-control-height-md);
  font-size: var(--ds-font-size-xs);
  font-weight: var(--ds-font-weight-semibold);
  color: var(--ds-color-text);
  text-decoration: none;
  background-color: var(--ds-color-surface-active);
  border-radius: var(--ds-border-radius-circle);
}

.bell {
  position: relative;
  inline-size: var(--ds-control-height-lg);
  min-block-size: var(--ds-control-height-lg);
  padding: 0;
}

/* The count sits on the button's corner, clear of the bell, ringed in the bar's color */
.count {
  position: absolute;
  top: 0;
  right: 0;
  translate: 25% -25%;
  min-inline-size: var(--ds-space-lg);
  justify-content: center;
  padding: 0 var(--ds-space-sm);
  font-size: var(--ds-font-size-xxs);
  box-shadow: 0 0 0 var(--ds-border-width-md) var(--ds-color-bg-subtle);
}

.panel {
  display: grid;
  gap: var(--ds-space-md);
  inline-size: 24rem;
  padding: var(--ds-space-lg) 0 0;
}

.head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--ds-space-md);
  padding: 0 var(--ds-space-lg);
}

.tabs {
  padding: 0 var(--ds-space-md);
}

.tabPanel {
  max-block-size: 20rem;
  overflow-y: auto;
  outline: none;
}

.list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.item {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: start;
  gap: var(--ds-space-md);
  padding: var(--ds-space-md) var(--ds-space-md) var(--ds-space-md) var(--ds-space-lg);

  & + & {
    border-top: var(--ds-border-width-sm) solid var(--ds-color-border-subtle);
  }

  &[data-unread] {
    background-color: color-mix(in srgb, var(--ds-color-accent-subtle) 45%, transparent);
  }
}

.avatar {
  display: grid;
  place-items: center;
  inline-size: var(--ds-control-height-md);
  block-size: var(--ds-control-height-md);
  font-size: var(--ds-font-size-xs);
  font-weight: var(--ds-font-weight-semibold);
  color: var(--ds-color-accent-text);
  background-color: var(--ds-color-accent-subtle);
  border-radius: var(--ds-border-radius-circle);
}

.text {
  display: grid;
  gap: var(--ds-space-xs);

  & strong {
    font-weight: var(--ds-font-weight-semibold);
  }
}

/* The unread dot is the toggle: filled when unread, a ring on hover when read */
.toggle {
  display: grid;
  place-items: center;
  inline-size: var(--ds-control-height-sm);
  block-size: var(--ds-control-height-sm);
  padding: 0;
  background: transparent;
  border: none;
  border-radius: var(--ds-border-radius-circle);
  cursor: pointer;

  &:hover {
    background-color: var(--ds-color-surface-hover);
  }

  &:focus-visible {
    outline: var(--ds-border-width-md) solid var(--ds-color-focus-ring);
  }
}

.dot {
  inline-size: var(--ds-space-md);
  block-size: var(--ds-space-md);
  border: var(--ds-border-width-md) solid transparent;
  border-radius: var(--ds-border-radius-circle);

  .item[data-unread] & {
    background-color: var(--ds-color-accent);
  }

  .item:not([data-unread]) .toggle:is(:hover, :focus-visible) & {
    border-color: var(--ds-color-border-strong);
  }
}

.empty {
  display: grid;
  justify-items: center;
  gap: var(--ds-space-sm);
  padding: var(--ds-space-xl) var(--ds-space-lg);
  text-align: center;
  color: var(--ds-color-text-muted);
}

.foot {
  padding: var(--ds-space-md) var(--ds-space-lg);
  background-color: var(--ds-color-bg-subtle);
  border-top: var(--ds-border-width-sm) solid var(--ds-color-border-subtle);
}

.settings {
  display: inline-flex;
  align-items: center;
  gap: var(--ds-space-sm);
}

.srOnly {
  position: absolute;
  inline-size: var(--ds-space-xxs);
  block-size: var(--ds-space-xxs);
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}