Command palette

@yasinsensoy/web

Web · Components

NotificationCenter

Notification center: a bell button, an unread count badge and a dropdown notification list.

9 exports

Live example

Preview

App

Code

import { NotificationCenter } from "@yasinsensoy/web";

const notification = useNotifications(initialNotifications);

<NotificationCenter {...notification.props}
  footer={<a href="/notifications">View all</a>} />

Events

  1. Events appear here when you interact with the preview.

Import

import { NotificationCenter, useNotifications } from "@yasinsensoy/web";

NotificationCenter

componentClient component

Notification center: a bell button, an unread count badge and a dropdown notification list. For the app's top bar.

  • List: grouped by day (Today, Yesterday…), times written relatively ("3 min ago"); unread ones are highlighted with a dot. Rows have an icon or avatar, action buttons ("Accept") and removal with ×.
  • Tabs: All / Unread or your own categories.
  • Read: on press, on panel close or manually (readOn); "Mark all as read" is always available.
  • Live: when a new notification arrives the bell shakes slightly and screen readers say "New notification: …". It doesn't shake with the reduced motion preference.
  • Paged: with onLoadMore + hasMore, the next page at the end of the list.
  • Mobile: the panel becomes a bottom sheet; the back button and pulling down close it. With inline it is embedded in the page.

If you don't want to keep the state yourself, see useNotifications.

NotificationCenter(props: NotificationCenterProps): JSX.Element

Examples

const notifications = useNotifications(initialNotifications);
<NotificationCenter {...notifications.props} footer={<Link href="/notifications">See all</Link>} />

// With a server
<NotificationCenter items={list} onRead={(ids) => markRead(ids)} onLoadMore={next} hasMore={hasMore} />

Ready-made state for NotificationCenter: add, mark as read, remove. If you need to save to a server, wire up the component's events yourself; this hook only keeps client state.

useNotifications(initial?: readonly NotificationItem[]): UseNotificationsResult

Examples

const notifications = useNotifications(initialNotifications);
useEffect(() => channel.on("notification", (n) => notifications.add(n)), []);
<NotificationCenter {...notifications.props} />

An action button on a NotificationItem ("Accept", "Decline").

NameTypeDefaultDescription
labelReactNodeButton text.
onClick() => unknownCalled on press. If it returns a Promise, the button shows loading until it settles.
primary?booleanfalseThe primary action is filled, the others outlined.

A notification in the NotificationCenter.

NameTypeDefaultDescription
idstringUnique id.
titleReactNodeTitle ("Ayşe commented").
description?ReactNodeDetails; at most two lines are shown.
dateDate | string | numberTime of the notification; written relatively like "3 min ago" and grouped by day.
read?booleanfalseHas it been read? Unread ones are highlighted with a dot.
icon?IconPropLeading icon (when there is no avatar).
tone?Tone"primary"Color of the icon.
avatar?string | ReactNodeAvatar on the left: an image URL or an element (e.g. <Avatar … />).
href?stringURL to go to on press; in-site URLs open with UIProvider link.
actions?readonly NotificationAction[]Action buttons under the row. See NotificationAction.
category?stringCategory for splitting into tabs (e.g. "mention", "system"). See NotificationCenterProps.tabs.

A tab at the top of the NotificationCenter.

NameTypeDescription
valuestringKey of the tab: "all", "unread" or a NotificationItem.category.
labelReactNodeTab text.

When does NotificationCenter count notifications as read?

type NotificationReadMode = /** On pressing the notification (default). */ "click" /** When the panel closes, all shown notifications (the badge resets like on social networks). */ | "close" /** Only with "Mark all as read" and the button on the row. */ | "manual"

Classes that can be passed to the inner parts of NotificationCenter.

NameTypeDescription
trigger?stringBell button.
badge?stringUnread count badge.
panel?stringThe dropdown panel (or the inline box).
item?stringNotification row.
group?stringDay title.

Props of the NotificationCenter component.

NameTypeDefaultDescription
itemsreadonly NotificationItem[]Notifications. See NotificationItem. They don't need to come sorted; they are ordered newest first.
onRead?(ids: string[]) => voidCalled with the ids when notifications are read.
onReadAll?() => void"Mark all as read". If omitted, onRead is called with all unread ones.
onRemove?(id: string) => voidWhen removed with × on the row; if omitted, × isn't shown.
onClear?() => void"Clear all"; if omitted, the button isn't shown.
onSelect?(item: NotificationItem) => voidCalled when a notification is pressed (before navigating).
readOn?NotificationReadMode"click"When it counts as read. See NotificationReadMode.
tabs?boolean | readonly NotificationTab[]trueTabs at the top. true: "All" and "Unread"; an array: your own tabs (value is a category or "all"/"unread"); false: none.
groupByDay?booleantrueGroup by day ("Today", "Yesterday", "Monday, November 3")?
title?ReactNode"Bildirimler"` (`messages.notifications`)Panel title.
actions?ReactNodeExtra content next to the title (e.g. a settings link).
footer?ReactNodeFooter of the panel (e.g. a "See all" link).
empty?ReactNodeContent shown when there are no notifications.
onLoadMore?() => unknownLoads the next page; called when the end of the list is reached.
hasMore?booleanfalseAre there more notifications to load?
loading?booleanIs the first load in progress?
inline?booleanfalseA list embedded in the page instead of a panel (e.g. a /notifications page). The bell button isn't rendered.
trigger?ReactElementOpen button used instead of the bell.
icon?IconPropbellBell icon.
badgeTone?Tone"danger"Badge color.
announce?booleantrueShake the bell and announce to screen readers when a new notification arrives?
side?Placement"bottom-right"Position of the panel. See Placement.
open?booleanWhether the panel is open when controlled.
defaultOpen?booleanfalseIs it initially open when uncontrolled?
onOpenChange?(open: boolean) => voidWhen the panel opens or closes.
maxHeight?string"min(32rem, 70dvh)"Maximum list height.
className?stringClasses added to the container.
classNames?NotificationCenterClassNamesClasses added to the inner parts.
effect?EffectPropEffect or effects of the notification rows (e.g. "ripple", "border"). If omitted, UIProvider appearance.defaults.notificationCenter.effect. See BuiltinEffect.

Value returned by the useNotifications hook.

NameTypeDescription
itemsNotificationItem[]Notifications, newest to oldest.
unreadCountnumberNumber of unread notifications.
add(item: Omit<NotificationItem, "id" | "date"> & Partial<Pick<NotificationItem, "id" | "date">>) => stringAdds a new notification (at the top); id is generated if omitted, date defaults to now.
markRead(ids: readonly string[]) => voidMarks the given notifications as read.
markAllRead() => voidMarks all as read.
remove(id: string) => voidDeletes a notification.
clear() => voidRemoves all.
setItems(items: NotificationItem[]) => voidReplaces the list (e.g. the first page from the server).
props{ items: NotificationItem[]; onRead: (ids: string[]) => void; onReadAll: () => void; onRemove: (id: string) => void; onClear: () => void; }Props to spread onto NotificationCenter: <NotificationCenter {...notifications.props} />.