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

元素

Checkbox

Checkbox 用來選取一個選項、清單中的一個項目,或全部項目。它的狀態為已勾選、未勾選或不確定(兩者皆非:在部分選取時的「全選」)。它負責行為:按壓、鍵盤啟用、停用狀態,以及供螢幕閱讀器使用的 checkbox 角色及其值。外觀則由應用程式決定:Checkbox 本身沒有方框、標記、顏色、尺寸或標籤。它由指示器(方框)與其內由應用程式提供的標記組成,接著是其子元素(標籤),應用程式以 checked:、indeterminate: 和 disabled: 類別為各部分設定樣式,這些類別會跟隨 checkbox 的狀態。

el-checkbox.tsx
el-checkbox.tsxLive
import { mount, signal, Text, View } from "nsh";
import { Checkbox } from "nsh/base";
import { CheckMark, desktop, focusRing, hoverFill, Log, Panel, size, Split, ui } from "./palette";

// The parts: geometry and motion in classes (`disabled:` dims the row,
// following the checkbox; `group-checked:` / `group-indeterminate:` fade in
// the mark of the indicator's state, both kept mounted so each fades out
// too, and a check turning into a dash crosses over), colors from the
// palette by the value the app owns; the pointer's hover and the keyboard's
// focus ring from the palette too. Nothing moves with reduced motion.
const row = `flex-row items-center gap-3 ${desktop ? "px-3 py-2" : "px-4 py-3"} active:opacity-70 disabled:opacity-40`;
const indicator = "group size-5 items-center justify-center rounded-md border transition-colors motion-reduce:transition-none";

/** The indicator's surface: the accent while checked or indeterminate. */
const box = (on: boolean) => (on ? ui().accentFill : { ...ui().inset, borderColor: ui().faint });

/** A mark over the box, centered: hidden and a little smaller until its state's `group-*:` classes show it. */
const layer = "absolute inset-0 items-center justify-center opacity-0 scale-75 transition duration-150 ease-out motion-reduce:transition-none";

/** A check while checked, a dash while indeterminate. */
const mark = () => (
  <>
    <View class={`${layer} group-checked:opacity-100 group-checked:scale-100`}>
      <CheckMark size={14} weight={2.5} tint={ui().onAccent} />
    </View>
    <View class={`${layer} group-indeterminate:opacity-100 group-indeterminate:scale-100`}>
      <View class="h-0.5 w-2.5 rounded-full" style={{ backgroundColor: ui().onAccent }} />
    </View>
  </>
);

const items = [
  { id: "photos", label: "Photos", detail: "2,418 items · 6.1 GB" },
  { id: "documents", label: "Documents", detail: "312 files · 840 MB" },
  { id: "messages", label: "Messages", detail: "Attachments included · 1.2 GB" },
];

function Label(props: { label: string; detail: string; strong?: boolean }) {
  return (
    <View style={{ flex: 1, gap: 2 }}>
      <Text style={{ fontSize: size.label, fontWeight: props.strong ? "600" : "500", color: ui().text }}>{props.label}</Text>
      <Text style={{ fontSize: size.detail, color: ui().muted }}>{props.detail}</Text>
    </View>
  );
}

function App() {
  const selected = signal<string[]>(["photos"]);
  const events = signal<string[]>([]);
  const log = (e: string) => events.set([e, ...events()].slice(0, 3));
  const all = () => selected().length === items.length;
  const some = () => selected().length > 0 && !all();
  const line = () => <View style={{ height: 1, marginLeft: desktop ? 44 : 48, backgroundColor: ui().line }} />;
  return (
    <View style={{ flex: 1, backgroundColor: ui().page, padding: 20, paddingTop: 24, gap: 14 }}>
      <Text style={{ fontSize: 22, fontWeight: "700", color: ui().text }}>checkbox</Text>
      <Text style={{ color: ui().muted }}>
        {desktop ? "Click a row, or Tab to a checkbox and press Space" : "Tap a row: the label toggles it too"}
      </Text>

      <Split
        gap={14}
        inspector={
          <>
            <Panel title="checked · indeterminate">
              <Text style={{ fontSize: 13, color: ui().text }}>
                Back up everything: {all() ? "checked" : some() ? "indeterminate" : "unchecked"} · [{selected().join(", ")}]
              </Text>
            </Panel>
            <Log title="onChange({ checked })" lines={events()} minHeight={92} />
          </>
        }
      >
        <View style={{ ...ui().card, borderRadius: 16, overflow: "hidden" }}>
          <Checkbox
            checked={all()}
            indeterminate={some()}
            onChange={({ checked }) => {
              selected.set(checked ? items.map((i) => i.id) : []);
              log(`Back up everything → ${checked}`);
            }}
            class={row}
            hoverStyle={hoverFill()}
            focusStyle={focusRing()}
            indicatorClass={indicator}
            indicatorStyle={box(all() || some())}
            indicator={mark()}
          >
            <Label label="Back up everything" detail={`${selected().length} of ${items.length} selected`} strong />
          </Checkbox>
          <Each of={items}>
            {(item) => (
              <>
                {line()}
                <Checkbox
                  checked={selected().includes(item().id)}
                  onChange={({ checked }) => {
                    selected.set(checked ? [...selected(), item().id] : selected().filter((id) => id !== item().id));
                    log(`${item().label} → ${checked}`);
                  }}
                  class={row}
                  hoverStyle={hoverFill()}
                  focusStyle={focusRing()}
                  indicatorClass={indicator}
                  indicatorStyle={box(selected().includes(item().id))}
                  indicator={mark()}
                >
                  <Label label={item().label} detail={item().detail} />
                </Checkbox>
              </>
            )}
          </Each>
        </View>

        <View style={{ ...ui().card, borderRadius: 16, overflow: "hidden" }}>
          <Checkbox checked disabled class={row} indicatorClass={indicator} indicatorStyle={box(true)} indicator={mark()}>
            <Label label="System settings" detail="Always backed up" />
          </Checkbox>
        </View>
      </Split>
    </View>
  );
}

mount(App);
use nsh_base::{checkbox, theme};
use nsh_core::prelude::*;

fn notify() -> impl IntoElement {
    let on = signal(false);
    checkbox()
        .checked(on)
        .on_change(move |next: &bool| on.set(*next))
        .flex_row()
        .items_center()
        .gap_2()
        .indicator_style(|s| {
            s.size(px(20.0))
                .items_center()
                .justify_center()
                .rounded_md()
                .border_2()
                .border_color(move || theme().border)
                .transition_colors()
                .checked(|s| s.bg(move || theme().primary).border_color(move || theme().primary))
        })
        .indicator(text(move || if on.get() { "✓" } else { "" }).text_color(move || theme().primary_foreground))
        .child(text("Notify me"))
}

Checkbox 來自 nsh/base,即 Rust 中的 nsh-base crate。值由應用程式持有:按壓時以 onChange 回報下一個值,checkbox 則顯示應用程式回傳的 checked 與 indeterminate。在 Rust 中,checkbox() 會建立一個 checkbox,每個 prop 各有一個 setter(.checked(..)、.indicator_style(..)),每個事件各有一個監聽器(.on_change(..)),checkbox 本身使用型別化的樣式方法,其標籤則使用 .child(..);指示器的樣式接受跟隨 checkbox 的狀態方法(.checked(..)、.indeterminate(..)、.disabled_style(..))。

#屬性

屬性Rust型別預設值說明
checked.checked(..)booleanfalse是否已勾選。按壓時以 onChange 回報下一個值;checkbox 會顯示應用程式回傳的值。
indeterminate.indeterminate(..)booleanfalse既非已勾選也非未勾選,成立期間優先於 checked:螢幕閱讀器會讀出為混合狀態,按壓時則要求變為已勾選。
disabled.disabled(..)booleanfalse不接受按壓,也沒有按壓回饋;disabled: 樣式會套用於 checkbox 及其指示器,螢幕閱讀器會讀出為暗淡。
checkedStyle.checked_style(|s| ..)StyleProp<ViewStyle>無checkbox 在已勾選且未停用時的樣式,以 checked: 類別表示。
indeterminateStyle.indeterminate_style(|s| ..)StyleProp<ViewStyle>無checkbox 在不確定且未停用時的樣式,以 indeterminate: 類別表示。
accessibilityLabel.accessibility_label(..)string無螢幕閱讀器讀出的內容:當子元素沒有文字說明時,此 checkbox 所選取的對象。
role.role(..)AccessibilityRole"checkbox"當它有更具體的用途時,螢幕閱讀器所讀出的角色(已勾選的選單項目:"menuitem")。
indicatorClass、indicatorStyle.indicator_style(|s| ..)string、StyleProp<ViewStyle>無指示器,即顯示值的方框。
indicator.indicator(..)JSX.Element無指示器的內容:標記(勾號、橫線),由應用程式依其值顯示。
hitSlop.hit_slop(..)number | Partial<Edges>無超出其邊界多遠的按壓仍然有效,單位為點:所有邊使用同一個數字,或分別指定各邊。如同 view 的 hitSlop。
children.child(..)JSX.Element無指示器之後的內容:標籤,螢幕閱讀器會讀出。
class、style樣式方法string、StyleProp<ViewStyle>無checkbox 本身:圍繞指示器與標籤的列。
activeStyle、hoverStyle、focusStyle、disabledStyle.active(|s| ..)、.hover(..)、.focus(..)、.disabled_style(..)StyleProp<ViewStyle>無checkbox 在按下、懸停、取得焦點或停用時的樣式,以 <state>: 類別表示。

#事件

事件Rust說明
onChange.on_change(|checked: &bool| ..)checkbox 被按壓:點按、點擊、在取得鍵盤焦點時按 Space 或 Return,或螢幕閱讀器的啟用。它以 checked 回報 checkbox 所要求的值:從未勾選與不確定狀態為 true,從已勾選為 false。

已停用的 checkbox 不會回報任何事件。

#組成部分

部分位於樣式設定方式狀態
checkboxclass、stylechecked:、indeterminate:、disabled:、active:、hover:、focus:(或 checkedStyle、indeterminateStyle、disabledStyle 等)
指示器checkbox 內,第一個indicatorClass、indicatorStylechecked:、indeterminate:、disabled: 跟隨 checkbox
標記指示器內自己的元素(indicator)group-checked:、group-indeterminate: 及其他 group-*: 變體,搭配 group 於 indicatorClass
標籤checkbox 內,位於指示器之後自己的元素(children)group-*: 變體,搭配 group 於 checkbox 的 class

標記是應用程式的內容,因此顯示什麼由應用程式決定:已勾選時顯示勾號,不確定時顯示橫線,依應用程式持有的值來選擇;或同時放入兩個標記,由 group-checked: / group-indeterminate: 類別挑選要顯示的那一個。

#Indeterminate

當部分項目被選取時,「全選」checkbox 處於不確定狀態。按壓會要求變為已勾選,應用程式便選取所有項目:

const selected = signal<string[]>([]);
const all = () => selected().length === items.length;
const some = () => selected().length > 0 && !all();

<Checkbox
  checked={all()}
  indeterminate={some()}
  onChange={({ checked }) => selected.set(checked ? items.map((i) => i.id) : [])}
  class="flex-row items-center gap-2"
  indicatorClass="group size-5 items-center justify-center rounded-md border-2 border-border checked:border-primary checked:bg-primary indeterminate:border-primary indeterminate:bg-primary"
  indicator={
    <>
      <Text class="hidden text-primary-foreground group-checked:flex">✓</Text>
      <View class="hidden h-0.5 w-2.5 bg-primary-foreground group-indeterminate:flex" />
    </>
  }
>
  <Text>Select all</Text>
</Checkbox>

#停用

已停用的 checkbox 不接受按壓,螢幕閱讀器會將其讀出為暗淡。它的 disabled: 類別與其指示器的類別都會套用,並優先於其 checked: 與 indeterminate: 類別:

<Checkbox
  checked={on()}
  disabled={locked()}
  onChange={({ checked }) => on.set(checked)}
  class="flex-row items-center gap-2 disabled:opacity-50"
  indicatorClass="size-5 rounded-md border-2 border-border checked:border-primary checked:bg-primary disabled:border-border disabled:bg-muted"
>
  <Text>Share usage data</Text>
</Checkbox>

#使用 hitSlop 放大點擊目標

旁邊沒有標籤的 checkbox(例如列的選取方框)可能比手指還小。hitSlop 讓落在其附近的按壓也能生效,範圍以各邊之外的點數表示,且不會改變其版面配置或外觀:所有邊使用同一個數字,或分別指定各邊({ top: 8, bottom: 8 }),與 view 的 hitSlop 接受的方式相同。

<Checkbox
  checked={selected()}
  hitSlop={12}
  accessibilityLabel="Select row"
  onChange={({ checked }) => selected.set(checked)}
  indicatorClass="size-4 rounded border border-border checked:bg-primary"
/>

#無障礙

  • 對螢幕閱讀器而言,checkbox 是單一元素,角色為 "checkbox"(或其 role),並帶有其值:已勾選、未勾選,或在不確定時為混合。指示器與標記都是它的一部分。其標籤是子元素的文字;若沒有文字,請提供 accessibilityLabel。
  • disabled 會依各平台自身的方式播報(VoiceOver、TalkBack、UI Automation、瀏覽器的無障礙樹)。
  • 在 macOS 與 web 上,checkbox 是鍵盤焦點的停駐點:Tab 可以到達,Space 與 Return 可以切換,focus: 樣式會顯示焦點所在。

關於標籤、角色與播報的一般說明,請參閱 無障礙。

#平台備註

  • iOS、Android 與 Mac Catalyst:按壓是結束於 checkbox(包含其標籤)上的點按。
  • 桌面平台(macOS、Windows、Linux、GPUI)與 web:按壓是點擊。在 macOS 與 web 上,Space 與 Return 也會切換擁有鍵盤焦點的 checkbox。
  • 每個平台都會繪製應用程式所設定樣式的內容:指示器、標記及其轉場。