元素

Toolbar

工具栏是一栏控件:文本编辑器的格式栏、媒体播放器的播放控制、竖排的工具面板。Toolbar 负责它的键盘和语义:屏幕阅读器把它播报为一个工具栏(名字来自 accessibilityLabel),键盘一站就进入它,再用其 axis 方向上的方向键在项目之间移动。它的项目是其中任意层级的 Button、Toggle 和 Link。ToolbarGroup 是一段有名字的相关项目(撤销与重做、对齐方式),ToolbarSeparator 是应用在各段之间画的一条线。外观由应用决定:工具栏没有自己的颜色、内边距或间距,每个控件保留自己的状态。

el-toolbar.tsx
el-toolbar.tsx运行中
import { mount, signal, Text, View } from "nsh";
import { Button, Toggle, ToggleGroup, Toolbar, ToolbarGroup, ToolbarSeparator } from "nsh/base";
import { desktop, focusRing, hoverFill, Log, Split, ui } from "./palette";

type Mark = "bold" | "italic" | "underline";
type Align = "left" | "center" | "right";
type Tool = "select" | "pen" | "shape" | "text";

const marks: { id: Mark; label: string; glyph: string }[] = [
  { id: "bold", label: "Bold", glyph: "B" },
  { id: "italic", label: "Italic", glyph: "I" },
  { id: "underline", label: "Underline", glyph: "U" },
];
const aligns: { id: Align; label: string; glyph: string }[] = [
  { id: "left", label: "Align left", glyph: "⇤" },
  { id: "center", label: "Align center", glyph: "↔" },
  { id: "right", label: "Align right", glyph: "⇥" },
];
const tools: { id: Tool; label: string; glyph: string }[] = [
  { id: "select", label: "Select", glyph: "↖" },
  { id: "pen", label: "Pen", glyph: "✎" },
  { id: "shape", label: "Rectangle", glyph: "▢" },
  { id: "text", label: "Text", glyph: "T" },
];

const side = desktop ? 28 : 34;

function App() {
  const on = signal<Mark[]>(["bold"]);
  const align = signal<Align>("left");
  const tool = signal<Tool>("select");
  const history = signal<string[]>([]);
  const events = signal<string[]>([]);
  const log = (line: string) => events.set([line, ...events()].slice(0, 3));
  const edit = (what: string) => {
    history.set([...history(), what]);
    log(what);
  };
  const has = (m: Mark) => on().includes(m);
  // A tool: bare while off, raised while pressed, a hover fill, the focus ring.
  const look = (pressed: boolean) => ({
    style: { minWidth: side, height: side, paddingHorizontal: 6, borderRadius: 7, alignItems: "center", justifyContent: "center" } as const,
    class: "transition-colors active:opacity-70 disabled:opacity-35 motion-reduce:transition-none",
    pressedStyle: ui().raised,
    hoverStyle: pressed ? undefined : hoverFill(),
    focusStyle: focusRing(),
  });
  const glyph = (text: string, active: boolean) => <Text style={{ fontSize: 14, fontWeight: "600", color: active ? ui().text : ui().muted }}>{text}</Text>;
  const rule = (vertical = false) => (
    <ToolbarSeparator style={vertical ? { height: 1, alignSelf: "stretch", marginVertical: 4, backgroundColor: ui().line } : { width: 1, height: 16, marginHorizontal: 4, backgroundColor: ui().line }} />
  );
  const bar = { alignItems: "center", gap: 2, padding: 3, borderRadius: 10, ...ui().inset } as const;
  return (
    <View style={{ flex: 1, backgroundColor: ui().page, padding: 20, paddingTop: 24, gap: 14 }}>
      <Text style={{ fontSize: 22, fontWeight: "700", color: ui().text }}>toolbar</Text>
      <Text style={{ color: ui().muted }}>
        {desktop ? "Tab into a toolbar, then move with the arrow keys; Home and End jump to its ends" : "Tap the tools; a screen reader hears each bar as a toolbar"}
      </Text>

      <Split gap={14} inspector={<Log title={`${history().length} edits · ${on().join(", ") || "no marks"}, ${align()} · ${tool()}`} lines={events()} minHeight={76} />}>
        {/* A formatting bar: history, a toggle group of marks (a group of the toolbar), the alignment. */}
        <Toolbar accessibilityLabel="Formatting" style={{ ...bar, alignSelf: "flex-start" }}>
          <ToolbarGroup accessibilityLabel="History">
            <Button accessibilityLabel="Undo" disabled={history().length === 0} onPress={() => {
              history.set(history().slice(0, -1));
              log("Undo");
            }} {...look(false)}>
              {glyph("↶", true)}
            </Button>
          </ToolbarGroup>
          {rule()}
          <ToggleGroup style={{ flexDirection: "row", gap: 2 }}>
            <Each of={marks}>
              {(m) => (
                <Toggle
                  pressed={has(m().id)}
                  accessibilityLabel={m().label}
                  onChange={({ pressed }) => {
                    on.set(pressed ? [...on(), m().id] : on().filter((x) => x !== m().id));
                    edit(`${m().label}: ${pressed}`);
                  }}
                  {...look(has(m().id))}
                >
                  {glyph(m().glyph, has(m().id))}
                </Toggle>
              )}
            </Each>
          </ToggleGroup>
          {rule()}
          <ToolbarGroup accessibilityLabel="Alignment" style={{ gap: 2 }}>
            <Each of={aligns}>
              {(a) => (
                <Toggle
                  pressed={align() === a().id}
                  accessibilityLabel={a().label}
                  onChange={({ pressed }) => {
                    // Exactly one: the pressed alignment stays pressed.
                    if (pressed) align.set(a().id);
                    edit(`${a().label}`);
                  }}
                  {...look(align() === a().id)}
                >
                  {glyph(a().glyph, align() === a().id)}
                </Toggle>
              )}
            </Each>
          </ToolbarGroup>
        </Toolbar>

        <View style={{ flexDirection: "row", gap: 14, alignItems: "stretch" }}>
          {/* A vertical palette: Up and Down move along it. */}
          <Toolbar axis="vertical" accessibilityLabel="Tools" style={{ ...bar, alignSelf: "flex-start" }}>
            <Each of={tools}>
              {(t) => (
                <Toggle
                  pressed={tool() === t().id}
                  accessibilityLabel={t().label}
                  onChange={({ pressed }) => {
                    if (!pressed) return;
                    tool.set(t().id);
                    log(`Tool: ${t().label}`);
                  }}
                  {...look(tool() === t().id)}
                >
                  {glyph(t().glyph, tool() === t().id)}
                </Toggle>
              )}
            </Each>
            {rule(true)}
            <Button accessibilityLabel="Clear canvas" onPress={() => log("Clear canvas")} {...look(false)}>
              {glyph("⌫", false)}
            </Button>
          </Toolbar>
          <View style={{ flex: 1, ...ui().card, borderRadius: 14, padding: 14, gap: 6 }}>
            <Text
              style={{
                fontSize: 15,
                lineHeight: 22,
                color: ui().text,
                fontWeight: has("bold") ? "700" : "400",
                fontStyle: has("italic") ? "italic" : "normal",
                textDecorationLine: has("underline") ? "underline" : "none",
                textAlign: align(),
              }}
            >
              One stop of Tab per toolbar; its arrows move between the tools.
            </Text>
            <Text style={{ fontSize: 13, color: ui().muted }}>{`Tool: ${tools.find((t) => t.id === tool())!.label}`}</Text>
          </View>
        </View>
      </Split>
    </View>
  );
}

mount(App);
use nsh_base::{button, theme, toggle, toolbar, toolbar_group, toolbar_separator};
use nsh_core::prelude::*;

fn formatting() -> impl IntoElement {
    let bold = signal(false);
    toolbar()
        .accessibility_label("Formatting")
        .items_center()
        .gap_1()
        .child(
            toolbar_group()
                .accessibility_label("History")
                .child(button().accessibility_label("Undo").size_8().child(text("↶")))
                .child(button().accessibility_label("Redo").size_8().child(text("↷"))),
        )
        .child(toolbar_separator().w(px(1.0)).h_5().bg(move || theme().border))
        .child(
            toggle()
                .pressed(bold)
                .accessibility_label("Bold")
                .on_change(move |pressed: &bool| bold.set(*pressed))
                .size_8()
                .items_center()
                .justify_center()
                .pressed_style(|s| s.bg(move || theme().accent))
                .child(text("B")),
        )
}

Toolbar、ToolbarGroup 和 ToolbarSeparator 来自 nsh/base,在 Rust 中是 nsh-base crate。工具栏不持有值:其中的开关按钮被告知是否按下并报告 onChange,按钮报告 onPress,状态由应用保存。在 Rust 中由 toolbar()、toolbar_group() 和 toolbar_separator() 创建,每个属性对应一个设置方法(.axis(..)、.disabled(..)),再加上类型化的样式方法和放内容的 .child(..)。

#属性

#Toolbar

属性Rust类型默认值说明
axis.axis(..)"horizontal" | "vertical""horizontal"项目排列的方向:沿它移动的方向键(左右,或上下),以及它排列子元素的方向(一行或一列)。在工具栏创建时读取。
disabled.disabled(..)booleanfalse工具栏的每个项目都被禁用,不论它自己的 disabled:不响应按压,不是键盘焦点的一站,屏幕阅读器读作变暗。
accessibilityLabel.accessibility_label(..)string无屏幕阅读器对工具栏的称呼(“Formatting”),页面上有多个工具栏时使用。
children.child(..)JSX.Element无项目、它们的分组和分隔线,以及应用放在其间的其他内容(标题、占位、输入框)。
class、style样式方法string、StyleProp<ViewStyle>无栏的布局和外观:对齐、间距、内边距、底色。

#ToolbarGroup

属性Rust类型默认值说明
disabled.disabled(..)booleanfalse分组的每个项目都被禁用,不论它自己的 disabled。
accessibilityLabel.accessibility_label(..)string无屏幕阅读器对分组的称呼(“History”、“Alignment”)。
children.child(..)JSX.Element无分组的项目,以及应用放在其间的内容。
class、style样式方法string、StyleProp<ViewStyle>无分组的布局和外观。它沿工具栏的方向排列。

#ToolbarSeparator

属性Rust类型默认值说明
class、style样式方法string、StyleProp<ViewStyle>无这条线:横穿一行(h-5 w-px bg-border)或一列(h-px self-stretch bg-border)的细线。

#项目

工具栏的项目是其中的 Button、Toggle 和 Link,包括它的分组里的,以及放在它里面的 ToggleGroup 里的。Menu 或 Popover 的触发器也是项目:即放在它的 trigger 插槽里的视图,弹出层把按下、焦点和 expanded 状态交给它。放在工具栏里的 ToggleGroup 是它的一个分组:其中的开关按钮就是工具栏的项目,屏幕阅读器听到的是一个分组,它的 disabled 仍然禁用它自己的开关按钮。其他控件(Input、Select、Slider)各自是键盘焦点的一站,保留自己的方向键;把它们放在栏的末尾。项目打开的弹出层里显示的内容(弹出框的输入框和按钮、菜单的项)不属于工具栏。方向键按栏里排列的顺序经过各个项目,无论项目嵌套多深;应用稍后才显示的项目也会排在它所在的位置。

#部件

部件位于样式来自状态
栏Toolbar 的 class、style
分组栏ToolbarGroup 的 class、style
分隔线栏、分组ToolbarSeparator 的 class、style
项目栏、分组项目自己的 class、style它自己的:aria-pressed:、disabled:、hover:、focus:、active:

用 focus: 样式显示键盘所在的项目,用按下的开关按钮自己的底色显示它的按下状态(见设计指南)。

#竖排

工具面板是一列:axis="vertical" 让项目从上到下排列,上下键沿着它们移动。

<Toolbar axis="vertical" accessibilityLabel="Tools" class="gap-1 self-start rounded-lg bg-muted p-1">
  <Each of={["select", "pen", "shape", "text"]}>
    {(t) => (
      <Toggle
        pressed={tool() === t()}
        accessibilityLabel={t()}
        onChange={({ pressed }) => pressed && tool.set(t())}
        class="size-8 items-center justify-center rounded-md aria-pressed:bg-accent"
      >
        <Text>{t()[0].toUpperCase()}</Text>
      </Toggle>
    )}
  </Each>
</Toolbar>

#禁用

工具栏上的 disabled 禁用其中每个项目,相当于应用锁住的一栏;分组上的则禁用分组的项目。被禁用的项目不响应按压,方向键会跳过它,屏幕阅读器把它播报为变暗:

<Toolbar disabled={readonly()} accessibilityLabel="Review" class="flex-row gap-1">
  <Button onPress={approve} class="h-8 px-3 disabled:opacity-50">
    <Text>Approve</Text>
  </Button>
</Toolbar>

#无障碍

  • Toolbar 的角色是 "toolbar",名字来自 accessibilityLabel;它的项目仍然各自是一个元素。在 iOS 上工具栏是 VoiceOver 进入的语义分组。
  • ToolbarGroup 的角色是 "group",名字来自它的 accessibilityLabel,一段项目因此读作一个整体。放在工具栏里的 ToggleGroup 也是一个分组。
  • ToolbarSeparator 对屏幕阅读器不是元素。
  • disabled 按平台的方式播报(VoiceOver、TalkBack、UI Automation、浏览器的无障碍树)。

标签、角色和播报的一般说明见无障碍。

#键盘

在 macOS 和 Web 上,Tab 会到达响应按压的视图,Toolbar 遵循 WAI-ARIA 工具栏模式:

按键作用
Tab、Shift+Tab从焦点上次离开的项目进入工具栏(一开始是第一个未禁用的项目),一步离开。
右、左(axis="horizontal")把焦点移到下一个或上一个项目,从最后一个绕回第一个,反之亦然。
下、上(axis="vertical")把焦点移到下一个或上一个项目,从最后一个绕回第一个,反之亦然。
Home、End把焦点移到第一个或最后一个项目。
空格、回车按下获得焦点的项目。

方向键跳过被禁用的项目,不会按下任何东西:开关按钮只在被按下时改变。另一对方向键留给项目使用(菜单的触发器用下键打开菜单)。

#平台说明

  • iOS、Android 和 Mac Catalyst:每个项目用轻点按下;工具栏是屏幕阅读器播报的一个分组。
  • 桌面(macOS、Windows、Linux、GPUI)和 Web:按压就是点击。在 macOS 和 Web 上工具栏是 Tab 的一站,方向键、Home 和 End 在其项目之间移动(键盘)。
  • 每个平台都按应用的样式绘制:栏、分组、分隔线和项目。