通知センターNotificationCenterExperimental

通知の一覧と管理用の UI です。

プレビュー

状態とバリエーション

未読あり

未読がある場合はトリガーにバッジを出し、行を強調します。

すべて既読

未読がない場合はトリガーのバッジと一括既読操作を出しません。

空状態

通知がない場合も、空であることをポップオーバー内で伝えます。

プロパティ

表は横にスクロールできます
プロパティ初期値説明
notificationsNotification[]-表示する通知データです。
labelsNotificationCenterLabels-トリガー、見出し、空状態、操作名の文言を差し替えます。
onMarkAsRead(id: string) => void-未読通知を既読にする操作で呼び出します。
onLinkClick(id: string) => void-通知行をクリックしたときに呼び出します。
onClearAll() => void-未読通知をまとめて既読にする操作で呼び出します。

使い方

import { NotificationCenter, type Notification, type NotificationCenterLabels } from "@gunjo/ui"
import { useState } from "react"

const labels: NotificationCenterLabels = {
  toggle: "通知を開く",
  title: "通知",
  clearAll: "すべて既読",
  emptyTitle: "通知はありません",
  markAsRead: "既読にする",
  viewHistory: "通知履歴をすべて見る",
}

const initialNotifications: Notification[] = [
  {
    id: "project-approved",
    title: "レビューが承認されました",
    description: "デザインシステムの更新レビューが承認されました。",
    timestamp: "5分前",
    read: false,
  },
  {
    id: "comment",
    title: "新しいコメント",
    description: "Hikaby がプルリクエストにコメントしました。",
    timestamp: "1時間前",
    read: false,
  },
  {
    id: "deploy",
    title: "デプロイ完了",
    description: "本番デプロイ #1234 が完了しました。",
    timestamp: "昨日",
    read: true,
  },
]

export function Notifications() {
  const [notifications, setNotifications] = useState(initialNotifications)

  return (
    <NotificationCenter
      notifications={notifications}
      labels={labels}
      onMarkAsRead={(id) =>
        setNotifications((current) =>
          current.map((notification) =>
            notification.id === id ? { ...notification, read: true } : notification
          )
        )
      }
      onClearAll={() =>
        setNotifications((current) =>
          current.map((notification) => ({ ...notification, read: true }))
        )
      }
    />
  )
}

設計の判断

  • 未読は数ではなく点で出す。資料は「未読バッジは 9+ や 99+ で上限を設ける」を挙げています。GUNJO は数を出すのをやめ、ベルの右上に小さな点を1つ置くだけにしました。上限をいくつにするかを部品が決めずに済み、1件でも100件でも「まだ見ていないものがある」という同じ意味だけを伝えます。件数を見せたい画面は、渡した notifications を呼ぶ側で数えます。
  • 通知が無いときの画面を部品が持つ。0件のとき、薄いベルのアイコンと emptyTitle の一文が出ます。空のときの表示を呼ぶ側が書き忘れて、開いたら真っ白、が起きません。
  • 一括既読と設定への導線は、口だけ用意した。onClearAll を渡したときだけ、しかも未読があるときだけ「すべて既読」が出ます。押した先で何が起きるかは呼ぶ側が決めます。資料が挙げる時系列のグループ化(今日・今週・それ以前)は、GUNJO の NotificationCenter にはまだありません。並び順は渡した配列のままです。

使用コンポーネント

いつ・なぜ使うか(UIXHERO)

「いつ・なぜ使うか」の判断は、姉妹サイト UIXHERO の記事で解説しています。