本頁由機器翻譯,可能有錯誤。 閱讀英文原文

元素

選單

選單是由觸發元件開啟的一列動作。同一個結構適用於所有裝置,並按各裝置的慣例呈現:

el-menu.tsx
el-menu.tsxLive
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 會在指標移到其項目上或按下時,於該項目旁邊層疊展開。
  • 在手機和平板電腦上,於螢幕底部、背景遮罩之上顯示面板;Submenu 會在面板內取代選單本身的項目來顯示其項目,並附有一列由你提供的項目用來返回。

選擇項目、在選單外按一下、Escape、Android 返回按鈕和 VoiceOver 的退出手勢都會關閉選單。ContextMenu 是同一個選單,由次要按鈕(右鍵)在指標位置開啟,或在觸控螢幕上以手指按住其觸發元件開啟。

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。項目會自行傳到所屬選單,所以你可以自由排列:以 view 分組、標籤、以 If 出現或消失的項目。popupClass 為桌面平台上的彈出視窗和手機上的面板設定樣式;若兩者的樣式不同,可以用 platform.idiom(nsh)與 "desktop" 比較。

#選單列

由同一個 Toolbar 的項目作為觸發元件的選單會組成選單列:其中一個選單開啟時,指標移到另一個選單的觸發元件上,便會改為開啟該選單,方向鍵則可在各觸發元件之間移動。

<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>

在桌面平台上,次要按鈕會在指標所在位置開啟選單。在觸控螢幕上,以手指按住觸發元件會以面板形式開啟;觸發元件本身的按壓操作和周圍的捲動視圖仍然有效,而移動手指則會取消按住。

#可勾選項目

checked 令項目可被勾選:可以是選單顯示的設定(“Show sidebar”),或配合 role="menuitemradio" 成為一組中的單一選項(“Sort by”)。值由你管理:選擇項目時會呼叫 onSelect,由你設定下一個值,項目便會顯示該值。勾選時,其 checked: classes 和 checkedStyle 會生效,其指示器的亦然:指示器是內容之前的部分(indicatorClass、indicatorStyle),圍繞著你提供的標記(indicator)。標記只建立一次並保留;可用指示器的 checked: classes 來顯示和隱藏它,或使用標記自己的 group-checked: classes 配合 indicatorClass 中的 group。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: classes 設定樣式。

#進場與離場

在桌面平台上,彈出視窗隨你的 transition 出現和消失,與 Popover 相同:popupStartingClass 是它第一個影格的外觀,popupEndingClass 是關閉後的外觀,在 popupClass 的 transition-* classes 執行期間會一直保持,之後是 onClose。子選單採用相同的外觀,並隨所屬選單關閉。手機的面板會自行滑入和滑出。

<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 時,選單自行保持狀態(由 defaultOpen 起始,對應 Menu)。有 open 時,狀態由應用程式擁有,並在 onOpenChange({ open, reason }) 中更改:"triggerPress"、"select"、"pressOutside"、"backdropPress"、"cancel"、"contextMenu" 或 "longPress"。

#Props

Prop Rust 類型 預設值 說明
open .open(..) boolean 無 開啟狀態,由應用程式控制。
defaultOpen .default_open(..) boolean false 初始為開啟,適用於沒有 open 的情況。
placement .placement(..) "top" | "bottom" | "left" | "right" "bottom" 桌面平台:放在觸發元件的哪一邊。
align .align(..) "start" | "center" | "end" "start" 桌面平台:與觸發元件的對齊方式。
offset .offset(..) number 0 桌面平台:觸發元件與彈出視窗之間的間距。
margin .margin(..) number 0 桌面平台:與安全區域邊緣保持的距離。
popupClass、popupStyle .popup_class(..)、.popup_style(..) string、StyleProp<ViewStyle> 無 彈出視窗(桌面平台)或面板(手機),以及子選單的彈出視窗。
popupStartingClass、popupStartingStyle .popup_starting_class(..)、.popup_starting_style(..) string、StyleProp<ViewStyle> 無 桌面平台:彈出視窗在第一個影格的外觀,疊加於它們本身的外觀之上。
popupEndingClass、popupEndingStyle .popup_ending_class(..)、.popup_ending_style(..) string、StyleProp<ViewStyle> 無 桌面平台:彈出視窗關閉後的外觀,在其 transition 執行期間保持。
backdropClass、backdropStyle .backdrop_class(..)、.backdrop_style(..) string、StyleProp<ViewStyle> 無 手機:面板後方的背景遮罩。
trigger .trigger(..) JSX.Element 無 按下時開啟選單的元件。選單開啟期間,其 aria-expanded: classes 和 expandedStyle 會生效。
children .child(..) JSX.Element 無 項目,每次開啟時建立。

#ContextMenu

open、popupClass / popupStyle、起始和結束外觀、backdropClass / backdropStyle、trigger 和 children 與 Menu 相同,另有:

Prop Rust 類型 預設值 說明
disabled .disabled(..) boolean false 次要按鈕和按住的手指均不起作用。
offset .offset(..) number 0 桌面平台:指標與彈出視窗之間的間距。
margin .margin(..) number 0 桌面平台:與安全區域邊緣保持的距離。
Prop Rust 類型 預設值 說明
disabled .disabled(..) boolean false 不可選擇;套用 disabled: 樣式。
checked .checked(..) boolean 無 是否已勾選,令它成為可勾選項目;由應用程式擁有。
checkedStyle .checked_style(..) StyleProp<ViewStyle> 無 勾選時的樣式,以 checked: classes 設定。
role .role(..) AccessibilityRole 有 checked 時為 "menuitemcheckbox",否則為 "menuitem" 螢幕閱讀器如何理解它;"menuitemradio" 用於一組中的單一選項。
indicatorClass、indicatorStyle .indicator_class(..)、.indicator_style(..) string、StyleProp<ViewStyle> 無 指示器,即內容之前的部分;其 checked: 和 disabled: classes 跟隨項目。
indicator .indicator(..) JSX.Element 無 指示器內的標記。
closeOnSelect .close_on_select(..) boolean true 選擇它會關閉選單。
accessibilityLabel .accessibility_label(..) string 無 內容不足時,螢幕閱讀器所讀出的內容。
children、class、style .child(..)、樣式方法 無 內容和外觀,附有 hover:、active: 和 checked: 狀態。
Prop Rust 類型 預設值 說明
trigger .trigger(..) JSX.Element 無 開啟它的項目。
back .back(..) JSX.Element 無 手機:位於其項目上方、用來返回上一層項目的一列。
disabled .disabled(..) boolean false 不會開啟。
offset .offset(..) number 0 桌面平台:所屬選單與其彈出視窗之間的間距。
children .child(..) JSX.Element 無 它的項目。

MenuSeparator 是你可設定樣式的 view(class、style),與所屬層級的項目一同顯示。

#事件

事件 Rust 說明
onOpenChange(Menu、ContextMenu) .on_open_change(|e| ..) 已開啟或關閉,並附上原因。
onOpen(Menu、ContextMenu) .on_open(|| ..) 已顯示;作為面板時,在滑入完成後觸發。
onClose(Menu、ContextMenu) .on_close(|| ..) 已消失:作為面板時在滑出之後,作為彈出視窗時在其離場之後。應用程式將其關閉時亦會觸發。
onPosition(Menu、ContextMenu) .on_position(|e| ..) 桌面平台:彈出視窗已定位:placement(所放置的一邊)、bounds、available。
onCancel(Menu、ContextMenu) .on_cancel(|| ..) 使用者嘗試不作選擇而關閉(Escape 或返回、在選單外或背景遮罩上按下)。
onSelect(MenuItem) .on_select(|| ..) 項目已被選擇;可勾選項目會要求取得其另一個值。

#無障礙

對螢幕閱讀器而言,彈出視窗或面板是選單,其項目是選單項目;觸發元件會表明選單是否已展開。可勾選項目是核取方塊或選項按鈕選單項目,會讀作已勾選或未勾選;其指示器是項目的一部分。在 TSX 中,觸發元件是你傳入的元素,即 Button 或你自己的 view:它會自行接收按壓和展開狀態,所以按下時,其自身的 active: classes 和 activeStyle 會顯示,其自身的 onPress 也會執行。畫面上只有當前所顯示層級的項目,所以螢幕閱讀器會讀出目前存在的內容。