元素
Link
連結會帶用戶前往某處:網頁、另一個應用程式(mailto:、tel:),或應用程式本身的某個畫面。它負責處理行為:按下、獲得鍵盤焦點時的 Return 和 Space、停用狀態,以及供屏幕閱讀器使用的 link 角色。外觀則由應用程式決定:Link 本身沒有顏色、底線或標籤,顯示出來的是它的子元素。href 指明去向;它是供開啟時使用的資料,絕不會顯示為畫面上的文字。
import { mount, signal, Text, View } from "nsh";
import { Link } from "nsh/base";
import { desktop, focusRing, Icon, Log, Panel, size, Split, tint, ui } from "./palette";
// Links of the app's look: the accent with a quiet underline that fills in
// under the pointer, violet once visited (the app remembers what it
// opened), the palette's focus ring, dimmed while disabled. The color and
// the underline move with `transition-colors`, still with reduced motion.
const visited = signal(new Set<string>());
const events = signal<string[]>([]);
const route = signal("/docs/link");
const opened = ({ href }: { href: string }) => {
visited.set(new Set(visited()).add(href));
events.set([`onOpen ${href}`, ...events()].slice(0, 4));
};
function DocLink(props: { href: string; label: string; openWith?: "system" | "app"; disabled?: boolean; current?: boolean; icon?: boolean; inline?: boolean }) {
const seen = () => visited().has(props.href);
const color = () => (props.disabled ? ui().faint : props.current ? ui().text : seen() ? ui().violet : ui().accent);
return (
<Link
href={props.href}
openWith={props.openWith ?? "app"}
disabled={props.disabled}
current={props.current}
onOpen={(e: { href: string }) => {
opened(e);
if ((props.openWith ?? "app") === "app") route.set(e.href);
}}
class={`flex-row items-center gap-1 rounded-sm transition-colors motion-reduce:transition-none active:opacity-60 ${props.inline ? "align-text-bottom" : "self-start"}`}
focusStyle={focusRing(2)}
>
<Text
class="transition-colors motion-reduce:transition-none"
style={{
fontSize: props.inline ? 15 : size.label,
fontWeight: props.current ? "600" : "500",
color: color(),
textDecorationLine: props.current || props.disabled ? "none" : "underline",
textDecorationColor: tint(color(), 0.4),
}}
hoverStyle={props.disabled || props.current ? undefined : { textDecorationColor: color() }}
>
{props.label}
</Text>
{props.icon ? <Icon name="external" size={13} tint={color()} /> : null}
</Link>
);
}
function App() {
const crumb = (href: string, label: string) => <DocLink href={href} label={label} current={route() === href} />;
const slash = () => <Text style={{ color: ui().faint, fontSize: size.label }}>/</Text>;
return (
<View style={{ flex: 1, backgroundColor: ui().page, padding: 20, paddingTop: 24, gap: 14 }}>
<Text style={{ fontSize: 22, fontWeight: "700", color: ui().text }}>link</Text>
<Text style={{ color: ui().muted }}>{desktop ? "Click a link, or Tab to one and press Return" : "Tap a link"}</Text>
<Split
gap={14}
inspector={
<>
<Panel title="route · visited">
<Text style={{ fontSize: 13, color: ui().text }}>
{route()} · {visited().size} visited
</Text>
</Panel>
<Log title="onOpen({ href })" lines={events()} minHeight={110} />
</>
}
>
<View style={{ ...ui().card, borderRadius: 16, padding: desktop ? 16 : 18, gap: 14 }}>
<View style={{ flexDirection: "row", alignItems: "center", flexWrap: "wrap", gap: 6 }}>
{crumb("/docs", "Docs")}
{slash()}
{crumb("/docs/elements", "Elements")}
{slash()}
{crumb("/docs/link", "Link")}
</View>
<Text style={{ fontSize: 15, lineHeight: 22, color: ui().text }}>
{"Read the "}
<DocLink href="/docs/release-notes" label="release notes" inline />
{" and the "}
<DocLink href="/docs/migration" label="migration guide" inline />
{" before you update."}
</Text>
<View style={{ height: 1, backgroundColor: ui().line }} />
<View style={{ gap: 10 }}>
<DocLink href="https://example.com/" label="example.com" openWith="system" icon />
<DocLink href="/docs/archive" label="Archived docs" disabled />
</View>
</View>
</Split>
</View>
);
}
mount(App);use nsh_base::{link, theme};
use nsh_core::prelude::*;
fn docs() -> impl IntoElement {
link()
.href("https://example.com/docs")
.text_color(move || theme().primary)
.underline()
.hover(|s| s.opacity(0.8))
.focus(|s| s.rounded_sm().outline(px(2.0)).outline_color(move || theme().ring))
.child(text("Read the docs"))
}import { mount, Text } from "nsh";
import { Link } from "nsh/base";
function App() {
return (
<Link
href="https://example.com/docs"
class="self-start text-primary underline hover:opacity-80 focus:rounded-sm focus:outline-2 focus:outline-ring"
>
<Text>Read the docs</Text>
</Link>
);
}
mount(App);Link 來自 nsh/base,即 Rust 中的 nsh-base crate。在 Rust 中,link() 會建立一個連結,每個屬性有對應的 setter(.href(..)、.open_with(..)),每個事件有對應的監聽器(.on_open(..)、.on_press(..)),連結本身使用型別化的樣式方法,內容則使用 .child(..)。
#開啟
按下、點擊,或在獲得焦點的連結上按 Return 或 Space,會開啟它的 href:
onOpen({ href })回報連結的去向。- 在
openWith="system"(預設值)下,屬於另一個應用程式或網站的 URL,即帶有 scheme 的 URL(https:、mailto:、tel:,或其他應用程式註冊的 scheme),會在應用程式外開啟,方式與openUrl相同:先由嵌入式應用程式的 router 處理,再交給系統。其他的href,即應用程式自己的路徑(/orders/42)或#anchor,只會回報,並由應用程式自行處理。 onPress()回報這次按下。
在 openWith="app" 下,連結如何開啟由應用程式的 onOpen 處理器決定:自己的路由、應用程式內 web view 中的頁面,或先顯示確認。
import { Link } from "nsh/base";
<Link href={`/orders/${order.id}`} openWith="app" onOpen={({ href }) => router.push(href)} class="text-primary">
<Text>Order #{order.id}</Text>
</Link>
當應用程式在 nsh.json 中宣告 capability("openUrl": true,或 { "schemes": ["https", "mailto"] })時,在應用程式外開啟 URL 需要 openUrl capability。
#屬性
| 屬性 | Rust | 類型 | 預設值 | 說明 |
|---|---|---|---|---|
href |
.href(..) |
string |
無 | 連結的去向:URL,或應用程式自己的路徑。以 onOpen 回報,絕不顯示。 |
openWith |
.open_with(LinkOpenWith::App) |
"system" | "app"(LinkOpenWith) |
"system" |
由誰開啟 href:系統會開啟屬於另一個應用程式或網站的 URL;"app" 則交由 onOpen 處理。 |
disabled |
.disabled(..) |
boolean |
false |
不接受按下、沒有按下回饋、沒有鍵盤焦點;disabled: 樣式會套用,屏幕閱讀器會說明它處於變暗狀態。 |
current |
.current(..) |
boolean |
false |
指向用戶目前所在頁面的連結,用於導覽列或麵包屑:屏幕閱讀器會說明它是目前頁面。 |
accessibilityLabel |
.accessibility_label(..) |
string |
無 | 當內容提供的資訊不足(例如圖示)時,屏幕閱讀器所讀出的內容。 |
hitSlop |
.hit_slop(..) |
number | Partial<Edges> |
無 | 在邊界以外多遠的按下仍會計算在內,單位為點:所有邊使用同一個數字,或分別指定各邊。與 view 的 hitSlop 相同。 |
children |
.child(..) |
JSX.Element |
無 | 內容:標籤、圖示,或應用程式顯示的任何內容。 |
class、style |
樣式方法 | string、StyleProp<ViewStyle> |
無 | 連結的外觀:顏色、底線、內距;文字屬性會傳遞給其內的文字。 |
activeStyle、hoverStyle、focusStyle、disabledStyle |
.active(|s| ..)、.hover(..)、.focus(..)、.disabled_style(..) |
StyleProp<ViewStyle> |
無 | 連結在按下、懸停、聚焦或停用時的樣式,與 <state>: class 相同。 |
#事件
| 事件 | Rust | 說明 |
|---|---|---|
onOpen |
.on_open(|href: &SharedString| ..) |
連結開啟 href:在 openWith="app" 下由處理器開啟,在 "system" 下則得知系統被要求開啟的內容。它早於 onPress 觸發。 |
onPress |
.on_press(|| ..) |
連結被按下:輕按、點擊,或在連結獲得鍵盤焦點時按 Return 或 Space。 |
已停用的連結不會回報任何事件,對它的按下也不會傳到周圍任何可按下的 view。
#狀態
應用程式以 class 或樣式屬性為各個狀態設定樣式:
<Link
href="https://example.com/changelog"
class="self-start text-primary underline decoration-primary/40 transition-colors hover:decoration-primary active:opacity-70 focus:outline-2 focus:outline-ring disabled:text-muted-foreground disabled:no-underline"
>
<Text>Changelog</Text>
</Link>
已造訪的連結由應用程式自行記錄:它保存已開啟的 href,並按自己的狀態為這些連結設定樣式。
const visited = signal(new Set<string>());
<Link
href={href}
onOpen={({ href }) => visited.set(new Set(visited()).add(href))}
class={visited().has(href) ? "text-violet-600 underline" : "text-primary underline"}
>
<Text>{label}</Text>
</Link>
#在文字內
與文字內的所有 view 一樣,位於 <Text> 內的 Link 是段落的行內框:它位於行內,保有自己的狀態、焦點和角色,並且只佔一行。align-text-bottom 會把只有一行文字的連結對齊段落的基線:
<Text class="text-[15px] text-foreground">
By continuing you accept the{" "}
<Link href="https://example.com/terms" class="align-text-bottom">
<Text class="text-[15px] text-primary underline">terms of service</Text>
</Link>
.
</Text>
隨段落換行的連結,是帶有 role="link" 和 onPress 的巢狀 <Text>(參閱 Text),TextView 繪製文件中的連結時就是這樣做。
#使用 hitSlop 處理細小的目標
連結可能比手指還小:例如圖示或短詞。hitSlop 讓落在連結附近的按下也計算在內,距離以各邊向外的點數計算,不會改變連結的版面或外觀:所有邊使用同一個數字,或分別指定各邊({ top: 8, bottom: 8 }),與 view 的 hitSlop 接受的格式相同。
<Link href="https://example.com/help" hitSlop={12} accessibilityLabel="Help" class="size-5 items-center justify-center">
<Text>?</Text>
</Link>
#無障礙
- 對屏幕閱讀器而言,連結是單一元素,角色為
"link";其標籤是子元素的文字,或accessibilityLabel。href仍然只是資料:屏幕閱讀器讀出的是標籤。 current會讀作目前頁面(ARIA 的aria-current="page"),disabled則按平台對變暗元素的讀法讀出。- 在 macOS 和 web 上,連結是鍵盤焦點的停駐點:按 Tab 可到達,按 Return 和 Space 可開啟,
focus:樣式會顯示焦點所在位置。
有關標籤、角色和公告的一般說明,請參閱 Accessibility。
#平台備註
- iOS、Android 和 Mac Catalyst:按下指在連結上結束的輕按。
- 桌面平台(macOS、Windows、Linux、GPUI)和 web:按下指點擊。在 macOS 和 web 上,Return 和 Space 也可開啟獲得鍵盤焦點的連結。
- web 會把連結繪製為帶有
role="link"的元素。 - 各平台都會繪製應用程式所設定的樣式。