メインコンテンツまでスキップ

Notificationsブロック

Notifications は、MUI 上に構築された通知パネルです。

インストール

npm install @nodeblocks/frontend-notifications-block

必要なもの

項目用途
isNotificationsOpenポップオーバーパネルが開いているかどうかを制御する
setIsNotificationsOpenパネルを閉じる(例: 外側クリック時)
notificationsListパネルにレンダリングする通知項目の配列
onClickベルボタンのクリックを処理する(通常は開閉状態を切り替える)
unreadCount (任意)ベルボタンのバッジ件数を制御する
heading (任意)リストの上に表示するパネル見出し
emptyStateMessage (任意)リストが空のときに表示する文言
制御された開閉状態

Notifications はパネルの開閉状態を所有しません。アプリ側で isNotificationsOpen を保持し、setIsNotificationsOpen を渡してポップオーバーが正しく閉じられるようにしてください。

notificationsList の各項目は次をサポートします:

フィールド必須説明
titlestringはい通知見出し
createdAtstringはい相対時間表示に使用する ISO 日付文字列
textReactNodeいいえ任意の本文
hrefstringいいえ設定すると、項目がリンクとしてレンダリングされる
iconPartial<AvatarProps> & { isLoading?: boolean }いいえアバター画像、イニシャル、または読み込みプレースホルダー

コード例

ライブエディター
function Example() {
  const [isNotificationsOpen, setIsNotificationsOpen] = React.useState(false);
  const [unreadCount, setUnreadCount] = React.useState(10);

  const notificationsList = [
    {
      title: 'メールアドレスを更新しました',
      text: 'メールアドレスが正常に更新されました。',
      createdAt: new Date().toISOString(),
      href: '/settings',
    },
    {
      icon: {children: 'G'},
      title: '注文を発送しました',
      text: '注文 #2847 を発送しました。',
      href: '/orders',
      createdAt: new Date(Date.now() - 86400000).toISOString(),
    },
  ];

  return (
    <Notifications
      isNotificationsOpen={isNotificationsOpen}
      setIsNotificationsOpen={setIsNotificationsOpen}
      notificationsList={notificationsList}
      unreadCount={unreadCount}
      onClick={() => {
        setUnreadCount(0);
        setIsNotificationsOpen(!isNotificationsOpen);
      }}
    />
  );
}
結果
Loading...

主要プロパティ

コアプロパティ

プロパティ必須デフォルト説明
isNotificationsOpenbooleanはい-通知ポップオーバーが開いているかどうか
setIsNotificationsOpen(open: boolean) => voidはい-開閉状態を更新する。外側クリックで閉じるために必要
onClickReact.MouseEventHandler<HTMLButtonElement>はい-ベルボタンのクリックハンドラー。通常は開閉状態を切り替える
notificationsListNotificationItem[]はい-パネルにレンダリングする項目(上記の形状を参照)
unreadCountnumberいいえundefinedベルボタンのバッジ件数。50 を超える値は 50+ と表示される

コンテンツプロパティ

プロパティ必須デフォルト説明
headingstringいいえ'Notifications'リストの上に表示するパネル見出し
emptyStateMessagestringいいえ'No notifications'リストが空のときに表示するメッセージ
isLoadingbooleanいいえundefinedtrue のとき、リスト項目の代わりにローダーを表示する

レイアウトと構成プロパティ

プロパティ必須デフォルト説明
anchorElHTMLElement | nullいいえ内部状態ポップオーバーの位置決め用アンカー要素
setAnchorEl(el: HTMLElement | null) => voidいいえ内部 setterポップオーバーのアンカー要素を更新する
childrenBlocksOverride | ReactNodeいいえundefined複合サブコンポーネント、またはブロックオーバーライド関数
classNamestringいいえundefinedルートコンテナのクラス名(nbb-notifications も適用されます)
sxSxPropsいいえundefinedルート Stack 用の MUI システムスタイル

NotificationsStackPropschildren を除く)を継承するため、ルートでは標準のレイアウトプロパティをサポートします。デフォルトのブロック順序は buttonlist です。listItemloaderNotifications.List 内で内部的に使用されるデフォルトブロックです。

サブコンポーネントはコンテキストから値を読み取り、MUI プロパティの一部を受け取れます:

  • Notifications.ButtonIconButton のプロパティ。デフォルトは BadgeNotificationsOutlined をラップ
  • Notifications.ListStackProps に加え、任意の headingemptyStateMessageisLoadingformatRelativeDateText
  • Notifications.ListItemStackProps に加え、titlecreatedAt、任意の texthreficonformatRelativeDateText
  • Notifications.LoaderStackProps。デフォルトは CircularProgress

formatRelativeDateText のデフォルトは、date-fns による ISO 日付の解析と日本語ロケールを使用し、今日作成された通知には 今日 を返します。日付が別の形式またはロケールを使用する場合は、Notifications.List または Notifications.ListItem にカスタムフォーマッターを渡してください。

デフォルト UI ブロック

ブロックベース備考
Notifications (ルート)Stack rendered as asideトリガーボタンとポップオーバーリスト用のインラインラッパー
Notifications.ButtonIconButton + Badge未読件数バッジ付きのベルアイコン
Notifications.ListPopover + Typography見出し、区切り線、空の状態、読み込み状態を備えたスクロール可能なパネル
Notifications.ListItemStack + Avatar + Typographyhref が設定されている場合はアンカーとしてレンダリングされる
Notifications.LoaderStack + CircularProgressisLoadingtrue のときに表示される中央配置のスピナー

TypeScript

import {Notifications} from '@nodeblocks/frontend-notifications-block';
import type {ComponentProps} from 'react';

type NotificationsProps = ComponentProps<typeof Notifications>;

type NotificationItem = NotificationsProps['notificationsList'][number];

function ActivityNotifications({items, unreadCount}: {items: NotificationItem[]; unreadCount: number}) {
const [isOpen, setIsOpen] = React.useState(false);

return (
<Notifications
isNotificationsOpen={isOpen}
setIsNotificationsOpen={setIsOpen}
notificationsList={items}
heading="アクティビティ"
unreadCount={unreadCount}
onClick={() => setIsOpen(prev => !prev)}
/>
);
}