Two ways to supply rows
The items array is the short path. A row is a title, an optional body, an optional time and a read flag, which covers most of what a product's bell renders.
The default slot is the escape hatch, for rows that carry an avatar, a diff, a pair of buttons or anything else the array cannot describe. Compose them from NbNotificationCenterItem, which is the same row the array path renders, so a product that outgrows items keeps its geometry, its read/unread treatment and its place in the keyboard order.
<template>
<NbNotificationCenter :unread-count="unread" @mark-all-read="markAll()">
<NbNotificationCenterItem
v-for="event in events"
:key="event.id"
:read="event.seen"
:time="event.createdAt"
:href="`/deploys/${event.deployId}`"
variant="warning"
>
<template #media>
<img class="avatar" :src="event.actor.avatarUrl" alt="" />
</template>
<template #title>
<strong>{{ event.actor.name }}</strong> rolled back
{{ event.service }}
</template>
Rollback finished in {{ event.duration }}s.
<template #actions>
<NbButton size="sm" variant="ghost" @click="mute(event)">Mute</NbButton>
</template>
</NbNotificationCenterItem>
</NbNotificationCenter>
</template>Two numbers move to you when you use the slot, and both for the same reason: the centre renders your slot, it does not read it, so it has no array to count.
unreadCount, or the badge and the trigger's accessible name both say zero.itemCount, or the centre cannot tell an empty feed from a slot you chose not to fill.
itemCount is the one that decides which of the four answers the panel gives, so leaving it out has consequences worth stating plainly rather than burying:
With itemCount | Without it |
|---|---|
itemCount: 0 renders #empty. | #empty never renders. The centre has no way to know the feed is empty. |
error with rows shows the stale strip over them. | error always resolves to the full role="alert" error region. |
The second row is a deliberate choice and not an accident: with the count unknown, assuming the slot has rows would let a failed fetch render as content, which is the exact defect this component was built to end. So a failure wins an unknown count. A dev-mode warning is printed the first time a panel opens with a filled default slot and no itemCount.
<NbNotificationCenter
:unread-count="feed.unread"
:item-count="feed.rows.length"
:loading="feed.pending"
:error="feed.failed"
>
<NbNotificationCenterItem v-for="row in feed.rows" :key="row.id" ... />
</NbNotificationCenter>loading and error are props and stay props: they are never inferred from your markup.
Slotted rows still take part in the keyboard model and still emit select. A row that carries the data-nb-notification-item attribute and is a control is picked up by the roving focus and, when activated, emits select with a null item and the original event. Null because the centre never saw an item object for that row: you already have the object in your own v-for, so bind your handler there when you want it typed, and use the centre's event when you only need "a row was activated" (closing the panel, logging, marking seen). NbNotificationCenterItem sets the attribute for you, on the rows that are controls. What counts as one is the next section, and it is a short list.
Rows you press, and rows you only read
A row is a control or it is text. There is no third thing, and the difference is not a style.
NbNotificationCenterItem renders a <button> by default, an <a> when you give it href, and a plain element when you say :interactive="false". That plain element is inert: no tabindex, no place in the arrow-key order, no select, no pointer cursor. Set it when the row is an announcement rather than a destination, and the reader is told nothing that turns out to be untrue.
<template>
<!-- Pressable rows, each going somewhere. -->
<NbNotificationCenter :items="items" @select="go($event)" />
<!-- A feed with nowhere to go: rows are read, not pressed. -->
<NbNotificationCenter :items="events" :interactive="false" />
<!-- Mixed, per row. The item wins over the centre. -->
<NbNotificationCenter
:items="[
{ id: 1, title: 'Deploy finished', href: '/deploys/91' },
{ id: 2, title: 'Quota reset for September', interactive: false },
]"
/>
</template>An inert row is inert in the accessibility tree, in the keyboard order and in the event contract, all three, or it is not inert at all. A <div> that keeps a tabindex, takes focus from the roving index and emits select on click, Enter and Space is a focusable, activatable widget with no role, no name and no value: WCAG 4.1.2 Name, Role, Value fails outright, and a screen reader announces a blank stop in the middle of a list.
An inert list is still a list you can reach
Take every control out of a scrolling box and you have made a region a keyboard cannot enter, which means content below the fold that a keyboard user cannot read. So when the list holds nothing focusable, the list itself becomes the tab stop: Tab reaches it, the arrow keys and Page Down scroll it, and it takes its place in the panel's tab cycle. One activatable row anywhere in the list and the extra stop disappears again, because the arrows already reach the bottom through the rows.
What the centre will not do for a slotted row
In the default slot the markup is yours, so the attribute is an opt-in and the centre checks what it was put on. It accepts a <button> that is not disabled, an <a href>, and any element carrying role="button", "link", "menuitem" or "option". Anything else keeps the attribute and gets nothing: no tabindex, no select, and a console warning in development saying which element and what to do about it.
<!-- Picked up: a control by construction. -->
<li><button data-nb-notification-item>Deploy finished</button></li>
<!-- Picked up: you supplied the role, so you own the key handling too,
and the centre supplies Enter and Space on top of it. -->
<li>
<div role="button" tabindex="0" data-nb-notification-item>Deploy finished</div>
</li>
<!-- Ignored, and warned about: nothing here is a control. -->
<li><div data-nb-notification-item>Deploy finished</div></li>The last case is not pedantry. Handing that <div> a tabindex is the same defect as the one above, only authored one repository further out, where nobody in this library would ever see it.
The count
The badge is capped, the accessible name is not. A 15px circle has room for 99+; a screen reader has room for the truth, so the button announces "Notifications, 142 unread" while the badge reads 99+. The badge itself is aria-hidden, because otherwise the same number is announced twice, once truthfully and once capped.
<template>
<!-- Counted from the array: every item where read is not true. -->
<NbNotificationCenter :items="items" />
<!-- Or stated outright, for a paged feed whose server knows a total the
loaded page does not. -->
<NbNotificationCenter :items="page" :unread-count="meta.unread" />
</template>formatUnread builds the unread half of that name. The default is English and counts in the two forms English has. Replace it with your t() call in any product that ships another language, because Portuguese, Polish and Arabic do not pluralise on n === 1.
<NbNotificationCenter :format-unread="(n) => t('notifications.unread', n)" />Mark all as read
The centre emits mark-all-read and does nothing else. It never mutates items: your list is your data, and a component that quietly flipped flags on it would be lying to the store that owns them. Until you push a new array back, the rows stay exactly as they were, which is also what makes an optimistic update and a failed request distinguishable.
The control disables itself at zero unread rather than disappearing, so the panel does not change shape as you use it.
A mark-all that hits the network is not instant, so say so with markAllPending. While it is true the control shows a spinner and markAllPendingLabel, swallows further clicks, and announces itself once from a role="status" region. It is aria-disabled, not disabled, on purpose: the user pressed that button, so it has focus, and adding a disabled attribute to a focused button drops focus to <body> for the length of the request. Without this prop, a slow server gives no feedback and the user sends four identical requests.
<template>
<NbNotificationCenter
:items="items"
:mark-all-pending="marking"
@mark-all-read="markAll()"
/>
</template>
<script setup lang="ts">
const marking = ref(false)
async function markAll() {
marking.value = true
try {
items.value = await api.markAllRead()
} catch (error) {
toast.error('Could not mark everything as read')
} finally {
marking.value = false
}
}
</script>An optimistic update is the other legitimate answer: push the read flags into items at once, leave markAllPending false, and put them back if the request fails. What is not an answer is doing neither.
The label is Mark all as read, and it is fixed rather than a prop, because a bell in two products that disagree on the wording is exactly the drift this component exists to end.
The four answers: loading, failed, empty, and a list
A panel that has been asked for notifications can be in exactly one of four states, and they must never share an element. A single .empty-text for both loading and empty makes a failed fetch read as "no contacts yet". All four are props here, none is an override of another's slot, and the component picks between them in one place so no future edit can make two of them true at once.
| State | You set | The panel renders |
|---|---|---|
| Loading | loading | Three NbSkeleton rows, aria-busy, one announcement. |
| Failed | error, and no rows | NbEmptyState kind="error" in a role="alert" region, with a retry. |
| Empty | neither, and no rows | NbEmptyState with emptyTitle and emptyDescription. |
| List | rows | The scrolling list. |
loading wins over everything: a request that has not come back is not yet a failure, and it is certainly not a result. The empty state cannot appear while either loading or error is true, which is the whole point.
"Rows" means the length of items, or itemCount when you supply rows through the default slot. Fill the slot without itemCount and the count is unknown, which is a third answer and not a zero: the panel then treats error as the failure state (never as the stale strip) and cannot reach the empty state at all. Two ways to supply rows has the table.
Failure is a state, not a flavour of empty
Give the panel error and it stops guessing. It says the request failed, it offers retryLabel, and it emits retry when the user takes it up; the component fetches nothing, so retry is the whole of its part in the recovery.
<script setup lang="ts">
const { data, pending, error, refresh } = useNotifications()
</script>
<template>
<NbNotificationCenter
:items="data"
:loading="pending"
:error="Boolean(error)"
error-title="Could not load notifications"
error-description="The activity service did not answer."
@retry="refresh()"
/>
</template>Note what does not happen: no offer to create the first notification, no "you're all caught up". NbEmptyState's kind="error" exists precisely so a view that could not load never invites you to make something.
A refresh that fails over rows you already have
If the panel already has rows and a background refresh fails, throwing those rows away to show a full-panel error would lose the notifications the user was reading. So it keeps them and says so above them, in a role="status" strip rather than a role="alert" one: nothing was lost, nothing is urgent, the list is just older than it looks.
If your product's failure needs more than a title, a sentence and a retry, #error replaces the whole region and still receives the retry callback so your own control keeps the same event contract.
<template #error="{ retry }">
<NbEmptyState kind="error" size="sm" title="Activity is offline">
<template #actions>
<NbButton size="sm" @click="retry()">Try again</NbButton>
<NbButton size="sm" variant="ghost" href="/status">Status page</NbButton>
</template>
</NbEmptyState>
</template>Replacing the bell
The #trigger slot swaps the button for anything you like, and it hands you the four things you cannot work out for yourself:
| Slot prop | What it is for |
|---|---|
triggerProps | id, aria-haspopup="dialog", aria-expanded, aria-controls and a default aria-label carrying the uncapped count. Spread it. |
setTriggerRef | Template ref setter. This is how Escape knows where to send focus back to. |
panelId | The teleported panel's id, if you would rather wire aria-controls yourself. |
toggle, show, close, open, unreadCount, badge | Behaviour and state, so your trigger can look like anything. |
<template #trigger="{ triggerProps, setTriggerRef, toggle, unreadCount }">
<NbButton
:ref="setTriggerRef"
v-bind="triggerProps"
variant="ghost"
size="sm"
icon="bell"
@click="toggle"
>
Activity<template v-if="unreadCount"> ({{ unreadCount }})</template>
</NbButton>
</template>aria-label comes last in triggerProps, so bind your own after the spread to replace it. Everything else should stay: a trigger without aria-expanded does not tell a screen reader whether the panel is open, and one without setTriggerRef leaves the panel unable to give focus back, which is a WCAG 2.4.3 Focus Order failure, not a cosmetic one. If you skip setTriggerRef anyway, the panel falls back to the first focusable element inside its own root, which in practice is your trigger. That is a safety net, not the contract.
Adding to the header
#header-start sits beside the mark-all control and costs you nothing:
<template>
<NbNotificationCenter :items="items" @mark-all-read="markAll">
<template #header-start>
<NbSelect
v-model="filter"
variant="fluid"
label="Filter"
name="notification-filter"
:options="filters"
/>
</template>
</NbNotificationCenter>
</template>#header-actions is the other one, and it is a trade: it replaces the mark-all control entirely, so you own the pending rules described in Mark all as read. You do not have to re-derive them. The slot hands you markAllProps, which is everything the built-in button binds including onClick, and markAllText, which is the label that swaps while a request is in flight:
<template #header-actions="{ markAllProps, markAllText }">
<NbButton size="sm" variant="ghost" v-bind="markAllProps">
{{ markAllText }}
</NbButton>
</template>Reach for #header-actions when the mark-all control itself has to change. Reach for #header-start for everything else.
Placement
align picks which trigger edge the panel lines up with (end for a bell at the right of a topbar, start for one in a left rail), width and maxHeight size it. The panel clamps itself inside the viewport, shortens its list when there is not enough room, and flips above the trigger only when below is genuinely cramped, so the same bell does not open in a different direction on every scroll position. It sits at --nb-zindex-menu, the same layer as the other two panels.
Only the list scrolls. The header and the footer stay put, so Mark all as read and your "See all activity" link cannot be pushed off the bottom of a long feed.
<template>
<NbNotificationCenter
:items="items"
align="start"
:width="320"
:max-height="320"
>
<template #footer="{ close }">
<NbButton
size="sm"
variant="ghost"
@click="
close()
go('/activity')
"
>
See all activity
</NbButton>
</template>
</NbNotificationCenter>
</template>In a sidebar
A bell at the foot of a sidebar should open beside the sidebar, not above its own row. Opening above lays the panel over the sidebar's items, and in an expanded sidebar the trigger is a full-width row, so the panel covers most of the navigation it came out of. Set side="right":
<NbNotificationCenter :items="items" side="right" align="end" />With side="right", align picks the edge on the other axis. end lines the panel's bottom up with the trigger's bottom, the way NbUserMenu opens at right-end, and pins it there, so a short feed sits against its trigger rather than floating above it. start lines the tops up. The panel is placed past the trigger's right edge, which for a full-width sidebar row means past the sidebar. When the viewport has no room for it on the right, it falls back to opening vertically, exactly as without the prop.
The default stays vertical, so every existing bell opens exactly where it did before.
On a phone
On a phone (below 672px, or a touch phone held sideways) a 360px popover off a 32px bell is most of the screen and none of its height, so the panel becomes a full-screen sheet instead. width, align and side are ignored there. The sheet sits inside the safe areas, its header controls are at least 44px tall, only the feed scrolls, and it carries its own close button, named by closeLabel (Close notifications), because nothing outside it is left to tap. It does not close when its trigger scrolls out of view, since it is no longer attached to it. Desktops are unchanged.
Leaving the page
A bell lives in a shell that outlives the view under it, so its panel can survive things it should not survive. Two of those are handled here:
- Navigation. The panel closes on a history navigation (back, forward, a hash change) and on anything the browser reports through the Navigation API, which is how a
pushStaterouter change is seen where the browser supports it. SetcloseOnNavigatetofalseif you want it to stay. In a router that predates that API, the reliable belt-and-braces is one line in your shell: watch the route and callclose({ restoreFocus: false })on the exposed instance. Pass the option: a bareclose()restores focus when the panel happened to have it, and on a route change that is a focus jump into the old page's shell that the user did not ask for. - A trigger that scrolls away. The panel is
position: fixed, so once the bell it hangs off has left the viewport the panel is floating over content it has nothing to do with. When that happens it closes rather than pinning itself to an edge. Focus is left where it is, because by then the user is somewhere else.
<template>
<NbNotificationCenter ref="bell" :items="items" />
</template>
<script setup lang="ts">
const bell = ref()
const route = useRoute()
watch(
() => route.fullPath,
() => bell.value?.close({ restoreFocus: false }),
)
</script>NbNotificationCenter
Props
| Prop | Type | Default | Description |
|---|---|---|---|
items | INotificationItem[] | [] | The rows. Omit and use the default slot for richer content. |
unreadCount | number | counted from items | Authoritative unread total. Required when you supply rows through the slot. |
itemCount | number | items.length, or unknown with a filled slot | How many rows are on screen. Required alongside unreadCount when you supply rows through the slot: without it #empty cannot render and error always resolves to the full error region. |
open | boolean | undefined | Optional v-model:open. Left undefined, the centre owns its own open state. |
label | string | 'Notifications' | Names the trigger and titles the panel. |
markAllLabel | string | 'Mark all as read' | Label of the mark-all control. |
markAllPending | boolean | false | The host's mark-all request is in flight: spinner, pending label, further clicks ignored, focus kept. |
markAllPendingLabel | string | 'Marking all as read' | Shown and announced while markAllPending is true. Present tense. |
emptyTitle | string | 'No notifications' | Title of the built-in empty state. |
emptyDescription | string | 'New activity shows up here.' | Body of the built-in empty state. |
loading | boolean | false | Renders placeholder rows. Suppresses the empty state. |
loadingLabel | string | 'Loading notifications' | Announced from the role="status" region while loading. |
error | boolean | false | The request failed. A state of its own: never rendered as emptiness. |
errorTitle | string | 'Could not load notifications' | Title of the built-in error state. |
errorDescription | string | 'The request failed. Nothing has been lost.' | Body of the built-in error state. |
errorStaleLabel | string | 'These may be out of date.' | Shown on the strip when a refresh fails over rows already loaded. |
retryLabel | string | 'Try again' | Label of the retry control in both failure presentations. |
showRetry | boolean | true | Offer a retry control. Turn it off when you retry on your own and a button would be a lie. |
maxCount | number | 99 | Counts above this render as n+ on the badge only. |
width | number | 360 | Panel width in px. Ignored on a phone, where the panel is a sheet. |
closeLabel | string | catalogue ('Close notifications') | Accessible name of the close button the phone sheet carries. |
maxHeight | number | 384 | Tallest the scrolling list may get, in px. Clamped further on a short viewport. |
align | 'start' | 'end' | 'end' | Which trigger edge the panel lines up with. |
side | 'vertical' | 'right' | 'vertical' | Where the panel opens. vertical opens below the trigger and flips above when below is cramped. right opens beside it, for a bell in a sidebar, and falls back to vertical when there is no room. |
showMarkAll | boolean | true | Renders the mark-all control. |
interactive | boolean | true | Default activatability of rows. false makes them inert: no tabindex, no select. Per-item wins. |
closeOnSelect | boolean | true | Closes the panel when a row is activated. |
closeOnNavigate | boolean | true | Closes on a history navigation, a hash change, or a Navigation API navigate event. |
disabled | boolean | false | The trigger does not open. |
icon | string | 'bell' | Phosphor icon for the trigger and the empty state. |
locale | string | runtime locale | BCP 47 tag for relative times. |
unreadLabel | string | 'Unread' | Visually hidden state word. |
readLabel | string | 'Read' | Visually hidden state word. |
formatUnread | (count: number) => string | English, two forms | Builds the unread half of the trigger's accessible name: "1 unread", "4 unread". |
INotificationItem
| Field | Type | Description |
|---|---|---|
id | string | number | Required. Stable key across refetches. |
title | string | Required. One line, no trailing full stop. |
body | string | Supporting sentence. Clipped at three lines. |
time | string | number | Date | ISO string, epoch ms or a Date. Rendered relative to now. |
timeLabel | string | Pre-formatted time. Wins over time for display. |
read | boolean | Anything not true counts as unread. |
variant | 'neutral' | 'info' | 'success' | 'warning' | 'error' | Colours the leading icon. |
icon | string | null | Overrides the variant icon. null removes the icon gutter. |
href | string | Renders the row as a link. |
interactive | boolean | Overrides the centre's interactive for this row. false is inert. |
Events
| Event | Payload | Description |
|---|---|---|
select | (item: INotificationItem | null, event: Event) | A control row was activated. The item is null for a row you rendered into the default slot, because the centre never saw an object for it. Inert rows never fire it. |
mark-all-read | none | The user asked for everything to be marked read. Nothing changes until you say so. |
retry | none | The user asked for a failed request to be tried again. The centre fetches nothing, so this is the whole of its part in it. |
open | none | The panel opened. |
close | none | The panel closed, by any route. |
update:open | boolean | Fired on every open and close, controlled or not. |
Slots
| Slot | Props | Description |
|---|---|---|
default | { close, items } | Replaces the generated rows. Render NbNotificationCenterItems inside a list, and pass unreadCount and itemCount with it: the centre renders this slot, it does not read it. |
trigger | { triggerProps, setTriggerRef, panelId, open, toggle, show, close, unreadCount, badge } | Replaces the bell entirely. Spread triggerProps and bind setTriggerRef, or the panel loses its ARIA relationship and its focus return. See Replacing the bell. |
header-start | { close, unreadCount } | Adds to the header, beside the mark-all control. Where a filter field goes. |
header-actions | { close, unreadCount, markAllRead, markAllPending, markAllProps, markAllText } | Replaces the mark-all control. Spread markAllProps onto your own button to keep the pending rules. See Adding to the header. |
empty | none | Replaces the empty state. Empty only: failure has its own slot. |
error | { retry } | Replaces the failure state. retry() emits retry for you. |
footer | { close } | Fixed strip below the scrolling list. |
Exposed
| Member | Type | Description |
|---|---|---|
show() | () => void | Opens the panel. Named show because open is already a prop. |
close() | (options?: { restoreFocus?: boolean }) => void | Closes it. Focus returns to the trigger when the panel had it; pass { restoreFocus: false } to suppress that, or true to force it. |
toggle() | () => void | Toggles it. |
isOpen | ComputedRef<boolean> | Current state. |
unreadCount | ComputedRef<number> | The resolved count, capped by nothing. |
NbNotificationCenterItem
One row. Usable on its own inside the default slot, and what the items array renders.
variant takes the library's status words: neutral, info, success, warning, error. error, not danger, because that is what NbToast, NbMessage and NbBanner say, and the whole point of raising a toast and letting the same event land here is that you do not translate a status word at the boundary. danger is accepted and treated as error so an existing danger call does not render an unstyled row, but it is deprecated and will not be documented anywhere else. The colour behind error is --nb-c-danger, which is what this library calls that token. The API word and the token name disagree on purpose, and neither is renamed to please the other.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
title | string | '' | Headline. The #title slot overrides it. |
body | string | '' | Supporting sentence. The default slot overrides it. |
time | string | number | Date | none | Rendered as a relative <time> with a machine-readable value. |
timeLabel | string | none | Pre-formatted time. Wins over time for display. |
read | boolean | false | Drops the dot, the tint and the heavier title. |
variant | 'neutral' | 'info' | 'success' | 'warning' | 'error' | 'neutral' | Colours the leading icon. |
icon | string | null | per variant | null removes the icon gutter. |
href | string | none | Renders the row as a link. Wins over interactive. |
interactive | boolean | true | false renders an inert row: no tabindex, no select, no cursor. |
locale | string | runtime | BCP 47 tag for the relative time. |
unreadLabel | string | 'Unread' | Visually hidden state word. |
readLabel | string | 'Read' | Visually hidden state word. |
Events
| Event | Payload | Description |
|---|---|---|
select | Event | The row was activated. Only fires on a control row: with interactive: false and no href there is nothing to activate. |
Slots
| Slot | Description |
|---|---|
default | The body, in place of the body prop. |
title | The headline, in place of the title prop. |
media | Leading element, in place of the variant icon. An avatar goes here. |
actions | Per-row controls. Rendered outside the activatable row, never inside it. |