Skip to content
Nacre
Color palette
中文

Sidebar

stableSidebar · SidebarGroup · SidebarItem · SidebarAccount · FocusRegionOpen in playground
Sidebar
Runs the real component in your browser.

The app’s main navigation: Sidebar is the full-height column (256 expanded / a 52 icon rail collapsed, resizable from the right edge), SidebarGroup groups items and can collapse, SidebarItem is a navigation item, and SidebarAccount is the account entry at the bottom whose account menu grows out of it in place when pressed.

Status: stable · Components: Sidebar, SidebarGroup, SidebarItem, SidebarAccount, FocusRegion

import { Sidebar, SidebarGroup, SidebarItem, SidebarAccount, FocusRegion } from 'nacre'

Mark the current page with active; counts go in trailing (on the icon’s top right when collapsed). The sidebar draws no fill, letting the window material show through.

import { View, signal } from 'nsh'
import { Badge, Sidebar, SidebarGroup, SidebarItem } from 'nacre'
const pages = () => [
{ key: 'dashboard', icon: 'dashboard', label: 'Dashboard' },
{ key: 'chat', icon: 'chat', label: 'Chat' },
{ key: 'history', icon: 'history', label: 'Chat history' },
] as const
export function SidebarUsage() {
const current = signal('chat')
return (
<View class="h-[240px] rounded-xl border border-border overflow-hidden">
<Sidebar collapsed={false}>
<SidebarGroup title="Workbench">
{pages().map((page) => (
<SidebarItem
icon={page.icon}
label={page.label}
active={current() === page.key}
trailing={page.key === 'chat' ? <Badge count={3} /> : undefined}
onPress={() => current.set(page.key)}
/>
))}
</SidebarGroup>
</Sidebar>
</View>
)
}

Call animateLayout(“spatial”) before changing collapsed; give onResize to drag the right edge to resize (200–320, double-click returns to 256), and onCollapsedChange to collapse by dragging it narrow. Collapsed, each item shows a tooltip on hover.

import { View, signal } from 'nsh'
import { Badge, Button, SIDEBAR_WIDTH, Sidebar, SidebarGroup, SidebarItem, animateLayout } from 'nacre'
const pages = () => [
{ key: 'dashboard', icon: 'dashboard', label: 'Dashboard' },
{ key: 'chat', icon: 'chat', label: 'Chat' },
{ key: 'history', icon: 'history', label: 'Chat history' },
] as const
export function SidebarCollapse() {
const current = signal('chat')
const collapsed = signal(false)
const width = signal(SIDEBAR_WIDTH)
const toggle = () => {
animateLayout('spatial')
collapsed.update((v) => !v)
}
return (
<View class="flex-row flex-wrap items-start gap-4">
<View class="h-[240px] rounded-xl border border-border overflow-hidden">
<Sidebar
collapsed={collapsed()}
width={width()}
onResize={(next) => width.set(next)}
onCollapsedChange={(next) => collapsed.set(next)}
resizeLabel="Resize sidebar"
>
<SidebarGroup title="Workbench" collapsed={collapsed()}>
{pages().map((page) => (
<SidebarItem
icon={page.icon}
label={page.label}
collapsed={collapsed()}
active={current() === page.key}
trailing={page.key === 'chat' ? <Badge testID="demo-collapse-badge" count={3} /> : undefined}
onPress={() => current.set(page.key)}
/>
))}
</SidebarGroup>
</Sidebar>
</View>
<Button testID="demo-collapse-toggle" size="md" variant="outline" icon="sidebar-left" onPress={toggle}>
{collapsed() ? 'Expand sidebar' : 'Collapse sidebar'}
</Button>
</View>
)
}

collapsible turns the group title into a disclosure button; open / onOpenChange are controlled, and expandLabel / collapseLabel give the button’s screen reader name. When the sidebar is collapsed, every item in the group shows.

import { View, signal } from 'nsh'
import { Sidebar, SidebarGroup, SidebarItem } from 'nacre'
export function SidebarGroups() {
const current = signal('profile')
const settingsOpen = signal(true)
return (
<View class="h-[280px] rounded-xl border border-border overflow-hidden">
<Sidebar collapsed={false}>
<SidebarGroup title="Workbench">
<SidebarItem icon="chat" label="Chat" active={current() === 'chat'} onPress={() => current.set('chat')} />
</SidebarGroup>
<SidebarGroup
title="Personal settings"
collapsible
open={settingsOpen()}
onOpenChange={(open) => settingsOpen.set(open)}
expandLabel="Expand personal settings"
collapseLabel="Collapse personal settings"
>
<SidebarItem icon="user" label="Profile" active={current() === 'profile'} onPress={() => current.set('profile')} />
<SidebarItem icon="bell" label="Notifications" active={current() === 'notify'} onPress={() => current.set('notify')} />
</SidebarGroup>
</Sidebar>
</View>
)
}

SidebarAccount goes at the very bottom of the sidebar: the avatar carries the presence status, and the line under the name shows the status and its duration; pressing it grows the account menu, for switching status and signing out.

import { View, signal } from 'nsh'
import { Avatar, PresenceDot, SIDEBAR_WIDTH, Sidebar, SidebarAccount } from 'nacre'
export function SidebarAccountMenu() {
const open = signal(false)
const away = signal(false)
return (
<View class="h-[160px] rounded-xl border border-border overflow-hidden">
<Sidebar collapsed={false}>
<View class="flex-1" />
{/* 账号菜单按侧栏贴窗口左下定位:放在应用侧栏的最底部 */}
<SidebarAccount
testID="demo-account"
menuTestID="demo-account-menu"
closeTestID="demo-account-close"
accessibilityLabel="Account actions"
tooltip="Lin Jiayi"
avatar={() => <Avatar name="Lin Jiayi" self size={28} badge={<PresenceDot ring kind={away() ? 'offline' : 'online'} label={away() ? 'Suspended' : 'Online'} />} />}
name="Lin Jiayi"
status={away() ? 'Suspended · Not taking new chats' : 'Online · 01:24:08'}
collapsed={false}
width={SIDEBAR_WIDTH}
open={open()}
onPress={() => open.set(true)}
onClose={() => open.set(false)}
items={[
{ label: 'Online', presence: 'online', checked: !away(), onSelect: () => away.set(false) },
{ label: 'Suspended', presence: 'offline', checked: away(), onSelect: () => away.set(true) },
{ type: 'separator' },
{ label: 'Sign out', icon: 'logout', destructive: true, onSelect: () => {} },
]}
/>
</Sidebar>
</View>
)
}
Key Action
F6 / Shift+F6 Cycle through the page’s FocusRegion boundaries in either direction. Each has a unique id and an order. Use entry={() => item ? () => item.focus() : null} to focus the current destination or skip an empty region.
Tab Move between navigation items, collapsible group titles and the account entry
Space / Return Open a navigation item; toggle a group; open the account menu
↑ ↓ / Home / End With focus on a navigation item, move to the previous / next item or the first / last in display order (stopping at the ends, skipping collapsed groups); with arrowActivates the item opens at once (the gallery), otherwise focus moves and Return opens it. Arrow keys are not the sidebar’s while focus is elsewhere
→(画廊) Enter the selected page. At the reading pane, Left returns to navigation; on a component page, Right again enters the first example. Tabs, fields and menus keep their own arrow keys.
↑ ↓ / Home / End(菜单) With the account menu open, move between menu items, wrapping at the ends
Esc Close the account menu and return focus to the account entry
  • SidebarItem is a button whose screen reader name is label, and the current item reads as selected; collapsed to an icon, label is also the hover tooltip.
  • A collapsible group title is a button whose open state reads as expanded, with its screen reader name from expandLabel / collapseLabel (the library has no built-in text).
  • SidebarAccount requires accessibilityLabel (“Account actions”), and the entry reads as expanded / collapsed; the menu is role=“menu”.
  • resizeLabel gives the resize handle’s screen reader name.

Do

  • Mark the current item with active (solid fill + brand text color)
  • Give each item a tooltip when collapsed (label becomes the tooltip automatically)
  • Call animateLayout(“spatial”) before toggling
  • Put the account entry at the very bottom of the sidebar

Don’t

  • Bold the current item (its width jumps)
  • Put business content or filters in the sidebar
  • Mix sizes between group titles and navigation items

Property descriptions follow the Chinese source comments.

Prop Type Default Description
collapsed * boolean —
width number — 展开宽度,默认 256(reui SIDEBAR_WIDTH)
onResize (width: number) => void — 给了就能拖右缘调宽(200–320),双击右缘回到 256;拖动中每步回调
onCollapsedChange (collapsed: boolean) => void — 拖到 140 以下收起、收起后往外拖过 140 展开;不给就只能调宽、不能拖收
resizeLabel string — 拖动条的无障碍名(库不碰 i18n)
header JSX.Element — 顶部一段(标题栏高):访达式整列通顶,红绿灯与开合钮放这里;由调用方给,含窗口拖动
testID string —
children JSX.Element —

* required

Prop Type Default Description
title string —
collapsed boolean — 侧栏收成图标栏:不显示组标题,组内项照常显示
collapsible boolean — 组标题可点开合(网页版导航面板的分支)
open boolean — 受控开合;不给就用内部状态,默认展开
defaultOpen boolean true
onOpenChange (open: boolean) => void —
expandLabel string — 开合按钮的无障碍名与 tooltip,由调用方给文案(库不碰 i18n)
collapseLabel string —
testID string —
children JSX.Element —

* required

Prop Type Default Description
icon * IconName —
label * string —
active boolean —
expanded boolean — 作为弹层触发器时的展开态
collapsed boolean —
trailing JSX.Element — 右侧计数或在线点;收起时叠在图标右上角
size 'sm' | 'default' | 'lg' 'default' sm / default / lg:桌面 28 / 36 / 48,手机 44 / 48 / 56
onPress () => void —
arrowActivates boolean — ↑↓ 走到这一项时直接打开它(onPress),像 Finder 侧栏;不给时只移动焦点,回车才打开。 打开要拉数据的导航(会话页、看板)别开:按住方向键扫过会连发请求
onEnterContent () => void — 双栏浏览:焦点在这一项时按 →,进入它对应的正文;没给时不接管 →
tabOut { next?: () => void; previous?: () => void } — 整组只算一个 Tab 停点:焦点在任一项上时 Tab 交给 next、Shift+Tab 交给 previous,不再逐项走完。 NSH 没有 tabIndex=-1(FINDINGS 76),各项仍可聚焦,这里在键盘层面做成 roving 的效果。
navGroup string — 方向键在哪一组项之间走(同一个侧栏的项给同一个名字),默认 sidebar
buttonRef (node: ViewRef) => void — 拿到这一项的按钮(焦点区域的入口:F6 进侧栏时落在当前页那一项)
testID string —

* required

Prop Type Default Description
avatar * (inMenu: boolean) => JSX.Element — 28 的头像(含状态点);inMenu 为真时画在菜单底行,状态点不要再挂 testID(同一时刻会有两份)
name * string —
status * string — 名字下一行:状态 · 时长,或连接中断的提示
statusTone 'muted' | 'warning' —
collapsed * boolean —
width * number — 侧栏展开宽:展开时账号菜单与侧栏同宽
open * boolean — 账号菜单开着
onPress * () => void —
onClose * () => void —
items * readonly GrowMenuEntry[] —
accessibilityLabel * string — 读屏名(「账号操作」),触发行与菜单底行共用
tooltip string — 收起成 36 圆钮时的悬停提示;展开时不显示(名字就在旁边)
testID string —
menuTestID string —
closeTestID string — 菜单底行
statusTestID string — 状态那一行文字

* required

Prop Type Default Description
id * string — 区域的身份(同一窗口里唯一)
order * number — F6 的先后(小的在前)
entry * FocusEntry — 进区域时焦点落在哪(当前页的导航项、选中的分段、第一个示例的页签)
class string —
testID string —
children * JSX.Element —

* required