Skip to content
Nacre
Color palette
中文

MenuContent

experimentalMenuContentOpen in playground
MenuContent
Runs the real component in your browser.

Embeddable menu content: a column of menu rows (items, group headings, separators, checkable items) to put inside a Popover. Rows, marks and checks are the same as GrowMenu’s, with the arrow keys, Home / End and typeahead built in; with closeOnSelect={false} choosing keeps the menu open, for menus such as appearance and accent color where people try several in a row.

Status: experimental · Components: MenuContent

import { MenuContent } from 'nacre'

Put it in a Popover (flush: MenuContent has its own inset). onClose sets the Popover’s open to false and the Popover returns focus to the trigger. Items can carry an icon, a shortcut, a disabled or destructive state; group headings are written explicitly as type: “heading”.

import { Text, View, signal } from 'nsh'
import { IconButton, MenuContent, Popover, TEXT_SIZE } from 'nacre'
export function MenuContentUsage() {
const open = signal(false)
const last = signal('')
return (
<View class="flex-row items-center gap-3">
<Popover
open={open()}
onOpenChange={(next) => open.set(next)}
flush
trigger={({ open, ref }) => (
<IconButton testID="demo-menuc-trigger" icon="more" label="More actions" pressed={open()} expanded={open()} tooltip={open() ? null : undefined} viewRef={ref} />
)}
>
<MenuContent
testID="demo-menuc"
width={220}
onClose={() => open.set(false)}
onSelect={(item) => last.set(item.label ?? '')}
entries={[
{ type: 'heading', label: 'Chat' },
{ label: 'Mark as pending', icon: 'history', shortcut: '⌘P' },
{ label: 'Transfer', icon: 'reply' },
{ label: 'Export history', icon: 'download', disabled: true },
{ type: 'separator' },
{ label: 'End chat', icon: 'close', destructive: true },
]}
/>
</Popover>
<Text testID="demo-menuc-last" class={`${TEXT_SIZE.small} text-muted-foreground`}>{`$Last chosen: ${last() || '—'}`}</Text>
</View>
)
}

closeOnSelect={false}: choosing only moves the check and the menu stays open so people can try several; Escape, a press outside or the trigger closes it. The check sits at the end of the row; the start shows an icon or a color swatch.

Phone: closeOnSelect={false}: choosing an item only moves the check and the menu stays open for more; tap outside or the trigger again to close it.

import { signal, token } from 'nsh'
import { IconButton, MenuContent, Popover } from 'nacre'
const APPEARANCES = [
{ value: 'system', icon: 'monitor', label: () => 'System' },
{ value: 'light', icon: 'sun', label: () => 'Light' },
{ value: 'dark', icon: 'moon', label: () => 'Dark' },
] as const
export function MenuContentCheckable() {
const open = signal(false)
const appearance = signal<string>('system')
const skin = signal<string>('teal')
const icon = () => APPEARANCES.find((a) => a.value === appearance())?.icon ?? 'monitor'
return (
<Popover
open={open()}
onOpenChange={(next) => open.set(next)}
align="start"
flush
trigger={({ open, ref }) => (
<IconButton testID="demo-menuc-theme-trigger" icon={icon()} label="Appearance" pressed={open()} expanded={open()} tooltip={open() ? null : undefined} viewRef={ref} />
)}
>
<MenuContent
testID="demo-menuc-theme"
width={220}
closeOnSelect={false}
entries={[
...APPEARANCES.map((a) => ({ label: a.label(), icon: a.icon, checked: appearance() === a.value, onSelect: () => appearance.set(a.value) })),
{ type: 'separator' },
{ type: 'heading', label: 'Accent' },
{ label: 'Longbridge teal', swatch: token('brand') as string, checked: skin() === 'teal', onSelect: () => skin.set('teal') },
{ label: 'Neutral', swatch: token('foreground') as string, checked: skin() === 'neutral', onSelect: () => skin.set('neutral') },
]}
/>
</Popover>
)
}

Give items a value and MenuContent a value (an array for several): matching items are checked. It closes after a choice by default.

import { signal } from 'nsh'
import { IconButton, MenuContent, Popover } from 'nacre'
export function MenuContentValue() {
const open = signal(false)
const sort = signal('latest')
return (
<Popover
open={open()}
onOpenChange={(next) => open.set(next)}
align="start"
flush
trigger={({ open, ref }) => (
<IconButton testID="demo-menuc-sort-trigger" icon="filter" label="Sort" pressed={open()} expanded={open()} tooltip={open() ? null : undefined} viewRef={ref} />
)}
>
<MenuContent
testID="demo-menuc-sort"
width={200}
value={sort()}
onSelect={(item) => item.value && sort.set(item.value)}
onClose={() => open.set(false)}
entries={[
{ label: 'Newest first', value: 'latest' },
{ label: 'Longest wait first', value: 'waiting' },
{ label: 'Unread first', value: 'unread' },
]}
/>
</Popover>
)
}
Key Action
↑ ↓ Move between choosable rows, wrapping at the ends; with no current row Down goes to the first and Up to the last
Home / End Go to the first / last row
字母 / 数字 Jump to the next row whose label starts with it (keep typing to extend the prefix)
Space / Return Choose the current row; closes afterwards when closeOnSelect is true
Esc Close (the surrounding Popover handles it) and return focus to the trigger
  • The block’s role is menu; plain rows read as menuitem, checkable rows as menuitemcheckbox with their checked state.
  • The pointer and arrow keys move a single soft brand-tinted fill, with no inset ring (the current item is itself the focus indicator). The checked item keeps its checkmark and destructive actions keep their color.
  • While mounted it owns the arrow and letter keys: use it only inside a floating surface, never laid out on a page.

Do

  • For a custom menu row, import MENU_CURRENT_ROW and MENU_CURRENT_TEXT from the package entry point
  • Put it in a Popover with flush
  • Use closeOnSelect={false} for settings people try in a row (appearance, accent color)
  • Set heading explicitly for group titles
  • Put destructive items last, in red

Don’t

  • Lay it out on a page as a list (use ListRow / lists)
  • A plain column of actions from a button (use DropdownMenu)
  • Picking one form value from fixed options (use SelectMenu / Combobox)
  • Context menus (use the system menu)

Property descriptions follow the Chinese source comments.

Prop Type Default Description
entries * readonly MenuContentEntry[] — 项:普通项、组标题、分隔线、勾选项
onSelect (item: MenuContentItem) => void — 选了一项(项自己的 onSelect 先跑)
closeOnSelect boolean true 选完要不要收起,默认 true:选完调 onClose。外观、主题色这类「连着试几个」的菜单传 false,选了只换勾、菜单留着, Esc、点外面、再点触发器才收起(由外面的 Popover 负责)
onClose () => void — 选完收起(closeOnSelect 为 true 时):一般是把外面 Popover 的 open 设成 false,焦点由 Popover 交回触发器
value string | readonly string[] — 勾选值:单选给一个值,多选给数组;项的 value 命中就打勾
width number — 宽度(pt);不给时随外面的面
testID string — 菜单的 testID;行是 ${testID}-item-${序号}(序号按 entries 里滤掉假值后的位置数,分隔线、组标题也占号)
accessibilityLabel string — 读屏对这个菜单的称呼

* required