Command palette

@yasinsensoy/native

React Native · Components

NotificationCenter

Notification center (same API as web): a bell button, an unread count badge and the notification list it opens.

9 exports

Live example

The preview runs in the browser with the web component; the native component takes the same props and looks the same on iOS and Android.

Preview

App

Code

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

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/native";

Notification center (same API as web): a bell button, an unread count badge and the notification list it opens.

  • List: grouped by day (Today, Yesterday…), times written relatively; unread ones dotted and highlighted. An icon or avatar, action buttons, deleting with ×. Long lists scroll in a virtual list.
  • Tabs: All / Unread or your own categories.
  • Read: on press, when the panel closes, or manually (readOn).
  • Live: when a new notification arrives the bell shakes and screen readers announce it (no shake with reduced motion).
  • Mobile: on a phone the panel is a bottom sheet; pulling down and the back button close it. Embedded in the screen with inline.

If you do not want to hold the state yourself, see useNotifications.

NotificationCenter(props: NotificationCenterProps): JSX.Element

Examples

const notifications = useNotifications(initialNotifications);
<NotificationCenter {...notifications.props} />

Ready-made state for NotificationCenter: add, mark as read, delete. If you need to save to a server, bind the component's events yourself; this hook only holds 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"). NotificationAction on web; on native that name is the action of device notifications.

NameTypeDefaultDescription
labelReactNodeButton text.
onPress() => unknownCalled when pressed. If it returns a Promise the button shows loading until it finishes.
primary?booleanfalseThe primary action is filled, others are outlined.

A notification in the NotificationCenter (same fields as on web).

NameTypeDefaultDescription
idstringUnique id.
titleReactNodeTitle ("Jane commented").
description?ReactNodeDetails; at most two lines are visible.
dateDate | string | numberTime of the notification; written relatively like "3 min ago", grouped by day.
read?booleanfalseWhether it is read. Unread ones are highlighted and dotted.
icon?IconPropLeading icon (when there is no avatar).
tone?Tone"primary"Icon color.
avatar?string | ReactNodeAvatar on the left: an image address or an element (e.g. <Avatar … />).
href?stringAddress to open when pressed (openLink: in-app paths navigate).
actions?readonly NotificationItemAction[]Action buttons below the row.
category?stringCategory for splitting into tabs (e.g. "mention", "system").

A tab on top of the NotificationCenter.

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

When the NotificationCenter counts notifications as read (same as web).

type NotificationReadMode = /** When a notification is pressed (default). */ "click" /** When the panel closes, all shown notifications. */ | "close" /** Only with "Mark all as read" and the button on the row. */ | "manual"

Styles of the inner parts of NotificationCenter.

NameTypeDescription
trigger?ViewStyleBell button.
badge?ViewStyleUnread count badge.
panel?ViewStyleThe opened panel (or the inline box).
item?ViewStyleNotification row.
group?ViewStyleDay heading.

Props of the NotificationCenter component (same names as on web).

NameTypeDefaultDescription
itemsreadonly NotificationItem[]Notifications; they need not be 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 deleted with × on a row; if omitted × is not shown.
onClear?() => void"Clear all"; if omitted the button is not shown.
onSelect?(item: NotificationItem) => voidCalled when a notification is pressed (before navigating).
readOn?NotificationReadMode"click"When it counts as read.
tabs?boolean | readonly NotificationTab[]trueTabs on top: true All/Unread, an array your own tabs, false none.
groupByDay?booleantrueGroup by day ("Today", "Yesterday")?
title?ReactNodethe `notifications` textPanel title.
actions?ReactNodeExtra content next to the title (e.g. a settings button).
footer?ReactNodeBottom of the panel (e.g. "See all").
empty?ReactNodeContent shown when there are no notifications.
onLoadMore?() => unknownLoads the next page; called when reaching the end of the list.
hasMore?booleanfalseWhether there are more notifications to load.
loading?booleanWhether the initial load is in progress.
inline?booleanA list embedded in the screen instead of a panel (e.g. a notifications screen). No bell is drawn.
trigger?ReactElementOpen button to use 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"Panel position (on a tablet); on a phone it opens from the bottom.
open?booleanWhether the panel is open in controlled use.
defaultOpen?booleanfalseOpen initially in uncontrolled use?
onOpenChange?(open: boolean) => voidWhen the panel opens or closes.
maxHeight?numberList height limit (dp). If omitted, as much as fits the screen (at most 512).
style?ViewStyleContainer style.
styles?NotificationCenterStylesStyles of the inner parts.
effect?EffectPropEffect of the rows (e.g. "ripple"). Defaults to appearance.defaults.notificationCenter.effect.
testID?stringTest id (bell button).

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); an id is generated if not given, 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() => voidDeletes 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} />.