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, and the URL the desktops show while the pointer rests on
the link (Where it goes).
import { mount, signal, Text, View } from "nsh";
import { Link } from "nsh/base";
import { desktop, Log, Panel, Split, ui, sectionSurface, linkColor, linkLook, linkSize, LinkText } from "./palette";
// Links of the shared look (`linkLook`, `LinkText`, `linkColor`): the
// accent with a quiet underline that a bar grows over from the left under
// the pointer (always there on the current page), violet once visited (the
// app remembers what it opened), the palette's focus ring, faint while
// disabled; a site outside the app has an arrow that nudges out under the
// pointer. The color fades and the bar slides, 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 color = () => linkColor({ disabled: props.disabled, current: props.current, visited: visited().has(props.href) });
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);
}}
{...linkLook({ inline: props.inline })}
>
<LinkText label={props.label} color={color()} size={props.inline ? 15 : undefined} current={props.current} disabled={props.disabled} icon={props.icon} />
</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: linkSize }}>/</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={{ ...sectionSurface({ padded: true }), 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_visible(|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-visible:rounded-sm focus-visible:outline-2 focus-visible: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"] }).
#Where it goes
On the desktops (macOS, Windows, Linux, GPUI) and the web in the desktop idiom, the pointer resting on a link for half a second shows where it goes, as a mail app or a browser’s status bar does: a hint below the link with its destination, until the pointer leaves or presses it. The keyboard moving the focus to a link (Tab) shows it too. A user checks the destination before following it, whatever the link’s text says.
- The destination is one line, left to right in a right-to-left app too. A URL shows as it is parsed: an international host in its ASCII form
(
https://xn--mnchen-3ya.de), so a letter of another script passing for a Latin one shows as what it is, and the host after auser@, a longuser:password@shortened so the host stays in view. - The scheme, the host and the port always show whole, at their full width, also for an IPv6 host
(
http://[2001:db8::1]:8080) or a long host. A long path and query lose their middle to an ellipsis, keeping their start and end (https://docs.example.com/guides/getting-…arch=arm64#verify); in a window too narrow for the line, only the path and query give way, again in their middle. - A scheme-relative URL (
//cdn.example.com/lib.js) shows its host the same way. A path of the app’s (/settings,docs/intro) or an#anchorshows as written, with control, bidirectional and invisible characters as percent-escapes. A disabled link shows no hint. - A link’s
titleshows above the destination in the same hint (<Link href="https://nsh.dev/docs" title="The guide">). It is optional: without it the hint shows the destination alone. It names the page; the link’s content andaccessibilityLabelare still what it reads as. - Links in text show it the same way: a TextView document’s links show their Markdown title
(
[docs](https://example.com "The guide")) above the destination. - An app’s
tooltipon a link is a separate overlay of its own, not the link’s title. It shows above by default and the destination below, so they usually sit apart; either can flip to the other side near a window’s edge, and then they may overlap.
The hint draws with the theme’s popover tokens (popover, border, muted-foreground for the URL), so every app
gives its users this check.
#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, and shown as the link’s hint on the desktops. |
title |
.title(..) |
string |
none | What the linked page is called: shown above the destination in the link’s hint. Optional. |
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). |
hitSlop |
.hit_slop(..) |
number | Partial<Edges> |
none | How far outside its bounds presses still count, in points: one number for every side, or the sides named. As a view’s hitSlop. |
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-visible:outline-2 focus-visible: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.
#A small target with hitSlop
A link can be smaller than a finger: an icon, a short word. hitSlop makes presses that land near it count, in points
past each side, without changing its layout or its look: one number for every side, or the sides named
({ top: 8, bottom: 8 }), as a view’s hitSlop takes them.
<Link href="https://example.com/help" hitSlop={12} accessibilityLabel="Help" class="size-5 items-center justify-center">
<Text>?</Text>
</Link>
#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-visible: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 pointer resting on a link shows its URL (Where it goes).
- The web renders a link with an
hrefas an<a href>(crawlers follow it; a click with a modifier key or the middle button opens it in a new tab, and a right click shows the browser’s menu for links), one without as an element withrole="link". - Terminal: the text inside an enabled link whose
hrefis a URL (not a path of the app’s) is an OSC 8 hyperlink to it, which the terminal emulator opens itself on a modified click, outsideopenWith. The Linux console (TERM=linux) shows plain text. - Every platform draws what the app styles.