Elements
Link
A link takes the user somewhere: a web page, another app (mailto:, tel:), or a screen of the app itself. It owns
the behavior: presses, Return and Space while it has the keyboard focus, the disabled state, and the link role for
screen readers. The look is the app’s: a Link has no color, underline or label of its own, and its children are what
shows. href says where it goes; it is data for the opening, never text on screen.
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 comes from nsh/base, the nsh-base crate in Rust. In Rust, link() makes one
with a setter per prop (.href(..), .open_with(..)), a listener per event (.on_open(..), .on_press(..)), the
typed style methods for the link and .child(..) for its content.
#Opening
A press, a click, or Return or Space on the focused link opens its href:
onOpen({ href })reports where it goes.- With
openWith="system"(the default), a URL of another app or site, one with a scheme (https:,mailto:,tel:, a scheme another app registered), opens outside the app, asopenUrlopens it: the embedding app’s router takes it first, then the system does. Any otherhref, a path of the app’s (/orders/42) or#anchor, is reported and stays with the app. onPress()reports the press.
With openWith="app", the app’s onOpen handler is how the link opens: a route of its own, a page in an in-app web
view, a confirmation first.
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>
Opening a URL outside the app takes the openUrl capability when the app declares capabilities in nsh.json
("openUrl": true, or { "schemes": ["https", "mailto"] }).
#Props
| Prop | Rust | Type | Default | Description |
|---|---|---|---|---|
href | .href(..) | string | none | Where the link goes: a URL, or a path of the app’s. Reported with onOpen, never shown. |
openWith | .open_with(LinkOpenWith::App) | "system" | "app" (LinkOpenWith) | "system" | Who opens href: the system opens a URL of another app or site; "app" leaves it to onOpen. |
disabled | .disabled(..) | boolean | false | No presses, no press feedback, no keyboard focus; disabled: styles apply, and screen readers say it is dimmed. |
current | .current(..) | boolean | false | The link to the page the user is on, in a navigation or a breadcrumb: screen readers say it is the current page. |
accessibilityLabel | .accessibility_label(..) | string | none | What screen readers say, when the content says too little (an icon). |
children | .child(..) | JSX.Element | none | The content: a label, an icon, anything the app shows. |
class, style | style methods | string, StyleProp<ViewStyle> | none | The link’s look: its color, underline, padding; text properties pass to the texts inside it. |
activeStyle, hoverStyle, focusStyle, disabledStyle | .active(|s| ..), .hover(..), .focus(..), .disabled_style(..) | StyleProp<ViewStyle> | none | The link’s style while pressed, hovered, focused or disabled, as the <state>: classes. |
#Events
| Event | Rust | Description |
|---|---|---|
onOpen | .on_open(|href: &SharedString| ..) | The link opens href: with openWith="app" the handler opens it, with "system" it hears what the system was asked to open. It comes before onPress. |
onPress | .on_press(|| ..) | The link was pressed: a tap, a click, or Return or Space while it has the keyboard focus. |
A disabled link reports nothing, and a press on it reaches no pressable view around it.
#States
The app styles each state with classes or style props:
<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>
A visited link is the app’s to remember: it keeps the hrefs it opened and styles those links by its own state.
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>
#Inside text
A Link inside a <Text> is an inline box of the paragraph, as every view inside a text is: it sits in the line,
keeps its own states, focus and role, and stays on one line. align-text-bottom places a link with one line of text on
the paragraph’s baseline:
<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>
A link that wraps with the paragraph’s lines is a nested <Text> with role="link" and onPress (see
Text), as TextView draws the links of a document.
#Accessibility
- A link is one element for screen readers, with the role
"link"; its label is the text of its children, oraccessibilityLabel.hrefstays data: screen readers say the label. currentis said as the current page (ARIA’saria-current="page"), anddisabledas the platform says a dimmed element.- On macOS and the web a link is a stop of the keyboard focus: Tab reaches it, Return and Space open it, and
focus:styles show where the focus is.
See Accessibility for labels, roles and announcements in general.
#Platform notes
- iOS, Android and Mac Catalyst: a press is a tap that ends on the link.
- Desktops (macOS, Windows, Linux, GPUI) and the web: a press is a click. On macOS and the web, Return and Space also open the link that has the keyboard focus.
- The web renders a link as an element with
role="link". - Every platform draws what the app styles.