元素

Menu

菜单是由触发元素打开的一组操作。同一个结构适用于每种设备,并按各自的习惯呈现:

el-menu.tsx
el-menu.tsx运行中
import { type JSX, mount, signal, Text, View } from "nsh";
import { ContextMenu, Menu, MenuItem, MenuSeparator, type OpenChangeReason, Submenu } from "nsh/base";
import { button, buttonText, desktop, Inspector, MenuMark, menuIndicator, Panel, PopIn, type PopupSide, Stage, tint, ui, wide } from "./palette";

/** A sheet at the bottom on phones; on desktops the popup's box keeps no
 * look, its `Items` have it. */
const surface = () =>
  desktop ? undefined : { ...ui().card, borderTopLeftRadius: 20, borderTopRightRadius: 20, paddingHorizontal: 10, paddingTop: 10, paddingBottom: 30 };

/** A menu's items: on desktops on a surface that grows out of the trigger's
 * side each time it opens (`PopIn`), on phones in the sheet that slides up. */
function Items(props: { side?: PopupSide; children: JSX.Element }) {
  if (!desktop) return props.children;
  return (
    <PopIn side={props.side} style={{ ...ui().card, borderRadius: 12, padding: 5, minWidth: 210 }}>
      {props.children}
    </PopIn>
  );
}

const backdrop = { backgroundColor: "rgba(0,0,0,0.45)" };
const rowHeight = () => (desktop ? 32 : 46);
const row = () => ({ flexDirection: "row" as const, alignItems: "center" as const, gap: 8, height: rowHeight(), paddingHorizontal: 10, borderRadius: 8 });

/**
 * One row. A checkable row (`checked`) leads with its mark: a check for a
 * setting, a dot for one choice of a group (`radio`), which fades in as the
 * app checks the item; rows of the same menu keep the mark's room so their
 * labels line up (`inset`).
 */
function Item(props: { label: string; onSelect?: () => void; checked?: boolean; radio?: boolean; inset?: boolean; disabled?: boolean; keepOpen?: boolean; danger?: boolean }) {
  return (
    <MenuItem
      onSelect={props.onSelect}
      disabled={props.disabled}
      checked={props.checked}
      role={props.radio ? "menuitemradio" : undefined}
      indicatorClass={menuIndicator}
      indicator={props.checked === undefined ? undefined : <MenuMark kind={props.radio ? "dot" : "check"} tint={ui().accent} />}
      closeOnSelect={!props.keepOpen}
      hoverStyle={{ backgroundColor: tint(ui().accent, 0.08), borderColor: tint(ui().accent, 0.24) }}
      activeStyle={{ backgroundColor: tint(ui().accent, 0.14), borderColor: tint(ui().accent, 0.34) }}
      disabledStyle={{ opacity: 0.35 }}
      style={{ ...row(), borderWidth: 1, borderColor: "transparent" }}
    >
      <If when={props.inset}>
        <View style={{ width: 16 }} />
      </If>
      <Text style={{ numberOfLines: 1, flexGrow: 1, flexShrink: 0, fontSize: 14, color: props.danger ? ui().down : ui().text }}>{props.label}</Text>
    </MenuItem>
  );
}

function Separator() {
  return <MenuSeparator style={{ height: 1, marginVertical: 4, marginHorizontal: 6, backgroundColor: ui().border }} />;
}

function Share(props: { onSelect: (what: string) => void }) {
  return (
    <Submenu
      offset={4}
      trigger={
        <View hoverStyle={{ backgroundColor: tint(ui().accent, 0.08), borderColor: tint(ui().accent, 0.24) }} activeStyle={{ backgroundColor: tint(ui().accent, 0.14), borderColor: tint(ui().accent, 0.34) }} style={{ ...row(), borderWidth: 1, borderColor: "transparent" }}>
          <Text style={{ numberOfLines: 1, flexGrow: 1, flexShrink: 0, fontSize: 14, color: ui().text }}>Share</Text>
          <Text style={{ fontSize: 14, color: ui().faint }}>›</Text>
        </View>
      }
      back={
        <View activeStyle={{ backgroundColor: tint(ui().accent, 0.14), borderColor: tint(ui().accent, 0.34) }} style={{ ...row(), borderWidth: 1, borderColor: "transparent" }}>
          <Text style={{ fontSize: 14, fontWeight: "600", color: ui().accent }}>‹ Share</Text>
        </View>
      }
    >
      <Items side="right">
        <Item label="Mail" onSelect={() => props.onSelect("Mail")} />
        <Item label="Messages" onSelect={() => props.onSelect("Messages")} />
        <Item label="Copy link" onSelect={() => props.onSelect("Copy link")} />
      </Items>
    </Submenu>
  );
}

/** A group's title: not an item, nothing to press. */
function Label(props: { text: string }) {
  return <Text style={{ fontSize: 12, fontWeight: "600", color: ui().faint, paddingHorizontal: 10, paddingTop: 6, paddingBottom: 2 }}>{props.text}</Text>;
}

const SORTS = ["Name", "Date modified", "Size"];

function App() {
  const last = signal("—");
  const reasons = signal<string[]>([]);
  const hidden = signal(false);
  const sidebar = signal(true);
  const minimap = signal(false);
  const sort = signal("Name");
  const pinned = signal(false);
  const changed = (name: string) => ({ open, reason }: { open: boolean; reason: OpenChangeReason }) =>
    reasons.set([`${name} ${open ? "opened" : "closed"} · ${reason}`, ...reasons()].slice(0, 8));
  const choose = (what: string) => last.set(what);
  const menu = () => (
    <Menu
      offset={4}
      margin={8}
      popupStyle={surface()}
      backdropStyle={backdrop}
      onOpenChange={changed("File")}
      trigger={
        <View {...button("secondary", { align: "flex-start" })}>
          <Text style={buttonText()}>File ▾</Text>
        </View>
      }
    >
      <Items>
        <Item label="New" onSelect={() => choose("New")} />
        <Item label="Open…" onSelect={() => choose("Open…")} />
        <Item label="Paste" disabled />
        <Separator />
        <Share onSelect={(what) => choose(`Share › ${what}`)} />
        <Separator />
        <Item label="Delete" danger onSelect={() => choose("Delete")} />
      </Items>
    </Menu>
  );
  const view = () => (
    <Menu
      offset={4}
      margin={8}
      popupStyle={surface()}
      backdropStyle={backdrop}
      onOpenChange={changed("View")}
      trigger={
        <View {...button("secondary", { align: "flex-start" })}>
          <Text style={buttonText()}>View ▾</Text>
        </View>
      }
    >
      <Items>
        {/* Settings keep the menu open: each shows its new value at once. */}
        <Item label="Show sidebar" checked={sidebar()} keepOpen onSelect={() => sidebar.set(!sidebar())} />
        <Item label="Show minimap" checked={minimap()} keepOpen onSelect={() => minimap.set(!minimap())} />
        <Item label="Show hidden files" checked={hidden()} keepOpen onSelect={() => hidden.set(!hidden())} />
        <Separator />
        <Label text="Sort by" />
        {/* One choice of a group: choosing closes the menu. */}
        <Each of={SORTS}>{(by) => <Item label={by()} radio checked={sort() === by()} onSelect={() => sort.set(by())} />}</Each>
      </Items>
    </Menu>
  );
  const context = () => (
    <ContextMenu
      margin={8}
      popupStyle={surface()}
      backdropStyle={backdrop}
      onOpenChange={changed("Context")}
      trigger={
        <View
          style={{
            height: wide() ? 220 : 150,
            borderRadius: 14,
            borderWidth: 2,
            borderStyle: "dashed",
            borderColor: ui().faint,
            alignItems: "center",
            justifyContent: "center",
            gap: 4,
          }}
        >
          <Text style={{ fontSize: 15, fontWeight: "600", color: ui().text }}>{pinned() ? "ContextMenu · pinned" : "ContextMenu"}</Text>
          <Text style={{ fontSize: 13, color: ui().muted }}>{desktop ? "Right click here" : "Hold a finger here"}</Text>
        </View>
      }
    >
      <Items>
        <Item label="Pinned" checked={pinned()} onSelect={() => pinned.set(!pinned())} />
        <Item label="Copy" inset onSelect={() => choose("Copy")} />
        <Item label="Rename" inset onSelect={() => choose("Rename")} />
        <Separator />
        <Item label="Delete" inset danger onSelect={() => choose("Delete")} />
      </Items>
    </ContextMenu>
  );
  const panels = () => (
    <>
      <Panel title="onSelect · checked">
        <Text style={{ fontSize: 13, color: ui().text }}>{last()}</Text>
        <Text style={{ fontSize: 13, color: ui().muted }}>
          sidebar {sidebar() ? "on" : "off"} · minimap {minimap() ? "on" : "off"} · hidden files {hidden() ? "shown" : "hidden"} · by {sort()} · {pinned() ? "pinned" : "not pinned"}
        </Text>
      </Panel>
      <Panel title="onOpenChange({ open, reason })" grow>
        <View style={{ gap: 2 }}>
          <Each of={reasons()}>{(e, i) => <Text style={{ fontSize: 12, color: i() === 0 ? ui().text : ui().faint }}>{e()}</Text>}</Each>
        </View>
      </Panel>
    </>
  );
  return (
    <View style={{ flex: 1, backgroundColor: ui().page, padding: 20, paddingTop: 24, gap: 14 }}>
      <Text style={{ fontSize: 22, fontWeight: "700", color: ui().text }}>menu</Text>
      <Text style={{ color: ui().muted }}>
        {desktop ? "Click File; hover Share to open its submenu. View has settings and a choice of one. Right click the box below" : "Tap File: a sheet; Share shows its items in place. View has settings and a choice of one"}
      </Text>

      {() =>
        wide() ? (
          <View style={{ flex: 1, flexDirection: "row", gap: 16 }}>
            <Stage>
              <View style={{ flexDirection: "row", gap: 8 }}>
                {menu()}
                {view()}
              </View>
              {context()}
            </Stage>
            <Inspector>{panels()}</Inspector>
          </View>
        ) : (
          <>
            <View style={{ flexDirection: "row", gap: 8 }}>
              {menu()}
              {view()}
            </View>
            {context()}
            {panels()}
          </>
        )
      }
    </View>
  );
}

mount(App);
  • 在桌面平台和桌面浏览器中,它是触发元素处的弹出层;指针移到 Submenu 的条目上或按下它时,子菜单在条目旁级联展开。
  • 在手机和平板上,它是屏幕底部、覆盖在背景层之上的 sheet;Submenu 在 sheet 中就地显示它的条目,取代菜单的条目,并由你提供的一行返回上一级。

选择一个条目、外部按压、Escape、Android 返回键和 VoiceOver 的退出手势都会关闭它。ContextMenu 是同样的菜单,由辅助按键(右键单击)在指针处打开,或在触摸屏上由手指按住触发元素打开。

在获得焦点的触发元素上按 Return、Space 或 Down 键打开菜单时,键盘焦点落在第一个可用的条目上;按 Up 键打开时落在最后一个上,之后用方向键在条目间移动。按压打开时不聚焦任何条目。 在桌面和终端中,在 Submenu 的条目上按 Right、Return 或 Space 键打开它,键盘焦点落在它第一个可用的条目上;在它的条目上按 Left 键或按 Escape 键关闭它,焦点回到它的条目上。

use nsh_base::{menu, menu_item, menu_separator, submenu};
use nsh_core::prelude::*;

fn item(label: &'static str, on_select: impl Fn() + 'static) -> impl IntoElement {
    menu_item()
        .on_select(on_select)
        .px_3()
        .py_2()
        .rounded_md()
        .child(text(label))
}

fn file_menu() -> impl IntoElement {
    menu()
        .offset(px(4.0))
        .trigger(view().px_3().py_2().child(text("File")))
        .child(item("New", || log::info!("new")))
        .child(item("Open…", || log::info!("open")))
        .child(menu_separator().h(px(1.0)).my_1().bg(rgb(0xe5e7eb)))
        .child(
            submenu()
                .trigger(view().px_3().py_2().child(text("Share ›")))
                .back(view().px_3().py_2().child(text("‹ Back")))
                .child(item("Mail", || log::info!("mail")))
                .child(item("Copy link", || log::info!("link"))),
        )
}

这五个组件都来自 nsh/base。条目会自己找到所属的菜单,所以你可以自由地编排它们:放在视图里分组、加标签、用 If 让条目出现和消失。popupClass 在桌面上设置弹出层的样式,在手机上设置 sheet 的样式;两者需要不同样式时,可以把 platform.idiom(nsh)与 "desktop" 比较。

#菜单栏

同一个 Toolbar 里的多个 Menu 组成菜单栏:其中一个菜单打开时,指针移到另一个菜单的触发器上就会改为打开那个菜单,方向键在各触发器之间移动。

<Toolbar accessibilityLabel="Menu bar">
  <Menu trigger={<View><Text>File</Text></View>}>{/* … */}</Menu>
  <Menu trigger={<View><Text>Edit</Text></View>}>{/* … */}</Menu>
</Toolbar>

#上下文菜单

import { ContextMenu, MenuItem } from "nsh/base";

<ContextMenu
  popupClass="min-w-40 rounded-lg bg-white p-1 shadow-lg"
  backdropClass="bg-black/40"
  trigger={<PhotoTile photo={photo} />}
>
  <MenuItem onSelect={() => share(photo)}><Text>Share</Text></MenuItem>
  <MenuItem onSelect={() => remove(photo)}><Text>Delete</Text></MenuItem>
</ContextMenu>

在桌面平台上,辅助按键在指针所在处打开它。在触摸屏上,手指按住触发元素会以 sheet 的形式打开它;触发元素自己的按压和周围的滚动视图照常工作,移动手指会取消这次按住。

#可勾选的条目

checked 让条目可以勾选:菜单显示的一项设置(「显示侧边栏」),或者在 role="menuitemradio" 时,一组选项中的一个(「排序方式」)。值由你持有:选择条目会调用 onSelect,你设置下一个值,条目就显示它。勾选时,它的 checked: 类和 checkedStyle 生效,它的指示部件(位于内容之前,indicatorClass、indicatorStyle)也一样,部件里是你给它的标记(indicator)。标记只创建一次并保留下来,由指示部件的 checked: 类显示和隐藏它;也可以在 indicatorClass 中加 group,用标记自己的 group-checked: 类。closeOnSelect={false} 让菜单保持打开,这样设置会立刻显示新的值。

use nsh_base::{menu, menu_item};
use nsh_core::{element::StyleRefinement, prelude::*};

fn view_menu(sidebar: Signal<bool>, sort: Signal<&'static str>) -> impl IntoElement {
    // The indicator part shows the mark while the item is checked.
    let part = |s: StyleRefinement| s.w(px(16.0)).opacity(0.0).checked(|s| s.opacity(1.0));
    let by = move |label: &'static str| {
        menu_item()
            .role(AccessibilityRole::MenuItemRadio)
            .checked(move || Some(sort.get() == label))
            .indicator_style(part)
            .indicator(text("✓"))
            .on_select(move || sort.set(label))
            .flex_row()
            .gap_2()
            .px_3()
            .py_2()
            .child(text(label))
    };
    menu()
        .trigger(view().px_3().py_2().child(text("View")))
        .child(
            menu_item()
                .checked(move || Some(sidebar.get()))
                .close_on_select(false)
                .indicator_style(part)
                .indicator(text("✓"))
                .on_select(move || sidebar.set(!sidebar.get_untracked()))
                .flex_row()
                .gap_2()
                .px_3()
                .py_2()
                .child(text("Show sidebar")),
        )
        .child(by("Name"))
        .child(by("Date modified"))
}

放在末端的标记则写在内容里,在条目上加 group,用 group-checked: 类设置它的样式。

#出现与离开

在桌面平台上,弹出层像 Popover 一样随你的过渡出现和离开:popupStartingClass 是它第一帧的外观,popupEndingClass 是关闭后的外观,在 popupClass 的 transition-* 类播放期间它会保留,随后触发 onClose。子菜单使用同样的外观,并随菜单一起关闭。手机上的 sheet 自带滑入和滑出的动作。

<Menu
  popupClass="min-w-40 rounded-lg bg-white p-1 shadow-lg transition-all duration-100"
  popupStartingClass="opacity-0 -translate-y-1"
  popupEndingClass="opacity-0"
  trigger={<Text>File</Text>}
>
  <MenuItem onSelect={newFile}><Text>New</Text></MenuItem>
</Menu>

#打开状态

没有 open 时,菜单自己保存状态(Menu 的初始值为 defaultOpen)。有 open 时,状态由应用持有,并在 onOpenChange({ open, reason }) 中修改它,原因为 "triggerPress"、"select"、"pressOutside"、"backdropPress"、"cancel"、"contextMenu" 或 "longPress"。

#属性

属性Rust类型默认值说明
open.open(..)boolean无是否打开,由应用控制。
defaultOpen.default_open(..)booleanfalse没有 open 时,初始是否打开。
placement.placement(..)"top" | "bottom" | "left" | "right""bottom"桌面:它位于触发元素的哪一侧。
align.align(..)"start" | "center" | "end""start"桌面:它如何与触发元素对齐。
offset.offset(..)number0桌面:触发元素与弹出层之间的间距。
margin.margin(..)number0桌面:它与安全区边缘保持的距离。
popupClass、popupStylepopup_class、popup_stylestring、StyleProp<ViewStyle>无弹出层(桌面)或 sheet(手机),以及子菜单的弹出层。
popupStartingClass、popupStartingStylepopup_starting_class、popup_starting_stylestring、StyleProp<ViewStyle>无桌面:弹出层第一帧的外观,叠加在它们自己的外观之上。
popupEndingClass、popupEndingStylepopup_ending_class、popup_ending_stylestring、StyleProp<ViewStyle>无桌面:弹出层关闭后的外观,过渡播放期间保留。
backdropClass、backdropStylebackdrop_class、backdrop_stylestring、StyleProp<ViewStyle>无手机:sheet 后面的背景层。
trigger.trigger(..)JSX.Element无按下时打开它的元素。菜单打开时,它的 aria-expanded: 类和 expandedStyle 生效。
children.child(..)JSX.Element无条目,每次打开时创建。

#ContextMenu

open、popupClass / popupStyle、起始与结束外观、backdropClass / backdropStyle、trigger 和 children 与 Menu 相同,另外还有:

属性Rust类型默认值说明
disabled.disabled(..)booleanfalse辅助按键和按住的手指都不起作用。
offset.offset(..)number0桌面:指针与弹出层之间的间距。
margin.margin(..)number0桌面:它与安全区边缘保持的距离。
属性Rust类型默认值说明
disabled.disabled(..)booleanfalse不可选择;disabled: 样式生效。
checked.checked(..)boolean无是否勾选,设置后它就是可勾选的条目;值由应用持有。
checkedStyle.checked_style(..)StyleProp<ViewStyle>无勾选时的样式,与 checked: 类相同。
role.role(..)AccessibilityRole有 checked 时为 "menuitemcheckbox",否则为 "menuitem"它对屏幕阅读器而言是什么;一组选项中的一个用 "menuitemradio"。
indicatorClass、indicatorStyle.indicator_class(..)、.indicator_style(..)string、StyleProp<ViewStyle>无指示部件,位于内容之前;它的 checked: 和 disabled: 类跟随条目。
indicator.indicator(..)JSX.Element无指示部件里的标记。
closeOnSelect.close_on_select(..)booleantrue选择它会关闭菜单。
accessibilityLabel.accessibility_label(..)string无内容不足以说明时,屏幕阅读器读出的文字。
children、class、style.child(..)、样式方法无它的内容和外观,支持 hover:、active: 和 checked: 状态。
属性Rust类型默认值说明
trigger.trigger(..)JSX.Element无打开它的条目。
back.back(..)JSX.Element无手机:它的条目上方返回上一级条目的那一行。
disabled.disabled(..)booleanfalse它不会打开。
offset.offset(..)number0桌面端:它的弹出层与外层菜单之间的间距。
children.child(..)JSX.Element无它的条目。

MenuSeparator 是一个由你设置样式(class、style)的视图,与所在层级的条目一起显示。

#事件

事件Rust说明
onOpenChange(Menu、ContextMenu).on_open_change(|e| ..)它打开或关闭了,附带原因。
onOpen(Menu、ContextMenu).on_open(|| ..)它已显示;以 sheet 呈现时,在它进入之后。
onClose(Menu、ContextMenu).on_close(|| ..)它已消失:以 sheet 呈现时在它离开之后,以弹出层呈现时在离开过渡之后。应用关闭它时也会触发。
onPosition(Menu、ContextMenu).on_position(|e| ..)桌面:弹出层已放置:placement(它所在的一侧)、bounds、available。
onCancel(Menu、ContextMenu).on_cancel(|| ..)用户没有选择条目就尝试关闭它(Escape 或返回键、外部按压或按下背景层)。
onSelect(MenuItem).on_select(|| ..)条目被选择;可勾选的条目请求它的另一个值。

#无障碍

对屏幕阅读器而言,弹出层或 sheet 是一个菜单,其中的条目是菜单项;触发元素会告诉屏幕阅读器菜单是否已展开。可勾选的条目是复选或单选菜单项,读作已勾选或未勾选;它的标记是条目的一部分。在 TSX 中,触发元素就是你传入的元素(Button 或你自己的视图都可以):按压和展开状态都在它自己身上,所以它自己的 active: 类和 activeStyle 会在按下时生效,它自己的 onPress 也照常运行。屏幕上只有当前显示层级的条目,所以屏幕阅读器读到的就是显示出来的内容。